KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > batik > script > rhino > RhinoClassLoader


1 /*
2
3    Copyright 2002-2004 The Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16
17  */

18 package org.apache.batik.script.rhino;
19
20 import java.io.File JavaDoc;
21 import java.io.FilePermission JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.URLClassLoader JavaDoc;
25 import java.security.AccessController JavaDoc;
26 import java.security.AccessControlContext JavaDoc;
27 import java.security.CodeSource JavaDoc;
28 import java.security.Permission JavaDoc;
29 import java.security.PermissionCollection JavaDoc;
30 import java.security.ProtectionDomain JavaDoc;
31 import java.security.PrivilegedAction JavaDoc;
32
33 import org.mozilla.javascript.GeneratedClassLoader;
34
35 /**
36  * This class loader implementation will work whether or not the
37  * documentURL is null.
38  *
39  * @author <a HREF="mailto:vincent.hardy@sun.com">Vincent Hardy</a>
40  * @version $Id: RhinoClassLoader.java,v 1.10 2004/08/18 07:14:57 vhardy Exp $
41  */

42 public class RhinoClassLoader extends URLClassLoader JavaDoc implements GeneratedClassLoader {
43     /**
44      * URL for the document referencing the script.
45      */

46     protected URL JavaDoc documentURL;
47
48     /**
49      * CodeSource for classes defined by this loader
50      */

51     protected CodeSource JavaDoc codeSource;
52
53     /**
54      * The AccessControlContext which can be associated with
55      * code loaded by this class loader if it was running
56      * stand-alone (i.e., not invoked by code with lesser
57      * priviledges).
58      */

59     protected AccessControlContext JavaDoc rhinoAccessControlContext;
60
61     /**
62      * Constructor.
63      * @param documentURL the URL from which to load classes and resources
64      * @param parent the parent class loader for delegation
65      */

66     public RhinoClassLoader(URL JavaDoc documentURL, ClassLoader JavaDoc parent){
67         super(documentURL != null ? new URL JavaDoc[]{documentURL} : new URL JavaDoc[]{},
68               parent);
69         this.documentURL = documentURL;
70         if (documentURL != null){
71             codeSource = new CodeSource JavaDoc(documentURL, null);
72         }
73
74         //
75
// Create the Rhino ProtectionDomain
76
// and AccessControlContext
77
//
78
ProtectionDomain JavaDoc rhinoProtectionDomain
79             = new ProtectionDomain JavaDoc(codeSource,
80                                    getPermissions(codeSource));
81
82         rhinoAccessControlContext
83             = new AccessControlContext JavaDoc(new ProtectionDomain JavaDoc[]{
84                 rhinoProtectionDomain});
85     }
86
87     /**
88      * Helper, returns the URL array from the parent loader
89      */

90     static URL JavaDoc[] getURL(ClassLoader JavaDoc parent) {
91         if (parent instanceof RhinoClassLoader) {
92             URL JavaDoc documentURL = ((RhinoClassLoader)parent).documentURL;
93             if (documentURL != null) {
94                 return new URL JavaDoc[] {documentURL};
95             } else {
96                 return new URL JavaDoc[] {};
97             }
98         } else {
99             return new URL JavaDoc[] {};
100         }
101     }
102
103     /**
104      * Define and load a Java class
105      */

106     public Class JavaDoc defineClass(String JavaDoc name,
107                              byte[] data) {
108         return super.defineClass(name, data, 0, data.length, codeSource);
109     }
110
111     /**
112      * Links the Java class.
113      */

114     public void linkClass(Class JavaDoc clazz) {
115         super.resolveClass(clazz);
116     }
117
118     /**
119      * Returns the AccessControlContext which should be associated with
120      * RhinoCode.
121      */

122     public AccessControlContext JavaDoc getAccessControlContext() {
123         return rhinoAccessControlContext;
124     }
125
126     /**
127      * Returns the permissions for the given CodeSource object.
128      * Compared to URLClassLoader, this adds a FilePermission so
129      * that files under the same root directory as the document
130      * can be read.
131      */

132     protected PermissionCollection JavaDoc getPermissions(CodeSource JavaDoc codesource) {
133         PermissionCollection JavaDoc perms = null;
134
135         if (codesource != null) {
136             perms = super.getPermissions(codesource);
137         }
138
139         if (documentURL != null && perms != null) {
140             Permission JavaDoc p = null;
141             Permission JavaDoc dirPerm = null;
142             try {
143                 p = documentURL.openConnection().getPermission();
144             } catch (IOException JavaDoc e){
145                 p = null;
146             }
147
148             if (p instanceof FilePermission JavaDoc){
149                 String JavaDoc path = p.getName();
150                 if (!path.endsWith(File.separator)) {
151                     // We are dealing with a file, as we would expect
152
// from a document file URL
153
int dirEnd = path.lastIndexOf(File.separator);
154                     if (dirEnd != -1){
155                         // Include trailing file separator
156
path = path.substring(0, dirEnd + 1);
157                         path += "-";
158                         dirPerm = new FilePermission JavaDoc(path, "read");
159                         perms.add(dirPerm);
160                     }
161                 }
162             }
163         }
164
165         return perms;
166     }
167 }
168
169
Popular Tags