jaulib v1.3.0
Jau Support Library (C++, Java, ..)
PropertyAccess.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2020 Gothel Software e.K.
4 * Copyright (c) 2012 Gothel Software e.K.
5 * Copyright (c) 2012 JogAmp Community.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files (the
9 * "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 */
26
27package org.jau.sys;
28
29import java.security.*;
30import java.util.HashSet;
31
32import org.jau.sec.SecurityUtil;
33
34
35/** Helper routines for accessing properties. */
36public class PropertyAccess {
37 /** trusted build-in property prefix 'jnlp.' */
38 public static final String jnlp_prefix = "jnlp." ;
39 /** trusted build-in property prefix 'javaws.' */
40 public static final String javaws_prefix = "javaws.";
41
42 static final HashSet<String> trustedPrefixes;
43 static final HashSet<String> trusted;
44
45 static {
46 trustedPrefixes = new HashSet<String>();
47 trustedPrefixes.add(javaws_prefix);
48 trustedPrefixes.add(jnlp_prefix);
49 trustedPrefixes.add("jau.");
50 // 'jogamp.' and maybe other trusted prefixes will be added later via 'addTrustedPrefix()'
51
52 trusted = new HashSet<String>();
53 trusted.add("sun.java2d.opengl");
54 trusted.add("sun.java2d.noddraw");
55 trusted.add("sun.java2d.d3d");
56 trusted.add("sun.awt.noerasebackground");
57 }
58
59 /**
60 * @param prefix New prefix to be registered as trusted.
61 * @throws SecurityException as thrown by {@link SecurityUtil#checkAllPermissions()}.
62 */
63 protected static final void addTrustedPrefix(final String prefix) throws SecurityException {
65 trustedPrefixes.add(prefix);
66 }
67
68 public static final boolean isTrusted(final String propertyKey) {
69 final int dot1 = propertyKey.indexOf('.');
70 if(0<=dot1) {
71 return trustedPrefixes.contains(propertyKey.substring(0, dot1+1)) || trusted.contains(propertyKey);
72 } else {
73 return false;
74 }
75 }
76
77 /** @see #getProperty(String, boolean) */
78 public static final int getIntProperty(final String property, final boolean jnlpAlias, final int defaultValue) {
79 int i=defaultValue;
80 try {
81 final String sv = PropertyAccess.getProperty(property, jnlpAlias);
82 if(null!=sv) {
83 i = Integer.parseInt(sv);
84 }
85 } catch (final NumberFormatException nfe) {}
86 return i;
87 }
88
89 /** @see #getProperty(String, boolean) */
90 public static final long getLongProperty(final String property, final boolean jnlpAlias, final long defaultValue) {
91 long l=defaultValue;
92 try {
93 final String sv = PropertyAccess.getProperty(property, jnlpAlias);
94 if(null!=sv) {
95 l = Long.parseLong(sv);
96 }
97 } catch (final NumberFormatException nfe) {}
98 return l;
99 }
100
101 /** @see #getProperty(String, boolean) */
102 public static final boolean getBooleanProperty(final String property, final boolean jnlpAlias) {
103 return Boolean.valueOf(PropertyAccess.getProperty(property, jnlpAlias)).booleanValue();
104 }
105
106 /** @see #getProperty(String, boolean) */
107 public static final boolean getBooleanProperty(final String property, final boolean jnlpAlias, final boolean defaultValue) {
108 final String valueS = PropertyAccess.getProperty(property, jnlpAlias);
109 if(null != valueS) {
110 return Boolean.valueOf(valueS).booleanValue();
111 }
112 return defaultValue;
113 }
114
115 /** @see #getProperty(String, boolean) */
116 public static final boolean isPropertyDefined(final String property, final boolean jnlpAlias) {
117 return (PropertyAccess.getProperty(property, jnlpAlias) != null) ? true : false;
118 }
119
120 /**
121 * Query the property with the name <code>propertyKey</code>.
122 * <p>
123 * If <code>jnlpAlias</code> is <code>true</code> and the plain <code>propertyKey</code>
124 * could not be resolved, an attempt to resolve the JNLP aliased <i>trusted property</i> is made.<br>
125 * Example: For the propertyName <code>OneTwo</code>, the jnlp alias name is <code>jnlp.OneTwo</code>, which is considered trusted.<br>
126 * </p>
127 *
128 * @param propertyKey the property name to query.
129 * @param jnlpAlias true if a fallback attempt to query the JNLP aliased <i>trusted property</i> shall be made,
130 * otherwise false.
131 * @return the property value if exists, or null
132 *
133 * @throws NullPointerException if the property name is null
134 * @throws IllegalArgumentException if the property name is of length 0
135 * @throws SecurityException if access is not allowed to the given <code>propertyKey</code>
136 *
137 * @see System#getProperty(String)
138 */
139 public static final String getProperty(final String propertyKey, final boolean jnlpAlias)
140 throws SecurityException, NullPointerException, IllegalArgumentException {
141 if(null == propertyKey) {
142 throw new NullPointerException("propertyKey is NULL");
143 }
144 if(0 == propertyKey.length()) {
145 throw new IllegalArgumentException("propertyKey is empty");
146 }
147 String s=null;
148
149 if( isTrusted(propertyKey) ) {
150 // 'trusted' property (jnlp., javaws., jogamp., ..)
151 s = getTrustedPropKey(propertyKey);
152 } else {
153 // may throw SecurityException, AccessControlerException
154 s = System.getProperty(propertyKey);
155 }
156 if( null == s && jnlpAlias ) {
157 // Try 'jnlp.' aliased property ..
158 if( !propertyKey.startsWith(jnlp_prefix) ) {
159 // Properties within the namespace "jnlp." or "javaws." should be considered trusted,
160 // i.e. always granted w/o special privileges.
161 s = getTrustedPropKey(jnlp_prefix + propertyKey);
162 }
163 }
164 return s;
165 }
166
167 /** See {@link #getProperty(String, boolean)}, additionally allows a <code>defaultValue</code> if property value is <code>null</code>. */
168 public static final String getProperty(final String propertyKey, final boolean jnlpAlias, final String defaultValue)
169 throws SecurityException, NullPointerException, IllegalArgumentException {
170 final String s = PropertyAccess.getProperty(propertyKey, jnlpAlias);
171 if( null != s ) {
172 return s;
173 } else {
174 return defaultValue;
175 }
176 }
177
178 private static final String getTrustedPropKey(final String propertyKey) {
179 return SecurityUtil.doPrivileged(new PrivilegedAction<String>() {
180 @Override
181 public String run() {
182 try {
183 return System.getProperty(propertyKey);
184 } catch (final SecurityException se) {
185 throw new SecurityException("Could not access trusted property '"+propertyKey+"'", se);
186 }
187 }
188 });
189 }
190}
static< T > T doPrivileged(final PrivilegedAction< T > o)
Call wrapper for java.security.AccessController#doPrivileged(PrivilegedAction).
static final void checkAllPermissions()
Throws an SecurityException if an installed SecurityManager does not permit the requested AllPermissi...
Helper routines for accessing properties.
static final boolean isTrusted(final String propertyKey)
static final void addTrustedPrefix(final String prefix)
static final long getLongProperty(final String property, final boolean jnlpAlias, final long defaultValue)
static final String javaws_prefix
trusted build-in property prefix 'javaws.
static final String getProperty(final String propertyKey, final boolean jnlpAlias)
Query the property with the name propertyKey.
static final boolean getBooleanProperty(final String property, final boolean jnlpAlias)
static final String jnlp_prefix
trusted build-in property prefix 'jnlp.
static final boolean getBooleanProperty(final String property, final boolean jnlpAlias, final boolean defaultValue)
static final String getProperty(final String propertyKey, final boolean jnlpAlias, final String defaultValue)
See getProperty(String, boolean), additionally allows a defaultValue if property value is null.
static final int getIntProperty(final String property, final boolean jnlpAlias, final int defaultValue)
static final boolean isPropertyDefined(final String property, final boolean jnlpAlias)