-
Notifications
You must be signed in to change notification settings - Fork 864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reading configuration from config files #1722
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this PR is looking very good, but why do we have this file and the other .config files in the top-level directory? I suppose that we need one just to test the file reading part of the codebase, but since they won't be used by the runtime or tests otherwise, do we really need all of them? That's really my only concern with this PR at this point. Thanks! |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# This file is loaded by the RhinoPropertiesTest and can be used to modify values in this module | ||
# | ||
# rhino.printTrees=true | ||
# rhino.printICode=true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# THIS FILE IS NOT USED BY DEFAULT | ||
# There is a RhinoPropertiesLoader in this maven module | ||
# If you want to reconfigure something, please use rhino-config-for-this-module.config (see RhinoPropertiesTest) | ||
|
||
testconfig.bar=value4-mod | ||
TESTCONFIG_BAZ=value5 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package org.mozilla.javascript.config; | ||
|
||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
import java.util.Objects; | ||
|
||
/** | ||
* RhinoConfig provides typesafe and static access methods to a {@link RhinoProperties} default | ||
* instance. | ||
* | ||
* @author Roland Praml, Foconis Analytics GmbH | ||
*/ | ||
public class RhinoConfig { | ||
|
||
private static final RhinoProperties INSTANCE = | ||
// We still assume, that a security manager could be in place! | ||
AccessController.doPrivileged( | ||
(PrivilegedAction<RhinoProperties>) RhinoProperties::init); | ||
|
||
/** | ||
* Returns the property as string. | ||
* | ||
* <p>If the value is not set, <code>defaultVaule</code> is returned. | ||
*/ | ||
private static String get(String property, String defaultValue) { | ||
Object ret = INSTANCE.get(property); | ||
if (ret != null) { | ||
return ret.toString(); | ||
} | ||
return defaultValue; | ||
} | ||
|
||
/** Returns the property as string with null as default. */ | ||
public static String get(String property) { | ||
return get(property, (String) null); | ||
} | ||
|
||
/** | ||
* Returns the property as enum. | ||
* | ||
* <p>If the property is set to any of the enum names (case-insensitive), this enum value is | ||
* returned, otherwise <code>defaultValue</code> is returned. | ||
* | ||
* <p>Note: <code>defaultValue</code> must be specified to derive the enum class and its values. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public static <T extends Enum<T>> T get(String property, T defaultValue) { | ||
Objects.requireNonNull(defaultValue, "defaultValue must not be null"); | ||
Object ret = INSTANCE.get(property); | ||
if (ret != null) { | ||
Class<T> enumType = (Class<T>) defaultValue.getDeclaringClass(); | ||
// We make a case insentive lookup here. | ||
for (T enm : enumType.getEnumConstants()) { | ||
if (enm.name().equalsIgnoreCase(ret.toString())) { | ||
return enm; | ||
} | ||
} | ||
} | ||
return defaultValue; | ||
} | ||
|
||
/** | ||
* Returns the property as boolean. | ||
* | ||
* <p>A property is true, if it is either <code>Boolean.TRUE</code> or if and only if the string | ||
* representation is equal to {@code "true"} (case-insensitive). If the property is not set, | ||
* <code>defaultValue</code> is returned | ||
* | ||
* <p>Same behaviour as {@link Boolean#getBoolean(String)} | ||
*/ | ||
public static boolean get(String property, boolean defaultValue) { | ||
Object ret = INSTANCE.get(property); | ||
if (ret instanceof Boolean) { | ||
return (Boolean) ret; | ||
} else if (ret == null) { | ||
return defaultValue; | ||
} | ||
|
||
return Boolean.parseBoolean(ret.toString()); | ||
} | ||
|
||
/** | ||
* Returns the property as integer. | ||
* | ||
* <p>if the property is not set or not a valid integer value, <code>defaultValue</code> is | ||
* returned. | ||
*/ | ||
public static int get(String property, int defaultValue) { | ||
Object ret = INSTANCE.get(property); | ||
if (ret instanceof Number) { | ||
return ((Number) ret).intValue(); | ||
} else if (ret != null) { | ||
try { | ||
return Integer.decode(ret.toString()); | ||
} catch (NumberFormatException e) { | ||
// ignore invalid values. See Integer.getInteger | ||
} | ||
} | ||
return defaultValue; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q: Should we change this to
A: Probably not, as this will break other things!
And JavaPolicySecurity has to be removed anyway in the future (see #1353)