KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.io.InputStream JavaDoc;
22 import java.net.URL JavaDoc;
23 import org.openide.ErrorManager;
24
25 /**
26  * A special classloader capable to combine system classpath (IDE modules) and
27  * user project classpath into one. Classes loaded by this classloader can link
28  * with module classes running in the IDE and access resources on project
29  * classpath at the same time.
30  *
31  * @author Tomas Pavek
32  */

33
34 final class FormClassLoader extends ClassLoader JavaDoc {
35
36     private ClassLoader JavaDoc systemClassLoader;
37     private ClassLoader JavaDoc projectClassLoader;
38
39     FormClassLoader(ClassLoader JavaDoc projectClassLoader) {
40         this.systemClassLoader = (ClassLoader JavaDoc) org.openide.util.Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
41         this.projectClassLoader = projectClassLoader;
42     }
43
44     ClassLoader JavaDoc getProjectClassLoader() {
45         return projectClassLoader;
46     }
47
48     protected Class JavaDoc findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
49         int type = ClassPathUtils.getClassLoadingType(name);
50         if (type == ClassPathUtils.UNSPECIFIED_CLASS) {
51             if (projectClassLoader == null)
52                 throw new ClassNotFoundException JavaDoc(ClassPathUtils.getBundleString("MSG_NullClassPath")); // NOI18N
53
return projectClassLoader.loadClass(name);
54         }
55         if (type == ClassPathUtils.SYSTEM_CLASS)
56             return systemClassLoader.loadClass(name);
57         // otherwise type == ClassPathUtils.SYSTEM_CLASS_WITH_PROJECT
58

59         Class JavaDoc c = null;
60         String JavaDoc filename = name.replace('.', '/').concat(".class"); // NOI18N
61
URL JavaDoc url = systemClassLoader.getResource(filename);
62         if (url == null && projectClassLoader != null)
63             url = projectClassLoader.getResource(filename);
64         if (url != null) {
65             try {
66                 InputStream JavaDoc is = url.openStream();
67                 byte[] data = null;
68                 int first;
69                 int available = is.available();
70                 while ((first = is.read()) != -1) {
71                     int length = is.available();
72                     if (length != available) { // Workaround for issue 4401122
73
length++;
74                     }
75                     byte[] b = new byte[length];
76                     b[0] = (byte) first;
77                     int count = 1;
78                     while (count < length) {
79                         int read = is.read(b, count, length - count);
80                         assert (read != -1);
81                         count += read;
82                     }
83                     if (data == null) {
84                         data = b;
85                     }
86                     else {
87                         byte[] temp = new byte[data.length + count];
88                         System.arraycopy(data, 0, temp, 0, data.length);
89                         System.arraycopy(b, 0, temp, data.length, count);
90                         data = temp;
91                     }
92                 }
93                 int dot = name.lastIndexOf('.');
94                 if (dot != -1) { // Is there anything we should do for the default package?
95
String JavaDoc packageName = name.substring(0, dot);
96                     Package JavaDoc pakcage = getPackage(packageName);
97                     if (pakcage == null) {
98                         // PENDING are we able to determine the attributes somehow?
99
definePackage(packageName, null, null, null, null, null, null, null);
100                     }
101                 }
102                 c = defineClass(name, data, 0, data.length);
103             }
104             catch (Exception JavaDoc ex) {
105                 ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, ex);
106             }
107         }
108         if (c == null)
109             throw new ClassNotFoundException JavaDoc(name);
110
111         return c;
112     }
113
114     public URL JavaDoc getResource(String JavaDoc name) {
115         URL JavaDoc url = projectClassLoader != null ? projectClassLoader.getResource(name) : null;
116         if (url == null)
117             url = systemClassLoader.getResource(name);
118         return url;
119     }
120 }
121
Popular Tags