1 55 56 package com.sun.org.apache.xml.internal.dtm; 57 58 import java.io.BufferedReader ; 59 import java.io.File ; 60 import java.io.FileInputStream ; 61 import java.io.IOException ; 62 import java.io.InputStream ; 63 import java.io.InputStreamReader ; 64 import java.util.Properties ; 65 66 81 class FactoryFinder { 82 83 private static boolean debug; 84 85 90 private static String foundFactory = null; 91 92 93 static { 95 try { 96 String val = 97 SecuritySupport.getInstance().getSystemProperty("jaxp.debug"); 98 debug = val != null && (! "false".equals(val)); 100 } catch (SecurityException se) { 101 debug = false; 102 } 103 } 104 105 128 static Object find(String factoryId, String fallbackClassName) 129 throws ConfigurationError 130 { 131 SecuritySupport ss = SecuritySupport.getInstance(); 132 ClassLoader cl = FactoryFinder.class.getClassLoader(); 133 dPrint("find factoryId=" + factoryId); 134 135 try { 137 String systemProp = ss.getSystemProperty(factoryId); 138 if (systemProp != null) { 139 dPrint("found system property, value=" + systemProp); 140 141 return newInstance(systemProp, cl, true); 142 } 143 144 } catch (SecurityException se) { 145 } 147 148 149 synchronized (FactoryFinder.class) { 150 156 if (foundFactory == null) { 157 158 Properties xalanProperties = null; 160 try { 161 String javah = ss.getSystemProperty("java.home"); 162 String configFile = javah + File.separator + 163 "lib" + File.separator + "xalan.properties"; 164 165 File f = new File (configFile); 166 FileInputStream fis = ss.getFileInputStream(f); 167 xalanProperties = new Properties (); 168 xalanProperties.load(fis); 169 fis.close(); 170 171 } catch (Exception x) { 172 } 176 177 if (xalanProperties != null) { 178 foundFactory = xalanProperties.getProperty(factoryId); 179 if (foundFactory != null) { 180 dPrint("found in xalan.properties, value=" + foundFactory); 181 } 182 } else { 183 findJarServiceProvider(factoryId); 186 187 if (foundFactory == null) { 188 if (fallbackClassName == null) { 189 throw new ConfigurationError( 190 "Provider for " + factoryId + " cannot be found", null); 191 } 192 193 dPrint("using fallback, value=" + fallbackClassName); 194 foundFactory = fallbackClassName; 195 } 196 } 197 } 198 } 199 200 return newInstance(foundFactory, cl, true); 201 } 202 203 private static void dPrint(String msg) { 204 if (debug) { 205 System.err.println("JAXP: " + msg); 206 } 207 } 208 209 222 private static Object newInstance(String className, ClassLoader cl, 223 boolean doFallback) 224 throws ConfigurationError 225 { 226 228 try { 229 Class providerClass; 230 if (cl == null) { 231 providerClass = Class.forName(className); 241 } else { 242 try { 243 providerClass = cl.loadClass(className); 244 } catch (ClassNotFoundException x) { 245 if (doFallback) { 246 cl = FactoryFinder.class.getClassLoader(); 248 providerClass = cl.loadClass(className); 249 } else { 250 throw x; 251 } 252 } 253 } 254 Object instance = providerClass.newInstance(); 255 dPrint("created new instance of " + providerClass + 256 " using ClassLoader: " + cl); 257 return instance; 258 } catch (ClassNotFoundException x) { 259 throw new ConfigurationError( 260 "Provider " + className + " not found", x); 261 } catch (Exception x) { 262 throw new ConfigurationError( 263 "Provider " + className + " could not be instantiated: " + x, 264 x); 265 } 266 } 267 268 273 private static String findJarServiceProvider(String factoryId) 274 throws ConfigurationError 275 { 276 SecuritySupport ss = SecuritySupport.getInstance(); 277 String serviceId = "META-INF/services/" + factoryId; 278 InputStream is = null; 279 ClassLoader cl = FactoryFinder.class.getClassLoader(); 281 is = ss.getResourceAsStream(cl, serviceId); 282 283 if (is == null) { 284 return null; 286 } 287 288 dPrint("found jar resource=" + serviceId + 289 " using ClassLoader: " + cl); 290 291 BufferedReader rd; 308 try { 309 rd = new BufferedReader (new InputStreamReader (is, "UTF-8")); 310 } catch (java.io.UnsupportedEncodingException e) { 311 rd = new BufferedReader (new InputStreamReader (is)); 312 } 313 314 String factoryClassName = null; 315 try { 316 factoryClassName = rd.readLine(); 319 rd.close(); 320 } catch (IOException x) { 321 return null; 323 } 324 325 if (factoryClassName != null && 326 ! "".equals(factoryClassName)) { 327 dPrint("found in resource, value=" 328 + factoryClassName); 329 330 return factoryClassName; 335 } 336 337 return null; 339 } 340 341 static class ConfigurationError extends Error { 342 private Exception exception; 343 344 348 ConfigurationError(String msg, Exception x) { 349 super(msg); 350 this.exception = x; 351 } 352 353 Exception getException() { 354 return exception; 355 } 356 } 357 } 358 | Popular Tags |