KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > module > bridge > AuxClassLoader


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.apache.tools.ant.module.bridge;
21
22 import java.io.IOException JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import org.openide.util.Enumerations;
26 import org.openide.util.Exceptions;
27
28 /**
29  * Loads classes in the following order:
30  * 1. JRE (well, actually app loader, but minus org.apache.tools.** and org.netbeans.**)
31  * 2. Ant JARs - whatever is in the "main" class loader.
32  * 3. Some NetBeans module class loader.
33  * 4. Some other JAR from $nbhome/ant/nblib/*.jar.
34  * Used for two cases:
35  * A. bridge.jar for #4 and the Ant module for #3.
36  * B. ant/nblib/o-n-m-foo.jar for #4 and modules/o-n-m-foo.jar for #3.
37  * Lightly inspired by ProxyClassLoader, but much less complex.
38  * @author Jesse Glick
39  */

40 final class AuxClassLoader extends AntBridge.AllPermissionURLClassLoader {
41     
42     private static boolean masked(String JavaDoc name) {
43         return name.startsWith("org.apache.tools.") && !name.startsWith("org.apache.tools.ant.module."); // NOI18N
44
}
45     
46     private final ClassLoader JavaDoc nbLoader;
47     
48     public AuxClassLoader(ClassLoader JavaDoc nbLoader, ClassLoader JavaDoc antLoader, URL JavaDoc extraJar) {
49         super(new URL JavaDoc[] {extraJar}, antLoader);
50         this.nbLoader = nbLoader;
51     }
52     
53     @Override JavaDoc
54     protected Class JavaDoc<?> findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
55         if (!masked(name)) {
56             try {
57                 return nbLoader.loadClass(name);
58             } catch (ClassNotFoundException JavaDoc cnfe) {
59                 // OK, didn't find it.
60
}
61         }
62         try {
63             return super.findClass(name);
64         } catch (UnsupportedClassVersionError JavaDoc e) {
65             // May be thrown during unit tests in case there is a JDK mixup.
66
Exceptions.attachMessage(e, "loading: " + name);
67             throw e;
68         }
69     }
70     
71     @Override JavaDoc
72     public URL JavaDoc findResource(String JavaDoc name) {
73         if (!masked(name)) {
74             URL JavaDoc u = nbLoader.getResource(name);
75             if (u != null) {
76                 return u;
77             }
78         }
79         return super.findResource(name);
80     }
81     
82     @Override JavaDoc
83     public Enumeration JavaDoc<URL JavaDoc> findResources(String JavaDoc name) throws IOException JavaDoc {
84         // XXX probably wrong now... try to fix somehow
85
return Enumerations.removeDuplicates (
86             Enumerations.concat (
87                 nbLoader.getResources(name),
88                 super.findResources(name)
89             )
90         );
91     }
92     
93     // XXX should maybe do something with packages... but oh well, it is rather hard.
94

95 }
96
Popular Tags