KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > execution > NbClassLoader


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.openide.execution;
21
22 import java.io.InputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.net.URLClassLoader JavaDoc;
26 import java.security.AllPermission JavaDoc;
27 import java.security.CodeSource JavaDoc;
28 import java.security.Permission JavaDoc;
29 import java.security.PermissionCollection JavaDoc;
30 import java.util.Enumeration JavaDoc;
31 import java.util.HashMap JavaDoc;
32 import java.util.jar.Manifest JavaDoc;
33 import org.openide.filesystems.FileObject;
34 import org.openide.filesystems.FileStateInvalidException;
35 import org.openide.filesystems.FileSystem;
36 import org.openide.filesystems.URLMapper;
37 import org.openide.util.Exceptions;
38 import org.openide.util.Lookup;
39 import org.openide.windows.InputOutput;
40
41 /** A class loader which is capable of loading classes from the Repository.
42  * XXX the only useful thing this class does is effectively make
43  * ExecutionEngine.createPermissions public! Consider deprecating this class...
44  * @see <a HREF="@JAVA/API@/org/netbeans/api/java/classpath/ClassPath.html#getClassLoader(boolean)"><code>ClassPath.getClassLoader(...)</code></a>
45 * @author Ales Novak, Petr Hamernik, Jaroslav Tulach, Ian Formanek
46 */

47 public class NbClassLoader extends URLClassLoader JavaDoc {
48     /** I/O for classes defined by this classloader. May be <code>null</code>. */
49     protected InputOutput inout;
50     /** Cached PermissionCollections returned from ExecutionEngine. */
51     private HashMap JavaDoc permissionCollections;
52     /** Default permissions */
53     private PermissionCollection JavaDoc defaultPermissions;
54     
55     private static ClassLoader JavaDoc systemClassLoader() {
56         return (ClassLoader JavaDoc)Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
57     }
58     
59     /** Create a new class loader retrieving classes from the core IDE as well as the Repository.
60      * @see FileSystemCapability#EXECUTE
61      * @see FileSystemCapability#fileSystems
62      * @deprecated Misuses classpath.
63     */

64     public NbClassLoader () {
65         super(new URL JavaDoc[0], systemClassLoader());
66     }
67
68     /** Create a new class loader retrieving classes from the core IDE as well as the Repository,
69     * and redirecting system I/O.
70      * @param io an I/O tab in the Output Window
71      * @see org.openide.filesystems.Repository#getFileSystems
72      * @deprecated Misuses classpath.
73      */

74     public NbClassLoader(InputOutput io) {
75         super(new URL JavaDoc[0], systemClassLoader());
76         inout = io;
77     }
78     
79     /**
80      * Create a new class loader retrieving classes from a set of package roots.
81      * @param roots a set of package roots
82      * @param parent a parent loader
83      * @param io an I/O tab in the Output Window, or null
84      * @throws FileStateInvalidException if some of the roots are not valid
85      * @since XXX
86      */

87     public NbClassLoader(FileObject[] roots, ClassLoader JavaDoc parent, InputOutput io) throws FileStateInvalidException {
88         super(createRootURLs(roots), parent);
89         inout = io;
90     }
91
92     /** Create a new class loader retrieving classes from the core IDE as well as specified file systems.
93      * @param fileSystems file systems to load classes from
94      * @deprecated Misuses classpath.
95     */

96     public NbClassLoader (FileSystem[] fileSystems) {
97         super(new URL JavaDoc[0], systemClassLoader(), null);
98         Thread.dumpStack();
99     }
100
101     /** Create a new class loader.
102      * @param fileSystems file systems to load classes from
103      * @param parent fallback class loader
104      * @deprecated Misuses classpath.
105     */

106     public NbClassLoader (FileSystem[] fileSystems, ClassLoader JavaDoc parent) {
107         super(new URL JavaDoc[0], parent);
108         Thread.dumpStack();
109     }
110
111     /** Create a URL to a resource specified by name.
112     * Same behavior as in the super method, but handles names beginning with a slash.
113     * @param name resource name
114     * @return URL to that resource or <code>null</code>
115     */

116     public URL JavaDoc getResource (String JavaDoc name) {
117         return super.getResource (name.startsWith ("/") ? name.substring (1) : name); // NOI18N
118
}
119
120     /* Needs to be overridden so that packages are correctly defined
121        based on manifest for e.g. JarFileSystem's in the repository.
122        Otherwise URLClassLoader, not understanding nbfs:/..../foo.jar,
123        would simply define packages loaded from such a URL with no
124        particular info. We want it to have specification version and
125        all that good stuff. */

126     protected Class JavaDoc findClass (final String JavaDoc name) throws ClassNotFoundException JavaDoc {
127         if (name.indexOf ('.') != -1) {
128             String JavaDoc pkg = name.substring (0, name.lastIndexOf ('.'));
129             if (getPackage (pkg) == null) {
130                 String JavaDoc resource = name.replace ('.', '/') + ".class"; // NOI18N
131
URL JavaDoc[] urls = getURLs ();
132                 for (int i = 0; i < urls.length; i++) {
133                     // System.err.println (urls[i].toString ());
134
FileObject root = URLMapper.findFileObject(urls[i]);
135
136                     if (root == null) {
137                         continue;
138                     }
139                     try {
140                         FileObject fo = root.getFileObject(resource);
141
142                         if (fo != null) {
143                             // Got it. If there is an associated manifest, load it.
144
FileObject manifo = root.getFileObject("META-INF/MANIFEST.MF");
145
146                             if (manifo == null)
147                                 manifo = root.getFileObject("meta-inf/manifest.mf");
148                             if (manifo != null) {
149                                 // System.err.println (manifo.toString () + " " + manifo.getClass ().getName () + " " + manifo.isValid ());
150
Manifest JavaDoc mani = new Manifest JavaDoc();
151                                 InputStream JavaDoc is = manifo.getInputStream();
152
153                                 try {
154                                     mani.read(is);
155                                 }
156                                 finally {
157                                     is.close();
158                                 }
159                                 definePackage(pkg, mani, urls[i]);
160                             }
161                             break;
162                         }
163                     }
164                     catch (IOException JavaDoc ioe) {
165                         Exceptions.attachLocalizedMessage(ioe,
166                                                           urls[i].toString());
167                         Exceptions.printStackTrace(ioe);
168                         continue;
169                     }
170                 }
171             }
172         }
173         return super.findClass (name);
174     }
175     
176     /** Sets a PermissionsCollectio which will be used
177      * for ProtectionDomain of newly created classes.
178      *
179      * @param defaultPerms
180      */

181     public void setDefaultPermissions(PermissionCollection JavaDoc defaultPerms) {
182         if (defaultPerms != null && !defaultPerms.isReadOnly()) {
183             defaultPerms.setReadOnly();
184         }
185         this.defaultPermissions = defaultPerms;
186     }
187
188     /* @return a PermissionCollection for given CodeSource. */
189     protected final synchronized PermissionCollection JavaDoc getPermissions(CodeSource JavaDoc cs) {
190
191         if (permissionCollections != null) {
192             PermissionCollection JavaDoc pc = (PermissionCollection JavaDoc) permissionCollections.get(cs);
193             if (pc != null) {
194                 return pc;
195             }
196         }
197
198         return createPermissions(cs, inout);
199     }
200
201     /**
202     * @param cs CodeSource
203     * @param inout InputOutput passed to @seeExecutionEngine#createPermissions(java.security.CodeSource, org.openide.windows.InpuOutput).
204     * @return a PermissionCollection for given CodeSource.
205     */

206     private PermissionCollection JavaDoc createPermissions(CodeSource JavaDoc cs, InputOutput inout) {
207         PermissionCollection JavaDoc pc;
208         if (inout == null) {
209             if (defaultPermissions != null) {
210                 pc = defaultPermissions;
211             } else {
212                 pc = super.getPermissions(cs);
213             }
214         } else {
215             ExecutionEngine engine = ExecutionEngine.getDefault();
216             pc = engine.createPermissions(cs, inout);
217             if (defaultPermissions != null) {
218                 addAllPermissions(pc, defaultPermissions);
219             } else {
220                 pc.add(new AllPermission JavaDoc());
221             }
222         }
223         if (permissionCollections == null) {
224             permissionCollections = new HashMap JavaDoc(7);
225         }
226         permissionCollections.put(cs, pc);
227         return pc;
228     }
229     
230     /**
231      * Copies all permissions from <tt>src</tt> into <tt>target</tt>
232      *
233      * @param target To where put permissions
234      * @param src From where take paermissions
235      */

236     private static void addAllPermissions(PermissionCollection JavaDoc target, PermissionCollection JavaDoc src) {
237         Enumeration JavaDoc e = src.elements();
238         
239         while (e.hasMoreElements()) {
240             target.add((Permission JavaDoc) e.nextElement());
241         }
242     }
243
244
245     /**
246      * Creates URLs for file objects.
247      * @param roots file roots
248      * @return array of URLs
249      */

250     private static URL JavaDoc[] createRootURLs(FileObject[] roots) throws FileStateInvalidException {
251         URL JavaDoc[] urls = new URL JavaDoc[roots.length];
252         for (int i = 0; i < roots.length; i++) {
253             urls[i] = roots[i].getURL();
254         }
255         return urls;
256     }
257 }
258
Popular Tags