1 package groovy.lang; 2 3 import groovy.security.GroovyCodeSourcePermission; 4 5 import java.io.ByteArrayInputStream ; 6 import java.io.File ; 7 import java.io.FileInputStream ; 8 import java.io.FileNotFoundException ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.net.MalformedURLException ; 12 import java.net.URL ; 13 import java.security.AccessController ; 14 import java.security.CodeSource ; 15 import java.security.PrivilegedActionException ; 16 import java.security.PrivilegedExceptionAction ; 17 import java.security.cert.Certificate ; 18 19 25 public class GroovyCodeSource { 26 27 31 private CodeSource codeSource; 32 33 private String name; 34 35 private InputStream inputStream; 36 37 Certificate [] certs; 38 39 public GroovyCodeSource(String script, String name, String codeBase) { 40 this(new ByteArrayInputStream (script.getBytes()), name, codeBase); 41 } 42 43 55 public GroovyCodeSource(InputStream inputStream, String name, String codeBase) { 56 this.inputStream = inputStream; 57 this.name = name; 58 SecurityManager sm = System.getSecurityManager(); 59 if (sm != null) { 60 sm.checkPermission(new GroovyCodeSourcePermission(codeBase)); 61 } 62 try { 63 this.codeSource = new CodeSource (new URL ("file", "", codeBase), (java.security.cert.Certificate [])null); 64 } catch (MalformedURLException murle) { 65 throw new RuntimeException ("A CodeSource file URL cannot be constructed from the supplied codeBase: " + codeBase); 66 } 67 } 68 69 72 GroovyCodeSource(InputStream inputStream, String name, final File path, final Certificate [] certs) { 73 this.inputStream = inputStream; 74 this.name = name; 75 try { 76 this.codeSource = (CodeSource ) AccessController.doPrivileged( new PrivilegedExceptionAction () { 77 public Object run() throws MalformedURLException { 78 return new CodeSource (path.toURI().toURL(), certs); 80 } 81 }); 82 } catch (PrivilegedActionException pae) { 83 throw new RuntimeException ("Could not construct a URL from: " + path); 85 } 86 } 87 88 public GroovyCodeSource(final File file) throws FileNotFoundException { 89 this.inputStream = new FileInputStream (file); 90 try { 93 Object [] info = (Object []) AccessController.doPrivileged( new PrivilegedExceptionAction () { 94 public Object run() throws MalformedURLException { 95 Object [] info = new Object [2]; 96 info[0] = file.getAbsolutePath(); 97 info[1] = new CodeSource (file.toURI().toURL(), (Certificate []) null); 99 return info; 100 } 101 }); 102 this.name = (String ) info[0]; 103 this.codeSource = (CodeSource ) info[1]; 104 } catch (PrivilegedActionException pae) { 105 throw new RuntimeException ("Could not construct a URL from: " + file); 106 } 107 } 108 109 public GroovyCodeSource(URL url) throws IOException { 110 if (url == null) { 111 throw new RuntimeException ("Could not construct a GroovyCodeSource from a null URL"); 112 } 113 this.inputStream = url.openStream(); 114 this.name = url.toExternalForm(); 115 this.codeSource = new CodeSource (url, (java.security.cert.Certificate [])null); 116 } 117 118 CodeSource getCodeSource() { 119 return codeSource; 120 } 121 122 public InputStream getInputStream() { 123 return inputStream; 124 } 125 126 public String getName() { 127 return name; 128 } 129 } 130 | Popular Tags |