1 55 56 package com.sun.org.apache.xml.internal.serialize; 57 58 import java.io.InputStream ; 59 import java.io.IOException ; 60 import java.io.File ; 61 import java.io.FileInputStream ; 62 63 import java.util.Properties ; 64 import java.io.BufferedReader ; 65 import java.io.InputStreamReader ; 66 67 81 class ObjectFactory { 82 83 87 private static final String DEFAULT_PROPERTIES_FILENAME = "xerces.properties"; 89 90 91 private static final boolean DEBUG = false; 92 93 98 private static Properties fXercesProperties = null; 99 100 105 private static long fLastModified = -1; 106 107 111 129 static Object createObject(String factoryId, String fallbackClassName) 130 throws ConfigurationError { 131 return createObject(factoryId, null, fallbackClassName); 132 } 134 156 static Object createObject(String factoryId, 157 String propertiesFilename, 158 String fallbackClassName) 159 throws ConfigurationError 160 { 161 if (DEBUG) debugPrintln("debug is on"); 162 163 SecuritySupport ss = SecuritySupport.getInstance(); 164 ClassLoader cl = findClassLoader(); 165 166 try { 168 String systemProp = ss.getSystemProperty(factoryId); 169 if (systemProp != null) { 170 if (DEBUG) debugPrintln("found system property, value=" + systemProp); 171 return newInstance(systemProp, cl, true); 172 } 173 } catch (SecurityException se) { 174 } 176 177 String factoryClassName = null; 179 if (propertiesFilename == null) { 181 File propertiesFile = null; 182 boolean propertiesFileExists = false; 183 try { 184 String javah = ss.getSystemProperty("java.home"); 185 propertiesFilename = javah + File.separator + 186 "lib" + File.separator + DEFAULT_PROPERTIES_FILENAME; 187 propertiesFile = new File (propertiesFilename); 188 propertiesFileExists = ss.getFileExists(propertiesFile); 189 } catch (SecurityException e) { 190 fLastModified = -1; 192 fXercesProperties = null; 193 } 194 195 synchronized (ObjectFactory.class) { 196 boolean loadProperties = false; 197 try { 198 if(fLastModified >= 0) { 200 if(propertiesFileExists && 201 (fLastModified < (fLastModified = ss.getLastModified(propertiesFile)))) { 202 loadProperties = true; 203 } else { 204 if(!propertiesFileExists) { 206 fLastModified = -1; 207 fXercesProperties = null; 208 } } 210 } else { 211 if(propertiesFileExists) { 213 loadProperties = true; 214 fLastModified = ss.getLastModified(propertiesFile); 215 } } 217 if(loadProperties) { 218 fXercesProperties = new Properties (); 220 FileInputStream fis = ss.getFileInputStream(propertiesFile); 221 fXercesProperties.load(fis); 222 fis.close(); 223 } 224 } catch (Exception x) { 225 fXercesProperties = null; 226 fLastModified = -1; 227 } 231 } 232 if(fXercesProperties != null) { 233 factoryClassName = fXercesProperties.getProperty(factoryId); 234 } 235 } else { 236 try { 237 FileInputStream fis = ss.getFileInputStream(new File (propertiesFilename)); 238 Properties props = new Properties (); 239 props.load(fis); 240 fis.close(); 241 factoryClassName = props.getProperty(factoryId); 242 } catch (Exception x) { 243 } 247 } 248 if (factoryClassName != null) { 249 if (DEBUG) debugPrintln("found in " + propertiesFilename + ", value=" + factoryClassName); 250 return newInstance(factoryClassName, cl, true); 251 } 252 253 Object provider = findJarServiceProvider(factoryId); 255 if (provider != null) { 256 return provider; 257 } 258 259 if (fallbackClassName == null) { 260 throw new ConfigurationError( 261 "Provider for " + factoryId + " cannot be found", null); 262 } 263 264 if (DEBUG) debugPrintln("using fallback, value=" + fallbackClassName); 265 return newInstance(fallbackClassName, cl, true); 266 } 268 272 273 private static void debugPrintln(String msg) { 274 if (DEBUG) { 275 System.err.println("JAXP: " + msg); 276 } 277 } 279 283 static ClassLoader findClassLoader() 284 throws ConfigurationError 285 { 286 SecuritySupport ss = SecuritySupport.getInstance(); 287 288 ClassLoader context = ss.getContextClassLoader(); 291 ClassLoader system = ss.getSystemClassLoader(); 292 293 ClassLoader chain = system; 294 while (true) { 295 if (context == chain) { 296 ClassLoader current = ObjectFactory.class.getClassLoader(); 305 306 chain = system; 307 while (true) { 308 if (current == chain) { 309 return system; 312 } 313 if (chain == null) { 314 break; 315 } 316 chain = ss.getParentClassLoader(chain); 317 } 318 319 return current; 322 } 323 324 if (chain == null) { 325 break; 327 } 328 329 chain = ss.getParentClassLoader(chain); 332 }; 333 334 return context; 337 } 339 342 static Object newInstance(String className, ClassLoader cl, 343 boolean doFallback) 344 throws ConfigurationError 345 { 346 try{ 348 Class providerClass = findProviderClass(className, cl, doFallback); 349 Object instance = providerClass.newInstance(); 350 if (DEBUG) debugPrintln("created new instance of " + providerClass + 351 " using ClassLoader: " + cl); 352 return instance; 353 } catch (ClassNotFoundException x) { 354 throw new ConfigurationError( 355 "Provider " + className + " not found", x); 356 } catch (Exception x) { 357 throw new ConfigurationError( 358 "Provider " + className + " could not be instantiated: " + x, 359 x); 360 } 361 } 362 363 366 static Class findProviderClass(String className, ClassLoader cl, 367 boolean doFallback) 368 throws ClassNotFoundException , ConfigurationError 369 { 370 SecurityManager security = System.getSecurityManager(); 373 try{ 374 if(security != null){ 375 security.checkPackageAccess(className); 376 } 377 }catch(SecurityException e){ 378 throw e ; 379 } 380 Class providerClass; 381 if (cl == null) { 382 providerClass = Class.forName(className); 392 } else { 393 try { 394 providerClass = cl.loadClass(className); 395 } catch (ClassNotFoundException x) { 396 if (doFallback) { 397 ClassLoader current = ObjectFactory.class.getClassLoader(); 399 if (current == null) { 400 providerClass = Class.forName(className); 401 } else if (cl != current) { 402 cl = current; 403 providerClass = cl.loadClass(className); 404 } else { 405 throw x; 406 } 407 } else { 408 throw x; 409 } 410 } 411 } 412 413 return providerClass; 414 } 415 416 421 private static Object findJarServiceProvider(String factoryId) 422 throws ConfigurationError 423 { 424 SecuritySupport ss = SecuritySupport.getInstance(); 425 String serviceId = "META-INF/services/" + factoryId; 426 InputStream is = null; 427 428 ClassLoader cl = findClassLoader(); 430 431 is = ss.getResourceAsStream(cl, serviceId); 432 433 if (is == null) { 435 ClassLoader current = ObjectFactory.class.getClassLoader(); 436 if (cl != current) { 437 cl = current; 438 is = ss.getResourceAsStream(cl, serviceId); 439 } 440 } 441 442 if (is == null) { 443 return null; 445 } 446 447 if (DEBUG) debugPrintln("found jar resource=" + serviceId + 448 " using ClassLoader: " + cl); 449 450 BufferedReader rd; 467 try { 468 rd = new BufferedReader (new InputStreamReader (is, "UTF-8")); 469 } catch (java.io.UnsupportedEncodingException e) { 470 rd = new BufferedReader (new InputStreamReader (is)); 471 } 472 473 String factoryClassName = null; 474 try { 475 factoryClassName = rd.readLine(); 478 rd.close(); 479 } catch (IOException x) { 480 return null; 482 } 483 484 if (factoryClassName != null && 485 ! "".equals(factoryClassName)) { 486 if (DEBUG) debugPrintln("found in resource, value=" 487 + factoryClassName); 488 489 return newInstance(factoryClassName, cl, false); 494 } 495 496 return null; 498 } 499 500 504 507 static class ConfigurationError 508 extends Error { 509 510 514 515 private Exception exception; 516 517 521 525 ConfigurationError(String msg, Exception x) { 526 super(msg); 527 this.exception = x; 528 } 530 534 535 Exception getException() { 536 return exception; 537 } 539 } 541 } | Popular Tags |