GlueGen v2.6.0-rc-20250712
GlueGen, Native Binding Generator for Java™ (public API).
PropertyAccess.java
Go to the documentation of this file.
1/**
2 * Copyright 2012 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29package com.jogamp.common.util;
30
31import java.security.*;
32import java.util.HashSet;
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 // 'jogamp.' and maybe other trusted prefixes will be added later via 'addTrustedPrefix()'
50
51 trusted = new HashSet<String>();
52 trusted.add("sun.java2d.opengl");
53 trusted.add("sun.java2d.noddraw");
54 trusted.add("sun.java2d.d3d");
55 trusted.add("sun.awt.noerasebackground");
56 }
57
58 /**
59 * @param prefix New prefix to be registered as trusted.
60 * @throws AccessControlException as thrown by {@link SecurityUtil#checkAllPermissions()}.
61 */
62 protected static final void addTrustedPrefix(final String prefix) throws AccessControlException {
64 trustedPrefixes.add(prefix);
65 }
66
67 public static final boolean isTrusted(final String propertyKey) {
68 final int dot1 = propertyKey.indexOf('.');
69 if(0<=dot1) {
70 return trustedPrefixes.contains(propertyKey.substring(0, dot1+1)) || trusted.contains(propertyKey);
71 } else {
72 return false;
73 }
74 }
75
76 /** @see #getProperty(String, boolean) */
77 public static final int getIntProperty(final String property, final boolean jnlpAlias, final int defaultValue) {
78 int i=defaultValue;
79 try {
80 final String sv = PropertyAccess.getProperty(property, jnlpAlias);
81 if(null!=sv) {
82 i = Integer.parseInt(sv);
83 }
84 } catch (final NumberFormatException nfe) {}
85 return i;
86 }
87
88 /** @see #getProperty(String, boolean) */
89 public static final long getLongProperty(final String property, final boolean jnlpAlias, final long defaultValue) {
90 long l=defaultValue;
91 try {
92 final String sv = PropertyAccess.getProperty(property, jnlpAlias);
93 if(null!=sv) {
94 l = Long.parseLong(sv);
95 }
96 } catch (final NumberFormatException nfe) {}
97 return l;
98 }
99
100 /** @see #getProperty(String, boolean) */
101 public static final boolean getBooleanProperty(final String property, final boolean jnlpAlias) {
102 return Boolean.valueOf(PropertyAccess.getProperty(property, jnlpAlias)).booleanValue();
103 }
104
105 /** @see #getProperty(String, boolean) */
106 public static final boolean getBooleanProperty(final String property, final boolean jnlpAlias, final boolean defaultValue) {
107 final String valueS = PropertyAccess.getProperty(property, jnlpAlias);
108 if(null != valueS) {
109 return Boolean.valueOf(valueS).booleanValue();
110 }
111 return defaultValue;
112 }
113
114 /** @see #getProperty(String, boolean) */
115 public static final boolean isPropertyDefined(final String property, final boolean jnlpAlias) {
116 return (PropertyAccess.getProperty(property, jnlpAlias) != null) ? true : false;
117 }
118
119 /**
120 * Query the property with the name <code>propertyKey</code>.
121 * <p>
122 * If <code>jnlpAlias</code> is <code>true</code> and the plain <code>propertyKey</code>
123 * could not be resolved, an attempt to resolve the JNLP aliased <i>trusted property</i> is made.<br>
124 * Example: For the propertyName <code>OneTwo</code>, the jnlp alias name is <code>jnlp.OneTwo</code>, which is considered trusted.<br>
125 * </p>
126 *
127 * @param propertyKey the property name to query.
128 * @param jnlpAlias true if a fallback attempt to query the JNLP aliased <i>trusted property</i> shall be made,
129 * otherwise false.
130 * @return the property value if exists, or null
131 *
132 * @throws NullPointerException if the property name is null
133 * @throws IllegalArgumentException if the property name is of length 0
134 * @throws SecurityException if access is not allowed to the given <code>propertyKey</code>
135 *
136 * @see System#getProperty(String)
137 */
138 public static final String getProperty(final String propertyKey, final boolean jnlpAlias)
139 throws SecurityException, NullPointerException, IllegalArgumentException {
140 if(null == propertyKey) {
141 throw new NullPointerException("propertyKey is NULL");
142 }
143 if(0 == propertyKey.length()) {
144 throw new IllegalArgumentException("propertyKey is empty");
145 }
146 String s=null;
147
148 if( isTrusted(propertyKey) ) {
149 // 'trusted' property (jnlp., javaws., jogamp., ..)
150 s = getTrustedPropKey(propertyKey);
151 } else {
152 // may throw SecurityException, AccessControlerException
153 s = System.getProperty(propertyKey);
154 }
155 if( null == s && jnlpAlias ) {
156 // Try 'jnlp.' aliased property ..
157 if( !propertyKey.startsWith(jnlp_prefix) ) {
158 // Properties within the namespace "jnlp." or "javaws." should be considered trusted,
159 // i.e. always granted w/o special privileges.
160 s = getTrustedPropKey(jnlp_prefix + propertyKey);
161 }
162 }
163 return s;
164 }
165
166 /** See {@link #getProperty(String, boolean)}, additionally allows a <code>defaultValue</code> if property value is <code>null</code>. */
167 public static final String getProperty(final String propertyKey, final boolean jnlpAlias, final String defaultValue)
168 throws SecurityException, NullPointerException, IllegalArgumentException {
169 final String s = PropertyAccess.getProperty(propertyKey, jnlpAlias);
170 if( null != s ) {
171 return s;
172 } else {
173 return defaultValue;
174 }
175 }
176
177 private static final String getTrustedPropKey(final String propertyKey) {
178 return SecurityUtil.doPrivileged(new PrivilegedAction<String>() {
179 @Override
180 public String run() {
181 try {
182 return System.getProperty(propertyKey);
183 } catch (final SecurityException se) {
184 throw new SecurityException("Could not access trusted property '"+propertyKey+"'", se);
185 }
186 }
187 });
188 }
189}
Helper routines for accessing properties.
static final boolean getBooleanProperty(final String property, final boolean jnlpAlias)
static final boolean getBooleanProperty(final String property, final boolean jnlpAlias, final boolean defaultValue)
static final String javaws_prefix
trusted build-in property prefix 'javaws.
static final boolean isPropertyDefined(final String property, final boolean jnlpAlias)
static final String jnlp_prefix
trusted build-in property prefix 'jnlp.
static final void addTrustedPrefix(final String prefix)
static final boolean isTrusted(final String propertyKey)
static final long getLongProperty(final String property, final boolean jnlpAlias, final long defaultValue)
static final String getProperty(final String propertyKey, final boolean jnlpAlias)
Query the property with the name propertyKey.
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< 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...