1 31 package org.jruby.runtime.load; 32 33 import java.io.BufferedInputStream ; 34 import java.io.FileNotFoundException ; 35 import java.io.IOException ; 36 import java.io.InputStreamReader ; 37 import java.net.URL ; 38 import java.util.jar.JarEntry ; 39 import java.util.jar.JarInputStream ; 40 import java.util.jar.Manifest ; 41 42 import org.jruby.Ruby; 43 import org.jruby.runtime.builtin.IRubyObject; 44 import org.jruby.runtime.load.LoadServiceResource; 45 46 52 public class JarredScript implements Library { 53 private final LoadServiceResource resource; 54 55 public JarredScript(LoadServiceResource resource) { 56 this.resource = resource; 57 } 58 59 public LoadServiceResource getResource() { 60 return this.resource; 61 } 62 63 public void load(Ruby runtime) { 64 URL jarFile = resource.getURL(); 65 66 runtime.getJavaSupport().addToClasspath(jarFile); 68 69 try { 70 JarInputStream in = new JarInputStream (new BufferedInputStream (jarFile.openStream())); 71 72 Manifest mf = in.getManifest(); 73 String rubyInit = mf.getMainAttributes().getValue("Ruby-Init"); 74 if (rubyInit != null) { 75 JarEntry entry = in.getNextJarEntry(); 76 while (entry != null && !entry.getName().equals(rubyInit)) { 77 entry = in.getNextJarEntry(); 78 } 79 if (entry != null) { 80 IRubyObject old = runtime.getGlobalVariables().isDefined("$JAR_URL") ? runtime.getGlobalVariables().get("$JAR_URL") : runtime.getNil(); 81 try { 82 runtime.getGlobalVariables().set("$JAR_URL", runtime.newString("jar:" + jarFile + "!/")); 83 runtime.loadScript("init", new InputStreamReader (in), false); 84 } finally { 85 runtime.getGlobalVariables().set("$JAR_URL", old); 86 } 87 } 88 } 89 in.close(); 90 } catch (FileNotFoundException e) { 91 throw runtime.newIOErrorFromException(e); 92 } catch (IOException e) { 93 throw runtime.newIOErrorFromException(e); 94 } 95 } 96 } 97 | Popular Tags |