1 8 9 package javax.xml.datatype; 10 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.io.IOException ; 14 import java.io.InputStream ; 15 16 import java.util.Properties ; 17 import java.io.BufferedReader ; 18 import java.io.InputStreamReader ; 19 import java.net.URL ; 20 21 31 class FactoryFinder { 32 33 36 private static final String CLASS_NAME = "javax.xml.datatype.FactoryFinder"; 37 38 41 private static boolean debug = false; 42 43 46 private static Properties cacheProps = new Properties (); 47 48 51 private static boolean firstTime = true; 52 53 56 private static SecuritySupport ss = new SecuritySupport (); 57 58 65 static { 66 try { 67 debug = ss.getSystemProperty("jaxp.debug") != null; 68 } catch (Exception x) { 69 ; } 71 } 72 73 78 private static void debugPrintln(String msg) { 79 if (debug) { 80 System.err.println( 81 CLASS_NAME 82 + ":" 83 + msg); 84 } 85 } 86 87 96 private static ClassLoader findClassLoader() 97 throws ConfigurationError { 98 ClassLoader classLoader; 99 100 103 classLoader = ss.getContextClassLoader(); 104 105 debugPrintln( 106 "Using context class loader: " 107 + classLoader); 108 109 if (classLoader == null) { 110 classLoader = FactoryFinder .class.getClassLoader(); 113 debugPrintln( 114 "Using the class loader of FactoryFinder: " 115 + classLoader); 116 } 117 118 return classLoader; 119 } 120 121 131 private static Object newInstance( 132 String className, 133 ClassLoader classLoader) 134 throws ConfigurationError { 135 136 try { 137 Class spiClass; 138 if (classLoader == null) { 139 spiClass = Class.forName(className); 140 } else { 141 spiClass = classLoader.loadClass(className); 142 } 143 144 if (debug) { 145 debugPrintln("Loaded " + className + " from " + which(spiClass)); 146 } 147 148 return spiClass.newInstance(); 149 } catch (ClassNotFoundException x) { 150 throw new ConfigurationError( 151 "Provider " + className + " not found", x); 152 } catch (Exception x) { 153 throw new ConfigurationError( 154 "Provider " + className + " could not be instantiated: " + x, 155 x); 156 } 157 } 158 159 171 static Object find(String factoryId, String fallbackClassName) 172 throws ConfigurationError { 173 174 ClassLoader classLoader = findClassLoader(); 175 176 try { 178 String systemProp = ss.getSystemProperty(factoryId); 179 if (systemProp != null) { 180 debugPrintln("found " + systemProp + " in the system property " + factoryId); 181 return newInstance(systemProp, classLoader); 182 } 183 } catch (SecurityException se) { 184 ; } 186 187 try { 189 String javah = ss.getSystemProperty("java.home"); 190 String configFile = javah + File.separator + "lib" + File.separator + "jaxp.properties"; 191 String factoryClassName = null; 192 if (firstTime) { 193 synchronized (cacheProps) { 194 if (firstTime) { 195 File f = new File (configFile); 196 firstTime = false; 197 if (ss.doesFileExist(f)) { 198 debugPrintln("Read properties file " + f); 199 cacheProps.load(ss.getFileInputStream(f)); 200 } 201 } 202 } 203 } 204 factoryClassName = cacheProps.getProperty(factoryId); 205 debugPrintln("found " + factoryClassName + " in $java.home/jaxp.properties"); 206 207 if (factoryClassName != null) { 208 return newInstance(factoryClassName, classLoader); 209 } 210 } catch (Exception ex) { 211 if (debug) { 212 ex.printStackTrace(); 213 } 214 } 215 216 Object provider = findJarServiceProvider(factoryId); 218 if (provider != null) { 219 return provider; 220 } 221 222 if (fallbackClassName == null) { 223 throw new ConfigurationError( 224 "Provider for " + factoryId + " cannot be found", null); 225 } 226 227 debugPrintln("loaded from fallback value: " + fallbackClassName); 228 return newInstance(fallbackClassName, classLoader); 229 } 230 231 236 private static Object findJarServiceProvider(String factoryId) 237 throws ConfigurationError 238 { 239 240 String serviceId = "META-INF/services/" + factoryId; 241 InputStream is = null; 242 243 ClassLoader cl = ss.getContextClassLoader(); 245 if (cl != null) { 246 is = ss.getResourceAsStream(cl, serviceId); 247 248 if (is == null) { 250 cl = FactoryFinder .class.getClassLoader(); 251 is = ss.getResourceAsStream(cl, serviceId); 252 } 253 } else { 254 cl = FactoryFinder .class.getClassLoader(); 257 is = ss.getResourceAsStream(cl, serviceId); 258 } 259 260 if (is == null) { 261 return null; 263 } 264 265 debugPrintln("found jar resource=" + serviceId + 266 " using ClassLoader: " + cl); 267 268 BufferedReader rd; 269 try { 270 rd = new BufferedReader (new InputStreamReader (is, "UTF-8")); 271 } catch (java.io.UnsupportedEncodingException e) { 272 rd = new BufferedReader (new InputStreamReader (is)); 273 } 274 275 String factoryClassName = null; 276 try { 277 factoryClassName = rd.readLine(); 280 rd.close(); 281 } catch (IOException x) { 282 return null; 284 } 285 286 if (factoryClassName != null && 287 ! "".equals(factoryClassName)) { 288 debugPrintln("found in resource, value=" 289 + factoryClassName); 290 291 return newInstance(factoryClassName, cl); 292 } 293 294 return null; 296 } 297 298 301 static class ConfigurationError extends Error { 302 303 306 private Exception exception; 307 308 315 ConfigurationError(String msg, Exception x) { 316 super(msg); 317 this.exception = x; 318 } 319 320 325 Exception getException() { 326 return exception; 327 } 328 } 329 330 331 332 339 private static String which(Class clazz) { 340 try { 341 String classnameAsResource = clazz.getName().replace('.', '/') + ".class"; 342 343 ClassLoader loader = clazz.getClassLoader(); 344 345 URL it; 346 347 if (loader != null) { 348 it = loader.getResource(classnameAsResource); 349 } else { 350 it = ClassLoader.getSystemResource(classnameAsResource); 351 } 352 353 if (it != null) { 354 return it.toString(); 355 } 356 } catch (Throwable t) { 357 if (debug) { 359 t.printStackTrace(); 360 } 361 } 362 return "unknown location"; 363 } 364 365 366 367 371 private abstract static class ClassLoaderFinder { 372 373 378 abstract ClassLoader getContextClassLoader(); 379 } 380 381 384 static class ClassLoaderFinderConcrete extends ClassLoaderFinder { 385 386 391 ClassLoader getContextClassLoader() { 392 return Thread.currentThread().getContextClassLoader(); 393 } 394 } 395 } 396
| Popular Tags
|