1 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 40 public final class ParserLoader extends URLClassLoader { 41 42 private static final String PARSER_PACKAGE = "org.apache.xerces"; private static final String USER_PREFIXES[] = new String [] { 45 "org.netbeans.tax.io.XNIBuilder", "org.netbeans.modules.xml.tools.action.XMLCompiler" }; 48 49 private static final String CODENAME_BASE = "org.netbeans.modules.xml.tax"; 51 private static final String XERCES_ARCHIVE = "modules/autoload/ext/xerces2.jar"; 54 private static final String MODULE_ARCHIVE = "modules/autoload/xml-tax.jar"; 57 private ClassLoader parentLoader; 59 60 private static ParserLoader instance = null; 62 63 64 private ParserLoader(URL[] locations) { 65 super(locations); 66 parentLoader = (ClassLoader ) Lookup.getDefault().lookup(ClassLoader .class); 67 } 68 69 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(); if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Isolated library URL=" + xer2url); 83 URL module = installedFileLocator.locate(MODULE_ARCHIVE, CODENAME_BASE, false).toURL(); if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Isolated module URL=" + module); 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 104 public Class loadClass(String name) throws ClassNotFoundException { 105 Class clazz = null; 106 107 if (name.startsWith(PARSER_PACKAGE)) { 108 109 clazz = super.findLoadedClass(name); if (clazz == null) { 111 clazz = super.findClass(name); } 113 114 } else { 115 116 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); if (clazz == null) { 126 clazz = super.findClass(name); } 128 } 129 } 130 } 131 132 134 if (clazz == null) { 135 clazz = parentLoader.loadClass(name); 136 } 137 } 138 139 return clazz; 140 } 141 142 143 144 148 public URL getResource(String name) { 149 URL in = super.getResource(name); 150 if (in == null) { 151 in = parentLoader.getResource(name); 152 } 153 154 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("Resource: " + name + " =>" + in); 156 return in; 157 } 158 159 163 public InputStream getResourceAsStream(String 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 181 public Enumeration findResources(String 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 191 public static void main(String args[]) throws Exception { 192 193 ParserLoader me = ParserLoader.getInstance(); 194 195 Class apache = me.loadClass("org.apache.xerces.util.QName"); Class java = me.loadClass("java.lang.String"); Class netbeans = me.loadClass("org.openide.util.Mutex"); 199 System.err.println("apache " + apache.getClassLoader()); System.err.println("netbeans " + netbeans.getClassLoader()); System.err.println("java " + java.getClassLoader()); 203 } 204 205 } 206 | Popular Tags |