Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.jruby.embed;
  
  import java.net.URL;
  import java.util.Arrays;
  
the IsolatedScriptingContainer detects the whether it is used with a Thread.currentThread.contextClassLoader (J2EE) or with the classloader which loaded IsolatedScriptingContainer.class (OSGi case) the setup of LOAD_PATH and GEM_PATH and JRUBY_HOME uses ONLY uri: or uri:classloader: protocol paths. i.e. everything lives within one or more classloaders - no jars added from jave.class.path or similar "magics" 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 a typical setup for the ContextClassLoader case and OSGi case looks likes this:
  • LOAD_PATH == [ "uri:classloader:/META-INF/jruby.home/lib/ruby/1.9/site_ruby", "uri:classloader:/META-INF/jruby.home/lib/ruby/shared", "uri:classloader:/META-INF/jruby.home/lib/ruby/1.9", "uri:classloader:" ]
  • Gem::Specification.dirs == [ "uri:classloader:/specifications", "uri:classloader:/META-INF/jruby.home/lib/ruby/gems/shared/specifications" ] here very resource is loaded via Thread.currentTHread.getContextClassLoader().getResourceAsStream(...) 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. NOTE: Gem.path is base for determine the Gem::Specification.dirs and Gem::Specification.dirs is used to find gemspec files of the installed gems.
  •  
     public class IsolatedScriptingContainer extends ScriptingContainer {
     
         private static final String JRUBYDIR = "/.jrubydir";
         private static final String JRUBY_HOME = "/META-INF/jruby.home";
         
         public IsolatedScriptingContainer()
         {
             this(.);
         }
     
                                            LocalVariableBehavior behavior )
         {
             this(scopebehaviortrue);
         }
     
         public IsolatedScriptingContainerLocalContextScope scope )
         {
             this(scope.);
         }
     
         public IsolatedScriptingContainerLocalVariableBehavior behavior )
         {
             this(.behavior);
         }
     
                                            LocalVariableBehavior behavior,
                                            boolean lazy )
         {
             super(scopebehaviorlazy);
     
             // get the right classloader
             ClassLoader cl = this.getClass().getClassLoader();
             if (cl == nullcl = Thread.currentThread().getContextClassLoader();
             setClassLoadercl );
     
             setLoadPaths( Arrays.asList"uri:classloader:" ) );
     
             // set the right jruby home
             setHomeDirectory"uri:classloader:" +  );
     
             // setup the isolated GEM_PATH, i.e. without $HOME/.gem/**
             runScriptlet("require 'rubygems/defaults/jruby';"
                     + "Gem::Specification.reset;"
                     + "Gem::Specification.add_dir 'uri:classloader:" +  + "/lib/ruby/gems/shared';"
                     + "Gem::Specification.add_dir 'uri:classloader:';");
         }
     
         public void addLoadPathClassLoader cl ) {
             addLoadPathcl );
         }
     
         public void addLoadPathClassLoader clString ref ) {
             addLoadPath(createUri(clref));
         }
     
         public void addBundleToLoadPathBundle cl ) {
             addBundleToLoadPathcl );
         }
     
         public void addBundleToLoadPathBundle clString ref ) {
             addLoadPath(createUriFromBundle(clref));
         }
     
        private String createUriFromBundleBundle clString ref) {
            URL url = cl.getResourceref );
            if ( url == null && ref.startsWith"/" ) ) {
                url = cl.getResourceref.substring( 1 ) );
            }
            if ( url == null ) {
                throw new RuntimeException"reference " + ref + " not found on bundle " + cl );
            }
            return "uri:" + url.toString().replaceFirstref + "$""" );
        }
        private void addLoadPath(String uri) {
            runScriptlet"$LOAD_PATH << '" + uri + "' unless $LOAD_PATH.member?( '" + uri + "' )" );
        }
        public void addBundleToGemPathBundle cl ) {
            addBundleToGemPathcl"/specifications" +  );
        }
        public void addBundleToGemPathBundle clString ref ) {
            addGemPath(createUriFromBundle(clref));
        }
        public void addGemPathClassLoader cl ) {
            addGemPathcl"/specifications" +  );
        }
        public void addGemPathClassLoader clString ref ) {
            addGemPath(createUri(clref));
        }
        private String createUri(ClassLoader clString ref) {
            URL url = cl.getResourceref );
            if ( url == null && ref.startsWith"/" ) ) {
                url = cl.getResourceref.substring( 1 ) );
            }
            if ( url == null ) {
                throw new RuntimeException"reference " + ref + " not found on classloader " + cl );
            }
            return "uri:" + url.toString().replaceFirstref + "$""" );
        }
        private void addGemPath(String uri) {
            runScriptlet"Gem::Specification.add_dir '" + uri + "' unless Gem::Specification.dirs.member?( '" + uri + "' )" );
        }
    New to GrepCode? Check out our FAQ X