Injecting native Java objects as Global Variables

I have this requirement that I need to inject native Java objects as
Global Variables.

If I have instantiate the runtime org.jruby.Ruby object instance, I
could do that as follows

IRubyObject globValue = JavaUtil.convertJavaToRuby(runtime, facade);
IRubyObject javaUtilities =
runtime.getObject().getConstant(“JavaUtilities”);
globValue = javaUtilities.callMethod(runtime.getCurrentContext(),
“wrap”, globValue);
GlobalVariable globVar = new GlobalVariable(runtime, “$”+partnerName,
globValue);
runtime.defineVariable(globVar);

But how do I achieve the same through
com.sun.script.jruby.JRubyScriptEngine interface? I do not see anyway I
could wrap the object to be usable on Ruby side?

thanks,
pankaj


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hi pankaj
if i understood You wright and You are using java 6, it can be done like
this:

public static ScriptEngine getEngine(Object root) {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine rubyEngine = m.getEngineByName(“jruby”);
rubyEngine.setContext(new SimpleScriptContext());
rubyEngine.getContext().setAttribute(“root”, root,
ScriptContext.ENGINE_SCOPE);
return rubyEngine;
}

and then in script You reference it as $root

Best greetings,
Paweł Wielgus.

Thanks Pawel and Aaron. That helped. I was prefixing $ in the name when
calling setAttribute. Your example snippets showed that I should remove
the $ prefix.

Related query: How do I supply RUBYLIB path to the JRubyScriptEngine
instance? Also how do I supply environment variables to the same
instance?


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Do You mean standard jruby libraries by rubylib?
If so just add jruby_home to classpath.
As for environment variables maybe the same way as root variable will
do the job.

Best greetings,
Paweł Wielgus.

Paweł Wielgus wrote:

Do You mean standard jruby libraries by rubylib?
If so just add jruby_home to classpath.
As for environment variables maybe the same way as root variable will
do the job.

Pawel,

I mean user has a own Ruby library, and wants to run a script which uses
this library. Now I need to create the ScriptEngine inside the server
JVM which would evaluate the script. But the ScriptEngine would have no
knowledge of the user Ruby library. So they need to provide something
like what RUBYLIB does to standalone Ruby interpreter.

I guess the ScriptContext injects global variables, and does not set the
ENV hash.

thanks,
pankaj


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hi,
I have this in my ruby script:

[“jruby-openssl-0.2.3”].each do |lib|
path = “#{ROOT}/lib/#{lib}/lib”
$LOAD_PATH.unshift(File.expand_path(path))
end

it loads jruby-openssl but i don’t know exactly how it works,
i’m not the author of that snippet,
ROOT is the base directory of where additional libraries are located
but what is LOAD_PATH i don’t know for sure.

I think it should work like in normal ruby script anyway.
If You won’t manage send me an info so i will ask author of that code.

Best greetings,
Paweł Wielgus.