1 55 56 57 package com.sun.org.apache.xerces.internal.xinclude; 58 59 import java.io.InputStream ; 60 import java.io.IOException ; 61 import java.io.File ; 62 import java.io.FileInputStream ; 63 64 import java.util.Properties ; 65 import java.io.BufferedReader ; 66 import java.io.InputStreamReader ; 67 68 82 class ObjectFactory { 83 84 88 private static final String DEFAULT_PROPERTIES_FILENAME = "xerces.properties"; 90 91 92 private static final boolean DEBUG = false; 93 94 97 private static final int DEFAULT_LINE_LENGTH = 80; 98 99 104 private static Properties fXercesProperties = null; 105 106 111 private static long fLastModified = -1; 112 113 117 135 static Object createObject(String factoryId, String fallbackClassName) 136 throws ConfigurationError { 137 return createObject(factoryId, null, fallbackClassName); 138 } 140 162 static Object createObject(String factoryId, 163 String propertiesFilename, 164 String fallbackClassName) 165 throws ConfigurationError 166 { 167 if (DEBUG) debugPrintln("debug is on"); 168 169 SecuritySupport ss = SecuritySupport.getInstance(); 170 ClassLoader cl = findClassLoader(); 171 172 try { 174 String systemProp = ss.getSystemProperty(factoryId); 175 if (systemProp != null) { 176 if (DEBUG) debugPrintln("found system property, value=" + systemProp); 177 return newInstance(systemProp, cl, true); 178 } 179 } catch (SecurityException se) { 180 } 182 183 String factoryClassName = null; 185 if (propertiesFilename == null) { 187 File propertiesFile = null; 188 boolean propertiesFileExists = false; 189 try { 190 String javah = ss.getSystemProperty("java.home"); 191 propertiesFilename = javah + File.separator + 192 "lib" + File.separator + DEFAULT_PROPERTIES_FILENAME; 193 propertiesFile = new File (propertiesFilename); 194 propertiesFileExists = ss.getFileExists(propertiesFile); 195 } catch (SecurityException e) { 196 fLastModified = -1; 198 fXercesProperties = null; 199 } 200 201 synchronized (ObjectFactory.class) { 202 boolean loadProperties = false; 203 try { 204 if(fLastModified >= 0) { 206 if(propertiesFileExists && 207 (fLastModified < (fLastModified = ss.getLastModified(propertiesFile)))) { 208 loadProperties = true; 209 } else { 210 if(!propertiesFileExists) { 212 fLastModified = -1; 213 fXercesProperties = null; 214 } } 216 } else { 217 if(propertiesFileExists) { 219 loadProperties = true; 220 fLastModified = ss.getLastModified(propertiesFile); 221 } } 223 if(loadProperties) { 224 fXercesProperties = new Properties (); 226 FileInputStream fis = ss.getFileInputStream(propertiesFile); 227 fXercesProperties.load(fis); 228 fis.close(); 229 } 230 } catch (Exception x) { 231 fXercesProperties = null; 232 fLastModified = -1; 233 } 237 } 238 if(fXercesProperties != null) { 239 factoryClassName = fXercesProperties.getProperty(factoryId); 240 } 241 } else { 242 try { 243 FileInputStream fis = ss.getFileInputStream(new File (propertiesFilename)); 244 Properties props = new Properties (); 245 props.load(fis); 246 fis.close(); 247 factoryClassName = props.getProperty(factoryId); 248 } catch (Exception x) { 249 } 253 } 254 if (factoryClassName != null) { 255 if (DEBUG) debugPrintln("found in " + propertiesFilename + ", value=" + factoryClassName); 256 return newInstance(factoryClassName, cl, true); 257 } 258 259 Object provider = findJarServiceProvider(factoryId); 261 if (provider != null) { 262 return provider; 263 } 264 265 if (fallbackClassName == null) { 266 throw new ConfigurationError( 267 "Provider for " + factoryId + " cannot be found", null); 268 } 269 270 if (DEBUG) debugPrintln("using fallback, value=" + fallbackClassName); 271 return newInstance(fallbackClassName, cl, true); 272 } 274 278 279 private static void debugPrintln(String msg) { 280 if (DEBUG) { 281 System.err.println("JAXP: " + msg); 282 } 283 } 285 289 static ClassLoader findClassLoader() 290 throws ConfigurationError 291 { 292 SecuritySupport ss = SecuritySupport.getInstance(); 293 294 ClassLoader context = ss.getContextClassLoader(); 297 ClassLoader system = ss.getSystemClassLoader(); 298 299 ClassLoader chain = system; 300 while (true) { 301 if (context == chain) { 302 ClassLoader current = ObjectFactory.class.getClassLoader(); 311 312 chain = system; 313 while (true) { 314 if (current == chain) { 315 return system; 318 } 319 if (chain == null) { 320 break; 321 } 322 chain = ss.getParentClassLoader(chain); 323 } 324 325 return current; 328 } 329 330 if (chain == null) { 331 break; 333 } 334 335 chain = ss.getParentClassLoader(chain); 338 }; 339 340 return context; 343 } 345 348 static Object newInstance(String className, ClassLoader cl, 349 boolean doFallback) 350 throws ConfigurationError 351 { 352 try{ 354 Class providerClass = findProviderClass(className, cl, doFallback); 355 Object instance = providerClass.newInstance(); 356 if (DEBUG) debugPrintln("created new instance of " + providerClass + 357 " using ClassLoader: " + cl); 358 return instance; 359 } catch (ClassNotFoundException x) { 360 throw new ConfigurationError( 361 "Provider " + className + " not found", x); 362 } catch (Exception x) { 363 throw new ConfigurationError( 364 "Provider " + className + " could not be instantiated: " + x, 365 x); 366 } 367 } 368 369 372 static Class findProviderClass(String className, ClassLoader cl, 373 boolean doFallback) 374 throws ClassNotFoundException , ConfigurationError 375 { 376 SecurityManager security = System.getSecurityManager(); 379 try{ 380 if (security != null) { 381 final int lastDot = className.lastIndexOf("."); 382 String packageName = className; 383 if (lastDot != -1) packageName = className.substring(0, lastDot); 384 security.checkPackageAccess(packageName); 385 } 386 }catch(SecurityException e){ 387 throw e ; 388 } 389 Class providerClass; 390 if (cl == null) { 391 providerClass = Class.forName(className); 401 } else { 402 try { 403 providerClass = cl.loadClass(className); 404 } catch (ClassNotFoundException x) { 405 if (doFallback) { 406 ClassLoader current = ObjectFactory.class.getClassLoader(); 408 if (current == null) { 409 providerClass = Class.forName(className); 410 } else if (cl != current) { 411 cl = current; 412 providerClass = cl.loadClass(className); 413 } else { 414 throw x; 415 } 416 } else { 417 throw x; 418 } 419 } 420 } 421 422 return providerClass; 423 } 424 425 430 private static Object findJarServiceProvider(String factoryId) 431 throws ConfigurationError 432 { 433 SecuritySupport ss = SecuritySupport.getInstance(); 434 String serviceId = "META-INF/services/" + factoryId; 435 InputStream is = null; 436 437 ClassLoader cl = findClassLoader(); 439 440 is = ss.getResourceAsStream(cl, serviceId); 441 442 if (is == null) { 444 ClassLoader current = ObjectFactory.class.getClassLoader(); 445 if (cl != current) { 446 cl = current; 447 is = ss.getResourceAsStream(cl, serviceId); 448 } 449 } 450 451 if (is == null) { 452 return null; 454 } 455 456 if (DEBUG) debugPrintln("found jar resource=" + serviceId + 457 " using ClassLoader: " + cl); 458 459 BufferedReader rd; 476 try { 477 rd = new BufferedReader (new InputStreamReader (is, "UTF-8"), DEFAULT_LINE_LENGTH); 478 } catch (java.io.UnsupportedEncodingException e) { 479 rd = new BufferedReader (new InputStreamReader (is), DEFAULT_LINE_LENGTH); 480 } 481 482 String factoryClassName = null; 483 try { 484 factoryClassName = rd.readLine(); 487 rd.close(); 488 } catch (IOException x) { 489 return null; 491 } 492 493 if (factoryClassName != null && 494 ! "".equals(factoryClassName)) { 495 if (DEBUG) debugPrintln("found in resource, value=" 496 + factoryClassName); 497 498 return newInstance(factoryClassName, cl, false); 503 } 504 505 return null; 507 } 508 509 513 516 static class ConfigurationError 517 extends Error { 518 519 523 524 private Exception exception; 525 526 530 534 ConfigurationError(String msg, Exception x) { 535 super(msg); 536 this.exception = x; 537 } 539 543 544 Exception getException() { 545 return exception; 546 } 548 } 550 } | Popular Tags |