[Q] in too deep?

Here is how my objects are instantiated. (jruby -> rails -> java ->
scripting engine (jruby) -> ruby)

Rails get started with ‘jruby -S script/server -e production’
Rails initializer has a java object creation.
The java service creation involves reading a ruby class file (from java
code) and instantiate it a java service implementor.
This is done by calling ScriptEngine’s eval.

Now I was wondering if I can get hold of rails environments from this
ruby class.
Somewhere I read if you can getCurrentInstance() of jruby, you can read
constant RAILS_ENV from kernel, but org.jruby.Ruby.getCurrentInstance()
returns null.

What I’d like to get out of this is the value of RAILS_ENV and database
configurations for that environment.
Is it possible to get them?

Thanks,
-Kevin

On Thu, Jan 14, 2010 at 1:13 PM, Kevin S. Lim [email protected] wrote:

constant RAILS_ENV from kernel,
but org.jruby.Ruby.getCurrentInstance() returns null.
What I’d like to get out of this is the value of RAILS_ENV and database
configurations for that environment.
Is it possible to get them?

I think in this case the problem is that the ScriptEngine doesn’t know
anything about the JRuby instance that called out to Java. If it were
a classloader-global runtime (see my other thread on this possibility)
or if there’s a way to get the ScriptEngine to look somewhere specific
(threadlocal?) it may be possible to do this.

Yoko, what do you think?

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

On Thu, Jan 14, 2010 at 4:45 PM, Charles Oliver N.
[email protected] wrote:

Somewhere I read if you can getCurrentInstance() of jruby, you can read
(threadlocal?) it may be possible to do this.
Sort of. ScriptEngine doesn’t expose Ruby runtime rather than it
doesn’t know. So, users can’t use Ruby runtime directly on JSR223.

Kevin,
If you are explicitly using RAILS_ENV environment variable in your
Ruby code, you might be able to get the value by setting local
variable to transient before you get engine factory,

System.setProperty(“org.jruby.embed.localvariable.behavior”,
“transient”);

and calling get method after the evaluation,

Object obj = engine.get(“RAILS_ENV”);

At least, you need to use RAILS_ENV and its value explicitly in Ruby
code like, return ENV[‘RAILS_ENV’].

Hope this helps.
-Yoko


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Thanks Yoko and Charles,

What if I change it not to use jsr223 ScriptEngine but use direct jruby
(even though I read it’s not recommended?)?
That is from

jruby -> rails -> java -> scripting engine -> ruby

to

jruby -> rails -> java -> jruby (use evalScriptlet) -> ruby

Would it be possible to get to the rails environment then?

This brings me another question actually.
While back, when I tried this, the second jruby runtime was created by

runtime = Ruby.newInstance(); // because getCurrentInstance() is
deprecated?

And I had thought that this new runtime instance is different than the
original jruby runtime which started rails.
From the java code(third one on above sequences), can I get the original
jruby runtime or rails environments?

Thanks,
Kevin

On Jan 14, 2010, at 3:48 PM, Yoko H. wrote:

Now I was wondering if I can get hold of rails environments from this ruby
a classloader-global runtime (see my other thread on this possibility)

-Yoko


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Yoko,

Now ‘moving to classloader-global-runtime’ sounds promising to me :slight_smile:

I will try the system property passing you suggested, for now.

Thanks bunch!

-Kevin

On Jan 14, 2010, at 6:38 PM, Yoko H. wrote:

to
And I had thought that this new runtime instance is different than the original jruby runtime which started rails.
be able to return the same instance as the original runtime.
This is just an example, so you have other couple of options, I think.

On Thu, Jan 14, 2010 at 4:45 PM, Charles Oliver N.

class.
or if there’s a way to get the ScriptEngine to look somewhere specific
System.setProperty(“org.jruby.embed.localvariable.behavior”, “transient”);


http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hi Kevin,

On Thu, Jan 14, 2010 at 8:09 PM, Kevin S. Lim [email protected] wrote:

Would it be possible to get to the rails environment then?

This brings me another question actually.
While back, when I tried this, the second jruby runtime was created by

runtime = Ruby.newInstance(); // because getCurrentInstance() is deprecated?

And I had thought that this new runtime instance is different than the original jruby runtime which started rails.
From the java code(third one on above sequences), can I get the original jruby runtime or rails environments?

Ah, I honestly understood what you wanted to do, now.
Yes. Currently, Ruby runtime isn’t a static, so you’ll get another
runtime instance. This happens even though you use
org.jruby.Ruby.newInstacne() directly.

We had a discussion recently about a static Ruby runtime,
http://old.nabble.com/Moving-to-a-classloader-global-runtime!-td27045093.html.
When we have this new runtime, JRuby Embed as well as JSR223 impl will
be able to return the same instance as the original runtime.

But, we don’t have such kind of runtime right now, so you probably
need a trick. For example, using Java’s system property:

in a code on rails:
java.lang.System.set_property(“RAILS_ENV”, value)

in another code:
java.lang.System.get_property(“RAILS_ENV”)

This is just an example, so you have other couple of options, I think.

-Yoko

Here is how my objects are instantiated. (jruby -> rails -> java ->
but org.jruby.Ruby.getCurrentInstance() returns null.
Sort of. ScriptEngine doesn’t expose Ruby runtime rather than it


http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs