Search in sources :

Example 66 with Enumeration

use of java.util.Enumeration in project XobotOS by xamarin.

the class LogFactory method getConfigurationFile.

/**
     * Locate a user-provided configuration file.
     * <p>
     * The classpath of the specified classLoader (usually the context classloader)
     * is searched for properties files of the specified name. If none is found,
     * null is returned. If more than one is found, then the file with the greatest
     * value for its PRIORITY property is returned. If multiple files have the
     * same PRIORITY value then the first in the classpath is returned.
     * <p> 
     * This differs from the 1.0.x releases; those always use the first one found.
     * However as the priority is a new field, this change is backwards compatible.
     * <p>
     * The purpose of the priority field is to allow a webserver administrator to
     * override logging settings in all webapps by placing a commons-logging.properties
     * file in a shared classpath location with a priority > 0; this overrides any
     * commons-logging.properties files without priorities which are in the
     * webapps. Webapps can also use explicit priorities to override a configuration
     * file in the shared classpath if needed. 
     */
private static final Properties getConfigurationFile(ClassLoader classLoader, String fileName) {
    Properties props = null;
    double priority = 0.0;
    URL propsUrl = null;
    try {
        Enumeration urls = getResources(classLoader, fileName);
        if (urls == null) {
            return null;
        }
        while (urls.hasMoreElements()) {
            URL url = (URL) urls.nextElement();
            Properties newProps = getProperties(url);
            if (newProps != null) {
                if (props == null) {
                    propsUrl = url;
                    props = newProps;
                    String priorityStr = props.getProperty(PRIORITY_KEY);
                    priority = 0.0;
                    if (priorityStr != null) {
                        priority = Double.parseDouble(priorityStr);
                    }
                    if (isDiagnosticsEnabled()) {
                        logDiagnostic("[LOOKUP] Properties file found at '" + url + "'" + " with priority " + priority);
                    }
                } else {
                    String newPriorityStr = newProps.getProperty(PRIORITY_KEY);
                    double newPriority = 0.0;
                    if (newPriorityStr != null) {
                        newPriority = Double.parseDouble(newPriorityStr);
                    }
                    if (newPriority > priority) {
                        if (isDiagnosticsEnabled()) {
                            logDiagnostic("[LOOKUP] Properties file at '" + url + "'" + " with priority " + newPriority + " overrides file at '" + propsUrl + "'" + " with priority " + priority);
                        }
                        propsUrl = url;
                        props = newProps;
                        priority = newPriority;
                    } else {
                        if (isDiagnosticsEnabled()) {
                            logDiagnostic("[LOOKUP] Properties file at '" + url + "'" + " with priority " + newPriority + " does not override file at '" + propsUrl + "'" + " with priority " + priority);
                        }
                    }
                }
            }
        }
    } catch (SecurityException e) {
        if (isDiagnosticsEnabled()) {
            logDiagnostic("SecurityException thrown while trying to find/read config files.");
        }
    }
    if (isDiagnosticsEnabled()) {
        if (props == null) {
            logDiagnostic("[LOOKUP] No properties file of name '" + fileName + "' found.");
        } else {
            logDiagnostic("[LOOKUP] Properties file of name '" + fileName + "' found at '" + propsUrl + '"');
        }
    }
    return props;
}
Also used : Enumeration(java.util.Enumeration) Properties(java.util.Properties) URL(java.net.URL)

Example 67 with Enumeration

use of java.util.Enumeration in project XobotOS by xamarin.

the class LogFactory method getFactory.

// --------------------------------------------------------- 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) {
        // 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) {
            // 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;
            }
        }
    }
    // 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) {
        // 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;
    }
    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) {
            // 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
            ;
        }
    }
    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..");
            }
        }
    }
    if (factory == null) {
        if (isDiagnosticsEnabled()) {
            logDiagnostic("[LOOKUP] Loading the default LogFactory implementation '" + FACTORY_DEFAULT + "' via the same classloader that loaded this LogFactory" + " class (ie not looking in the context classloader).");
        }
        // Note: unlike the above code which can try to load custom LogFactory
        // implementations via the TCCL, we don't try to load the default LogFactory
        // implementation via the context classloader because:
        // * that can cause problems (see comments in newFactory method)
        // * no-one should be customising the code of the default class
        // Yes, we do give up the ability for the child to ship a newer
        // version of the LogFactoryImpl class and have it used dynamically
        // by an old LogFactory class in the parent, but that isn't 
        // necessarily a good idea anyway.
        factory = newFactory(FACTORY_DEFAULT, thisClassLoader, contextClassLoader);
    }
    if (factory != null) {
        /**
             * Always cache using context class loader.
             */
        cacheFactory(contextClassLoader, factory);
        if (props != null) {
            Enumeration names = props.propertyNames();
            while (names.hasMoreElements()) {
                String name = (String) names.nextElement();
                String value = props.getProperty(name);
                factory.setAttribute(name, value);
            }
        }
    }
    return factory;
}
Also used : Enumeration(java.util.Enumeration) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Properties(java.util.Properties) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) BufferedReader(java.io.BufferedReader)

Example 68 with Enumeration

use of java.util.Enumeration in project XobotOS by xamarin.

the class LogFactoryImpl method getAttributeNames.

/**
     * Return an array containing the names of all currently defined
     * configuration attributes.  If there are no such attributes, a zero
     * length array is returned.
     */
public String[] getAttributeNames() {
    Vector names = new Vector();
    Enumeration keys = attributes.keys();
    while (keys.hasMoreElements()) {
        names.addElement((String) keys.nextElement());
    }
    String[] results = new String[names.size()];
    for (int i = 0; i < results.length; i++) {
        results[i] = (String) names.elementAt(i);
    }
    return (results);
}
Also used : Enumeration(java.util.Enumeration) Vector(java.util.Vector)

Example 69 with Enumeration

use of java.util.Enumeration in project XobotOS by xamarin.

the class AbstractSessionContext method getIds.

public final Enumeration getIds() {
    final Iterator<SSLSession> i = sessionIterator();
    return new Enumeration<byte[]>() {

        private SSLSession next;

        public boolean hasMoreElements() {
            if (next != null) {
                return true;
            }
            while (i.hasNext()) {
                SSLSession session = i.next();
                if (session.isValid()) {
                    next = session;
                    return true;
                }
            }
            next = null;
            return false;
        }

        public byte[] nextElement() {
            if (hasMoreElements()) {
                byte[] id = next.getId();
                next = null;
                return id;
            }
            throw new NoSuchElementException();
        }
    };
}
Also used : Enumeration(java.util.Enumeration) SSLSession(javax.net.ssl.SSLSession) NoSuchElementException(java.util.NoSuchElementException)

Example 70 with Enumeration

use of java.util.Enumeration in project XobotOS by xamarin.

the class TestSuite method countTestCases.

/**
     * Counts the number of test cases that will be run by this test.
     */
public int countTestCases() {
    int count = 0;
    for (Enumeration e = tests(); e.hasMoreElements(); ) {
        Test test = (Test) e.nextElement();
        count = count + test.countTestCases();
    }
    return count;
}
Also used : Enumeration(java.util.Enumeration)

Aggregations

Enumeration (java.util.Enumeration)1418 IOException (java.io.IOException)247 ArrayList (java.util.ArrayList)183 File (java.io.File)122 Vector (java.util.Vector)98 Properties (java.util.Properties)97 HashMap (java.util.HashMap)96 List (java.util.List)89 URL (java.net.URL)74 HashSet (java.util.HashSet)71 Map (java.util.Map)71 Hashtable (java.util.Hashtable)70 Iterator (java.util.Iterator)70 Set (java.util.Set)67 InputStream (java.io.InputStream)62 ZipEntry (java.util.zip.ZipEntry)62 ServletContext (javax.servlet.ServletContext)55 ZipFile (java.util.zip.ZipFile)51 FileInputStream (java.io.FileInputStream)48 ServletException (javax.servlet.ServletException)46