1 16 19 20 package org.apache.xalan.lib.sql; 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 try{ 464 if (security != null){ 465 security.checkPackageAccess(className); 466 } 467 }catch(SecurityException e){ 468 throw e; 469 } 470 471 Class providerClass; 472 if (cl == null) { 473 providerClass = Class.forName(className); 483 } else { 484 try { 485 providerClass = cl.loadClass(className); 486 } catch (ClassNotFoundException x) { 487 if (doFallback) { 488 ClassLoader current = ObjectFactory.class.getClassLoader(); 490 if (current == null) { 491 providerClass = Class.forName(className); 492 } else if (cl != current) { 493 cl = current; 494 providerClass = cl.loadClass(className); 495 } else { 496 throw x; 497 } 498 } else { 499 throw x; 500 } 501 } 502 } 503 504 return providerClass; 505 } 506 507 512 private static String findJarServiceProviderName(String factoryId) 513 { 514 SecuritySupport ss = SecuritySupport.getInstance(); 515 String serviceId = SERVICES_PATH + factoryId; 516 InputStream is = null; 517 518 ClassLoader cl = findClassLoader(); 520 521 is = ss.getResourceAsStream(cl, serviceId); 522 523 if (is == null) { 525 ClassLoader current = ObjectFactory.class.getClassLoader(); 526 if (cl != current) { 527 cl = current; 528 is = ss.getResourceAsStream(cl, serviceId); 529 } 530 } 531 532 if (is == null) { 533 return null; 535 } 536 537 debugPrintln("found jar resource=" + serviceId + 538 " using ClassLoader: " + cl); 539 540 BufferedReader rd; 557 try { 558 rd = new BufferedReader (new InputStreamReader (is, "UTF-8")); 559 } catch (java.io.UnsupportedEncodingException e) { 560 rd = new BufferedReader (new InputStreamReader (is)); 561 } 562 563 String factoryClassName = null; 564 try { 565 factoryClassName = rd.readLine(); 568 rd.close(); 569 } catch (IOException x) { 570 return null; 572 } 573 574 if (factoryClassName != null && 575 ! "".equals(factoryClassName)) { 576 debugPrintln("found in resource, value=" 577 + factoryClassName); 578 579 return factoryClassName; 584 } 585 586 return null; 588 } 589 590 594 597 static class ConfigurationError 598 extends Error { 599 600 604 605 private Exception exception; 606 607 611 615 ConfigurationError(String msg, Exception x) { 616 super(msg); 617 this.exception = x; 618 } 620 624 625 Exception getException() { 626 return exception; 627 } 629 } 631 } | Popular Tags |