1 55 56 57 58 package com.sun.org.apache.xerces.internal.parsers; 59 60 import java.io.InputStream ; 61 import java.io.IOException ; 62 import java.io.File ; 63 import java.io.FileInputStream ; 64 65 import java.util.Properties ; 66 import java.io.BufferedReader ; 67 import java.io.InputStreamReader ; 68 69 83 class ObjectFactory { 84 85 89 private static final String DEFAULT_PROPERTIES_FILENAME = "xerces.properties"; 91 92 93 private static final boolean DEBUG = false; 94 95 98 private static final int DEFAULT_LINE_LENGTH = 80; 99 100 105 private static Properties fXercesProperties = null; 106 107 112 private static long fLastModified = -1; 113 114 118 136 static Object createObject(String factoryId, String fallbackClassName) 137 throws ConfigurationError { 138 return createObject(factoryId, null, fallbackClassName); 139 } 141 163 static Object createObject(String factoryId, 164 String propertiesFilename, 165 String fallbackClassName) 166 throws ConfigurationError 167 { 168 if (DEBUG) debugPrintln("debug is on"); 169 170 SecuritySupport ss = SecuritySupport.getInstance(); 171 ClassLoader cl = findClassLoader(); 172 173 try { 175 String systemProp = ss.getSystemProperty(factoryId); 176 if (systemProp != null) { 177 if (DEBUG) debugPrintln("found system property, value=" + systemProp); 178 return newInstance(systemProp, cl, true); 179 } 180 } catch (SecurityException se) { 181 } 183 184 String factoryClassName = null; 186 if (propertiesFilename == null) { 188 File propertiesFile = null; 189 boolean propertiesFileExists = false; 190 try { 191 String javah = ss.getSystemProperty("java.home"); 192 propertiesFilename = javah + File.separator + 193 "lib" + File.separator + DEFAULT_PROPERTIES_FILENAME; 194 propertiesFile = new File (propertiesFilename); 195 propertiesFileExists = ss.getFileExists(propertiesFile); 196 } catch (SecurityException e) { 197 fLastModified = -1; 199 fXercesProperties = null; 200 } 201 202 synchronized (ObjectFactory.class) { 203 boolean loadProperties = false; 204 try { 205 if(fLastModified >= 0) { 207 if(propertiesFileExists && 208 (fLastModified < (fLastModified = ss.getLastModified(propertiesFile)))) { 209 loadProperties = true; 210 } else { 211 if(!propertiesFileExists) { 213 fLastModified = -1; 214 fXercesProperties = null; 215 } } 217 } else { 218 if(propertiesFileExists) { 220 loadProperties = true; 221 fLastModified = ss.getLastModified(propertiesFile); 222 } } 224 if(loadProperties) { 225 fXercesProperties = new Properties (); 227 FileInputStream fis = ss.getFileInputStream(propertiesFile); 228 fXercesProperties.load(fis); 229 fis.close(); 230 } 231 } catch (Exception x) { 232 fXercesProperties = null; 233 fLastModified = -1; 234 } 238 } 239 if(fXercesProperties != null) { 240 factoryClassName = fXercesProperties.getProperty(factoryId); 241 } 242 } else { 243 try { 244 FileInputStream fis = ss.getFileInputStream(new File (propertiesFilename)); 245 Properties props = new Properties (); 246 props.load(fis); 247 fis.close(); 248 factoryClassName = props.getProperty(factoryId); 249 } catch (Exception x) { 250 } 254 } 255 if (factoryClassName != null) { 256 if (DEBUG) debugPrintln("found in " + propertiesFilename + ", value=" + factoryClassName); 257 return newInstance(factoryClassName, cl, true); 258 } 259 260 Object provider = findJarServiceProvider(factoryId); 262 if (provider != null) { 263 return provider; 264 } 265 266 if (fallbackClassName == null) { 267 throw new ConfigurationError( 268 "Provider for " + factoryId + " cannot be found", null); 269 } 270 271 if (DEBUG) debugPrintln("using fallback, value=" + fallbackClassName); 272 return newInstance(fallbackClassName, cl, true); 273 } 275 279 280 private static void debugPrintln(String msg) { 281 if (DEBUG) { 282 System.err.println("JAXP: " + msg); 283 } 284 } 286 290 static ClassLoader findClassLoader() 291 throws ConfigurationError 292 { 293 SecuritySupport ss = SecuritySupport.getInstance(); 294 295 ClassLoader context = ss.getContextClassLoader(); 298 ClassLoader system = ss.getSystemClassLoader(); 299 300 ClassLoader chain = system; 301 while (true) { 302 if (context == chain) { 303 ClassLoader current = ObjectFactory.class.getClassLoader(); 312 313 chain = system; 314 while (true) { 315 if (current == chain) { 316 return system; 319 } 320 if (chain == null) { 321 break; 322 } 323 chain = ss.getParentClassLoader(chain); 324 } 325 326 return current; 329 } 330 331 if (chain == null) { 332 break; 334 } 335 336 chain = ss.getParentClassLoader(chain); 339 }; 340 341 return context; 344 } 346 349 static Object newInstance(String className, ClassLoader cl, 350 boolean doFallback) 351 throws ConfigurationError 352 { 353 try{ 355 Class providerClass = findProviderClass(className, cl, doFallback); 356 Object instance = providerClass.newInstance(); 357 if (DEBUG) debugPrintln("created new instance of " + providerClass + 358 " using ClassLoader: " + cl); 359 return instance; 360 } catch (ClassNotFoundException x) { 361 throw new ConfigurationError( 362 "Provider " + className + " not found", x); 363 } catch (Exception x) { 364 throw new ConfigurationError( 365 "Provider " + className + " could not be instantiated: " + x, 366 x); 367 } 368 } 369 370 373 static Class findProviderClass(String className, ClassLoader cl, 374 boolean doFallback) 375 throws ClassNotFoundException , ConfigurationError 376 { 377 SecurityManager security = System.getSecurityManager(); 380 try{ 381 if (security != null) { 382 final int lastDot = className.lastIndexOf("."); 383 String packageName = className; 384 if (lastDot != -1) packageName = className.substring(0, lastDot); 385 security.checkPackageAccess(packageName); 386 } 387 }catch(SecurityException e){ 388 throw e ; 389 } 390 Class providerClass; 391 if (cl == null) { 392 providerClass = Class.forName(className); 402 } else { 403 try { 404 providerClass = cl.loadClass(className); 405 } catch (ClassNotFoundException x) { 406 if (doFallback) { 407 ClassLoader current = ObjectFactory.class.getClassLoader(); 409 if (current == null) { 410 providerClass = Class.forName(className); 411 } else if (cl != current) { 412 cl = current; 413 providerClass = cl.loadClass(className); 414 } else { 415 throw x; 416 } 417 } else { 418 throw x; 419 } 420 } 421 } 422 423 return providerClass; 424 } 425 426 431 private static Object findJarServiceProvider(String factoryId) 432 throws ConfigurationError 433 { 434 SecuritySupport ss = SecuritySupport.getInstance(); 435 String serviceId = "META-INF/services/" + factoryId; 436 InputStream is = null; 437 438 ClassLoader cl = findClassLoader(); 440 441 is = ss.getResourceAsStream(cl, serviceId); 442 443 if (is == null) { 445 ClassLoader current = ObjectFactory.class.getClassLoader(); 446 if (cl != current) { 447 cl = current; 448 is = ss.getResourceAsStream(cl, serviceId); 449 } 450 } 451 452 if (is == null) { 453 return null; 455 } 456 457 if (DEBUG) debugPrintln("found jar resource=" + serviceId + 458 " using ClassLoader: " + cl); 459 460 BufferedReader rd; 477 try { 478 rd = new BufferedReader (new InputStreamReader (is, "UTF-8"), DEFAULT_LINE_LENGTH); 479 } catch (java.io.UnsupportedEncodingException e) { 480 rd = new BufferedReader (new InputStreamReader (is), DEFAULT_LINE_LENGTH); 481 } 482 483 String factoryClassName = null; 484 try { 485 factoryClassName = rd.readLine(); 488 rd.close(); 489 } catch (IOException x) { 490 return null; 492 } 493 494 if (factoryClassName != null && 495 ! "".equals(factoryClassName)) { 496 if (DEBUG) debugPrintln("found in resource, value=" 497 + factoryClassName); 498 499 return newInstance(factoryClassName, cl, false); 504 } 505 506 return null; 508 } 509 510 514 517 static class ConfigurationError 518 extends Error { 519 520 524 525 private Exception exception; 526 527 531 535 ConfigurationError(String msg, Exception x) { 536 super(msg); 537 this.exception = x; 538 } 540 544 545 Exception getException() { 546 return exception; 547 } 549 } 551 } | Popular Tags |