About JRuby and OSGi

Hi dear JRuby folks,

I was wondering whether i could place here some questions of mine about
integrating JRuby with OSGi.
Basically, i am interested in whether there have been any attempts to
develop OSGi bundles, that contain Ruby code.
There could be different approaches for achieving that: for example, one
is
to define a proper packaging contract for a
Ruby bundle and to prepare the underlying OSGi runtime (even by
extending
the OSGi specification) so that it could
host such bundles. Other is to use some thin wrapping layer, that could
be
Java-based, just to glue the Ruby code to
the OSGi container. Maybe there could be invented a lot more ideas in
this
direction. Are you aware of similar efforts
being done by someone ? Have someone tried to make such experiments ? I
suppose that there could emerge
some issues with getting this to work cleanly. One of them could be
classloading/resource-related.

What could you kindly tell me about the analogy of class loading in Ruby
?
How could a Ruby program dynamically
load resources or even classes ? Is there such an analogous mechanism in
Ruby at all ?

Do things change in a Rails environment ? (Since Java EE application
servers
typically define complex hierarchy of
class loaders that can handle applications and their modules, and thus
one
could question himself whether Rails
defines some different class loading approaches from a ‘Ruby SE’
environment
:slight_smile: )

And what about JRuby ? How does it handle class loading ? If i have a
JRuby-based OSGi bundle, could i somehow
force it to use the underlying OSGi class loading ? Or not ?

Thank you for your attention,
Krum.

I hadn’t noticed this until now…replies inline below…

On Tue, May 11, 2010 at 7:42 AM, Krum B. [email protected]
wrote:

Hi dear JRuby folks,

I was wondering whether i could place here some questions of mine about
integrating JRuby with OSGi.

The JRuby jars we publish have been "bnd"ed so they can be used with
OSGi, but that’s about as far as we have gone.

being done by someone ? Have someone tried to make such experiments ? I
suppose that there could emerge
some issues with getting this to work cleanly. One of them could be
classloading/resource-related.

The closest analog, I suppose, would be mkristian’s work on
integrating RubyGems and Maven:

http://github.com/mkristian/jruby-maven-plugins

There, he’s basically taking the package information from the RubyGem
and converting it to Maven (pom) data, with an explicit dependency on
JRuby of course. This seems like a pretty good approach, since it
single-sources the dependencies and project metadata from RubyGems
themselves.

He also has the reverse direction, making Maven artifacts appear to be
RubyGems, There could be something equivalent for non-OSGi JRuby
bundles.

I’m very interested in trying to make JRuby work better in an OSGi
environment, but I frankly have almost no knowledge of how OSGi is
structured. You’d definitely have support from JRuby proper if you
wanted to try to make this work well.

What could you kindly tell me about the analogy of class loading in Ruby ?
How could a Ruby program dynamically
load resources or even classes ? Is there such an analogous mechanism in
Ruby at all ?

Most Ruby code is loaded explicitly at runtime by filename, e.g.
“require ‘path/to/my/library’” looking for path/to/my/library.rb
somewhere in the load path. The load path is roughly analogous to a
class path, and so then the filename given to require is roughly
analogous to a classloader resource lookup (and when completely
bundled into jar files, that’s what it actually is in JRuby too).

Under normal operation, Ruby code will be interpreted for a while and
then piece of it will compile to JVM bytecode, loaded under a child
classloader of whatever classloader loaded JRuby. These classes are
mostly just “bags of methods” and have no real utility to Java
consumers; they’re just a vector to get bytecode-compiled Ruby code
loaded into memory.

There are newer options in JRuby (and in the jrubyc compiler) to
precompile Ruby scripts entirely ahead-of-time. In that case, you’d
precompile them and just bundle everything as part of some jar file.

The classloading has been a tricky issue for OSGi and JRuby in the
past, but we have never really understood how to do it properly. We
basically need to be able to load new bytecode at runtime to speed hot
code up, or else we need to iron out remaining challenges in the
ahead-of-time compilation process. Maybe you can help us understand
OSGi’s requirements here.

Do things change in a Rails environment ? (Since Java EE application servers
typically define complex hierarchy of
class loaders that can handle applications and their modules, and thus one
could question himself whether Rails
defines some different class loading approaches from a ‘Ruby SE’ environment
:slight_smile: )

Rails applications deployed to production generally don’t change much
after their initial boot cycle. Methods may come into existence later,
like if a rarely-used controller calls a new “find” variant on an
ActiveRecord model. Much of the Rails + application code will exist in
a static form on disk, but some is generated after startup as part of
the boot process or as part of lazily-created methods and adapters.

It has always been my hope that we could unify the load path and class
path concepts, but we have so far not attempted this. There’s also
still a small but important need for “eval” to work, which means that
under many circumstances JRuby will need to be able to load new
bytecode at runtime, but it is also possible to just leave it
interpreted if the performance cost is not too high.

And what about JRuby ? How does it handle class loading ? If i have a
JRuby-based OSGi bundle, could i somehow
force it to use the underlying OSGi class loading ? Or not ?

JRuby proper doesn’t do much for classloading tricks. Where things get
complicated are when loading jitted code (as described above) or when
people dynamically load jar files into Ruby applications (a la
“require ‘something.jar’”) which adds something.jar (located somewhere
in the load path) to a JRuby-instance-wide URLClassLoader. The former
case may present challenges to OSGi, but the loaded code should only
depend upon JRuby proper, and therefore could be seen as a
wholly-contained child domain. The latter would definitely require
some OSGi love, since ideally those required libraries would get
sourced out of OSGi (or at least would not conflict with how OSGi
wants to locate and load resources).

And I’ll say it again: I’d love it if we played better in an OSGi
environment, and we will entertain any suggestions to help us get
there. We’re just not really OSGi experts, so we need help deciding
what direction to go and what changes need to be made :slight_smile:

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hi Gergo

Sounds interesting - but could you elaborate a bit? Why/how would you
change the “packaging contract”, as opposed to have a handy packaging
tool for OSGI ruby.
JRuby is becoming such a nice Java citizen (with the gotchas Charlie
mentioned), so OSGI framework may/should not have to know much about
Ruby.

I was just shooting at random. This is a very new idea of mine, and i
haven’t spent any time and effort to make it more clear. It would be
worth
exploring this opportunity if i had enough time and innovation spirit,
which, i rather lack right now.

I’ve done something like that with Equinox (Eclipse’s OSGI container)

  • as many others I guess.
    Not sure if this is relevant for you, but I had
    • a org.jruby bundle
    • a com.sun.script (for JSR233 framework)
    • a com.sun.script.jruby (for JRubyScriptingEngine&co)
    • my own bundles containing Ruby code and some Java to load them

This works fine, just going to drop the JSR223 baggage in favor of the
new embed API. Haven’t run into any jit-related or similar issues.
The classloading trick was to use Eclipse-BuddyPolicy to let org.jruby
access the scripts without explicit dependencies on their bundles.
I’d be curious of any generic, pure-OSGI solution for this.

That’s very very interesting to me! Are you saying that you have found a
generic way to make a JRuby application run inside Equinox, being
modularized (bundlized) at the same time ?
What about those ‘some Java to load them’ code ? Is it specific for your
bundles ? Could it be easily extended to support arbitrary Ruby script
and
thus, to make it available as a bundle ?

Thanks,
Krum.


View this message in context:
http://old.nabble.com/About-JRuby-and-OSGi-tp28520964p28581085.html
Sent from the JRuby - User mailing list archive at Nabble.com.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hi Krum,

2010/5/11 Krum B. [email protected]:

to define a proper packaging contract for a
Ruby bundle and to prepare the underlying OSGi runtime (even by extending
the OSGi specification) so that it could
host such bundles.

Sounds interesting - but could you elaborate a bit? Why/how would you
change the “packaging contract”, as opposed to have a handy packaging
tool for OSGI ruby.
JRuby is becoming such a nice Java citizen (with the gotchas Charlie
mentioned), so OSGI framework may/should not have to know much about
Ruby.

Other is to use some thin wrapping layer, that could be
Java-based, just to glue the Ruby code to
the OSGi container. Maybe there could be invented a lot more ideas in this
direction. Are you aware of similar efforts
being done by someone ? Have someone tried to make such experiments ? I
suppose that there could emerge
some issues with getting this to work cleanly. One of them could be
classloading/resource-related.

I’ve done something like that with Equinox (Eclipse’s OSGI container)

  • as many others I guess.
    Not sure if this is relevant for you, but I had
    • a org.jruby bundle
    • a com.sun.script (for JSR233 framework)
    • a com.sun.script.jruby (for JRubyScriptingEngine&co)
    • my own bundles containing Ruby code and some Java to load them

This works fine, just going to drop the JSR223 baggage in favor of the
new embed API. Haven’t run into any jit-related or similar issues.
The classloading trick was to use Eclipse-BuddyPolicy to let org.jruby
access the scripts without explicit dependencies on their bundles.
I’d be curious of any generic, pure-OSGI solution for this.

Good luck,

Gergo


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

I am currently experimenting with the same thing, but more generally I
am interested in supporting OSGI service implementation bundles that are
implemented in any javax.script (jsr223)Â conforming scripting language.
I am thinking of doing this using the OSGI extender pattern.

Unfortunately, JRuby 1.5 won’t work with OSGI because it uses
getContextClassLoader a few places (5 according to my latest search).
Use of the context class loader is not allowed in (standard) OSGI and
means that JRuby is incompatible with OSGI (non-portable hacks aside).
At least for 1 of these 5 usages of the context class loader is
critical, namely in the jsr233 support (see bug
http://jira.codehaus.org/browse/JRUBY-4806).

I will be happy to co-operate with you regarding jsr223, jruby and OSGI
but first I think we need the JRuby team to fix it’s classloader issues
(Properly easy - I guess it is an awareness thing).

/Morten

— Den tirs 11/5/10 skrev Krum B. [email protected]:

Fra: Krum B. [email protected]
Emne: [jruby-user] About JRuby and OSGi
Til: [email protected]
Dato: tirsdag 11. maj 2010 09.42

Hi dear JRuby folks,

I was wondering whether i could place here some questions of mine about
integrating JRuby with OSGi.
Basically, i am interested in whether there have been any attempts to
develop OSGi bundles, that contain Ruby code.

There could be different approaches for achieving that: for example, one
is to define a proper packaging contract for a
Ruby bundle and to
prepare the underlying OSGi runtime (even by extending the OSGi
specification) so that it could
host such bundles. Other is to use some thin wrapping layer, that could
be Java-based,
just to glue the Ruby code to
the OSGi container. Maybe there could be
invented a lot more ideas in this direction. Are you aware of similar
efforts
being done by someone ? Have someone tried to make such
experiments ? I suppose that there could emerge
some issues with getting
this to work cleanly. One of them could be
classloading/resource-related.

What could you kindly tell me about the analogy of class loading in Ruby
? How
could a Ruby program dynamically
load resources or even classes ? Is
there such an analogous mechanism in Ruby at all ?

Do things
change in a Rails environment ? (Since Java EE application servers
typically define complex hierarchy of
class loaders that can handle
applications and their modules, and thus one could question himself
whether Rails
defines some different class loading approaches from a
‘Ruby SE’ environment :slight_smile: )

And what about JRuby ? How does it handle class loading ? If i
have a JRuby-based OSGi bundle, could i somehow
force it to use the
underlying OSGi class loading ? Or not ?

Thank you for your attention,
Krum.

Hi Morten,

On Fri, May 21, 2010 at 2:21 PM, Morten [email protected] wrote:

jsr233 support (see bug http://jira.codehaus.org/browse/JRUBY-4806).

This is exactly the same bug filed,
http://jira.codehaus.org/browse/JRUBY-4798. Not like embed core, there’s
no
workaround for JSR223 impl. So, maybe, we need JRuby 1.5.1?

I will be happy to co-operate with you regarding jsr223, jruby and OSGI but
first I think we need the JRuby team to fix it’s classloader issues
(Properly easy - I guess it is an awareness thing).

Yes, classloader problem is the real cause of this issue. I mean setting
null to context class loader to get things work should not be needed.
Have
you looked at http://jira.codehaus.org/browse/JRUBY-4774 that Paul
mention
in JRUBY-4798? I want to figure out why OSGi and some other platforms
that
use custom classloader doesn’t work without such workaround.

…and, nice to have your help. This bug was there since 1.5.0.RC1. If
you
could use from RC version, the bug was gone before the final release.
I’m
happy if you will try RC version on OSGi platform and report a bug for
later
releases.

-Yoko

Krum,

2010/5/17 recursion [email protected]:

It would be worth exploring this opportunity if i had enough time and innovation spirit,
which, i rather lack right now.

Right, I agree it’d be cool to have a streamlined way to create
(almost) pure Ruby bundles. It doesn’t sound impossible at least.
However, I should have mentioned that I’m no OSGI expert and fairly
new to JRuby too.

access the scripts without explicit dependencies on their bundles.
I’d be curious of any generic, pure-OSGI solution for this.

That’s very very interesting to me! Are you saying that you have found a
generic way to make a JRuby application run inside Equinox, being
modularized (bundlized) at the same time ?
Well, my app has been still basically a Java/Eclipse one, with some
tasks delegated to JRuby parts which then called back to Java.
Although I was surprised on how easy this was and how nicely Jruby
played with rest of the stuff (integration works as a charm), this
wasn’t anything new.
What I haven’t done much of yet is to implement java classes from
JRuby, partly because I was stuck with Jruby 1.2.
Even with that old version, plugging into the platform works great by
just extending Java interfaces within JRuby, and returning/registering
those (proxy) objects back to to Java frameworks programmatically
(as opposed to declaratively). I expect this to be even more fun with
1.5 and the become_java! sweetness.

What about those ‘some Java to load them’ code ? Is it specific for your
bundles ?

Well, they are kind of specific: building a java object structure for
my model, with domain-specific interfaces.
Other than that, it’s just using the standard JRuby embedding APIs to
run the scripts (so far JSR223, but can’t want to 1.5 for this as
well).

Could it be easily extended to support arbitrary Ruby script and
thus, to make it available as a bundle ?

I guess what you (and me, later:) are looking for is more direct
integration, pure-or-mostly-Ruby bundles, even at declarative places
where Java classes are expected, eg. OSGI activators or Eclipse
extensions.
… Well I remember some guys actually had success with the latter
(through some trick using a simple generic java factory class + a
script location combo in the plugin.xml, rather than a plain FQCN).
I’m not sure if something like that is possible with OSGI, but it
would be great. I hope to find some time to look into this soon.

Good luck,
Gergo


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email