📄 resourcemanager.java
字号:
while (parser.hasMoreTokens()) { try { // System.out.println("loading"); String className = parser.nextToken(); Class c = helper.loadClass(className, loader); factories.add(new NamedWeakReference(c, className)); } catch (Exception e) { // ignore ClassNotFoundException, IllegalArgumentException } } // System.out.println("adding to cache: " + factories); perLoaderCache.put(facProp, factories); return new FactoryEnumeration(factories, loader); } } } /** * Retrieves a factory from a list of packages specified in a * property. * * The property is gotten from the environment and the provider * resource file associated with the given context and concatenated. * classSuffix is added to the end of this list. * See getProperty(). The resulting property value is a list of package * prefixes. *<p> * This method then constructs a list of class names by concatenating * each package prefix with classSuffix and attempts to load and * instantiate the class until one succeeds. * Any class that cannot be loaded is ignored. * The resulting object is then cached in a two-level hash table, * keyed first by the context class loader and then by the property's * value and classSuffix. * The next time threads of the same context class loader call this * method, they use the cached factory. * If no factory can be loaded, NO_FACTORY is recorded in the table * so that next time it'll return quickly. * * @param propName The non-null property name * @param env The possibly null environment properties * @param ctx The possibly null context * @param classSuffix The non-null class name * (e.g. ".ldap.ldapURLContextFactory). * @param defaultPkgPrefix The non-null default package prefix. * (e.g., "com.sun.jndi.url"). * @return An factory object; null if none. * @exception NamingException If encounter problem while reading the provider * property file, or problem instantiating the factory. * * @see javax.naming.spi.NamingManager#getURLContext * @see javax.naming.spi.NamingManager#getURLObject */ public static Object getFactory(String propName, Hashtable env, Context ctx, String classSuffix, String defaultPkgPrefix) throws NamingException { // Merge property with provider property and supplied default String facProp = getProperty(propName, env, ctx, true); if (facProp != null) facProp += (":" + defaultPkgPrefix); else facProp = defaultPkgPrefix; // Cache factory based on context class loader, class name, and // property val ClassLoader loader = helper.getContextClassLoader(); String key = classSuffix + " " + facProp; Map perLoaderCache = null; synchronized (urlFactoryCache) { perLoaderCache = (Map) urlFactoryCache.get(loader); if (perLoaderCache == null) { perLoaderCache = new HashMap(11); urlFactoryCache.put(loader, perLoaderCache); } } synchronized (perLoaderCache) { Object factory = null; WeakReference factoryRef = (WeakReference) perLoaderCache.get(key); if (factoryRef == NO_FACTORY) { return null; } else if (factoryRef != null) { factory = factoryRef.get(); if (factory != null) { // check if weak ref has been cleared return factory; } } // Not cached; find first factory and cache StringTokenizer parser = new StringTokenizer(facProp, ":"); String className; while (factory == null && parser.hasMoreTokens()) { className = parser.nextToken() + classSuffix; try { // System.out.println("loading " + className); factory = helper.loadClass(className, loader).newInstance(); } catch (InstantiationException e) { NamingException ne = new NamingException("Cannot instantiate " + className); ne.setRootCause(e); throw ne; } catch (IllegalAccessException e) { NamingException ne = new NamingException("Cannot access " + className); ne.setRootCause(e); throw ne; } catch (Exception e) { // ignore ClassNotFoundException, IllegalArgumentException, // etc. } } // Cache it. perLoaderCache.put(key, (factory != null) ? new WeakReference(factory) : NO_FACTORY); return factory; } } // ---------- Private methods ---------- /* * Returns the properties contained in the provider resource file * of an object's package. Returns an empty hash table if the * object is null or the resource file cannot be found. The * results are cached. * * @throws NamingException if an error occurs while reading the file. */ private static Hashtable getProviderResource(Object obj) throws NamingException { if (obj == null) { return (new Hashtable(1)); } synchronized (propertiesCache) { Class c = obj.getClass(); Hashtable props = (Hashtable)propertiesCache.get(c); if (props != null) { return props; } props = new Properties(); InputStream istream = helper.getResourceAsStream(c, PROVIDER_RESOURCE_FILE_NAME); if (istream != null) { try { ((Properties)props).load(istream); } catch (IOException e) { NamingException ne = new ConfigurationException( "Error reading provider resource file for " + c); ne.setRootCause(e); throw ne; } } propertiesCache.put(c, props); return props; } } /* * Returns the Hashtable (never null) that results from merging * all application resource files available to this thread's * context class loader. The properties file in <java.home>/lib * is also merged in. The results are cached. * * SECURITY NOTES: * 1. JNDI needs permission to read the application resource files. * 2. Any class will be able to use JNDI to view the contents of * the application resource files in its own classpath. Give * careful consideration to this before storing sensitive * information there. * * @throws NamingException if an error occurs while reading a resource * file. */ private static Hashtable getApplicationResources() throws NamingException { ClassLoader cl = helper.getContextClassLoader(); synchronized (propertiesCache) { Hashtable result = (Hashtable)propertiesCache.get(cl); if (result != null) { return result; } try { NamingEnumeration resources = helper.getResources(cl, APP_RESOURCE_FILE_NAME); while (resources.hasMore()) { Properties props = new Properties(); props.load((InputStream)resources.next()); if (result == null) { result = props; } else { mergeTables(result, props); } } // Merge in properties from file in <java.home>/lib. InputStream istream = helper.getJavaHomeLibStream(JRELIB_PROPERTY_FILE_NAME); if (istream != null) { Properties props = new Properties(); props.load(istream); if (result == null) { result = props; } else { mergeTables(result, props); } } } catch (IOException e) { NamingException ne = new ConfigurationException( "Error reading application resource file"); ne.setRootCause(e); throw ne; } if (result == null) { result = new Hashtable(11); } propertiesCache.put(cl, result); return result; } } /* * Merge the properties from one hash table into another. Each * property in props2 that is not in props1 is added to props1. * For each property in both hash tables that is one of the * standard JNDI properties that specify colon-separated lists, * the values are concatenated and stored in props1. */ private static void mergeTables(Hashtable props1, Hashtable props2) { Enumeration keys = props2.keys(); while (keys.hasMoreElements()) { String prop = (String)keys.nextElement(); Object val1 = props1.get(prop); if (val1 == null) { props1.put(prop, props2.get(prop)); } else if (isListProperty(prop)) { String val2 = (String)props2.get(prop); props1.put(prop, ((String)val1) + ":" + val2); } } } /* * Is a property one of the standard JNDI properties that specify * colon-separated lists? */ private static boolean isListProperty(String prop) { prop = prop.intern(); for (int i = 0; i < listProperties.length; i++) { if (prop == listProperties[i]) { return true; } } return false; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -