Home | History | Annotate | Download | only in logging
      1 /*
      2  * Copyright 2001-2004 The Apache Software Foundation.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package org.apache.commons.logging;
     18 
     19 
     20 import java.lang.reflect.Constructor;
     21 import java.util.Hashtable;
     22 
     23 import org.apache.commons.logging.impl.NoOpLog;
     24 
     25 
     26 /**
     27  * <p>Factory for creating {@link Log} instances.  Applications should call
     28  * the <code>makeNewLogInstance()</code> method to instantiate new instances
     29  * of the configured {@link Log} implementation class.</p>
     30  *
     31  * <p>By default, calling <code>getInstance()</code> will use the following
     32  * algorithm:</p>
     33  * <ul>
     34  * <li>If Log4J is available, return an instance of
     35  *     <code>org.apache.commons.logging.impl.Log4JLogger</code>.</li>
     36  * <li>If JDK 1.4 or later is available, return an instance of
     37  *     <code>org.apache.commons.logging.impl.Jdk14Logger</code>.</li>
     38  * <li>Otherwise, return an instance of
     39  *     <code>org.apache.commons.logging.impl.NoOpLog</code>.</li>
     40  * </ul>
     41  *
     42  * <p>You can change the default behavior in one of two ways:</p>
     43  * <ul>
     44  * <li>On the startup command line, set the system property
     45  *     <code>org.apache.commons.logging.log</code> to the name of the
     46  *     <code>org.apache.commons.logging.Log</code> implementation class
     47  *     you want to use.</li>
     48  * <li>At runtime, call <code>LogSource.setLogImplementation()</code>.</li>
     49  * </ul>
     50  *
     51  * @deprecated Use {@link LogFactory} instead - The default factory
     52  *  implementation performs exactly the same algorithm as this class did
     53  *
     54  * @author Rod Waldhoff
     55  * @version $Id: LogSource.java 155426 2005-02-26 13:10:49Z dirkv $
     56  *
     57  * @deprecated Please use {@link java.net.URL#openConnection} instead.
     58  *     Please visit <a href="http://android-developers.blogspot.com/2011/09/androids-http-clients.html">this webpage</a>
     59  *     for further details.
     60  */
     61 @Deprecated
     62 public class LogSource {
     63 
     64     // ------------------------------------------------------- Class Attributes
     65 
     66     static protected Hashtable logs = new Hashtable();
     67 
     68     /** Is log4j available (in the current classpath) */
     69     static protected boolean log4jIsAvailable = false;
     70 
     71     /** Is JDK 1.4 logging available */
     72     static protected boolean jdk14IsAvailable = false;
     73 
     74     /** Constructor for current log class */
     75     static protected Constructor logImplctor = null;
     76 
     77 
     78     // ----------------------------------------------------- Class Initializers
     79 
     80     static {
     81 
     82         // Is Log4J Available?
     83         try {
     84             if (null != Class.forName("org.apache.log4j.Logger")) {
     85                 log4jIsAvailable = true;
     86             } else {
     87                 log4jIsAvailable = false;
     88             }
     89         } catch (Throwable t) {
     90             log4jIsAvailable = false;
     91         }
     92 
     93         // Is JDK 1.4 Logging Available?
     94         try {
     95             if ((null != Class.forName("java.util.logging.Logger")) &&
     96                 (null != Class.forName("org.apache.commons.logging.impl.Jdk14Logger"))) {
     97                 jdk14IsAvailable = true;
     98             } else {
     99                 jdk14IsAvailable = false;
    100             }
    101         } catch (Throwable t) {
    102             jdk14IsAvailable = false;
    103         }
    104 
    105         // Set the default Log implementation
    106         String name = null;
    107         try {
    108             name = System.getProperty("org.apache.commons.logging.log");
    109             if (name == null) {
    110                 name = System.getProperty("org.apache.commons.logging.Log");
    111             }
    112         } catch (Throwable t) {
    113         }
    114         if (name != null) {
    115             try {
    116                 setLogImplementation(name);
    117             } catch (Throwable t) {
    118                 try {
    119                     setLogImplementation
    120                             ("org.apache.commons.logging.impl.NoOpLog");
    121                 } catch (Throwable u) {
    122                     ;
    123                 }
    124             }
    125         } else {
    126             try {
    127                 if (log4jIsAvailable) {
    128                     setLogImplementation
    129                             ("org.apache.commons.logging.impl.Log4JLogger");
    130                 } else if (jdk14IsAvailable) {
    131                     setLogImplementation
    132                             ("org.apache.commons.logging.impl.Jdk14Logger");
    133                 } else {
    134                     setLogImplementation
    135                             ("org.apache.commons.logging.impl.NoOpLog");
    136                 }
    137             } catch (Throwable t) {
    138                 try {
    139                     setLogImplementation
    140                             ("org.apache.commons.logging.impl.NoOpLog");
    141                 } catch (Throwable u) {
    142                     ;
    143                 }
    144             }
    145         }
    146 
    147     }
    148 
    149 
    150     // ------------------------------------------------------------ Constructor
    151 
    152 
    153     /** Don't allow others to create instances */
    154     private LogSource() {
    155     }
    156 
    157 
    158     // ---------------------------------------------------------- Class Methods
    159 
    160 
    161     /**
    162      * Set the log implementation/log implementation factory
    163      * by the name of the class.  The given class
    164      * must implement {@link Log}, and provide a constructor that
    165      * takes a single {@link String} argument (containing the name
    166      * of the log).
    167      */
    168     static public void setLogImplementation(String classname) throws
    169             LinkageError, ExceptionInInitializerError,
    170             NoSuchMethodException, SecurityException,
    171             ClassNotFoundException {
    172         try {
    173             Class logclass = Class.forName(classname);
    174             Class[] argtypes = new Class[1];
    175             argtypes[0] = "".getClass();
    176             logImplctor = logclass.getConstructor(argtypes);
    177         } catch (Throwable t) {
    178             logImplctor = null;
    179         }
    180     }
    181 
    182 
    183     /**
    184      * Set the log implementation/log implementation factory
    185      * by class.  The given class must implement {@link Log},
    186      * and provide a constructor that takes a single {@link String}
    187      * argument (containing the name of the log).
    188      */
    189     static public void setLogImplementation(Class logclass) throws
    190             LinkageError, ExceptionInInitializerError,
    191             NoSuchMethodException, SecurityException {
    192         Class[] argtypes = new Class[1];
    193         argtypes[0] = "".getClass();
    194         logImplctor = logclass.getConstructor(argtypes);
    195     }
    196 
    197 
    198     /** Get a <code>Log</code> instance by class name */
    199     static public Log getInstance(String name) {
    200         Log log = (Log) (logs.get(name));
    201         if (null == log) {
    202             log = makeNewLogInstance(name);
    203             logs.put(name, log);
    204         }
    205         return log;
    206     }
    207 
    208 
    209     /** Get a <code>Log</code> instance by class */
    210     static public Log getInstance(Class clazz) {
    211         return getInstance(clazz.getName());
    212     }
    213 
    214 
    215     /**
    216      * Create a new {@link Log} implementation, based
    217      * on the given <i>name</i>.
    218      * <p>
    219      * The specific {@link Log} implementation returned
    220      * is determined by the value of the
    221      * <tt>org.apache.commons.logging.log</tt> property.
    222      * The value of <tt>org.apache.commons.logging.log</tt> may be set to
    223      * the fully specified name of a class that implements
    224      * the {@link Log} interface.  This class must also
    225      * have a public constructor that takes a single
    226      * {@link String} argument (containing the <i>name</i>
    227      * of the {@link Log} to be constructed.
    228      * <p>
    229      * When <tt>org.apache.commons.logging.log</tt> is not set,
    230      * or when no corresponding class can be found,
    231      * this method will return a Log4JLogger
    232      * if the log4j Logger class is
    233      * available in the {@link LogSource}'s classpath, or a
    234      * Jdk14Logger if we are on a JDK 1.4 or later system, or
    235      * NoOpLog if neither of the above conditions is true.
    236      *
    237      * @param name the log name (or category)
    238      */
    239     static public Log makeNewLogInstance(String name) {
    240 
    241         Log log = null;
    242         try {
    243             Object[] args = new Object[1];
    244             args[0] = name;
    245             log = (Log) (logImplctor.newInstance(args));
    246         } catch (Throwable t) {
    247             log = null;
    248         }
    249         if (null == log) {
    250             log = new NoOpLog(name);
    251         }
    252         return log;
    253 
    254     }
    255 
    256 
    257     /**
    258      * Returns a {@link String} array containing the names of
    259      * all logs known to me.
    260      */
    261     static public String[] getLogNames() {
    262         return (String[]) (logs.keySet().toArray(new String[logs.size()]));
    263     }
    264 
    265 
    266 }
    267