1 20 21 27 28 package javax.xml.stream; 29 30 import java.io.InputStream ; 31 import java.io.IOException ; 32 import java.io.File ; 33 import java.io.FileInputStream ; 34 35 import java.util.Properties ; 36 import java.io.BufferedReader ; 37 import java.io.InputStreamReader ; 38 39 47 class FactoryFinder { 48 49 52 private static boolean debug = false; 53 54 57 static Properties cacheProps = new Properties (); 58 59 63 static boolean firstTime = true; 64 65 69 static SecuritySupport ss = new SecuritySupport(); 70 71 static { 73 try { 76 String val = ss.getSystemProperty("jaxp.debug"); 77 debug = val != null && !"false".equals(val); 79 } 80 catch (SecurityException se) { 81 debug = false; 82 } 83 } 84 85 private static void dPrint(String msg) { 86 if (debug) { 87 System.err.println("JAXP: " + msg); 88 } 89 } 90 91 100 static private Class getProviderClass(String className, ClassLoader cl, 101 boolean doFallback) throws ClassNotFoundException 102 { 103 try { 104 if (cl == null) { 105 cl = ss.getContextClassLoader(); 106 if (cl == null) { 107 throw new ClassNotFoundException (); 108 } 109 else { 110 return cl.loadClass(className); 111 } 112 } 113 else { 114 return cl.loadClass(className); 115 } 116 } 117 catch (ClassNotFoundException e1) { 118 if (doFallback) { 119 return Class.forName(className, true, FactoryFinder.class.getClassLoader()); 121 } 122 else { 123 throw e1; 124 } 125 } 126 } 127 128 141 static Object newInstance(String className, ClassLoader cl, boolean doFallback) 142 throws ConfigurationError 143 { 144 try { 145 Class providerClass = getProviderClass(className, cl, doFallback); 146 Object instance = providerClass.newInstance(); 147 if (debug) { dPrint("created new instance of " + providerClass + 149 " using ClassLoader: " + cl); 150 } 151 return instance; 152 } 153 catch (ClassNotFoundException x) { 154 throw new ConfigurationError( 155 "Provider " + className + " not found", x); 156 } 157 catch (Exception x) { 158 throw new ConfigurationError( 159 "Provider " + className + " could not be instantiated: " + x, 160 x); 161 } 162 } 163 164 176 static Object find(String factoryId, String fallbackClassName) 177 throws ConfigurationError 178 { 179 dPrint("find factoryId =" + factoryId); 180 181 try { 183 String systemProp = ss.getSystemProperty(factoryId); 184 if (systemProp != null) { 185 dPrint("found system property, value=" + systemProp); 186 return newInstance(systemProp, null, true); 187 } 188 } 189 catch (SecurityException se) { 190 if (debug) se.printStackTrace(); 191 } 192 193 String configFile = null; 196 try { 197 String factoryClassName = null; 198 if (firstTime) { 199 synchronized (cacheProps) { 200 if (firstTime) { 201 configFile = ss.getSystemProperty("java.home") + File.separator + 202 "lib" + File.separator + "stax.properties"; 203 File f = new File (configFile); 204 firstTime = false; 205 if (ss.doesFileExist(f)) { 206 dPrint("Read properties file "+f); 207 cacheProps.load(ss.getFileInputStream(f)); 208 } 209 else { 210 configFile = ss.getSystemProperty("java.home") + File.separator + 211 "lib" + File.separator + "jaxp.properties"; 212 f = new File (configFile); 213 if (ss.doesFileExist(f)) { 214 dPrint("Read properties file "+f); 215 cacheProps.load(ss.getFileInputStream(f)); 216 } 217 } 218 } 219 } 220 } 221 factoryClassName = cacheProps.getProperty(factoryId); 222 223 if (factoryClassName != null) { 224 dPrint("found in " + configFile + " value=" + factoryClassName); 225 return newInstance(factoryClassName, null, true); 226 } 227 } 228 catch (Exception ex) { 229 if (debug) ex.printStackTrace(); 230 } 231 232 Object provider = findJarServiceProvider(factoryId); 234 if (provider != null) { 235 return provider; 236 } 237 if (fallbackClassName == null) { 238 throw new ConfigurationError( 239 "Provider for " + factoryId + " cannot be found", null); 240 } 241 242 dPrint("loaded from fallback value: " + fallbackClassName); 243 return newInstance(fallbackClassName, null, true); 244 } 245 246 251 private static Object findJarServiceProvider(String factoryId) 252 throws ConfigurationError 253 { 254 String serviceId = "META-INF/services/" + factoryId; 255 InputStream is = null; 256 257 ClassLoader cl = ss.getContextClassLoader(); 259 if (cl != null) { 260 is = ss.getResourceAsStream(cl, serviceId); 261 262 if (is == null) { 264 cl = FactoryFinder.class.getClassLoader(); 265 is = ss.getResourceAsStream(cl, serviceId); 266 } 267 } else { 268 cl = FactoryFinder.class.getClassLoader(); 270 is = ss.getResourceAsStream(cl, serviceId); 271 } 272 273 if (is == null) { 274 return null; 276 } 277 278 if (debug) { dPrint("found jar resource=" + serviceId + " using ClassLoader: " + cl); 280 } 281 282 BufferedReader rd; 283 try { 284 rd = new BufferedReader (new InputStreamReader (is, "UTF-8")); 285 } 286 catch (java.io.UnsupportedEncodingException e) { 287 rd = new BufferedReader (new InputStreamReader (is)); 288 } 289 290 String factoryClassName = null; 291 try { 292 factoryClassName = rd.readLine(); 295 rd.close(); 296 } catch (IOException x) { 297 return null; 299 } 300 301 if (factoryClassName != null && !"".equals(factoryClassName)) { 302 dPrint("found in resource, value=" + factoryClassName); 303 304 return newInstance(factoryClassName, cl, false); 309 } 310 311 return null; 313 } 314 315 static class ConfigurationError extends Error { 316 private Exception exception; 317 318 322 ConfigurationError(String msg, Exception x) { 323 super(msg); 324 this.exception = x; 325 } 326 327 Exception getException() { 328 return exception; 329 } 330 } 331 332 } 333 | Popular Tags |