25
26 package jdk.nashorn.internal.runtime.options;
27
28 import java.io.PrintWriter;
29 import java.security.AccessControlContext;
30 import java.security.AccessController;
31 import java.security.Permissions;
32 import java.security.PrivilegedAction;
33 import java.security.ProtectionDomain;
34 import java.text.MessageFormat;
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.Enumeration;
39 import java.util.HashMap;
40 import java.util.LinkedList;
41 import java.util.List;
42 import java.util.Locale;
43 import java.util.Map;
44 import java.util.MissingResourceException;
45 import java.util.PropertyPermission;
46 import java.util.ResourceBundle;
47 import java.util.StringTokenizer;
48 import java.util.TimeZone;
49 import java.util.TreeMap;
50 import java.util.TreeSet;
51 import jdk.nashorn.internal.runtime.QuotedStringTokenizer;
52
53 /**
54 * Manages global runtime options.
55 */
56 public final class Options {
57 // permission to just read nashorn.* System properties
58 private static AccessControlContext createPropertyReadAccCtxt() {
59 final Permissions perms = new Permissions();
60 perms.add(new PropertyPermission("nashorn.*", "read"));
61 return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
62 }
63
64 private static final AccessControlContext READ_PROPERTY_ACC_CTXT = createPropertyReadAccCtxt();
126 * Get the resource for this Options set, e.g. "nashorn"
127 * @return the resource
128 */
129 public String getResource() {
130 return resource;
131 }
132
133 @Override
134 public String toString() {
135 return options.toString();
136 }
137
138 /**
139 * Convenience function for getting system properties in a safe way
140
141 * @param name of boolean property
142 * @param defValue default value of boolean property
143 * @return true if set to true, default value if unset or set to false
144 */
145 public static boolean getBooleanProperty(final String name, final Boolean defValue) {
146 name.getClass(); // null check
147 if (!name.startsWith("nashorn.")) {
148 throw new IllegalArgumentException(name);
149 }
150
151 return AccessController.doPrivileged(
152 new PrivilegedAction<Boolean>() {
153 @Override
154 public Boolean run() {
155 try {
156 final String property = System.getProperty(name);
157 if (property == null && defValue != null) {
158 return defValue;
159 }
160 return property != null && !"false".equalsIgnoreCase(property);
161 } catch (final SecurityException e) {
162 // if no permission to read, assume false
163 return false;
164 }
165 }
166 }, READ_PROPERTY_ACC_CTXT);
167 }
168
169 /**
170 * Convenience function for getting system properties in a safe way
171
172 * @param name of boolean property
173 * @return true if set to true, false if unset or set to false
174 */
175 public static boolean getBooleanProperty(final String name) {
176 return getBooleanProperty(name, null);
177 }
178
179 /**
180 * Convenience function for getting system properties in a safe way
181 *
182 * @param name of string property
183 * @param defValue the default value if unset
184 * @return string property if set or default value
185 */
186 public static String getStringProperty(final String name, final String defValue) {
187 name.getClass(); // null check
188 if (! name.startsWith("nashorn.")) {
189 throw new IllegalArgumentException(name);
190 }
191
192 return AccessController.doPrivileged(
193 new PrivilegedAction<String>() {
194 @Override
195 public String run() {
196 try {
197 return System.getProperty(name, defValue);
198 } catch (final SecurityException e) {
199 // if no permission to read, assume the default value
200 return defValue;
201 }
202 }
203 }, READ_PROPERTY_ACC_CTXT);
204 }
205
206 /**
207 * Convenience function for getting system properties in a safe way
208 *
209 * @param name of integer property
210 * @param defValue the default value if unset
211 * @return integer property if set or default value
212 */
213 public static int getIntProperty(final String name, final int defValue) {
214 name.getClass(); // null check
215 if (! name.startsWith("nashorn.")) {
216 throw new IllegalArgumentException(name);
217 }
218
219 return AccessController.doPrivileged(
220 new PrivilegedAction<Integer>() {
221 @Override
222 public Integer run() {
223 try {
224 return Integer.getInteger(name, defValue);
225 } catch (final SecurityException e) {
226 // if no permission to read, assume the default value
227 return defValue;
228 }
229 }
230 }, READ_PROPERTY_ACC_CTXT);
231 }
232
233 /**
234 * Return an option given its resource key. If the key doesn't begin with
|
25
26 package jdk.nashorn.internal.runtime.options;
27
28 import java.io.PrintWriter;
29 import java.security.AccessControlContext;
30 import java.security.AccessController;
31 import java.security.Permissions;
32 import java.security.PrivilegedAction;
33 import java.security.ProtectionDomain;
34 import java.text.MessageFormat;
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.Enumeration;
39 import java.util.HashMap;
40 import java.util.LinkedList;
41 import java.util.List;
42 import java.util.Locale;
43 import java.util.Map;
44 import java.util.MissingResourceException;
45 import java.util.Objects;
46 import java.util.PropertyPermission;
47 import java.util.ResourceBundle;
48 import java.util.StringTokenizer;
49 import java.util.TimeZone;
50 import java.util.TreeMap;
51 import java.util.TreeSet;
52 import jdk.nashorn.internal.runtime.QuotedStringTokenizer;
53
54 /**
55 * Manages global runtime options.
56 */
57 public final class Options {
58 // permission to just read nashorn.* System properties
59 private static AccessControlContext createPropertyReadAccCtxt() {
60 final Permissions perms = new Permissions();
61 perms.add(new PropertyPermission("nashorn.*", "read"));
62 return new AccessControlContext(new ProtectionDomain[] { new ProtectionDomain(null, perms) });
63 }
64
65 private static final AccessControlContext READ_PROPERTY_ACC_CTXT = createPropertyReadAccCtxt();
127 * Get the resource for this Options set, e.g. "nashorn"
128 * @return the resource
129 */
130 public String getResource() {
131 return resource;
132 }
133
134 @Override
135 public String toString() {
136 return options.toString();
137 }
138
139 /**
140 * Convenience function for getting system properties in a safe way
141
142 * @param name of boolean property
143 * @param defValue default value of boolean property
144 * @return true if set to true, default value if unset or set to false
145 */
146 public static boolean getBooleanProperty(final String name, final Boolean defValue) {
147 Objects.requireNonNull(name);
148 if (!name.startsWith("nashorn.")) {
149 throw new IllegalArgumentException(name);
150 }
151
152 return AccessController.doPrivileged(
153 new PrivilegedAction<Boolean>() {
154 @Override
155 public Boolean run() {
156 try {
157 final String property = System.getProperty(name);
158 if (property == null && defValue != null) {
159 return defValue;
160 }
161 return property != null && !"false".equalsIgnoreCase(property);
162 } catch (final SecurityException e) {
163 // if no permission to read, assume false
164 return false;
165 }
166 }
167 }, READ_PROPERTY_ACC_CTXT);
168 }
169
170 /**
171 * Convenience function for getting system properties in a safe way
172
173 * @param name of boolean property
174 * @return true if set to true, false if unset or set to false
175 */
176 public static boolean getBooleanProperty(final String name) {
177 return getBooleanProperty(name, null);
178 }
179
180 /**
181 * Convenience function for getting system properties in a safe way
182 *
183 * @param name of string property
184 * @param defValue the default value if unset
185 * @return string property if set or default value
186 */
187 public static String getStringProperty(final String name, final String defValue) {
188 Objects.requireNonNull(name);
189 if (! name.startsWith("nashorn.")) {
190 throw new IllegalArgumentException(name);
191 }
192
193 return AccessController.doPrivileged(
194 new PrivilegedAction<String>() {
195 @Override
196 public String run() {
197 try {
198 return System.getProperty(name, defValue);
199 } catch (final SecurityException e) {
200 // if no permission to read, assume the default value
201 return defValue;
202 }
203 }
204 }, READ_PROPERTY_ACC_CTXT);
205 }
206
207 /**
208 * Convenience function for getting system properties in a safe way
209 *
210 * @param name of integer property
211 * @param defValue the default value if unset
212 * @return integer property if set or default value
213 */
214 public static int getIntProperty(final String name, final int defValue) {
215 Objects.requireNonNull(name);
216 if (! name.startsWith("nashorn.")) {
217 throw new IllegalArgumentException(name);
218 }
219
220 return AccessController.doPrivileged(
221 new PrivilegedAction<Integer>() {
222 @Override
223 public Integer run() {
224 try {
225 return Integer.getInteger(name, defValue);
226 } catch (final SecurityException e) {
227 // if no permission to read, assume the default value
228 return defValue;
229 }
230 }
231 }, READ_PROPERTY_ACC_CTXT);
232 }
233
234 /**
235 * Return an option given its resource key. If the key doesn't begin with
|