KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > groovy > lang > GroovyCodeSource


1 package groovy.lang;
2
3 import groovy.security.GroovyCodeSourcePermission;
4
5 import java.io.ByteArrayInputStream JavaDoc;
6 import java.io.File JavaDoc;
7 import java.io.FileInputStream JavaDoc;
8 import java.io.FileNotFoundException JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.net.MalformedURLException JavaDoc;
12 import java.net.URL JavaDoc;
13 import java.security.AccessController JavaDoc;
14 import java.security.CodeSource JavaDoc;
15 import java.security.PrivilegedActionException JavaDoc;
16 import java.security.PrivilegedExceptionAction JavaDoc;
17 import java.security.cert.Certificate JavaDoc;
18
19 /**
20  * CodeSource wrapper class that allows specific security policies to be associated with a class
21  * compiled from groovy source.
22  *
23  * @author Steve Goetze
24  */

25 public class GroovyCodeSource {
26     
27     /**
28      * The codeSource to be given the generated class. This can be used by policy file
29      * grants to administer security.
30      */

31     private CodeSource JavaDoc codeSource;
32     /** The name given to the generated class */
33     private String JavaDoc name;
34     /** The groovy source to be compiled and turned into a class */
35     private InputStream JavaDoc inputStream;
36     /** The certificates used to sign the items from the codesource */
37     Certificate JavaDoc[] certs;
38     
39     public GroovyCodeSource(String JavaDoc script, String JavaDoc name, String JavaDoc codeBase) {
40         this(new ByteArrayInputStream JavaDoc(script.getBytes()), name, codeBase);
41     }
42     
43     /**
44      * Construct a GroovyCodeSource for an inputStream of groovyCode that has an
45      * unknown provenance -- meaning it didn't come from a File or a URL (e.g. a String).
46      * The supplied codeBase will be used to construct a File URL that should match up
47      * with a java Policy entry that determines the grants to be associated with the
48      * class that will be built from the InputStream.
49      *
50      * The permission groovy.security.GroovyCodeSourcePermission will be used to determine if the given codeBase
51      * may be specified. That is, the current Policy set must have a GroovyCodeSourcePermission that implies
52      * the codeBase, or an exception will be thrown. This is to prevent callers from hijacking
53      * existing codeBase policy entries unless explicitly authorized by the user.
54      */

55     public GroovyCodeSource(InputStream JavaDoc inputStream, String JavaDoc name, String JavaDoc codeBase) {
56         this.inputStream = inputStream;
57         this.name = name;
58         SecurityManager JavaDoc sm = System.getSecurityManager();
59         if (sm != null) {
60             sm.checkPermission(new GroovyCodeSourcePermission(codeBase));
61         }
62         try {
63             this.codeSource = new CodeSource JavaDoc(new URL JavaDoc("file", "", codeBase), (java.security.cert.Certificate JavaDoc[])null);
64         } catch (MalformedURLException JavaDoc murle) {
65             throw new RuntimeException JavaDoc("A CodeSource file URL cannot be constructed from the supplied codeBase: " + codeBase);
66         }
67     }
68
69     /**
70      * Package private constructor called by GroovyClassLoader for signed jar entries
71      */

72     GroovyCodeSource(InputStream JavaDoc inputStream, String JavaDoc name, final File JavaDoc path, final Certificate JavaDoc[] certs) {
73         this.inputStream = inputStream;
74         this.name = name;
75         try {
76             this.codeSource = (CodeSource JavaDoc) AccessController.doPrivileged( new PrivilegedExceptionAction JavaDoc() {
77                 public Object JavaDoc run() throws MalformedURLException JavaDoc {
78                     //toURI().toURL() will encode, but toURL() will not.
79
return new CodeSource JavaDoc(path.toURI().toURL(), certs);
80                 }
81             });
82         } catch (PrivilegedActionException JavaDoc pae) {
83             //shouldn't happen
84
throw new RuntimeException JavaDoc("Could not construct a URL from: " + path);
85         }
86     }
87     
88     public GroovyCodeSource(final File JavaDoc file) throws FileNotFoundException JavaDoc {
89         this.inputStream = new FileInputStream JavaDoc(file);
90         //The calls below require access to user.dir - allow here since getName() and getCodeSource() are
91
//package private and used only by the GroovyClassLoader.
92
try {
93             Object JavaDoc[] info = (Object JavaDoc[]) AccessController.doPrivileged( new PrivilegedExceptionAction JavaDoc() {
94                 public Object JavaDoc run() throws MalformedURLException JavaDoc {
95                     Object JavaDoc[] info = new Object JavaDoc[2];
96                     info[0] = file.getAbsolutePath();
97                     //toURI().toURL() will encode, but toURL() will not.
98
info[1] = new CodeSource JavaDoc(file.toURI().toURL(), (Certificate JavaDoc[]) null);
99                     return info;
100                 }
101             });
102             this.name = (String JavaDoc) info[0];
103             this.codeSource = (CodeSource JavaDoc) info[1];
104         } catch (PrivilegedActionException JavaDoc pae) {
105             throw new RuntimeException JavaDoc("Could not construct a URL from: " + file);
106         }
107     }
108     
109     public GroovyCodeSource(URL JavaDoc url) throws IOException JavaDoc {
110         if (url == null) {
111             throw new RuntimeException JavaDoc("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 JavaDoc(url, (java.security.cert.Certificate JavaDoc[])null);
116     }
117     
118     CodeSource JavaDoc getCodeSource() {
119         return codeSource;
120     }
121
122     public InputStream JavaDoc getInputStream() {
123         return inputStream;
124     }
125
126     public String JavaDoc getName() {
127         return name;
128     }
129 }
130
Popular Tags