1 16 19 20 package com.sun.org.apache.xpath.internal.functions; 21 22 import java.io.InputStream ; 23 import java.io.IOException ; 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 27 import java.util.Properties ; 28 import java.io.BufferedReader ; 29 import java.io.InputStreamReader ; 30 31 48 class ObjectFactory { 49 50 54 private static final String DEFAULT_PROPERTIES_FILENAME = 56 "xalan.properties"; 57 58 private static final String SERVICES_PATH = "META-INF/services/"; 59 60 61 private static final boolean DEBUG = false; 62 63 68 private static Properties fXalanProperties = null; 69 70 75 private static long fLastModified = -1; 76 77 81 99 static Object createObject(String factoryId, String fallbackClassName) 100 throws ConfigurationError { 101 return createObject(factoryId, null, fallbackClassName); 102 } 104 126 static Object createObject(String factoryId, 127 String propertiesFilename, 128 String fallbackClassName) 129 throws ConfigurationError 130 { 131 Class factoryClass = lookUpFactoryClass(factoryId, 132 propertiesFilename, 133 fallbackClassName); 134 135 if (factoryClass == null) { 136 throw new ConfigurationError( 137 "Provider for " + factoryId + " cannot be found", null); 138 } 139 140 try{ 141 Object instance = factoryClass.newInstance(); 142 debugPrintln("created new instance of factory " + factoryId); 143 return instance; 144 } catch (Exception x) { 145 throw new ConfigurationError( 146 "Provider for factory " + factoryId 147 + " could not be instantiated: " + x, x); 148 } 149 } 151 173 static Class lookUpFactoryClass(String factoryId) 174 throws ConfigurationError 175 { 176 return lookUpFactoryClass(factoryId, null, null); 177 } 179 201 static Class lookUpFactoryClass(String factoryId, 202 String propertiesFilename, 203 String fallbackClassName) 204 throws ConfigurationError 205 { 206 String factoryClassName = lookUpFactoryClassName(factoryId, 207 propertiesFilename, 208 fallbackClassName); 209 ClassLoader cl = findClassLoader(); 210 211 if (factoryClassName == null) { 212 factoryClassName = fallbackClassName; 213 } 214 215 try{ 217 Class providerClass = findProviderClass(factoryClassName, 218 cl, 219 true); 220 debugPrintln("created new instance of " + providerClass + 221 " using ClassLoader: " + cl); 222 return providerClass; 223 } catch (ClassNotFoundException x) { 224 throw new ConfigurationError( 225 "Provider " + factoryClassName + " not found", x); 226 } catch (Exception x) { 227 throw new ConfigurationError( 228 "Provider "+factoryClassName+" could not be instantiated: "+x, 229 x); 230 } 231 } 233 255 static String lookUpFactoryClassName(String factoryId, 256 String propertiesFilename, 257 String fallbackClassName) 258 { 259 SecuritySupport ss = SecuritySupport.getInstance(); 260 261 try { 263 String systemProp = ss.getSystemProperty(factoryId); 264 if (systemProp != null) { 265 debugPrintln("found system property, value=" + systemProp); 266 return systemProp; 267 } 268 } catch (SecurityException se) { 269 } 271 272 String factoryClassName = null; 275 if (propertiesFilename == null) { 278 File propertiesFile = null; 279 boolean propertiesFileExists = false; 280 try { 281 String javah = ss.getSystemProperty("java.home"); 282 propertiesFilename = javah + File.separator + 283 "lib" + File.separator + DEFAULT_PROPERTIES_FILENAME; 284 propertiesFile = new File (propertiesFilename); 285 propertiesFileExists = ss.getFileExists(propertiesFile); 286 } catch (SecurityException e) { 287 fLastModified = -1; 289 fXalanProperties = null; 290 } 291 292 synchronized (ObjectFactory.class) { 293 boolean loadProperties = false; 294 try { 295 if(fLastModified >= 0) { 297 if(propertiesFileExists && 298 (fLastModified < (fLastModified = ss.getLastModified(propertiesFile)))) { 299 loadProperties = true; 300 } else { 301 if(!propertiesFileExists) { 303 fLastModified = -1; 304 fXalanProperties = null; 305 } } 307 } else { 308 if(propertiesFileExists) { 310 loadProperties = true; 311 fLastModified = ss.getLastModified(propertiesFile); 312 } } 314 if(loadProperties) { 315 fXalanProperties = new Properties (); 318 FileInputStream fis = 319 ss.getFileInputStream(propertiesFile); 320 fXalanProperties.load(fis); 321 fis.close(); 322 } 323 } catch (Exception x) { 324 fXalanProperties = null; 325 fLastModified = -1; 326 } 330 } 331 if(fXalanProperties != null) { 332 factoryClassName = fXalanProperties.getProperty(factoryId); 333 } 334 } else { 335 try { 336 FileInputStream fis = 337 ss.getFileInputStream(new File (propertiesFilename)); 338 Properties props = new Properties (); 339 props.load(fis); 340 fis.close(); 341 factoryClassName = props.getProperty(factoryId); 342 } catch (Exception x) { 343 } 347 } 348 if (factoryClassName != null) { 349 debugPrintln("found in " + propertiesFilename + ", value=" 350 + factoryClassName); 351 return factoryClassName; 352 } 353 354 return findJarServiceProviderName(factoryId); 356 } 358 362 363 private static void debugPrintln(String msg) { 364 if (DEBUG) { 365 System.err.println("JAXP: " + msg); 366 } 367 } 369 373 static ClassLoader findClassLoader() 374 throws ConfigurationError 375 { 376 SecuritySupport ss = SecuritySupport.getInstance(); 377 378 ClassLoader context = ss.getContextClassLoader(); 381 ClassLoader system = ss.getSystemClassLoader(); 382 383 ClassLoader chain = system; 384 while (true) { 385 if (context == chain) { 386 ClassLoader current = ObjectFactory.class.getClassLoader(); 395 396 chain = system; 397 while (true) { 398 if (current == chain) { 399 return system; 402 } 403 if (chain == null) { 404 break; 405 } 406 chain = ss.getParentClassLoader(chain); 407 } 408 409 return current; 412 } 413 414 if (chain == null) { 415 break; 417 } 418 419 chain = ss.getParentClassLoader(chain); 422 }; 423 424 return context; 427 } 429 432 static Object newInstance(String className, ClassLoader cl, 433 boolean doFallback) 434 throws ConfigurationError 435 { 436 try{ 438 Class providerClass = findProviderClass(className, cl, doFallback); 439 Object instance = providerClass.newInstance(); 440 debugPrintln("created new instance of " + providerClass + 441 " using ClassLoader: " + cl); 442 return instance; 443 } catch (ClassNotFoundException x) { 444 throw new ConfigurationError( 445 "Provider " + className + " not found", x); 446 } catch (Exception x) { 447 throw new ConfigurationError( 448 "Provider " + className + " could not be instantiated: " + x, 449 x); 450 } 451 } 452 453 456 static Class findProviderClass(String className, ClassLoader cl, 457 boolean doFallback) 458 throws ClassNotFoundException , ConfigurationError 459 { 460 SecurityManager security = System.getSecurityManager(); 463 if (security != null){ 464 final int lastDot = className.lastIndexOf("."); 465 String packageName = className; 466 if (lastDot != -1) 467 packageName = className.substring(0, lastDot); 468 security.checkPackageAccess(packageName); 469 } 470 471 472 Class providerClass; 473 if (cl == null) { 474 providerClass = Class.forName(className); 484 } else { 485 try { 486 providerClass = cl.loadClass(className); 487 } catch (ClassNotFoundException x) { 488 if (doFallback) { 489 ClassLoader current = ObjectFactory.class.getClassLoader(); 491 if (current == null) { 492 providerClass = Class.forName(className); 493 } else if (cl != current) { 494 cl = current; 495 providerClass = cl.loadClass(className); 496 } else { 497 throw x; 498 } 499 } else { 500 throw x; 501 } 502 } 503 } 504 505 return providerClass; 506 } 507 508 513 private static String findJarServiceProviderName(String factoryId) 514 { 515 SecuritySupport ss = SecuritySupport.getInstance(); 516 String serviceId = SERVICES_PATH + factoryId; 517 InputStream is = null; 518 519 ClassLoader cl = findClassLoader(); 521 522 is = ss.getResourceAsStream(cl, serviceId); 523 524 if (is == null) { 526 ClassLoader current = ObjectFactory.class.getClassLoader(); 527 if (cl != current) { 528 cl = current; 529 is = ss.getResourceAsStream(cl, serviceId); 530 } 531 } 532 533 if (is == null) { 534 return null; 536 } 537 538 debugPrintln("found jar resource=" + serviceId + 539 " using ClassLoader: " + cl); 540 541 BufferedReader rd; 558 try { 559 rd = new BufferedReader (new InputStreamReader (is, "UTF-8")); 560 } catch (java.io.UnsupportedEncodingException e) { 561 rd = new BufferedReader (new InputStreamReader (is)); 562 } 563 564 String factoryClassName = null; 565 try { 566 factoryClassName = rd.readLine(); 569 rd.close(); 570 } catch (IOException x) { 571 return null; 573 } 574 575 if (factoryClassName != null && 576 ! "".equals(factoryClassName)) { 577 debugPrintln("found in resource, value=" 578 + factoryClassName); 579 580 return factoryClassName; 585 } 586 587 return null; 589 } 590 591 595 598 static class ConfigurationError 599 extends Error { 600 601 605 606 private Exception exception; 607 608 612 616 ConfigurationError(String msg, Exception x) { 617 super(msg); 618 this.exception = x; 619 } 621 625 626 Exception getException() { 627 return exception; 628 } 630 } 632 } | Popular Tags |