KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tax > parser > ParserLoader


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 package org.netbeans.modules.xml.tax.parser;
20
21 import org.openide.util.Lookup;
22
23 import java.io.*;
24 import java.net.*;
25 import java.util.*;
26 import org.openide.modules.InstalledFileLocator;
27
28 /**
29  * A filtering classloader (isolator) ensuring that a particuler version of
30  * Xerces2 parser is used. The filtering rule is inlined to perform well.
31  * <p>
32  * If the rule match it loads data from isolated resource otherwise delegates
33  * to a parent (TopManager.getSystemClassLoader()).
34  * <p>
35  * Use getInstance() followed by loadClass() method for obtaining the parser.
36  *
37  * @author Petr Kuzel
38  * @version
39  */

40 public final class ParserLoader extends URLClassLoader {
41     
42     // filtering "rules"
43
private static final String JavaDoc PARSER_PACKAGE = "org.apache.xerces"; // NOI18N
44
private static final String JavaDoc USER_PREFIXES[] = new String JavaDoc[] {
45         "org.netbeans.tax.io.XNIBuilder", // NOI18N
46
"org.netbeans.modules.xml.tools.action.XMLCompiler" //!!! outdated // NOI18N
47
};
48
49     private static final String JavaDoc CODENAME_BASE = "org.netbeans.modules.xml.tax"; // NOI18N
50

51     // parser library relative to installed or users directoty
52
private static final String JavaDoc XERCES_ARCHIVE = "modules/autoload/ext/xerces2.jar"; // NOI18N
53

54     // module.jar relative to installed or users directoty
55
private static final String JavaDoc MODULE_ARCHIVE = "modules/autoload/xml-tax.jar"; // NOI18N
56

57     // delegating classloader
58
private ClassLoader JavaDoc parentLoader;
59
60     // the only instance of this classloader
61
private static ParserLoader instance = null;
62     
63     /** Creates new ParserLoader */
64     private ParserLoader(URL[] locations) {
65         super(locations);
66         parentLoader = (ClassLoader JavaDoc) Lookup.getDefault().lookup(ClassLoader JavaDoc.class);
67     }
68
69     /**
70      * Bootstrapping method.
71      * @return ParserLoader or null if library can not be located
72      */

73     public static synchronized ParserLoader getInstance() {
74                         
75         if (instance != null) return instance;
76         
77         try {
78             InstalledFileLocator installedFileLocator = InstalledFileLocator.getDefault();
79
80             URL xer2url = installedFileLocator.locate(XERCES_ARCHIVE, CODENAME_BASE, false).toURL(); // NOI18N
81
if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Isolated library URL=" + xer2url); // NOI18N
82

83             // The isolating classloader itself (not parent) must also see&load interface
84
// implementation class because all subsequent classes must be loaded by the
85
// isolating classloader (not its parent that does not see isolated jar)
86
URL module = installedFileLocator.locate(MODULE_ARCHIVE, CODENAME_BASE, false).toURL(); // NOI18N
87
if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Isolated module URL=" + module); // NOI18N
88

89             instance = new ParserLoader(new URL[] {xer2url, module});
90
91         } catch (MalformedURLException ex) {
92             if ( Util.THIS.isLoggable() ) Util.THIS.debug (ex);
93         }
94                
95         return instance;
96     }
97     
98     /**
99      * Use simple filtering rule for distingvishing
100      * of classes that must be loaded from particular
101      * library version.
102      * ASSUMPTION parentLoader see bootstrap resources
103      */

104     public Class JavaDoc loadClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
105         Class JavaDoc clazz = null;
106         
107         if (name.startsWith(PARSER_PACKAGE)) {
108                 
109             clazz = super.findLoadedClass(name); //no class duplication allowed
110
if (clazz == null) {
111                 clazz = super.findClass(name); //define new class
112
}
113             
114         } else {
115             
116             // all potential users of this class loader (using isolated classes) must
117
// be known in compile time to eliminate multiple loaded classes by
118
// multiple instance of this classloader
119

120             for (int i = 0; i<USER_PREFIXES.length; i++) {
121                 if (name.startsWith(USER_PREFIXES[i])) {
122            
123                     synchronized (this) {
124                         clazz = super.findLoadedClass(name); //no class duplication allowed
125
if (clazz == null) {
126                             clazz = super.findClass(name); //define new class
127
}
128                     }
129                 }
130             }
131             
132             // delegate to parent
133

134             if (clazz == null) {
135                 clazz = parentLoader.loadClass(name);
136             }
137         }
138         
139         return clazz;
140     }
141
142     
143     
144     /*
145      * Prefer isolated library when looking for resource.
146      * ASSUMPTION parentLoader see bootstrap resources
147      */

148     public URL getResource(String JavaDoc name) {
149         URL in = super.getResource(name);
150         if (in == null) {
151             in = parentLoader.getResource(name);
152         }
153
154         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("Resource: " + name + " =>" + in); // NOI18N
155

156         return in;
157     }
158
159     /*
160      * Prefer isolated library when looking for resource stream.
161      * ASSUMPTION parentLoader see bootstrap resources
162      */

163     public InputStream getResourceAsStream(String JavaDoc name) {
164         try {
165             URL url = this.getResource(name);
166             if (url == null) {
167                 return null;
168             } else {
169                 return url.openStream();
170             }
171         } catch (IOException ex) {
172             return null;
173         }
174     }
175
176     /*
177      * Prefer isolated library when looking for resources.
178      * It is defacto implementation of getResources() that is final.
179      * //!!! this inplemetation does not isolate bootstrap resources
180      */

181     public Enumeration findResources(String JavaDoc name) throws IOException {
182         Enumeration en1 = super.findResources(name);
183         Enumeration en2 = parentLoader.getResources(name);
184
185         return org.openide.util.Enumerations.concat (en1, en2);
186     }
187
188     /**
189      * Perform basic self test.
190      */

191     public static void main(String JavaDoc args[]) throws Exception JavaDoc {
192         
193         ParserLoader me = ParserLoader.getInstance();
194         
195         Class JavaDoc apache = me.loadClass("org.apache.xerces.util.QName"); // NOI18N
196
Class JavaDoc java = me.loadClass("java.lang.String"); // NOI18N
197
Class JavaDoc netbeans = me.loadClass("org.openide.util.Mutex"); // NOI18N
198

199         System.err.println("apache " + apache.getClassLoader()); // NOI18N
200
System.err.println("netbeans " + netbeans.getClassLoader()); // NOI18N
201
System.err.println("java " + java.getClassLoader()); // NOI18N
202

203     }
204     
205 }
206
Popular Tags