001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.form.project;
043:
044: import java.io.InputStream;
045: import java.io.IOException;
046: import java.net.URL;
047: import org.openide.ErrorManager;
048:
049: /**
050: * A special classloader capable to combine system classpath (IDE modules) and
051: * user project classpath into one. Classes loaded by this classloader can link
052: * with module classes running in the IDE and access resources on project
053: * classpath at the same time.
054: *
055: * @author Tomas Pavek
056: */
057:
058: final class FormClassLoader extends ClassLoader {
059:
060: private ClassLoader systemClassLoader;
061: private ClassLoader projectClassLoader;
062:
063: FormClassLoader(ClassLoader projectClassLoader) {
064: this .systemClassLoader = org.openide.util.Lookup.getDefault()
065: .lookup(ClassLoader.class);
066: this .projectClassLoader = projectClassLoader;
067: }
068:
069: ClassLoader getProjectClassLoader() {
070: return projectClassLoader;
071: }
072:
073: @Override
074: protected Class findClass(String name)
075: throws ClassNotFoundException {
076: ClassPathUtils.ClassLoadingType type = ClassPathUtils
077: .getClassLoadingType(name);
078: if (type == null) {
079: if (projectClassLoader == null) {
080: throw new ClassNotFoundException(ClassPathUtils
081: .getBundleString("MSG_NullClassPath")); // NOI18N
082: }
083: return projectClassLoader.loadClass(name);
084: }
085: if (type == ClassPathUtils.SYSTEM_CLASS) {
086: return systemClassLoader.loadClass(name);
087: }
088: // otherwise type == ClassPathUtils.SYSTEM_CLASS_WITH_PROJECT
089:
090: Class c = null;
091: String filename = name.replace('.', '/').concat(".class"); // NOI18N
092: URL url = systemClassLoader.getResource(filename);
093: if (url == null && projectClassLoader != null)
094: url = projectClassLoader.getResource(filename);
095: if (url != null) {
096: InputStream is = null;
097: try {
098: is = url.openStream();
099: byte[] data = null;
100: int first;
101: int available = is.available();
102: while ((first = is.read()) != -1) {
103: int length = is.available();
104: if (length != available) { // Workaround for issue 4401122
105: length++;
106: }
107: byte[] b = new byte[length];
108: b[0] = (byte) first;
109: int count = 1;
110: while (count < length) {
111: int read = is.read(b, count, length - count);
112: assert (read != -1);
113: count += read;
114: }
115: if (data == null) {
116: data = b;
117: } else {
118: byte[] temp = new byte[data.length + count];
119: System.arraycopy(data, 0, temp, 0, data.length);
120: System
121: .arraycopy(b, 0, temp, data.length,
122: count);
123: data = temp;
124: }
125: }
126: int dot = name.lastIndexOf('.');
127: if (dot != -1) { // Is there anything we should do for the default package?
128: String packageName = name.substring(0, dot);
129: Package pakcage = getPackage(packageName);
130: if (pakcage == null) {
131: // PENDING are we able to determine the attributes somehow?
132: definePackage(packageName, null, null, null,
133: null, null, null, null);
134: }
135: }
136: c = defineClass(name, data, 0, data.length);
137: } catch (Exception ex) {
138: ErrorManager.getDefault().notify(
139: ErrorManager.INFORMATIONAL, ex);
140: } finally {
141: if (is != null) {
142: try {
143: is.close();
144: } catch (IOException ioex) {
145: // ignore
146: }
147: }
148: }
149: }
150: if (c == null)
151: throw new ClassNotFoundException(name);
152:
153: return c;
154: }
155:
156: @Override
157: public URL getResource(String name) {
158: URL url = projectClassLoader != null ? projectClassLoader
159: .getResource(name) : null;
160: if (url == null)
161: url = systemClassLoader.getResource(name);
162: return url;
163: }
164: }
|