|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.jruby.embed.ScriptingContainer
public class ScriptingContainer
ScipritngContainer provides various methods and resources that are useful for embedding Ruby in Java. Using this class, users can run Ruby scripts from Java programs easily. Also, users can use methods defined or implemented by Ruby. ScriptingContainer has a couple of configuration parameters per container base. Those are a local context scope, local variable behavior, and property file. These parameters should be given when the container is instantiated; otherwise, default values are applied to. Below are examples. The first Example is a very simple Hello World. After initializing a ScriptingContainer, a Ruby script, puts "Hello World!", runs and produces "Hello World!."
Example 1:
ScriptingContainer container = new ScriptingContainer();
container.runScriptlet("puts \"Hello World!\"");
Produces:
Hello World!
The second example shows how to share variables between Java and Ruby.
In this example, a local variable "x" is shared. Unlike JSR223 JRuby engine,
Ruby's local, instance, global variables and constants are available to share on
ScrriptingContainer. (A class variable sharing does not work on current version)
Thus, "x" in Java is also "x" in Ruby.
Example 2:
ScriptingContainer container = new ScriptingContainer();
container.put("x", 12345);
container.runScriptlet("puts x.to_s(2)");
Produces:
11000000111001
The third examples shows how to keep local variables across multiple evaluations.
This feature simulates BSF engine for JRuby. In terms of Ruby semantics,
local variables should not survive after the evaluation has completed. Thus,
this behavior is optional, and users need to specify LocalVariableBehvior.PERSISTENT
when the container is instantiated.
Example 3:
ScriptingContainer container = new ScriptingContainer(LocalVariableBehavior.PERSISTENT);
container.runScriptlet("p=9.0");
container.runScriptlet("q = Math.sqrt p");
container.runScriptlet("puts \"square root of #{p} is #{q}\"");
System.out.println("Ruby used values: p = " + container.get("p") +
", q = " + container.get("q"));
Produces:
square root of 9.0 is 3.0
Ruby used values: p = 9.0, q = 3.0
Also, ScriptingContainer provides better i18n support. For example,
Unicode Escape Sequence can be included in Ruby scripts.
In addition, ScriptingContainer supports a parse-once-eval-many-times feature, invoking methods defined by Ruby, and getting an instance of a specified interface that has been implemented by Ruby.
Example 4:
ScriptingContainer container = new ScriptingContainer();
script =
"def message\n" +
"\"message: #\"\n" +
"end\n" +
"message";
container.put("@message", "What's up?");
EvalUnit unit = container.parse(script);
IRubyObject ret = unit.run();
System.out.println(JavaEmbedUtils.rubyToJava(ret));
container.put("@message", "Fabulous!");
ret = unit.run();
System.out.println(JavaEmbedUtils.rubyToJava(ret));
container.put("@message", "That's the way you are.");
ret = unit.run();
System.out.println(JavaEmbedUtils.rubyToJava(ret));
Produces:
message: What's up?
message: Fabulous!
message: That's the way you are.
See more details at project's
Wiki
| Constructor Summary | |
|---|---|
ScriptingContainer()
Constructs a ScriptingContainer with a default values. |
|
ScriptingContainer(LocalContextScope scope)
|
|
ScriptingContainer(LocalContextScope scope,
LocalVariableBehavior behavior)
|
|
ScriptingContainer(LocalContextScope scope,
LocalVariableBehavior behavior,
java.lang.String propertyname)
Constructs a ScriptingContainer with a specified local context scope, local variable behavior and property file. |
|
ScriptingContainer(LocalVariableBehavior behavior)
|
|
| Method Summary | ||
|---|---|---|
java.lang.Object |
callMethod(java.lang.Object receiver,
java.lang.String methodName,
org.jruby.runtime.Block block,
java.lang.Object... args)
Executes a method defined in Ruby script. |
|
|
callMethod(java.lang.Object receiver,
java.lang.String methodName,
java.lang.Class<T> returnType)
Executes a method defined in Ruby script. |
|
|
callMethod(java.lang.Object receiver,
java.lang.String methodName,
java.lang.Class<T> returnType,
EmbedEvalUnit unit)
Executes a method defined in Ruby script. |
|
java.lang.Object |
callMethod(java.lang.Object receiver,
java.lang.String methodName,
java.lang.Object... args)
Executes a method defined in Ruby script. |
|
|
callMethod(java.lang.Object receiver,
java.lang.String methodName,
java.lang.Object[] args,
org.jruby.runtime.Block block,
java.lang.Class<T> returnType)
Executes a method defined in Ruby script. |
|
|
callMethod(java.lang.Object receiver,
java.lang.String methodName,
java.lang.Object[] args,
org.jruby.runtime.Block block,
java.lang.Class<T> returnType,
EmbedEvalUnit unit)
Executes a method defined in Ruby script. |
|
|
callMethod(java.lang.Object receiver,
java.lang.String methodName,
java.lang.Object[] args,
java.lang.Class<T> returnType)
Executes a method defined in Ruby script. |
|
|
callMethod(java.lang.Object receiver,
java.lang.String methodName,
java.lang.Object[] args,
java.lang.Class<T> returnType,
EmbedEvalUnit unit)
Executes a method defined in Ruby script. |
|
|
callMethod(java.lang.Object receiver,
java.lang.String methodName,
java.lang.Object singleArg,
java.lang.Class<T> returnType)
Executes a method defined in Ruby script. |
|
|
callSuper(java.lang.Object receiver,
java.lang.Object[] args,
org.jruby.runtime.Block block,
java.lang.Class<T> returnType)
|
|
|
callSuper(java.lang.Object receiver,
java.lang.Object[] args,
java.lang.Class<T> returnType)
|
|
void |
clear()
Removes all of the mappings from this map. |
|
java.lang.Object |
get(java.lang.String key)
Returns a value to which the specified key is mapped in a variable map, or null if this map contains no mapping for the key. |
|
java.lang.String[] |
getArgv()
Returns an arguments' list. |
|
java.lang.Object |
getAttribute(java.lang.Object key)
Returns an attribute value associated with the specified key in a attribute map. |
|
java.util.Map |
getAttributeMap()
Returns a attribute map in one of LocalContextScope. |
|
org.jruby.util.ClassCache |
getClassCache()
Returns a ClassCache object that is tied to a class loader. |
|
java.lang.ClassLoader |
getClassLoader()
Returns a class loader object that is currently used. |
|
org.jruby.CompatVersion |
getCompatVersion()
Returns a Ruby version currently chosen, which is one of CompatVersion.RUBY1_8, CompatVersion.RUBY1_9, or CompatVersion.BOTH. |
|
org.jruby.RubyInstanceConfig.CompileMode |
getCompileMode()
Returns a compile mode currently chosen, which is one of CompileMode.JIT, CompileMode.FORCE, CompileMode.OFF. |
|
java.lang.String |
getCurrentDirectory()
Returns a current directory. |
|
java.util.Map |
getEnvironment()
Returns a map of environment variables. |
|
java.io.PrintStream |
getErr()
Deprecated. As of JRuby 1.5.0, Replaced by getError() |
|
java.io.PrintStream |
getError()
Returns an error stream assigned to STDERR and $stderr. |
|
java.io.Writer |
getErrorWriter()
Returns an error writer set in an attribute map. |
|
java.lang.String |
getHomeDirectory()
Returns a JRuby home directory. |
|
java.io.InputStream |
getIn()
Deprecated. As of JRuby 1.5.0, replaced by getInput(). |
|
java.io.InputStream |
getInput()
Returns an input stream assigned to STDIN and $stdin. |
|
|
getInstance(java.lang.Object receiver,
java.lang.Class<T> clazz)
Returns an instance of a requested interface type. |
|
int |
getJitLogEvery()
Returns the value of n, which means that jitted methods are logged in every n methods. |
|
int |
getJitMax()
Returns a value of a max class cache size. |
|
int |
getJitMaxSize()
Returns a value of a max size of the bytecode generated by compiler. |
|
int |
getJitThreshold()
Returns a value of the threshold that determines whether jitted methods' call reached to the limit or not. |
|
org.jruby.util.KCode |
getKCode()
Returns a value of KCode currently used. |
|
java.util.List<java.lang.String> |
getLoadPaths()
Returns a list of load paths for Ruby scripts/libraries. |
|
org.jruby.RubyInstanceConfig.LoadServiceCreator |
getLoadServiceCreator()
Returns a LoadServiceCreator currently used. |
|
java.io.PrintStream |
getOut()
Deprecated. As of JRuby 1.5.0, replaced by getOutput(). |
|
java.io.PrintStream |
getOutput()
Returns an output stream assigned to STDOUT and $stdout. |
|
org.jruby.Profile |
getProfile()
Returns a Profile currently used. |
|
java.lang.String[] |
getProperty(java.lang.String key)
Returns an array of values associated to a key. |
|
LocalContextProvider |
getProvider()
Returns a provider instance of LocalContextProvider. |
|
java.io.Reader |
getReader()
Returns a reader set in an attribute map. |
|
java.lang.String |
getRecordSeparator()
Returns a record separator. |
|
org.jruby.Ruby |
getRuntime()
Deprecated. As of JRuby 1.5.0. Use getProvider().getRuntime() method instead. |
|
java.lang.String |
getScriptFilename()
Returns a script filename to run. |
|
java.lang.String |
getSupportedRubyVersion()
Returns a version of JRuby that gets ScriptingContainer started. |
|
BiVariableMap |
getVarMap()
Returns a variable map in one of LocalContextScope. |
|
java.io.Writer |
getWriter()
Returns a writer set in an attribute map. |
|
boolean |
isObjectSpaceEnabled()
Tests whether the Object Space is enabled or not. |
|
boolean |
isRunRubyInProcess()
Tests whether Ruby runs in a process or not. |
|
EmbedRubyObjectAdapter |
newObjectAdapter()
Returns an instance of EmbedRubyObjectAdapter for embedders to invoke
methods defined by Ruby. |
|
EmbedRubyRuntimeAdapter |
newRuntimeAdapter()
Returns an instance of EmbedRubyRuntimeAdapter for embedders to parse
scripts. |
|
EmbedEvalUnit |
parse(java.io.InputStream istream,
java.lang.String filename,
int... lines)
Parses a script given by a input stream and return an object which can be run(). |
|
EmbedEvalUnit |
parse(PathType type,
java.lang.String filename,
int... lines)
Parses a script read from a specified path and return an object which can be run(). |
|
EmbedEvalUnit |
parse(java.io.Reader reader,
java.lang.String filename,
int... lines)
Parses a script given by a reader and return an object which can be run(). |
|
EmbedEvalUnit |
parse(java.lang.String script,
int... lines)
Parses a script and return an object which can be run(). |
|
java.lang.Object |
put(java.lang.String key,
java.lang.Object value)
Associates the specified value with the specified key in a variable map. |
|
java.lang.Object |
remove(java.lang.String key)
Removes the specified Ruby variable with the specified variable name in a variable map. |
|
java.lang.Object |
removeAttribute(java.lang.Object key)
Removes the specified value with the specified key in a attribute map. |
|
void |
resetErrorWriter()
|
|
void |
resetWriter()
|
|
java.lang.Object |
runScriptlet(java.io.InputStream istream,
java.lang.String filename)
Evaluates a script read from a input stream under the current scope (perhaps the top-level scope) and returns a result only if a script returns a value. |
|
java.lang.Object |
runScriptlet(PathType type,
java.lang.String filename)
Reads a script file from specified path and evaluates it under the current scope (perhaps the top-level scope) and returns a result only if a script returns a value. |
|
java.lang.Object |
runScriptlet(java.io.Reader reader,
java.lang.String filename)
Evaluates a script read from a reader under the current scope (perhaps the top-level scope) and returns a result only if a script returns a value. |
|
java.lang.Object |
runScriptlet(java.lang.String script)
Evaluates a script under the current scope (perhaps the top-level scope) and returns a result only if a script returns a value. |
|
void |
setArgv(java.lang.String[] argv)
Changes values of the arguments' list. |
|
java.lang.Object |
setAttribute(java.lang.Object key,
java.lang.Object value)
Associates the specified value with the specified key in a attribute map. |
|
void |
setClassCache(org.jruby.util.ClassCache cache)
Changes a ClassCache object to a given one. |
|
void |
setClassLoader(java.lang.ClassLoader loader)
Changes a class loader to a given loader. |
|
void |
setCompatVersion(org.jruby.CompatVersion version)
Changes a Ruby version to be evaluated into one of CompatVersion.RUBY1_8, CompatVersion.RUBY1_9, or CompatVersion.BOTH. |
|
void |
setCompileMode(org.jruby.RubyInstanceConfig.CompileMode mode)
Changes a compile mode to a given mode, which should be one of CompileMode.JIT, CompileMode.FORCE, CompileMode.OFF. |
|
void |
setCurrentDirectory(java.lang.String directory)
Changes a current directory to a given directory. |
|
void |
setEnvironment(java.util.Map environment)
Changes an environment variables' map. |
|
void |
setError(java.io.PrintStream pstream)
Changes STDERR and $stderr to a given print stream. |
|
void |
setError(java.io.Writer writer)
Changes STDERR and $stderr to a given writer. |
|
void |
setErrorWriter(java.io.Writer errorWriter)
Replaces a standard error by a specified writer. |
|
void |
setHomeDirectory(java.lang.String home)
Changes a JRuby home directory to a directory of a given name. |
|
void |
setInput(java.io.InputStream istream)
Changes STDIN and $stdin to a given input stream. |
|
void |
setInput(java.io.Reader reader)
Changes STDIN and $stdin to a given reader. |
|
void |
setJitLogEvery(int logEvery)
Changes a value of n, so that jitted methods are logged in every n methods. |
|
void |
setJitMax(int max)
Changes a value of a max class cache size. |
|
void |
setJitMaxSize(int maxSize)
Changes a value of a max size of the bytecode generated by compiler. |
|
void |
setJitThreshold(int threshold)
Changes a value of the threshold that determines whether jitted methods' call reached to the limit or not. |
|
void |
setKCode(org.jruby.util.KCode kcode)
Changes a value of KCode to a given value. |
|
void |
setLoadPaths(java.util.List<java.lang.String> paths)
Changes a list of load paths Ruby scripts/libraries. |
|
void |
setLoadServiceCreator(org.jruby.RubyInstanceConfig.LoadServiceCreator creator)
Changes a LoadServiceCreator to a given one. |
|
void |
setObjectSpaceEnabled(boolean enable)
Changes the value to determine whether the Object Space is enabled or not. |
|
void |
setOutput(java.io.PrintStream pstream)
Changes STDOUT and $stdout to a given output stream. |
|
void |
setOutput(java.io.Writer writer)
Changes STDOUT and $stdout to a given writer. |
|
void |
setProfile(org.jruby.Profile profile)
Changes a Profile to a given one. |
|
void |
setReader(java.io.Reader reader)
Replaces a standard input by a specified reader |
|
void |
setRecordSeparator(java.lang.String separator)
Changes a record separator to a given value. |
|
void |
setRunRubyInProcess(boolean inprocess)
Changes the value to determine whether Ruby runs in a process or not. |
|
void |
setScriptFilename(java.lang.String filename)
Changes a script filename to run. |
|
void |
setWriter(java.io.Writer writer)
Replaces a standard output by a specified writer. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public ScriptingContainer()
public ScriptingContainer(LocalContextScope scope)
public ScriptingContainer(LocalVariableBehavior behavior)
public ScriptingContainer(LocalContextScope scope,
LocalVariableBehavior behavior)
public ScriptingContainer(LocalContextScope scope,
LocalVariableBehavior behavior,
java.lang.String propertyname)
A property file can have key-values pairs. If multiple values are associated to a key, each value is separated by comma.
Example container.ids=ruby, jruby language.extension=rb
scope - is one of a local context scope defined by LocalContextScopebehavior - is one of a local variable behavior defined by LocalVariableBehaviorpropertyname - is a property file name| Method Detail |
|---|
public java.util.List<java.lang.String> getLoadPaths()
getLoadPaths in interface EmbedRubyInstanceConfigAdapterpublic void setLoadPaths(java.util.List<java.lang.String> paths)
setLoadPaths in interface EmbedRubyInstanceConfigAdapterpaths - a new list of load paths.public java.io.InputStream getInput()
getInput in interface EmbedRubyInstanceConfigAdapterpublic void setInput(java.io.InputStream istream)
setInput in interface EmbedRubyInstanceConfigAdapteristream - an input stream to be setpublic void setInput(java.io.Reader reader)
setInput in interface EmbedRubyInstanceConfigAdapterreader - a reader to be setpublic java.io.PrintStream getOutput()
getOutput in interface EmbedRubyInstanceConfigAdapterpublic void setOutput(java.io.PrintStream pstream)
setOutput in interface EmbedRubyInstanceConfigAdapterpstream - an output stream to be setpublic void setOutput(java.io.Writer writer)
setOutput in interface EmbedRubyInstanceConfigAdapterwriter - a writer to be setpublic java.io.PrintStream getError()
getError in interface EmbedRubyInstanceConfigAdapterpublic void setError(java.io.PrintStream pstream)
setError in interface EmbedRubyInstanceConfigAdapterpstream - a print stream to be setpublic void setError(java.io.Writer writer)
setError in interface EmbedRubyInstanceConfigAdapterwriter - a writer to be setpublic org.jruby.RubyInstanceConfig.CompileMode getCompileMode()
getCompileMode in interface EmbedRubyInstanceConfigAdapterpublic void setCompileMode(org.jruby.RubyInstanceConfig.CompileMode mode)
setCompileMode in interface EmbedRubyInstanceConfigAdaptermode - compile modepublic boolean isRunRubyInProcess()
isRunRubyInProcess in interface EmbedRubyInstanceConfigAdapterpublic void setRunRubyInProcess(boolean inprocess)
setRunRubyInProcess in interface EmbedRubyInstanceConfigAdapterinprocess - true when Ruby is set to run in the process, or false not to
run in the process.public org.jruby.CompatVersion getCompatVersion()
getCompatVersion in interface EmbedRubyInstanceConfigAdapterpublic void setCompatVersion(org.jruby.CompatVersion version)
setCompatVersion in interface EmbedRubyInstanceConfigAdapterversion - a Ruby versionpublic boolean isObjectSpaceEnabled()
isObjectSpaceEnabled in interface EmbedRubyInstanceConfigAdapterpublic void setObjectSpaceEnabled(boolean enable)
setObjectSpaceEnabled in interface EmbedRubyInstanceConfigAdapterenable - true to enable the Object Space, or false to disable.public java.util.Map getEnvironment()
getEnvironment in interface EmbedRubyInstanceConfigAdapterpublic void setEnvironment(java.util.Map environment)
setEnvironment in interface EmbedRubyInstanceConfigAdapterenvironment - a new map of environment variables.public java.lang.String getCurrentDirectory()
getCurrentDirectory in interface EmbedRubyInstanceConfigAdapterpublic void setCurrentDirectory(java.lang.String directory)
setCurrentDirectory in interface EmbedRubyInstanceConfigAdapterdirectory - a new directory to be set.public java.lang.String getHomeDirectory()
getHomeDirectory in interface EmbedRubyInstanceConfigAdapterpublic void setHomeDirectory(java.lang.String home)
setHomeDirectory in interface EmbedRubyInstanceConfigAdapterhome - a name of new JRuby home directory.public org.jruby.util.ClassCache getClassCache()
getClassCache in interface EmbedRubyInstanceConfigAdapterpublic void setClassCache(org.jruby.util.ClassCache cache)
setClassCache in interface EmbedRubyInstanceConfigAdaptercache - a new ClassCache object to be set.public java.lang.ClassLoader getClassLoader()
getClassLoader in interface EmbedRubyInstanceConfigAdapterpublic void setClassLoader(java.lang.ClassLoader loader)
setClassLoader in interface EmbedRubyInstanceConfigAdapterloader - a new class loader to be set.public org.jruby.Profile getProfile()
getProfile in interface EmbedRubyInstanceConfigAdapterpublic void setProfile(org.jruby.Profile profile)
setProfile in interface EmbedRubyInstanceConfigAdapterprofile - a new profiler to be set.public org.jruby.RubyInstanceConfig.LoadServiceCreator getLoadServiceCreator()
getLoadServiceCreator in interface EmbedRubyInstanceConfigAdapterpublic void setLoadServiceCreator(org.jruby.RubyInstanceConfig.LoadServiceCreator creator)
setLoadServiceCreator in interface EmbedRubyInstanceConfigAdaptercreator - a new LoadServiceCreatorpublic java.lang.String[] getArgv()
getArgv in interface EmbedRubyInstanceConfigAdapterpublic void setArgv(java.lang.String[] argv)
setArgv in interface EmbedRubyInstanceConfigAdapterargv - a new arguments' list.public java.lang.String getScriptFilename()