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.apache.tools.ant.module.bridge;
043:
044: import java.io.IOException;
045: import java.net.URL;
046: import java.util.Enumeration;
047: import org.openide.util.Enumerations;
048: import org.openide.util.Exceptions;
049:
050: /**
051: * Loads classes in the following order:
052: * 1. JRE (well, actually app loader, but minus org.apache.tools.** and org.netbeans.**)
053: * 2. Ant JARs - whatever is in the "main" class loader.
054: * 3. Some NetBeans module class loader.
055: * 4. Some other JAR from $nbhome/ant/nblib/*.jar.
056: * Used for two cases:
057: * A. bridge.jar for #4 and the Ant module for #3.
058: * B. ant/nblib/o-n-m-foo.jar for #4 and modules/o-n-m-foo.jar for #3.
059: * Lightly inspired by ProxyClassLoader, but much less complex.
060: * @author Jesse Glick
061: */
062: final class AuxClassLoader extends
063: AntBridge.AllPermissionURLClassLoader {
064:
065: private static boolean masked(String name) {
066: return name.startsWith("org.apache.tools.")
067: && !name.startsWith("org.apache.tools.ant.module."); // NOI18N
068: }
069:
070: private final ClassLoader nbLoader;
071:
072: public AuxClassLoader(ClassLoader nbLoader, ClassLoader antLoader,
073: URL extraJar) {
074: super (new URL[] { extraJar }, antLoader);
075: this .nbLoader = nbLoader;
076: }
077:
078: @Override
079: protected Class<?> findClass(String name)
080: throws ClassNotFoundException {
081: if (!masked(name)) {
082: try {
083: return nbLoader.loadClass(name);
084: } catch (ClassNotFoundException cnfe) {
085: // OK, didn't find it.
086: }
087: }
088: try {
089: return super .findClass(name);
090: } catch (UnsupportedClassVersionError e) {
091: // May be thrown during unit tests in case there is a JDK mixup.
092: Exceptions.attachMessage(e, "loading: " + name);
093: throw e;
094: }
095: }
096:
097: @Override
098: public URL findResource(String name) {
099: if (!masked(name)) {
100: URL u = nbLoader.getResource(name);
101: if (u != null) {
102: return u;
103: }
104: }
105: return super .findResource(name);
106: }
107:
108: @Override
109: public Enumeration<URL> findResources(String name)
110: throws IOException {
111: // XXX probably wrong now... try to fix somehow
112: return Enumerations
113: .removeDuplicates(Enumerations.concat(nbLoader
114: .getResources(name), super .findResources(name)));
115: }
116:
117: // XXX should maybe do something with packages... but oh well, it is rather hard.
118:
119: }
|