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-2007 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.visualweb.j2ee14classloaderprovider;
043:
044: import java.io.File;
045: import java.net.MalformedURLException;
046: import java.net.URL;
047: import java.net.URLClassLoader;
048: import java.util.ArrayList;
049: import java.util.Arrays;
050: import java.util.List;
051: import java.util.Properties;
052:
053: import org.netbeans.modules.visualweb.classloaderprovider.CommonClassloaderProvider;
054: import org.openide.ErrorManager;
055: import org.openide.filesystems.FileObject;
056: import org.openide.filesystems.FileUtil;
057: import org.openide.filesystems.URLMapper;
058:
059: /**
060: * This Common ClassLaoder provider simply return its ClassLoader which happens
061: * to be the module ClassLoader. This module declares dependencies on the
062: * modules that wrap the J2EE 1.4 platform libraries that are shared between the
063: * IDE implementation and the user project. This provider is available through
064: * the lookup.
065: * <p>
066: * The user project's meta data is be used to determine which J2EE platform it is using.
067: *
068: * @author Sandip V. Chitale (Sandip.Chitale@Sun.Com)
069: */
070: public class J2EE14CommonClassloaderProvider implements
071: CommonClassloaderProvider {
072: private String[] designtimeJars = {
073: // XXX Need to add these jars here so that META-INF/faces-config.xml files can be
074: // discovered and loaded by the JSF1.2 RI. Ideally thin jars containing
075: // the META-INF/faces-config.xml should be used. The classes should be loded
076: // from the modules themselves.
077: "jar:nbinst:///modules/org-netbeans-modules-visualweb-jsfsupport-designtime.jar!/", // NOI18N
078: "jar:nbinst:///modules/org-netbeans-modules-visualweb-jsfsupport-designtime_1_1.jar!/", // NOI18N
079: "jar:nbinst:///modules/org-netbeans-modules-visualweb-webui-designtime.jar!/", // NOI18N
080: // The following two jars are here for special handling of JSF1.1 Standard components
081: // and renderkits
082: "jar:nbinst:///modules/ext/jsf-1_2/jsf-api.jar!/", // NOI18N
083: "jar:nbinst:///modules/ext/jsf-1_2/jsf-impl.jar!/", // NOI18N
084: };
085:
086: private URLClassLoader urlClassLoader;
087:
088: public J2EE14CommonClassloaderProvider() {
089: }
090:
091: public ClassLoader getClassLoader() {
092: synchronized (this ) {
093: if (urlClassLoader == null) {
094: List normalizedUrls = new ArrayList();
095:
096: for (int i = 0; i < designtimeJars.length; i++) {
097: try {
098: URL url = new URL(designtimeJars[i]);
099: FileObject fileObject = URLMapper
100: .findFileObject(url);
101:
102: //file inside library is broken
103: if (fileObject == null)
104: continue;
105:
106: if ("jar".equals(url.getProtocol())) { //NOI18N
107: fileObject = FileUtil
108: .getArchiveFile(fileObject);
109: }
110: File f = FileUtil.toFile(fileObject);
111: if (f != null) {
112:
113: URL entry = f.toURI().toURL();
114: if (FileUtil.isArchiveFile(entry)) {
115: entry = FileUtil.getArchiveRoot(entry);
116: } else if (!f.exists()) {
117: // if file does not exist (e.g. build/classes folder
118: // was not created yet) then corresponding File will
119: // not be ended with slash. Fix that.
120: assert !entry.toExternalForm()
121: .endsWith("/") : f; // NOI18N
122: entry = new URL(entry.toExternalForm()
123: + "/"); // NOI18N
124: }
125: normalizedUrls.add(entry);
126:
127: }
128: } catch (MalformedURLException mue) {
129: ErrorManager.getDefault().notify(
130: ErrorManager.INFORMATIONAL, mue);
131: }
132: }
133: urlClassLoader = new JSF11SupportClassLoader(
134: (URL[]) normalizedUrls.toArray(new URL[0]),
135: getClass().getClassLoader());
136: }
137: }
138: return urlClassLoader;
139: }
140:
141: public boolean isCapableOf(Properties capabilities) {
142: if (J2EE_1_3.equals(capabilities.getProperty(J2EE_PLATFORM))
143: || J2EE_1_4.equals(capabilities
144: .getProperty(J2EE_PLATFORM))) {
145: return true;
146: }
147: return false;
148: }
149:
150: // XXX To be able to distinguish our specific project classloader during debugging.
151: private static class JSF11SupportClassLoader extends URLClassLoader {
152: private final URL[] urls;
153:
154: public JSF11SupportClassLoader(URL[] urls, ClassLoader parent) {
155: super (urls, parent);
156: this .urls = urls;
157: }
158:
159: public String toString() {
160: return super .toString() + "[urls="
161: + (urls == null ? null : Arrays.asList(urls)) + "]"; // NOI18N
162: }
163:
164: // XXX HACK Support JSF 1.1 components and bean infos
165: protected synchronized Class loadClass(String name,
166: boolean resolve) throws ClassNotFoundException {
167: // First, check if the class has already been loaded
168: Class c = findLoadedClass(name);
169: if (c == null) {
170: // Now check if this request is for special packages or name
171: String packageName = getPackageName(name);
172: if (packageName != null) {
173: if (packageName
174: .equals("javax.faces.component.html")
175: || // NOI18N
176: packageName
177: .equals("com.sun.faces.renderkit.html_basic")
178: || // NOI18N
179: packageName
180: .equals("org.netbeans.modules.visualweb.faces.dt.component")
181: || // NOI18N
182: packageName
183: .equals("org.netbeans.modules.visualweb.faces.dt.component.html")
184: || // NOI18N
185: packageName
186: .equals("org.netbeans.modules.visualweb.faces.dt_1_1.component")
187: || // NOI18N
188: packageName
189: .equals("org.netbeans.modules.visualweb.faces.dt_1_1.component.html")
190: || // NOI18N
191: name.equals("com.sun.faces.util.Util")) { // NOI18N
192: // find the class locally
193: c = findClass(name);
194: if (c != null) {
195: if (resolve) {
196: resolveClass(c);
197: }
198: return c;
199: }
200: }
201: }
202: }
203:
204: return super .loadClass(name, resolve);
205: }
206:
207: private static String getPackageName(String name) {
208: if (name == null) {
209: return null;
210: }
211:
212: int lastDotIndex = name.lastIndexOf('.');
213: if (lastDotIndex != -1) {
214: return name.substring(0, lastDotIndex);
215: }
216: return "";
217: }
218: }
219: }
|