Any guides to setting up jRuby within OSGi?

I’m trying to enable some ruby scripting (and hopefully eventually
webapps) from within an OSGi environment.

I can see that jruby-1.6.0 includes code to support OSGi [1] but I can’t
find much documentation on it. Is there a beginner’s guide somewhere?

As a trial I’ve created a bundle with a single Component (I’m using OSGi
Declarative Services):

@Component
public class RubyHWRunner {

    @Activate
    public void start(ComponentContext context) {
        Bundle bundle = context.getBundleContext().getBundle();
        OSGiScriptingContainer container
            = new OSGiScriptingContainer(bundle);
        container.runScriptlet("/ruby/hello_world.rb");
    }
}

where the /ruby/hello_world.rb is a trivial ruby script which I’ve
included within the bundle.

When the component is activated I get two exceptions:

(1) java.lang.ClassCastException: java.util.ArrayList cannot be cast to
[Ljava.lang.Object;
at
org.jruby.embed.osgi.utils.OSGiBundleClassLoaderHelper.internalGetFelixBundleClassLoader(OSGiBundleClassLoaderHelper.java:161)
at
org.jruby.embed.osgi.utils.OSGiBundleClassLoaderHelper.getBundleClassLoader(OSGiBundleClassLoaderHelper.java:110)
at
org.jruby.embed.osgi.internal.JRubyOSGiBundleClassLoader.addBundle(JRubyOSGiBundleClassLoader.java:103)
at
org.jruby.embed.osgi.internal.JRubyOSGiBundleClassLoader.(JRubyOSGiBundleClassLoader.java:78)
at
org.jruby.embed.osgi.OSGiScriptingContainer.(OSGiScriptingContainer.java:86)
at
org.jruby.embed.osgi.OSGiScriptingContainer.(OSGiScriptingContainer.java:73)
at
com.epimorphics.osgitest.ruby.RubyHWRunner.start(RubyHWRunner.java:25)

This one is specific to felix. If I switch to using equinox as the run
time then it goes away. Does this means that the jruby osgi isn’t
compatible with felix?

(2) java.io.IOException: Unable to find the /META-INF/jruby.home folder
in the bundle ‘org.jruby.jruby’; is the org.jruby.jruby bundle unzipped?
at
org.jruby.embed.osgi.utils.OSGiFileLocator.getFileInBundle(OSGiFileLocator.java:72)

This one happens whichever runtime I use.

This fits with the javadoc comment “Setup of jruby home pointing at the
jruby bundle by default. Supporting unzipped jruby bundle for now.” [2]

However, sorry for the stupid question (I’m something of newbie to OSGi)
but how can I set up a suitable “unzipped jruby bundle”?

I’m using eclipse + bndtools as my dev environment. If I put a suitably
renamed jruby.jar in my bndtools repository
(…/cnf/repo/org.jruby.jruby) I can load the jruby bundle fine but if I
unjar that jar into the same place I can’t persuade the runtime to
accept it (tested for both equinox and felix). I’ve tried setting an
explicit felix.auto.deploy.dir and putting the unjared jruby.jar in
there but to no avail.

Dave

[1] http://jira.codehaus.org/browse/JRUBY-5384
[2]
http://www.jruby.org/apidocs/org/jruby/embed/osgi/OSGiScriptingContainer.html

Update …

Seems like I had three problems, at least two my fault :frowning:

(1) Looks like I used the base jruby.jar instead of jruby-complete.jar
which is why there wasn’t a jruby.home to find.

(2) Using felix as the runtime gives multiple errors which largely
disappear under equinox (I would prefer to keep the option to use felix
so if anyone has workaround suggestions that would be great).

(3) The code I was running was borked, runScriplet takes ruby source not
a ruby file name, doh.

So under equinox I can now get at least a trivial example to run.

@Component
public class RubyHWRunner {

@Activate
public void start(ComponentContext context) {
    Bundle bundle = context.getBundleContext().getBundle();
    OSGiScriptingContainer container = new 

OSGiScriptingContainer(bundle);
container.runScriptlet(“puts ‘foobar’”);
}
}

does indeed print “foobar” when the container starts.

Under felix the same working example gives:

(1) java.lang.ClassCastException: java.util.ArrayList cannot be cast to
[Ljava.lang.Object;
at
org.jruby.embed.osgi.utils.OSGiBundleClassLoaderHelper.internalGetFelixBundleClassLoader(OSGiBundleClassLoaderHelper.java:161)
at
org.jruby.embed.osgi.utils.OSGiBundleClassLoaderHelper.getBundleClassLoader(OSGiBundleClassLoaderHelper.java:110)
at
org.jruby.embed.osgi.internal.JRubyOSGiBundleClassLoader.addBundle(JRubyOSGiBundleClassLoader.java:103)
at
org.jruby.embed.osgi.internal.JRubyOSGiBundleClassLoader.(JRubyOSGiBundleClassLoader.java:78)
at
org.jruby.embed.osgi.OSGiScriptingContainer.(OSGiScriptingContainer.java:86)
at
org.jruby.embed.osgi.OSGiScriptingContainer.(OSGiScriptingContainer.java:73)
at
com.epimorphics.osgitest.ruby.RubyHWRunner.start(RubyHWRunner.java:25)

(2) java.io.IOException: Unable to find the /META-INF/jruby.home folder
in the bundle ‘org.jruby.jruby’
at
org.jruby.embed.osgi.utils.OSGiFileLocator.getFileInBundle(OSGiFileLocator.java:75)
at
org.jruby.embed.osgi.utils.OSGiFileLocator.getFileInBundle(OSGiFileLocator.java:64)
at
org.jruby.embed.osgi.utils.OSGiFileLocator.getJRubyHomeFolder(OSGiFileLocator.java:56)
at
org.jruby.embed.osgi.OSGiScriptingContainer.(OSGiScriptingContainer.java:92)
at
org.jruby.embed.osgi.OSGiScriptingContainer.(OSGiScriptingContainer.java:73)
at
com.epimorphics.osgitest.ruby.RubyHWRunner.start(RubyHWRunner.java:25)

Caused by: java.lang.IllegalArgumentException: URI scheme is not “file”
at java.io.File.(File.java:366)
at
org.jruby.embed.osgi.utils.OSGiFileLocator.getFileInBundle(OSGiFileLocator.java:70)
… 22 more

Dave

Any updates on this?

As far as I can tell the jRuby OSGi support remains specific to the
Equinox runner and does not work under Felix.

Should I open a Jira?

Dave

Just curious why is JRuby limitted to Equinox ?

On Thu, Aug 11, 2011 at 3:11 AM, Dave R. [email protected]
wrote:

Any updates on this?

As far as I can tell the jRuby OSGi support remains specific to the
Equinox runner and does not work under Felix.

Should I open a Jira?

I know it’s definitely not intended that JRuby would only work under
Equinox. If you’ve got the source for a full test case available, I’d
be happy to take a look. Specifically, what version of felix are you
attempting to run under?