119 } else if (t.getDefaultValue() != null) { 120 set(t.getKey(), createOption(t, t.getDefaultValue())); 121 } 122 } 123 } 124 } 125 126 /** 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 236 * {@literal <resource>}.option it will be completed using the resource from this 237 * instance 238 * 239 * @param key key for option | 119 } else if (t.getDefaultValue() != null) { 120 set(t.getKey(), createOption(t, t.getDefaultValue())); 121 } 122 } 123 } 124 } 125 126 /** 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 private static void checkPropertyName(final String name) { 140 if (! Objects.requireNonNull(name).startsWith("nashorn.")) { 141 throw new IllegalArgumentException(name); 142 } 143 } 144 145 /** 146 * Convenience function for getting system properties in a safe way 147 148 * @param name of boolean property 149 * @param defValue default value of boolean property 150 * @return true if set to true, default value if unset or set to false 151 */ 152 public static boolean getBooleanProperty(final String name, final Boolean defValue) { 153 checkPropertyName(name); 154 return AccessController.doPrivileged( 155 new PrivilegedAction<Boolean>() { 156 @Override 157 public Boolean run() { 158 try { 159 final String property = System.getProperty(name); 160 if (property == null && defValue != null) { 161 return defValue; 162 } 163 return property != null && !"false".equalsIgnoreCase(property); 164 } catch (final SecurityException e) { 165 // if no permission to read, assume false 166 return false; 167 } 168 } 169 }, READ_PROPERTY_ACC_CTXT); 170 } 171 172 /** 173 * Convenience function for getting system properties in a safe way 174 175 * @param name of boolean property 176 * @return true if set to true, false if unset or set to false 177 */ 178 public static boolean getBooleanProperty(final String name) { 179 return getBooleanProperty(name, null); 180 } 181 182 /** 183 * Convenience function for getting system properties in a safe way 184 * 185 * @param name of string property 186 * @param defValue the default value if unset 187 * @return string property if set or default value 188 */ 189 public static String getStringProperty(final String name, final String defValue) { 190 checkPropertyName(name); 191 return AccessController.doPrivileged( 192 new PrivilegedAction<String>() { 193 @Override 194 public String run() { 195 try { 196 return System.getProperty(name, defValue); 197 } catch (final SecurityException e) { 198 // if no permission to read, assume the default value 199 return defValue; 200 } 201 } 202 }, READ_PROPERTY_ACC_CTXT); 203 } 204 205 /** 206 * Convenience function for getting system properties in a safe way 207 * 208 * @param name of integer property 209 * @param defValue the default value if unset 210 * @return integer property if set or default value 211 */ 212 public static int getIntProperty(final String name, final int defValue) { 213 checkPropertyName(name); 214 return AccessController.doPrivileged( 215 new PrivilegedAction<Integer>() { 216 @Override 217 public Integer run() { 218 try { 219 return Integer.getInteger(name, defValue); 220 } catch (final SecurityException e) { 221 // if no permission to read, assume the default value 222 return defValue; 223 } 224 } 225 }, READ_PROPERTY_ACC_CTXT); 226 } 227 228 /** 229 * Return an option given its resource key. If the key doesn't begin with 230 * {@literal <resource>}.option it will be completed using the resource from this 231 * instance 232 * 233 * @param key key for option |