⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logfactory.java

📁 logging日志的相关包 logging日志的相关包
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
     * <li>using JDK1.2+ and the calling code is loaded via the boot     *  classloader (only likely for embedded systems work).</li>     * </ul>     * Note that <code>factories</code> is a <i>Hashtable</i> (not a HashMap),     * and hashtables don't allow null as a key.     */    protected static LogFactory nullClassLoaderFactory = null;    /**     * Create the hashtable which will be used to store a map of     * (context-classloader -> logfactory-object). Version 1.2+ of Java     * supports "weak references", allowing a custom Hashtable class     * to be used which uses only weak references to its keys. Using weak     * references can fix memory leaks on webapp unload in some cases (though     * not all). Version 1.1 of Java does not support weak references, so we     * must dynamically determine which we are using. And just for fun, this     * code also supports the ability for a system property to specify an     * arbitrary Hashtable implementation name.     * <p>     * Note that the correct way to ensure no memory leaks occur is to ensure     * that LogFactory.release(contextClassLoader) is called whenever a      * webapp is undeployed.     */    private static final Hashtable createFactoryStore() {        Hashtable result = null;        String storeImplementationClass             = System.getProperty(HASHTABLE_IMPLEMENTATION_PROPERTY);        if (storeImplementationClass == null) {            storeImplementationClass = WEAK_HASHTABLE_CLASSNAME;        }        try {            Class implementationClass = Class.forName(storeImplementationClass);            result = (Hashtable) implementationClass.newInstance();                    } catch (Throwable t) {            // ignore            if (!WEAK_HASHTABLE_CLASSNAME.equals(storeImplementationClass)) {                // if the user's trying to set up a custom implementation, give a clue                if (isDiagnosticsEnabled()) {                    // use internal logging to issue the warning                    logDiagnostic("[ERROR] LogFactory: Load of custom hashtable failed");                } else {                    // we *really* want this output, even if diagnostics weren't                    // explicitly enabled by the user.                    System.err.println("[ERROR] LogFactory: Load of custom hashtable failed");                }            }        }        if (result == null) {            result = new Hashtable();        }        return result;    }    // --------------------------------------------------------- Static Methods    /**     * <p>Construct (if necessary) and return a <code>LogFactory</code>     * instance, using the following ordered lookup procedure to determine     * the name of the implementation class to be loaded.</p>     * <ul>     * <li>The <code>org.apache.commons.logging.LogFactory</code> system     *     property.</li>     * <li>The JDK 1.3 Service Discovery mechanism</li>     * <li>Use the properties file <code>commons-logging.properties</code>     *     file, if found in the class path of this class.  The configuration     *     file is in standard <code>java.util.Properties</code> format and     *     contains the fully qualified name of the implementation class     *     with the key being the system property defined above.</li>     * <li>Fall back to a default implementation class     *     (<code>org.apache.commons.logging.impl.LogFactoryImpl</code>).</li>     * </ul>     *     * <p><em>NOTE</em> - If the properties file method of identifying the     * <code>LogFactory</code> implementation class is utilized, all of the     * properties defined in this file will be set as configuration attributes     * on the corresponding <code>LogFactory</code> instance.</p>     *      * <p><em>NOTE</em> - In a multithreaded environment it is possible      * that two different instances will be returned for the same      * classloader environment.      * </p>     *     * @exception LogConfigurationException if the implementation class is not     *  available or cannot be instantiated.     */    public static LogFactory getFactory() throws LogConfigurationException {        // Identify the class loader we will be using        ClassLoader contextClassLoader = getContextClassLoader();        if (contextClassLoader == null) {            // This is an odd enough situation to report about. This            // output will be a nuisance on JDK1.1, as the system            // classloader is null in that environment.            if (isDiagnosticsEnabled()) {                logDiagnostic("Context classloader is null.");            }        }        // Return any previously registered factory for this class loader        LogFactory factory = getCachedFactory(contextClassLoader);        if (factory != null) {            return factory;        }        if (isDiagnosticsEnabled()) {            logDiagnostic(                    "[LOOKUP] LogFactory implementation requested for the first time for context classloader "                    + objectId(contextClassLoader));            logHierarchy("[LOOKUP] ", contextClassLoader);        }        // Load properties file.        //        // If the properties file exists, then its contents are used as        // "attributes" on the LogFactory implementation class. One particular        // property may also control which LogFactory concrete subclass is        // used, but only if other discovery mechanisms fail..        //        // As the properties file (if it exists) will be used one way or         // another in the end we may as well look for it first.        Properties props = getConfigurationFile(contextClassLoader, FACTORY_PROPERTIES);        // Determine whether we will be using the thread context class loader to        // load logging classes or not by checking the loaded properties file (if any).        ClassLoader baseClassLoader = contextClassLoader;        if (props != null) {            String useTCCLStr = props.getProperty(TCCL_KEY);            if (useTCCLStr != null) {                // The Boolean.valueOf(useTCCLStr).booleanValue() formulation                // is required for Java 1.2 compatability.                if (Boolean.valueOf(useTCCLStr).booleanValue() == false) {                    // Don't use current context classloader when locating any                    // LogFactory or Log classes, just use the class that loaded                    // this abstract class. When this class is deployed in a shared                    // classpath of a container, it means webapps cannot deploy their                    // own logging implementations. It also means that it is up to the                    // implementation whether to load library-specific config files                    // from the TCCL or not.                    baseClassLoader = thisClassLoader;                 }            }        }        // Determine which concrete LogFactory subclass to use.        // First, try a global system property        if (isDiagnosticsEnabled()) {            logDiagnostic(                    "[LOOKUP] Looking for system property [" + FACTORY_PROPERTY                     + "] to define the LogFactory subclass to use...");        }                try {            String factoryClass = System.getProperty(FACTORY_PROPERTY);            if (factoryClass != null) {                if (isDiagnosticsEnabled()) {                    logDiagnostic(                            "[LOOKUP] Creating an instance of LogFactory class '" + factoryClass                            + "' as specified by system property " + FACTORY_PROPERTY);                }                                factory = newFactory(factoryClass, baseClassLoader, contextClassLoader);            } else {                if (isDiagnosticsEnabled()) {                    logDiagnostic(                            "[LOOKUP] No system property [" + FACTORY_PROPERTY                             + "] defined.");                }            }        } catch (SecurityException e) {            if (isDiagnosticsEnabled()) {                logDiagnostic(                        "[LOOKUP] A security exception occurred while trying to create an"                        + " instance of the custom factory class"                        + ": [" + e.getMessage().trim()                        + "]. Trying alternative implementations...");            }            ;  // ignore        } catch(RuntimeException e) {            // This is not consistent with the behaviour when a bad LogFactory class is            // specified in a services file.            //            // One possible exception that can occur here is a ClassCastException when            // the specified class wasn't castable to this LogFactory type.            if (isDiagnosticsEnabled()) {                logDiagnostic(                        "[LOOKUP] An exception occurred while trying to create an"                        + " instance of the custom factory class"                        + ": [" + e.getMessage().trim()                        + "] as specified by a system property.");            }            throw e;        }        // Second, try to find a service by using the JDK1.3 class        // discovery mechanism, which involves putting a file with the name        // of an interface class in the META-INF/services directory, where the        // contents of the file is a single line specifying a concrete class         // that implements the desired interface.        if (factory == null) {            if (isDiagnosticsEnabled()) {                logDiagnostic(                        "[LOOKUP] Looking for a resource file of name [" + SERVICE_ID                        + "] to define the LogFactory subclass to use...");            }            try {                InputStream is = getResourceAsStream(contextClassLoader,                                                     SERVICE_ID);                if( is != null ) {                    // This code is needed by EBCDIC and other strange systems.                    // It's a fix for bugs reported in xerces                    BufferedReader rd;                    try {                        rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));                    } catch (java.io.UnsupportedEncodingException e) {                        rd = new BufferedReader(new InputStreamReader(is));                    }                    String factoryClassName = rd.readLine();                    rd.close();                    if (factoryClassName != null &&                        ! "".equals(factoryClassName)) {                        if (isDiagnosticsEnabled()) {                            logDiagnostic(                                    "[LOOKUP]  Creating an instance of LogFactory class " + factoryClassName                                    + " as specified by file '" + SERVICE_ID                                     + "' which was present in the path of the context"                                    + " classloader.");                        }                        factory = newFactory(factoryClassName, baseClassLoader, contextClassLoader );                    }                } else {                    // is == null                    if (isDiagnosticsEnabled()) {                        logDiagnostic(                            "[LOOKUP] No resource file with name '" + SERVICE_ID                            + "' found.");                    }                }            } catch( Exception ex ) {                // note: if the specified LogFactory class wasn't compatible with LogFactory                // for some reason, a ClassCastException will be caught here, and attempts will                // continue to find a compatible class.                if (isDiagnosticsEnabled()) {                    logDiagnostic(                        "[LOOKUP] A security exception occurred while trying to create an"                        + " instance of the custom factory class"                        + ": [" + ex.getMessage().trim()                        + "]. Trying alternative implementations...");                }                ; // ignore            }        }        // Third try looking into the properties file read earlier (if found)        if (factory == null) {            if (props != null) {                if (isDiagnosticsEnabled()) {                    logDiagnostic(                        "[LOOKUP] Looking in properties file for entry with key '"                         + FACTORY_PROPERTY                        + "' to define the LogFactory subclass to use...");                }                String factoryClass = props.getProperty(FACTORY_PROPERTY);                if (factoryClass != null) {                    if (isDiagnosticsEnabled()) {                        logDiagnostic(                            "[LOOKUP] Properties file specifies LogFactory subclass '"                             + factoryClass + "'");                    }                    factory = newFactory(factoryClass, baseClassLoader, contextClassLoader);                                        // TODO: think about whether we need to handle exceptions from newFactory                } else {                    if (isDiagnosticsEnabled()) {                        logDiagnostic(                            "[LOOKUP] Properties file has no entry specifying LogFactory subclass.");                    }                }            } else {                if (isDiagnosticsEnabled()) {                    logDiagnostic(                        "[LOOKUP] No properties file available to determine"                        + " LogFactory subclass from..");                }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -