Skip to content

Commit

Permalink
Adds options to set classloader for JSR223 engine.
Browse files Browse the repository at this point in the history
  • Loading branch information
yokolet committed Dec 16, 2010
1 parent 8740a6b commit b8cfd2b
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/org/jruby/embed/PropertyName.java
Expand Up @@ -53,6 +53,28 @@ public enum PropertyName {
*/
LOCALVARIABLE_BEHAVIOR("org.jruby.embed.localvariable.behavior"),

/**
* A key to get/set variables/constants retrieval policy. The assigned value
* must be true or false. When true is given, ScriptingContainer retrieve
* variables/constants from Ruby runtime lazily. When a variable or constant is
* requested from user program, ScriptingConainer actually attemps to get it.
* However, on JSR223, retrieval is done at the end of evaluation based on
* keys listed in Bindings.
*/
LAZINESS("org.jruby.embed.laziness"),

/**
* A key to get/set the value for classloader policy. The assigned value must
* be "current" or "none." When current is set, JSR223 engine sets a current
* classloader (the one used to initialize ScriptingContainer) to Ruby runtime.
* When none is set, no classloader is set to Ruby runtime.
* Default value is "none" for version 1.5.x, and "current" for 1.6.0 and later.
*
* This property is used only for JSR223 since ScriptingContainer users can
* set any classloader explicitely.
*/
CLASSLOADER("org.jruby.embed.classloader"),

/**
* A key to get/set compile mode. The assigned value is one of jit or force.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/org/jruby/embed/jsr223/JRubyEngineFactory.java
Expand Up @@ -169,6 +169,10 @@ public ScriptEngine getScriptEngine() {
LocalContextScope scope = SystemPropertyCatcher.getScope(LocalContextScope.SINGLETON);
LocalVariableBehavior behavior = SystemPropertyCatcher.getBehavior(LocalVariableBehavior.GLOBAL);
container = new ScriptingContainer(scope, behavior);
boolean useCurrent = SystemPropertyCatcher.useCurrentClassLoader(false);
if (useCurrent) {
container.setClassLoader(container.getClass().getClassLoader());
}
SystemPropertyCatcher.setConfiguration(container);
JRubyEngine engine = new JRubyEngine(container, this);
return (ScriptEngine)engine;
Expand Down
32 changes: 32 additions & 0 deletions src/org/jruby/embed/util/SystemPropertyCatcher.java
Expand Up @@ -102,6 +102,38 @@ public static LocalVariableBehavior getBehavior(LocalVariableBehavior defaultBeh
return behavior;
}

/**
* Gets a local variable behavior from System property. If no value is assigned to
* PropertyName.LOCALVARIABLE_BEHAVIOR, given default value is applied.
*
* @param defaultBehavior a default local variable behavior
* @return a local variable behavior
*/
public static boolean isLazy(boolean defaultLaziness) {
boolean lazy = defaultLaziness;
String s = System.getProperty(PropertyName.LAZINESS.toString());
if (s == null) {
return lazy;
}
return Boolean.parseBoolean(s);
}

/**
* Gets a value for classloader policy from System property.
*
* @param defaultPolicy default policy to use current classloader.
* @return true if current classloader is used, false otherwise.
*/
public static boolean useCurrentClassLoader(boolean defaultPolicy) {
String s = System.getProperty(PropertyName.CLASSLOADER.toString());
if (s == null) return defaultPolicy;
if ("current".equals(s)) {
return true;
} else {
return false;
}
}

/**
* Sets configuration parameters given by System properties. Compile mode and
* Compat version can be set.
Expand Down

0 comments on commit b8cfd2b

Please sign in to comment.