|
1 | 1 | package org.jruby.embed;
|
2 | 2 |
|
3 |
| -import java.net.URL; |
4 | 3 | import java.util.Arrays;
|
5 | 4 | import java.util.HashMap;
|
6 | 5 | import java.util.Map;
|
7 | 6 |
|
8 |
| -import org.osgi.framework.Bundle; |
9 |
| - |
10 | 7 | /**
|
11 |
| - * the IsolatedScriptingContainer detects the whether it is used with |
12 |
| - * a Thread.currentThread.contextClassLoader (J2EE) or with the classloader |
13 |
| - * which loaded IsolatedScriptingContainer.class (OSGi case) |
14 |
| - * |
15 |
| - * the setup of LOAD_PATH and GEM_PATH and JRUBY_HOME uses ONLY uri: or uri:classloader: |
16 |
| - * protocol paths. i.e. everything lives within one or more classloaders - no jars added from |
17 |
| - * jave.class.path or similar "magics" |
18 |
| - * |
19 |
| - * the root of the "main" classloader is add to LOAD_PATH and GEM_PATH. |
20 |
| - * |
21 |
| - * in the OSGi case there are helper methods to add ClassLoaders to the LOAD_PATH or GEM_PATH |
| 8 | + * the IsolatedScriptingContainer does set GEM_HOME and GEM_PATH and JARS_HOME |
| 9 | + * in such a way that it uses only resources which can be reached with classloader. |
22 | 10 | *
|
23 |
| - * a typical setup for the ContextClassLoader case and OSGi case looks likes this: |
24 |
| - * <li>LOAD_PATH == [ "uri:classloader:/META-INF/jruby.home/lib/ruby/1.9/site_ruby", |
25 |
| - * "uri:classloader:/META-INF/jruby.home/lib/ruby/shared", |
26 |
| - * "uri:classloader:/META-INF/jruby.home/lib/ruby/1.9", |
27 |
| - * "uri:classloader:" ]</li> |
28 |
| - * <li>Gem::Specification.dirs == [ "uri:classloader:/specifications", "uri:classloader:/META-INF/jruby.home/lib/ruby/gems/shared/specifications" ] |
29 |
| - * here very resource is loaded via <code>Thread.currentTHread.getContextClassLoader().getResourceAsStream(...)</code> |
| 11 | + * GEM_HOME is uri:classloader://META-INF/jruby.home/lib/ruby/gems/shared |
| 12 | + * GEM_PATH is uri:classloader:// |
| 13 | + * JARS_HOME is uri:classloader://jars |
30 | 14 | *
|
31 |
| - * <code>new URL( uri ).openStream()</code>, i.e. <code>new URL(classloader.getResource().toString()).openStream()</code> has to work for |
32 |
| - * those classloaders. felix, knoplerfish and equinox OSGi framework do work. |
33 |
| - * |
34 |
| - * NOTE: <code>Gem.path</code> is base for determine the <code>Gem::Specification.dirs</code> and <code>Gem::Specification.dirs</code> is |
35 |
| - * used to find gemspec files of the installed gems. |
| 15 | + * but whenever you want to set them via {@link #setEnvironment(Map)} this will be honored. |
36 | 16 | */
|
37 | 17 | public class IsolatedScriptingContainer extends ScriptingContainer {
|
38 | 18 |
|
39 |
| - private static final String JRUBYDIR = "/.jrubydir"; |
40 | 19 | private static final String JRUBY_HOME = "/META-INF/jruby.home";
|
41 | 20 |
|
42 | 21 | public IsolatedScriptingContainer()
|
@@ -74,75 +53,16 @@ public IsolatedScriptingContainer( LocalContextScope scope,
|
74 | 53 |
|
75 | 54 | @Override
|
76 | 55 | public void setEnvironment(Map environment) {
|
77 |
| - if (environment == null || !environment.containsKey("GEM_PATH")) { |
78 |
| - Map env = environment == null ? new HashMap() : new HashMap(environment); |
79 |
| - env.put("GEM_PATH", ""); |
| 56 | + if (environment == null || !environment.containsKey("GEM_PATH") |
| 57 | + || !environment.containsKey("GEM_HOME")|| !environment.containsKey("JARS_HOME")) { |
| 58 | + Map<String,String> env = environment == null ? new HashMap<String,String>() : new HashMap<String,String>(environment); |
| 59 | + if (!env.containsKey("GEM_PATH")) env.put("GEM_PATH", "uri:classloader://"); |
| 60 | + if (!env.containsKey("GEM_HOME")) env.put("GEM_HOME", "uri:classloader:/" + JRUBY_HOME); |
| 61 | + if (!env.containsKey("JARS_HOME")) env.put("JARS_HOME", "uri:classloader://jars"); |
80 | 62 | super.setEnvironment(env);
|
81 | 63 | }
|
82 | 64 | else {
|
83 | 65 | super.setEnvironment(environment);
|
84 | 66 | }
|
85 | 67 | }
|
86 |
| - |
87 |
| - public void addLoadPath( ClassLoader cl ) { |
88 |
| - addLoadPath( cl, JRUBYDIR ); |
89 |
| - } |
90 |
| - |
91 |
| - public void addLoadPath( ClassLoader cl, String ref ) { |
92 |
| - addLoadPath(createUri(cl, ref)); |
93 |
| - } |
94 |
| - |
95 |
| - public void addBundleToLoadPath( Bundle cl ) { |
96 |
| - addBundleToLoadPath( cl, JRUBYDIR ); |
97 |
| - } |
98 |
| - |
99 |
| - public void addBundleToLoadPath( Bundle cl, String ref ) { |
100 |
| - addLoadPath(createUriFromBundle(cl, ref)); |
101 |
| - } |
102 |
| - |
103 |
| - private String createUriFromBundle( Bundle cl, String ref) { |
104 |
| - URL url = cl.getResource( ref ); |
105 |
| - if ( url == null && ref.startsWith( "/" ) ) { |
106 |
| - url = cl.getResource( ref.substring( 1 ) ); |
107 |
| - } |
108 |
| - if ( url == null ) { |
109 |
| - throw new RuntimeException( "reference " + ref + " not found on bundle " + cl ); |
110 |
| - } |
111 |
| - return "uri:" + url.toString().replaceFirst( ref + "$", "" ); |
112 |
| - } |
113 |
| - |
114 |
| - private void addLoadPath(String uri) { |
115 |
| - runScriptlet( "$LOAD_PATH << '" + uri + "' unless $LOAD_PATH.member?( '" + uri + "' )" ); |
116 |
| - } |
117 |
| - |
118 |
| - public void addBundleToGemPath( Bundle cl ) { |
119 |
| - addBundleToGemPath( cl, "/specifications" + JRUBYDIR ); |
120 |
| - } |
121 |
| - |
122 |
| - public void addBundleToGemPath( Bundle cl, String ref ) { |
123 |
| - addGemPath(createUriFromBundle(cl, ref)); |
124 |
| - } |
125 |
| - |
126 |
| - public void addGemPath( ClassLoader cl ) { |
127 |
| - addGemPath( cl, "/specifications" + JRUBYDIR ); |
128 |
| - } |
129 |
| - |
130 |
| - public void addGemPath( ClassLoader cl, String ref ) { |
131 |
| - addGemPath(createUri(cl, ref)); |
132 |
| - } |
133 |
| - |
134 |
| - private String createUri(ClassLoader cl, String ref) { |
135 |
| - URL url = cl.getResource( ref ); |
136 |
| - if ( url == null && ref.startsWith( "/" ) ) { |
137 |
| - url = cl.getResource( ref.substring( 1 ) ); |
138 |
| - } |
139 |
| - if ( url == null ) { |
140 |
| - throw new RuntimeException( "reference " + ref + " not found on classloader " + cl ); |
141 |
| - } |
142 |
| - return "uri:" + url.toString().replaceFirst( ref + "$", "" ); |
143 |
| - } |
144 |
| - |
145 |
| - private void addGemPath(String uri) { |
146 |
| - runScriptlet( "require 'rubygems/defaults/jruby';Gem::Specification.add_dir '" + uri + "' unless Gem::Specification.dirs.member?( '" + uri + "' )" ); |
147 |
| - } |
148 | 68 | }
|
0 commit comments