1 8 9 package javax.xml.transform; 10 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 14 import java.util.Properties ; 15 import java.io.BufferedReader ; 16 import java.io.IOException ; 17 import java.io.InputStream ; 18 import java.io.InputStreamReader ; 19 import java.net.URL ; 20 21 32 class FactoryFinder { 33 35 private static boolean debug = false; 36 static Properties cacheProps= new Properties (); 37 static SecuritySupport ss = new SecuritySupport () ; 38 static boolean firstTime = true; 39 40 static { 42 try { 45 String val = ss.getSystemProperty("jaxp.debug"); 46 debug = val != null && (! "false".equals(val)); 48 } catch (SecurityException se) { 49 debug = false; 50 } 51 } 52 53 54 private static void dPrint(String msg) { 55 if (debug) { 56 System.err.println("JAXP: " + msg); 57 } 58 } 59 60 73 private static Object newInstance(String className, ClassLoader cl, 74 boolean doFallback) 75 throws ConfigurationError 76 { 77 79 try { 80 Class providerClass; 81 if (cl == null) { 82 providerClass = Class.forName(className); 86 } else { 87 try { 88 providerClass = cl.loadClass(className); 89 } catch (ClassNotFoundException x) { 90 if (doFallback) { 91 cl = FactoryFinder .class.getClassLoader(); 93 providerClass = Class.forName(className, true, cl); 94 } else { 95 throw x; 96 } 97 } 98 } 99 100 Object instance = providerClass.newInstance(); 101 dPrint("created new instance of " + providerClass + 102 " using ClassLoader: " + cl); 103 return instance; 104 } catch (ClassNotFoundException x) { 105 throw new ConfigurationError( 106 "Provider " + className + " not found", x); 107 } catch (Exception x) { 108 throw new ConfigurationError( 109 "Provider " + className + " could not be instantiated: " + x, 110 x); 111 } 112 } 113 114 126 static Object find(String factoryId, String fallbackClassName) 127 throws ConfigurationError 128 { 129 130 133 ClassLoader classLoader = ss.getContextClassLoader(); 134 135 if (classLoader == null) { 136 classLoader = FactoryFinder .class.getClassLoader(); 139 } 140 141 dPrint("find factoryId =" + factoryId); 142 143 try { 145 String systemProp = ss.getSystemProperty(factoryId); 146 if( systemProp!=null) { 147 dPrint("found system property, value=" + systemProp); 148 return newInstance(systemProp, classLoader, true ); 149 } 150 } catch (SecurityException se) { 151 } 154 155 try { 157 String javah = ss.getSystemProperty("java.home"); 158 String configFile = javah + File.separator + 159 "lib" + File.separator + "jaxp.properties"; 160 String factoryClassName = null; 161 if(firstTime){ 162 synchronized(cacheProps){ 163 if(firstTime){ 164 File f=new File ( configFile ); 165 firstTime = false; 166 if(ss.doesFileExist(f)){ 167 dPrint("Read properties file "+f); 168 cacheProps.load(ss.getFileInputStream(f)); 170 } 171 } 172 } 173 } 174 factoryClassName = cacheProps.getProperty(factoryId); 175 176 if(factoryClassName != null){ 177 dPrint("found in $java.home/jaxp.properties, value=" + factoryClassName); 178 return newInstance(factoryClassName, classLoader, true); 179 } 180 } catch(Exception ex ) { 181 if( debug ) ex.printStackTrace(); 182 } 183 184 Object provider = findJarServiceProvider(factoryId); 186 if (provider != null) { 187 return provider; 188 } 189 if (fallbackClassName == null) { 190 throw new ConfigurationError( 191 "Provider for " + factoryId + " cannot be found", null); 192 } 193 194 dPrint("loaded from fallback value: " + fallbackClassName); 195 return newInstance(fallbackClassName, classLoader, true); 196 } 197 198 203 private static Object findJarServiceProvider(String factoryId) 204 throws ConfigurationError 205 { 206 207 String serviceId = "META-INF/services/" + factoryId; 208 InputStream is = null; 209 210 ClassLoader cl = ss.getContextClassLoader(); 212 if (cl != null) { 213 is = ss.getResourceAsStream(cl, serviceId); 214 215 if (is == null) { 217 cl = FactoryFinder .class.getClassLoader(); 218 is = ss.getResourceAsStream(cl, serviceId); 219 } 220 } else { 221 cl = FactoryFinder .class.getClassLoader(); 224 is = ss.getResourceAsStream(cl, serviceId); 225 } 226 227 if (is == null) { 228 return null; 230 } 231 232 dPrint("found jar resource=" + serviceId + 233 " using ClassLoader: " + cl); 234 235 BufferedReader rd; 252 try { 253 rd = new BufferedReader (new InputStreamReader (is, "UTF-8")); 254 } catch (java.io.UnsupportedEncodingException e) { 255 rd = new BufferedReader (new InputStreamReader (is)); 256 } 257 258 String factoryClassName = null; 259 try { 260 factoryClassName = rd.readLine(); 263 rd.close(); 264 } catch (IOException x) { 265 return null; 267 } 268 269 if (factoryClassName != null && 270 ! "".equals(factoryClassName)) { 271 dPrint("found in resource, value=" 272 + factoryClassName); 273 274 return newInstance(factoryClassName, cl, false); 279 } 280 281 return null; 283 } 284 285 static class ConfigurationError extends Error { 286 private Exception exception; 287 288 292 ConfigurationError(String msg, Exception x) { 293 super(msg); 294 this.exception = x; 295 } 296 297 Exception getException() { 298 return exception; 299 } 300 } 301 302 } 303 | Popular Tags |