diff --git a/core/src/main/java/org/jruby/embed/IsolatedScriptingContainer.java b/core/src/main/java/org/jruby/embed/IsolatedScriptingContainer.java index 85ce646a21f..c11ad250264 100644 --- a/core/src/main/java/org/jruby/embed/IsolatedScriptingContainer.java +++ b/core/src/main/java/org/jruby/embed/IsolatedScriptingContainer.java @@ -2,8 +2,8 @@ import java.net.URL; import java.util.Arrays; - -import org.osgi.framework.Bundle; +import java.util.HashMap; +import java.util.Map; /** * the IsolatedScriptingContainer detects the whether it is used with @@ -16,7 +16,7 @@ * * the root of the "main" classloader is add to LOAD_PATH and GEM_PATH. * - * in the OSGi case there are helper methods to add ClassLoaders to the LOAD_PATH or GEM_PATH + * in the OSGi case see the OSGiIsolatedScriptingContainer * * a typical setup for the ContextClassLoader case and OSGi case looks likes this: *
new URL( uri ).openStream()
, i.e. new URL(classloader.getResource().toString()).openStream()
has to work for
+ * those classloaders. felix, knoplerfish and equinox OSGi framework do work.
+ */
+public class OSGiIsolatedScriptingContainer extends IsolatedScriptingContainer {
+
+ public OSGiIsolatedScriptingContainer()
+ {
+ this(LocalContextScope.SINGLETON);
+ }
+
+ public OSGiIsolatedScriptingContainer( LocalContextScope scope,
+ LocalVariableBehavior behavior )
+ {
+ this(scope, behavior, true);
+ }
+
+ public OSGiIsolatedScriptingContainer( LocalContextScope scope )
+ {
+ this(scope, LocalVariableBehavior.TRANSIENT);
+ }
+
+ public OSGiIsolatedScriptingContainer( LocalVariableBehavior behavior )
+ {
+ this(LocalContextScope.SINGLETON, behavior);
+ }
+
+ public OSGiIsolatedScriptingContainer( LocalContextScope scope,
+ LocalVariableBehavior behavior,
+ boolean lazy )
+ {
+ super(scope, behavior, lazy);
+ }
+
+ private Bundle toBundle(String symbolicName) {
+ BundleContext context = FrameworkUtil.getBundle(getClass()).getBundleContext();
+ Bundle bundle = null;
+ for (Bundle b : context.getBundles()) {
+ if (b.getSymbolicName().equals(symbolicName)) {
+ bundle = b;
+ break;
+ }
+ }
+ if (bundle == null ) {
+ throw new RuntimeException("unknown bundle: " + symbolicName);
+ }
+ return bundle;
+ }
+
+ private String createUri(Bundle cl, String ref) {
+ URL url = cl.getResource( ref );
+ if ( url == null && ref.startsWith( "/" ) ) {
+ url = cl.getResource( ref.substring( 1 ) );
+ }
+ if ( url == null ) {
+ throw new RuntimeException( "reference " + ref + " not found on classloader " + cl );
+ }
+ return "uri:" + url.toString().replaceFirst( ref + "$", "" );
+ }
+ /**
+ * add the classloader from the given bundle to the LOAD_PATH
+ * @param bundle
+ */
+ public void addBundleToLoadPath(Bundle bundle) {
+ addLoadPath(createUri(bundle, "/.jrubydir"));
+ }
+
+ /**
+ * add the classloader from the given bundle to the LOAD_PATH
+ * using the bundle symbolic name
+ *
+ * @param symbolicName
+ */
+ public void addBundleToLoadPath(String symbolicName) {
+ addBundleToLoadPath(toBundle(symbolicName));
+ }
+
+ /**
+ * add the classloader from the given bundle to the GEM_PATH
+ * @param bundle
+ */
+ public void addBundleToGemPath(Bundle bundle) {
+ addGemPath(createUri(bundle, "/specifications/.jrubydir"));
+ }
+
+ /**
+ * add the classloader from the given bundle to the GEM_PATH
+ * using the bundle symbolic name
+ *
+ * @param symbolicName
+ */
+ public void addBundleToGemPath(String symbolicName) {
+ addBundleToGemPath(toBundle(symbolicName));
+ }
+}
diff --git a/maven/jruby-complete/src/templates/osgi_many_bundles_with_embedded_gems/test/src/test/java/org/jruby/embed/osgi/test/JRubyOsgiEmbedTest.java b/maven/jruby-complete/src/templates/osgi_many_bundles_with_embedded_gems/test/src/test/java/org/jruby/embed/osgi/test/JRubyOsgiEmbedTest.java
index 933b6e3ae47..71710421dd8 100644
--- a/maven/jruby-complete/src/templates/osgi_many_bundles_with_embedded_gems/test/src/test/java/org/jruby/embed/osgi/test/JRubyOsgiEmbedTest.java
+++ b/maven/jruby-complete/src/templates/osgi_many_bundles_with_embedded_gems/test/src/test/java/org/jruby/embed/osgi/test/JRubyOsgiEmbedTest.java
@@ -42,7 +42,7 @@
import org.jruby.embed.LocalContextScope;
import org.jruby.embed.LocalVariableBehavior;
import org.jruby.embed.ScriptingContainer;
-import org.jruby.embed.IsolatedScriptingContainer;
+import org.jruby.embed.osgi.OSGiIsolatedScriptingContainer;
import org.junit.Test;
import org.junit.Ignore;
import org.junit.runner.RunWith;
@@ -80,8 +80,8 @@ public void testJRubyCreate() throws Exception {
// System.setProperty( "jruby.debug.loadService", "true" );
//System.setProperty( "jruby.native.enabled", "true" );
- IsolatedScriptingContainer jruby = new IsolatedScriptingContainer();
- jruby.addLoadPath( Scripts.class.getClassLoader() );
+ OSGiIsolatedScriptingContainer jruby = new OSGiIsolatedScriptingContainer();
+ jruby.addBundleToLoadPath( "org.jruby.osgi.scripts-bundle" );
jruby.addBundleToGemPath( FrameworkUtil.getBundle( Gems.class ) );
// run a script from LOAD_PATH
diff --git a/maven/jruby/src/templates/osgi_all_inclusive/src/test/java/org/jruby/embed/osgi/test/JRubyOsgiEmbedTest.java b/maven/jruby/src/templates/osgi_all_inclusive/src/test/java/org/jruby/embed/osgi/test/JRubyOsgiEmbedTest.java
index a091fb6959a..0f5510790bc 100644
--- a/maven/jruby/src/templates/osgi_all_inclusive/src/test/java/org/jruby/embed/osgi/test/JRubyOsgiEmbedTest.java
+++ b/maven/jruby/src/templates/osgi_all_inclusive/src/test/java/org/jruby/embed/osgi/test/JRubyOsgiEmbedTest.java
@@ -38,7 +38,7 @@
import org.jruby.embed.LocalContextScope;
import org.jruby.embed.LocalVariableBehavior;
import org.jruby.embed.ScriptingContainer;
-import org.jruby.embed.IsolatedScriptingContainer;
+import org.jruby.embed.osgi.OSGiIsolatedScriptingContainer;
import org.junit.Test;
import org.junit.Ignore;
import org.junit.runner.RunWith;
@@ -67,7 +67,7 @@ public void testJRubyCreate() throws InterruptedException {
System.err.println();
// System.setProperty( "jruby.debug.loadService", "true" );
- IsolatedScriptingContainer jruby = new IsolatedScriptingContainer();
+ OSGiIsolatedScriptingContainer jruby = new OSGiIsolatedScriptingContainer();
// run a script from LOAD_PATH
String hello = (String) jruby.runScriptlet( "require 'hello'; Hello.say" );