KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > project > ProjectClassLoader


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.netbeans.modules.form.project;
21
22 import java.io.InputStream JavaDoc;
23 import java.net.URL JavaDoc;
24 import org.netbeans.api.java.classpath.ClassPath;
25 import org.openide.ErrorManager;
26 import org.openide.filesystems.FileObject;
27 import org.openide.filesystems.FileStateInvalidException;
28
29 /**
30  * A class loader loading user classes from given project (execution classpath
31  * is used) with special care given to resources. When finding a resource, the
32  * the project's sources are tried first (before execution classpath) to allow
33  * components added to a form in this project to access resources without need
34  * to build the project first. Even if built, the resources in sources take
35  * precedence as they are likely more up-to-date.
36  *
37  * @author Tomas Pavek
38  */

39
40 class ProjectClassLoader extends ClassLoader JavaDoc {
41
42     private ClassLoader JavaDoc projectClassLoaderDelegate;
43     private ClassPath sources;
44     private ClassLoader JavaDoc systemClassLoader;
45
46     private ProjectClassLoader(ClassLoader JavaDoc projectClassLoaderDelegate, ClassPath sources) {
47         this.projectClassLoaderDelegate = projectClassLoaderDelegate;
48         this.sources = sources;
49         this.systemClassLoader = (ClassLoader JavaDoc) org.openide.util.Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
50     }
51
52     static ClassLoader JavaDoc getUpToDateClassLoader(FileObject fileInProject, ClassLoader JavaDoc clSoFar) {
53         ClassLoader JavaDoc existingCL = clSoFar instanceof ProjectClassLoader ?
54                 ((ProjectClassLoader)clSoFar).projectClassLoaderDelegate : clSoFar;
55         ClassPath classPath = ClassPath.getClassPath(fileInProject, ClassPath.EXECUTE);
56         ClassLoader JavaDoc actualCL = classPath != null ? classPath.getClassLoader(true) : null;
57         if (actualCL == existingCL)
58             return clSoFar;
59         if (actualCL == null)
60             return null;
61         return new ProjectClassLoader(actualCL, ClassPath.getClassPath(fileInProject, ClassPath.SOURCE));
62     }
63
64     protected Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
65         if (name.startsWith("org.apache.commons.logging.")) { // NOI18N HACK: Issue 50642
66
try {
67                 return systemClassLoader.loadClass(name);
68             } catch (ClassNotFoundException JavaDoc cnfex) {
69                 // The logging classes are not in the IDE, we can use ProjectClassLoader
70
}
71         }
72         Class JavaDoc c = null;
73         String JavaDoc filename = name.replace('.', '/').concat(".class"); // NOI18N
74
URL JavaDoc url = projectClassLoaderDelegate.getResource(filename);
75         if (url != null) {
76             try {
77                 InputStream JavaDoc is = url.openStream();
78                 byte[] data = null;
79                 int first;
80                 int available = is.available();
81                 while ((first = is.read()) != -1) {
82                     int length = is.available();
83                     if (length != available) { // Workaround for issue 4401122
84
length++;
85                     }
86                     byte[] b = new byte[length];
87                     b[0] = (byte) first;
88                     int count = 1;
89                     while (count < length) {
90                         int read = is.read(b, count, length - count);
91                         assert (read != -1);
92                         count += read;
93                     }
94                     if (data == null) {
95                         data = b;
96                     }
97                     else {
98                         byte[] temp = new byte[data.length + count];
99                         System.arraycopy(data, 0, temp, 0, data.length);
100                         System.arraycopy(b, 0, temp, data.length, count);
101                         data = temp;
102                     }
103                 }
104                 int dot = name.lastIndexOf('.');
105                 if (dot != -1) { // Is there anything we should do for the default package?
106
String JavaDoc packageName = name.substring(0, dot);
107                     Package JavaDoc pakcage = getPackage(packageName);
108                     if (pakcage == null) {
109                         // PENDING are we able to determine the attributes somehow?
110
definePackage(packageName, null, null, null, null, null, null, null);
111                     }
112                 }
113                 c = defineClass(name, data, 0, data.length);
114             }
115             catch (Exception JavaDoc ex) {
116                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
117             }
118         }
119         else if (ClassPathUtils.getClassLoadingType(name) == ClassPathUtils.SYSTEM_CLASS) {
120             // fallback to system classloader for indirectly loaded classes
121
// e.g. if a bean uses GroupLayout then supply it automatically
122
c = systemClassLoader.loadClass(name);
123         }
124         if (c == null)
125             throw new ClassNotFoundException JavaDoc(name);
126         return c;
127     }
128
129     protected URL JavaDoc findResource(String JavaDoc name) {
130         FileObject fo = sources.findResource(name);
131         if (fo != null) {
132             try {
133                 return fo.getURL();
134             } catch (FileStateInvalidException ex) {
135                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
136             }
137         }
138         return projectClassLoaderDelegate.getResource(name);
139     }
140 }
141
Popular Tags