/** Load all of the named resource. */ private void loadAllResources(Vector v, String name) { boolean anyLoaded = false; try { URL[] urls; ClassLoader cld = null; // First try the "application's" class loader. cld = SecuritySupport.getContextClassLoader(); if (cld == null) cld = this.getClass().getClassLoader(); if (cld != null) urls = SecuritySupport.getResources(cld, name); else urls = SecuritySupport.getSystemResources(name); if (urls != null) { if (LogSupport.isLoggable()) LogSupport.log("MimetypesFileTypeMap: getResources"); for (int i = 0; i < urls.length; i++) { URL url = urls[i]; InputStream clis = null; if (LogSupport.isLoggable()) LogSupport.log("MimetypesFileTypeMap: URL " + url); try { clis = SecuritySupport.openStream(url); if (clis != null) { v.addElement(new MimeTypeFile(clis)); anyLoaded = true; if (LogSupport.isLoggable()) LogSupport.log( "MimetypesFileTypeMap: " + "successfully loaded " + "mime types from URL: " + url); } else { if (LogSupport.isLoggable()) LogSupport.log( "MimetypesFileTypeMap: " + "not loading " + "mime types from URL: " + url); } } catch (IOException ioex) { if (LogSupport.isLoggable()) LogSupport.log("MimetypesFileTypeMap: can't load " + url, ioex); } catch (SecurityException sex) { if (LogSupport.isLoggable()) LogSupport.log("MimetypesFileTypeMap: can't load " + url, sex); } finally { try { if (clis != null) clis.close(); } catch (IOException cex) { } } } } } catch (Exception ex) { if (LogSupport.isLoggable()) LogSupport.log("MimetypesFileTypeMap: can't load " + name, ex); } // if failed to load anything, fall back to old technique, just in case if (!anyLoaded) { LogSupport.log("MimetypesFileTypeMap: !anyLoaded"); MimeTypeFile mf = loadResource("/" + name); if (mf != null) v.addElement(mf); } }
/** Load from the named resource. */ private MimeTypeFile loadResource(String name) { InputStream clis = null; try { clis = SecuritySupport.getResourceAsStream(this.getClass(), name); if (clis != null) { MimeTypeFile mf = new MimeTypeFile(clis); if (LogSupport.isLoggable()) LogSupport.log("MimetypesFileTypeMap: successfully " + "loaded mime types file: " + name); return mf; } else { if (LogSupport.isLoggable()) LogSupport.log("MimetypesFileTypeMap: not loading " + "mime types file: " + name); } } catch (IOException e) { if (LogSupport.isLoggable()) LogSupport.log("MimetypesFileTypeMap: can't load " + name, e); } catch (SecurityException sex) { if (LogSupport.isLoggable()) LogSupport.log("MimetypesFileTypeMap: can't load " + name, sex); } finally { try { if (clis != null) clis.close(); } catch (IOException ex) { } // ignore it } return null; }
private void debugDisplayClassLoader() { try { if( classLoader == SecuritySupport.getContextClassLoader() ) { debugPrintln("using thread context class loader ("+classLoader+") for search"); return; } } // The VM ran out of memory or there was some other serious problem. Re-throw. catch (VirtualMachineError vme) { throw vme; } // ThreadDeath should always be re-thrown catch (ThreadDeath td) { throw td; } catch (Throwable _) { ; // getContextClassLoader() undefined in JDK1.1 } if( classLoader==ClassLoader.getSystemClassLoader() ) { debugPrintln("using system class loader ("+classLoader+") for search"); return; } debugPrintln("using class loader ("+classLoader+") for search"); }
static { // Use try/catch block to support applets try { debug = ss.getSystemProperty("jaxp.debug") != null; } catch (Exception _) { debug = false; } }
/** * Read from system properties, or those in jaxp.properties * * @param property the property * @param systemProperty the name of the system property */ private void getSystemProperty(Property property, String systemProperty) { try { String value = SecuritySupport.getSystemProperty(systemProperty); if (value != null) { values[property.ordinal()] = value; states[property.ordinal()] = State.SYSTEMPROPERTY; return; } value = SecuritySupport.readJAXPProperty(systemProperty); if (value != null) { values[property.ordinal()] = value; states[property.ordinal()] = State.JAXPDOTPROPERTIES; } } catch (NumberFormatException e) { // invalid setting ignored } }
static { // Use try/catch block to support applets try { String val = SecuritySupport.getSystemProperty("jaxp.debug"); // Allow simply setting the prop to turn on debug debug = val != null && (! "false".equals(val)); } catch (Exception _) { debug = false; } }
/** * <p>Search the specified classloader for the given classname.</p> * * @param classname the fully qualified name of the class to search for * @param loader the classloader to search * * @return the source location of the resource, or null if it wasn't found */ private static String which(String classname, ClassLoader loader) { String classnameAsResource = classname.replace('.', '/') + ".class"; if( loader==null ) loader = ClassLoader.getSystemClassLoader(); //URL it = loader.getResource(classnameAsResource); URL it = ss.getResourceAsURL(loader, classnameAsResource); if (it != null) { return it.toString(); } else { return null; } }
/** Figure out which ClassLoader to use. For JDK 1.2 and later use the context ClassLoader. */ static ClassLoader findClassLoader() throws ConfigurationError { SecuritySupport ss = SecuritySupport.getInstance(); // Figure out which ClassLoader to use for loading the provider // class. If there is a Context ClassLoader then use it. ClassLoader context = ss.getContextClassLoader(); ClassLoader system = ss.getSystemClassLoader(); ClassLoader chain = system; while (true) { if (context == chain) { // Assert: we are on JDK 1.1 or we have no Context ClassLoader // or any Context ClassLoader in chain of system classloader // (including extension ClassLoader) so extend to widest // ClassLoader (always look in system ClassLoader if Xalan // is in boot/extension/system classpath and in current // ClassLoader otherwise); normal classloaders delegate // back to system ClassLoader first so this widening doesn't // change the fact that context ClassLoader will be consulted ClassLoader current = ObjectFactory.class.getClassLoader(); chain = system; while (true) { if (current == chain) { // Assert: Current ClassLoader in chain of // boot/extension/system ClassLoaders return system; } if (chain == null) { break; } chain = ss.getParentClassLoader(chain); } // Assert: Current ClassLoader not in chain of // boot/extension/system ClassLoaders return current; } if (chain == null) { // boot ClassLoader reached break; } // Check for any extension ClassLoaders in chain up to // boot ClassLoader chain = ss.getParentClassLoader(chain); } ; // Assert: Context ClassLoader not in chain of // boot/extension/system ClassLoaders return context; } // findClassLoader():ClassLoader
private void debugDisplayClassLoader() { try { if( classLoader == ss.getContextClassLoader() ) { debugPrintln("using thread context class loader ("+classLoader+") for search"); return; } } catch( Throwable _ ) { ; // getContextClassLoader() undefined in JDK1.1 } if( classLoader==ClassLoader.getSystemClassLoader() ) { debugPrintln("using system class loader ("+classLoader+") for search"); return; } debugPrintln("using class loader ("+classLoader+") for search"); }
/** * Returns an {@link Iterator} that enumerates all * the META-INF/services files that we care. */ private Iterator createServiceFileIterator() { if (classLoader == null) { return new SingleIterator() { protected Object value() { ClassLoader classLoader = XPathFactoryFinder.class.getClassLoader(); return SecuritySupport.getResourceAsURL(classLoader, SERVICE_ID); //return (ClassLoader.getSystemResource( SERVICE_ID )); } }; } else { try { //final Enumeration e = classLoader.getResources(SERVICE_ID); final Enumeration e = SecuritySupport.getResources(classLoader, SERVICE_ID); if (debug && !e.hasMoreElements()) { debugPrintln("no "+SERVICE_ID+" file was found"); } // wrap it into an Iterator. return new Iterator() { public void remove() { throw new UnsupportedOperationException(); } public boolean hasNext() { return e.hasMoreElements(); } public Object next() { return e.nextElement(); } }; } catch (IOException e) { if (debug) { debugPrintln("failed to enumerate resources "+SERVICE_ID); e.printStackTrace(); } return new ArrayList().iterator(); // empty iterator } } }
/* 192: */ /* 193: */ static ClassLoader findClassLoader() /* 194: */ throws ObjectFactory.ConfigurationError /* 195: */ { /* 196:396 */ SecuritySupport ss = SecuritySupport.getInstance(); /* 197: */ /* 198: */ /* 199: */ /* 200:400 */ ClassLoader context = ss.getContextClassLoader(); /* 201:401 */ ClassLoader system = ss.getSystemClassLoader(); /* 202: */ /* 203:403 */ ClassLoader chain = system; /* 204: */ for (; ; ) /* 205: */ { /* 206:405 */ if (context == chain) /* 207: */ { /* 208:414 */ ClassLoader current = ObjectFactory.class.getClassLoader(); /* 209: */ /* 210:416 */ chain = system; /* 211: */ for (; ; ) /* 212: */ { /* 213:418 */ if (current == chain) { /* 214:421 */ return system; /* 215: */ } /* 216:423 */ if (chain == null) { /* 217: */ break; /* 218: */ } /* 219:426 */ chain = ss.getParentClassLoader(chain); /* 220: */ } /* 221:431 */ return current; /* 222: */ } /* 223:434 */ if (chain == null) { /* 224: */ break; /* 225: */ } /* 226:441 */ chain = ss.getParentClassLoader(chain); /* 227: */ } /* 228:446 */ return context; /* 229: */ }
/** * <p>Lookup a {@link XPathFactory} for the given object model.</p> * * @param uri identifies the object model. * * @return {@link XPathFactory} for the given object model. */ private XPathFactory _newFactory(String uri) { XPathFactory xpathFactory; String propertyName = SERVICE_CLASS.getName() + ":" + uri; // system property look up try { debugPrintln("Looking up system property '"+propertyName+"'" ); String r = ss.getSystemProperty(propertyName); if(r!=null) { debugPrintln("The value is '"+r+"'"); xpathFactory = createInstance(r, true); if(xpathFactory != null) return xpathFactory; } else debugPrintln("The property is undefined."); } catch( Throwable t ) { if( debug ) { debugPrintln("failed to look up system property '"+propertyName+"'" ); t.printStackTrace(); } } String javah = ss.getSystemProperty( "java.home" ); String configFile = javah + File.separator + "lib" + File.separator + "jaxp.properties"; String factoryClassName = null ; // try to read from $java.home/lib/jaxp.properties try { if(firstTime){ synchronized(cacheProps){ if(firstTime){ File f=new File( configFile ); firstTime = false; if(ss.doesFileExist(f)){ debugPrintln("Read properties file " + f); cacheProps.load(ss.getFileInputStream(f)); } } } } factoryClassName = cacheProps.getProperty(propertyName); debugPrintln("found " + factoryClassName + " in $java.home/jaxp.properties"); if (factoryClassName != null) { xpathFactory = createInstance(factoryClassName, true); if(xpathFactory != null){ return xpathFactory; } } } catch (Exception ex) { if (debug) { ex.printStackTrace(); } } // try META-INF/services files Iterator sitr = createServiceFileIterator(); while(sitr.hasNext()) { URL resource = (URL)sitr.next(); debugPrintln("looking into " + resource); try { xpathFactory = loadFromService(uri, resource.toExternalForm(), ss.getURLInputStream(resource)); if (xpathFactory != null) { return xpathFactory; } } catch(IOException e) { if( debug ) { debugPrintln("failed to read "+resource); e.printStackTrace(); } } } // platform default if(uri.equals(XPathFactory.DEFAULT_OBJECT_MODEL_URI)) { debugPrintln("attempting to use the platform default W3C DOM XPath lib"); return createInstance("com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl", true); } debugPrintln("all things were tried, but none was found. bailing out."); return null; }
// Used for debugging purposes private static String which(Class<?> clazz) { return ss.getClassSource(clazz); }
/* 79: */ /* 80: */ static String lookUpFactoryClassName( String factoryId, String propertiesFilename, String fallbackClassName) /* 81: */ { /* 82:261 */ SecuritySupport ss = SecuritySupport.getInstance(); /* 83: */ try /* 84: */ { /* 85:265 */ String systemProp = ss.getSystemProperty(factoryId); /* 86:266 */ if (systemProp != null) /* 87: */ { /* 88:267 */ debugPrintln("found system property, value=" + systemProp); /* 89:268 */ return systemProp; /* 90: */ } /* 91: */ } /* 92: */ catch (SecurityException se) { } /* 93:276 */ String factoryClassName = null; /* 94:279 */ if (propertiesFilename == null) /* 95: */ { /* 96:280 */ File propertiesFile = null; /* 97:281 */ boolean propertiesFileExists = false; /* 98: */ try /* 99: */ { /* 100:283 */ String javah = ss.getSystemProperty("java.home"); /* 101:284 */ propertiesFilename = javah + File.separator + "lib" + File.separator + "xalan.properties"; /* 102: */ /* 103:286 */ propertiesFile = new File(propertiesFilename); /* 104:287 */ propertiesFileExists = ss.getFileExists(propertiesFile); /* 105: */ } /* 106: */ catch (SecurityException javah) /* 107: */ { /* 108:290 */ fLastModified = -1L; /* 109:291 */ fXalanProperties = null; /* 110: */ } /* 111:294 */ synchronized (ObjectFactory.class) /* 112: */ { /* 113:295 */ boolean loadProperties = false; /* 114:296 */ FileInputStream fis = null; /* 115: */ try /* 116: */ { /* 117:299 */ if (fLastModified >= 0L) /* 118: */ { /* 119:300 */ if ((propertiesFileExists) && (fLastModified < (ObjectFactory.fLastModified = ss.getLastModified(propertiesFile)))) /* 120: */ { /* 121:302 */ loadProperties = true; /* 122: */ } /* 123:305 */ else if (!propertiesFileExists) /* 124: */ { /* 125:306 */ fLastModified = -1L; /* 126:307 */ fXalanProperties = null; /* 127: */ } /* 128: */ } /* 129:312 */ else if (propertiesFileExists) /* 130: */ { /* 131:313 */ loadProperties = true; /* 132:314 */ fLastModified = ss.getLastModified(propertiesFile); /* 133: */ } /* 134:317 */ if (loadProperties) /* 135: */ { /* 136:320 */ fXalanProperties = new Properties(); /* 137:321 */ fis = ss.getFileInputStream(propertiesFile); /* 138:322 */ fXalanProperties.load(fis); /* 139: */ } /* 140: */ } /* 141: */ catch (Exception x) /* 142: */ { /* 143:325 */ fXalanProperties = null; /* 144:326 */ fLastModified = -1L; /* 145: */ } /* 146: */ finally /* 147: */ { /* 148:333 */ if (fis != null) { /* 149: */ try /* 150: */ { /* 151:335 */ fis.close(); /* 152: */ } /* 153: */ catch (IOException exc) { } /* 154: */ } /* 155: */ } /* 156: */ } /* 157:342 */ if (fXalanProperties != null) { /* 158:343 */ factoryClassName = fXalanProperties.getProperty(factoryId); /* 159: */ } /* 160: */ } /* 161: */ else /* 162: */ { /* 163:346 */ FileInputStream fis = null; /* 164: */ try /* 165: */ { /* 166:348 */ fis = ss.getFileInputStream(new File(propertiesFilename)); /* 167:349 */ Properties props = new Properties(); /* 168:350 */ props.load(fis); /* 169:351 */ factoryClassName = props.getProperty(factoryId); /* 170: */ } /* 171: */ catch (Exception x) { } finally /* 172: */ { /* 173:359 */ if (fis != null) { /* 174: */ try /* 175: */ { /* 176:361 */ fis.close(); /* 177: */ } /* 178: */ catch (IOException exc) { } /* 179: */ } /* 180: */ } /* 181: */ } /* 182:368 */ if (factoryClassName != null) /* 183: */ { /* 184:369 */ debugPrintln("found in " + propertiesFilename + ", value=" + factoryClassName); /* 185: */ /* 186:371 */ return factoryClassName; /* 187: */ } /* 188:375 */ return findJarServiceProviderName(factoryId); /* 189: */ }
/* 307: */ /* 308: */ private static String findJarServiceProviderName(String factoryId) /* 309: */ { /* 310:537 */ SecuritySupport ss = SecuritySupport.getInstance(); /* 311:538 */ String serviceId = "META-INF/services/" + factoryId; /* 312:539 */ InputStream is = null; /* 313: */ /* 314: */ /* 315:542 */ ClassLoader cl = findClassLoader(); /* 316: */ /* 317:544 */ is = ss.getResourceAsStream(cl, serviceId); /* 318:547 */ if (is == null) /* 319: */ { /* 320:548 */ ClassLoader current = ObjectFactory.class.getClassLoader(); /* 321:549 */ if (cl != current) /* 322: */ { /* 323:550 */ cl = current; /* 324:551 */ is = ss.getResourceAsStream(cl, serviceId); /* 325: */ } /* 326: */ } /* 327:555 */ if (is == null) { /* 328:557 */ return null; /* 329: */ } /* 330:560 */ debugPrintln("found jar resource=" + serviceId + " using ClassLoader: " + cl); /* 331: */ BufferedReader rd; /* 332: */ try /* 333: */ { /* 334:581 */ rd = new BufferedReader(new InputStreamReader(is, "UTF-8")); /* 335: */ } /* 336: */ catch (UnsupportedEncodingException e) /* 337: */ { /* 338:583 */ rd = new BufferedReader(new InputStreamReader(is)); /* 339: */ } /* 340:586 */ String factoryClassName = null; /* 341: */ try /* 342: */ { /* 343:590 */ factoryClassName = rd.readLine(); /* 344: */ } /* 345: */ catch (IOException x) /* 346: */ { /* 347:593 */ return null; /* 348: */ } /* 349: */ finally /* 350: */ { /* 351: */ try /* 352: */ { /* 353:598 */ rd.close(); /* 354: */ } /* 355: */ catch (IOException exc) { } /* 356: */ } /* 357:604 */ if ((factoryClassName != null) && (!"".equals(factoryClassName))) /* 358: */ { /* 359:606 */ debugPrintln("found in resource, value=" + factoryClassName); /* 360: */ /* 361: */ /* 362: */ /* 363: */ /* 364: */ /* 365: */ /* 366:613 */ return factoryClassName; /* 367: */ } /* 368:617 */ return null; /* 369: */ }
/** * <p>Lookup a {@link XPathFactory} for the given object model.</p> * * @param uri identifies the object model. * * @return {@link XPathFactory} for the given object model. */ private XPathFactory _newFactory(String uri) { XPathFactory xpf; String propertyName = SERVICE_CLASS.getName() + ":" + uri; // system property look up try { if (debug) debugPrintln("Looking up system property '"+propertyName+"'" ); String r = SecuritySupport.getSystemProperty(propertyName); if (r != null && r.length() > 0) { if (debug) debugPrintln("The value is '"+r+"'"); xpf = createInstance(r); if(xpf!=null) return xpf; } else if (debug) { debugPrintln("The property is undefined."); } } // The VM ran out of memory or there was some other serious problem. Re-throw. catch (VirtualMachineError vme) { throw vme; } // ThreadDeath should always be re-thrown catch (ThreadDeath td) { throw td; } catch (Throwable t) { if( debug ) { debugPrintln("failed to look up system property '"+propertyName+"'" ); t.printStackTrace(); } } String javah = SecuritySupport.getSystemProperty( "java.home" ); String configFile = javah + File.separator + "lib" + File.separator + "jaxp.properties"; String factoryClassName = null ; // try to read from $java.home/lib/jaxp.properties try { if(firstTime){ synchronized(cacheProps){ if(firstTime){ File f=new File( configFile ); firstTime = false; if(SecuritySupport.doesFileExist(f)){ if (debug) debugPrintln("Read properties file " + f); cacheProps.load(SecuritySupport.getFileInputStream(f)); } } } } factoryClassName = cacheProps.getProperty(propertyName); if (debug) debugPrintln("found " + factoryClassName + " in $java.home/jaxp.properties"); if (factoryClassName != null) { xpf = createInstance(factoryClassName); if(xpf != null){ return xpf; } } } catch (Exception ex) { if (debug) { ex.printStackTrace(); } } // try META-INF/services files Iterator sitr = createServiceFileIterator(); while(sitr.hasNext()) { URL resource = (URL)sitr.next(); if (debug) debugPrintln("looking into " + resource); try { xpf = loadFromServicesFile(uri, resource.toExternalForm(), SecuritySupport.getURLInputStream(resource)); if(xpf!=null) return xpf; } catch(IOException e) { if( debug ) { debugPrintln("failed to read "+resource); e.printStackTrace(); } } } // platform default if(uri.equals(XPathFactory.DEFAULT_OBJECT_MODEL_URI)) { if (debug) debugPrintln("attempting to use the platform default W3C DOM XPath lib"); return createInstance("org.apache.xpath.jaxp.XPathFactoryImpl"); } if (debug) debugPrintln("all things were tried, but none was found. bailing out."); return null; }
/** * Finds the name of the required implementation class in the specified order. The specified order * is the following: * * <ol> * <li>query the system property using <code>System.getProperty</code> * <li>read <code>$java.home/lib/<i>propertiesFilename</i></code> file * <li>read <code>META-INF/services/<i>factoryId</i></code> file * <li>use fallback classname * </ol> * * @return name of class that provides factory service, never null * @param factoryId Name of the factory to find, same as a property name * @param propertiesFilename The filename in the $java.home/lib directory of the properties file. * If none specified, ${java.home}/lib/xalan.properties will be used. * @param fallbackClassName Implementation class name, if nothing else is found. Use null to mean * no fallback. * @exception ObjectFactory.ConfigurationError */ static String lookUpFactoryClassName( String factoryId, String propertiesFilename, String fallbackClassName) { SecuritySupport ss = SecuritySupport.getInstance(); // Use the system property first try { String systemProp = ss.getSystemProperty(factoryId); if (systemProp != null) { if (DEBUG) debugPrintln("found system property, value=" + systemProp); return systemProp; } } catch (SecurityException se) { // Ignore and continue w/ next location } // Try to read from propertiesFilename, or // $java.home/lib/xalan.properties String factoryClassName = null; // no properties file name specified; use // $JAVA_HOME/lib/xalan.properties: if (propertiesFilename == null) { File propertiesFile = null; boolean propertiesFileExists = false; try { String javah = ss.getSystemProperty("java.home"); propertiesFilename = javah + File.separator + "lib" + File.separator + DEFAULT_PROPERTIES_FILENAME; propertiesFile = new File(propertiesFilename); propertiesFileExists = ss.getFileExists(propertiesFile); } catch (SecurityException e) { // try again... fLastModified = -1; fXalanProperties = null; } synchronized (ObjectFactory.class) { boolean loadProperties = false; FileInputStream fis = null; try { // file existed last time if (fLastModified >= 0) { if (propertiesFileExists && (fLastModified < (fLastModified = ss.getLastModified(propertiesFile)))) { loadProperties = true; } else { // file has stopped existing... if (!propertiesFileExists) { fLastModified = -1; fXalanProperties = null; } // else, file wasn't modified! } } else { // file has started to exist: if (propertiesFileExists) { loadProperties = true; fLastModified = ss.getLastModified(propertiesFile); } // else, nothing's changed } if (loadProperties) { // must never have attempted to read xalan.properties // before (or it's outdeated) fXalanProperties = new Properties(); fis = ss.getFileInputStream(propertiesFile); fXalanProperties.load(fis); } } catch (Exception x) { fXalanProperties = null; fLastModified = -1; // assert(x instanceof FileNotFoundException // || x instanceof SecurityException) // In both cases, ignore and continue w/ next location } finally { // try to close the input stream if one was opened. if (fis != null) { try { fis.close(); } // Ignore the exception. catch (IOException exc) { } } } } if (fXalanProperties != null) { factoryClassName = fXalanProperties.getProperty(factoryId); } } else { FileInputStream fis = null; try { fis = ss.getFileInputStream(new File(propertiesFilename)); Properties props = new Properties(); props.load(fis); factoryClassName = props.getProperty(factoryId); } catch (Exception x) { // assert(x instanceof FileNotFoundException // || x instanceof SecurityException) // In both cases, ignore and continue w/ next location } finally { // try to close the input stream if one was opened. if (fis != null) { try { fis.close(); } // Ignore the exception. catch (IOException exc) { } } } } if (factoryClassName != null) { if (DEBUG) debugPrintln("found in " + propertiesFilename + ", value=" + factoryClassName); return factoryClassName; } // Try Jar Service Provider Mechanism return findJarServiceProviderName(factoryId); } // lookUpFactoryClass(String,String):String
/** * Finds the implementation Class object in the specified order. The specified order is the * following: * * <ol> * <li>query the system property using <code>System.getProperty</code> * <li>read <code>$java.home/lib/<i>propertiesFilename</i></code> file * <li>read <code>META-INF/services/<i>factoryId</i></code> file * <li>use fallback classname * </ol> * * @return Class object of factory, never null * @param factoryId Name of the factory to find, same as a property name * @param propertiesFilename The filename in the $java.home/lib directory of the properties file. * If none specified, ${java.home}/lib/xerces.properties will be used. * @param fallbackClassName Implementation class name, if nothing else is found. Use null to mean * no fallback. * @exception ObjectFactory.ConfigurationError */ static Object createObject(String factoryId, String propertiesFilename, String fallbackClassName) throws ConfigurationError { if (DEBUG) debugPrintln("debug is on"); ClassLoader cl = findClassLoader(); // Use the system property first try { String systemProp = SecuritySupport.getInstance().getSystemProperty(factoryId); if (systemProp != null && systemProp.length() > 0) { if (DEBUG) debugPrintln("found system property, value=" + systemProp); return newInstance(systemProp, cl, true); } } catch (SecurityException se) { // Ignore and continue w/ next location } // Try to read from propertiesFilename, or $java.home/lib/xerces.properties String factoryClassName = null; // no properties file name specified; use $JAVA_HOME/lib/xerces.properties: if (propertiesFilename == null) { File propertiesFile = null; boolean propertiesFileExists = false; try { String javah = SecuritySupport.getInstance().getSystemProperty("java.home"); propertiesFilename = javah + File.separator + "lib" + File.separator + DEFAULT_PROPERTIES_FILENAME; propertiesFile = new File(propertiesFilename); propertiesFileExists = SecuritySupport.getInstance().getFileExists(propertiesFile); } catch (SecurityException e) { // try again... fLastModified = -1; fXercesProperties = null; } synchronized (ObjectFactory.class) { boolean loadProperties = false; FileInputStream fis = null; try { // file existed last time if (fLastModified >= 0) { if (propertiesFileExists && (fLastModified < (fLastModified = SecuritySupport.getInstance().getLastModified(propertiesFile)))) { loadProperties = true; } else { // file has stopped existing... if (!propertiesFileExists) { fLastModified = -1; fXercesProperties = null; } // else, file wasn't modified! } } else { // file has started to exist: if (propertiesFileExists) { loadProperties = true; fLastModified = SecuritySupport.getInstance().getLastModified(propertiesFile); } // else, nothing's changed } if (loadProperties) { // must never have attempted to read xerces.properties before (or it's outdeated) fXercesProperties = new Properties(); fis = SecuritySupport.getInstance().getFileInputStream(propertiesFile); fXercesProperties.load(fis); } } catch (Exception x) { fXercesProperties = null; fLastModified = -1; // assert(x instanceof FileNotFoundException // || x instanceof SecurityException) // In both cases, ignore and continue w/ next location } finally { // try to close the input stream if one was opened. if (fis != null) { try { fis.close(); } // Ignore the exception. catch (IOException exc) { } } } } if (fXercesProperties != null) { factoryClassName = fXercesProperties.getProperty(factoryId); } } else { FileInputStream fis = null; try { fis = SecuritySupport.getInstance().getFileInputStream(new File(propertiesFilename)); Properties props = new Properties(); props.load(fis); factoryClassName = props.getProperty(factoryId); } catch (Exception x) { // assert(x instanceof FileNotFoundException // || x instanceof SecurityException) // In both cases, ignore and continue w/ next location } finally { // try to close the input stream if one was opened. if (fis != null) { try { fis.close(); } // Ignore the exception. catch (IOException exc) { } } } } if (factoryClassName != null) { if (DEBUG) debugPrintln("found in " + propertiesFilename + ", value=" + factoryClassName); return newInstance(factoryClassName, cl, true); } // Try Jar Service Provider Mechanism Object provider = findJarServiceProvider(factoryId); if (provider != null) { return provider; } if (fallbackClassName == null) { throw new ConfigurationError("Provider for " + factoryId + " cannot be found", null); } if (DEBUG) debugPrintln("using fallback, value=" + fallbackClassName); return newInstance(fallbackClassName, cl, true); } // createObject(String,String,String):Object
/** * Find the name of service provider using Jar Service Provider Mechanism * * @return instance of provider class if found or null */ private static String findJarServiceProviderName(String factoryId) { SecuritySupport ss = SecuritySupport.getInstance(); String serviceId = SERVICES_PATH + factoryId; InputStream is = null; // First try the Context ClassLoader ClassLoader cl = findClassLoader(); is = ss.getResourceAsStream(cl, serviceId); // If no provider found then try the current ClassLoader if (is == null) { ClassLoader current = ObjectFactory.class.getClassLoader(); if (cl != current) { cl = current; is = ss.getResourceAsStream(cl, serviceId); } } if (is == null) { // No provider found return null; } if (DEBUG) debugPrintln("found jar resource=" + serviceId + " using ClassLoader: " + cl); // Read the service provider name in UTF-8 as specified in // the jar spec. Unfortunately this fails in Microsoft // VJ++, which does not implement the UTF-8 // encoding. Theoretically, we should simply let it fail in // that case, since the JVM is obviously broken if it // doesn't support such a basic standard. But since there // are still some users attempting to use VJ++ for // development, we have dropped in a fallback which makes a // second attempt using the platform's default encoding. In // VJ++ this is apparently ASCII, which is a subset of // UTF-8... and since the strings we'll be reading here are // also primarily limited to the 7-bit ASCII range (at // least, in English versions), this should work well // enough to keep us on the air until we're ready to // officially decommit from VJ++. [Edited comment from // jkesselm] BufferedReader rd; try { rd = new BufferedReader(new InputStreamReader(is, "UTF-8")); } catch (java.io.UnsupportedEncodingException e) { rd = new BufferedReader(new InputStreamReader(is)); } String factoryClassName = null; try { // XXX Does not handle all possible input as specified by the // Jar Service Provider specification factoryClassName = rd.readLine(); } catch (IOException x) { // No provider found return null; } finally { try { // try to close the reader. rd.close(); } // Ignore the exception. catch (IOException exc) { } } if (factoryClassName != null && !"".equals(factoryClassName)) { if (DEBUG) debugPrintln("found in resource, value=" + factoryClassName); // Note: here we do not want to fall back to the current // ClassLoader because we want to avoid the case where the // resource file was found using one ClassLoader and the // provider class was instantiated using a different one. return factoryClassName; } // No provider found return null; }
/** * Lookup a {@link XPathFactory} for the given object model. * * @param uri identifies the object model. * @return {@link XPathFactory} for the given object model. */ private XPathFactory _newFactory(String uri) throws XPathFactoryConfigurationException { XPathFactory xpathFactory = null; String propertyName = SERVICE_CLASS.getName() + ":" + uri; // system property look up try { debugPrintln("Looking up system property '" + propertyName + "'"); String r = ss.getSystemProperty(propertyName); if (r != null) { debugPrintln("The value is '" + r + "'"); xpathFactory = createInstance(r, true); if (xpathFactory != null) { return xpathFactory; } } else debugPrintln("The property is undefined."); } catch (Throwable t) { if (debug) { debugPrintln("failed to look up system property '" + propertyName + "'"); t.printStackTrace(); } } String javah = ss.getSystemProperty("java.home"); String configFile = javah + File.separator + "lib" + File.separator + "jaxp.properties"; // try to read from $java.home/lib/jaxp.properties try { if (firstTime) { synchronized (cacheProps) { if (firstTime) { File f = new File(configFile); firstTime = false; if (ss.doesFileExist(f)) { debugPrintln("Read properties file " + f); cacheProps.load(ss.getFileInputStream(f)); } } } } final String factoryClassName = cacheProps.getProperty(propertyName); debugPrintln("found " + factoryClassName + " in $java.home/jaxp.properties"); if (factoryClassName != null) { xpathFactory = createInstance(factoryClassName, true); if (xpathFactory != null) { return xpathFactory; } } } catch (Exception ex) { if (debug) { ex.printStackTrace(); } } // Try with ServiceLoader assert xpathFactory == null; xpathFactory = findServiceProvider(uri); // The following assertion should always be true. // Uncomment it, recompile, and run with -ea in case of doubts: // assert xpathFactory == null || xpathFactory.isObjectModelSupported(uri); if (xpathFactory != null) { return xpathFactory; } // platform default if (uri.equals(XPathFactory.DEFAULT_OBJECT_MODEL_URI)) { debugPrintln("attempting to use the platform default W3C DOM XPath lib"); return createInstance("com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl", true); } debugPrintln("all things were tried, but none was found. bailing out."); return null; }