Package de.danet.an.util.log4j

Source Code of de.danet.an.util.log4j.ApplLog4jFactory

/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* $Id: ApplLog4jFactory.java 2326 2007-03-27 21:59:44Z mlipp $
*
* $Log$
* Revision 1.2  2006/09/29 12:32:13  drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.1.1.2  2004/08/18 15:17:35  drmlipp
* Update to 1.2
*
* Revision 1.3  2004/06/22 11:52:04  lipp
* Some fixes to match new commons-logging.
*
* Revision 1.2  2003/06/27 08:51:46  lipp
* Fixed copyright/license information.
*
* Revision 1.1  2003/03/28 10:09:20  lipp
* Intriduced logging using commons-logging.
*
*/
package de.danet.an.util.log4j;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogConfigurationException;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.impl.Log4JLogger;

/**
* This class provides an alternate access to the log4j logging
* library for Apache commons logging.. The necessity arises from the
* usage of log4j as logging package in the JBoss application
* server. As log4j is already included in JBoss' classpath and
* configured by JBoss, we cannot have really independent application
* level logging.<P>
*
* The central problem is the static default hierarchy used by
* <code>org.apache.log4j.Category</code>. For a distinct application
* level logging we need an alternate hierarchy. This factory uses
* such an alternate hierarchy.<P>
*
* This implementation is based on the <code>Log4JFactory</code> from
* Apache.
*
* @author <a href="mailto:lipp@danet.de">Michael Lipp</a>
* @version $Revision: 2326 $
*/

public class ApplLog4jFactory extends LogFactory {
   
    /**
     * The configuration attributes for this {@link LogFactory}.
     */
    private Hashtable attributes = new Hashtable();

    // previously returned instances, to avoid creation of proxies
    private Hashtable instances = new Hashtable();

    /**
     * Return the configuration attribute with the specified name (if any),
     * or <code>null</code> if there is no such attribute.
     *
     * @param name Name of the attribute to return
     */
    public Object getAttribute(String name) {
        return (attributes.get(name));
    }

    /**
     * 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);
    }

    /**
     * Convenience method to derive a name from the specified class and
     * call <code>getInstance(String)</code> with it.
     *
     * @param clazz Class for which a suitable Log name will be derived
     *
     * @exception LogConfigurationException if a suitable <code>Log</code>
     *  instance cannot be returned
     */
    public Log getInstance(Class clazz)
        throws LogConfigurationException     {
        Log instance = (Log) instances.get(clazz);
        if(instance != null) {
            return instance;
        }

        instance=new Log4JLogger(ApplLogger.getLogger(clazz));
        instances.put(clazz, instance);
        return instance;
    }

    public Log getInstance(String name)
        throws LogConfigurationException {
        Log instance = (Log) instances.get(name);
        if( instance != null ) {
            return instance;
        }

        instance=new Log4JLogger(ApplLogger.getLogger(name));
        instances.put(name, instance);
        return instance;
    }

    /**
     * Release any internal references to previously created {@link Log}
     * instances returned by this factory.  This is useful environments
     * like servlet containers, which implement application reloading by
     * throwing away a ClassLoader.  Dangling references to objects in that
     * class loader would prevent garbage collection.
     */
    public void release() {
        instances.clear();
        // what's the log4j mechanism to cleanup ???
    }


    /**
     * Remove any configuration attribute associated with the specified name.
     * If there is no such attribute, no action is taken.
     *
     * @param name Name of the attribute to remove
     */
    public void removeAttribute(String name) {
        attributes.remove(name);
    }


    /**
     * Set the configuration attribute with the specified name.  Calling
     * this with a <code>null</code> value is equivalent to calling
     * <code>removeAttribute(name)</code>.
     *
     * @param name Name of the attribute to set
     * @param value Value of the attribute to set, or <code>null</code>
     *  to remove any setting for this attribute
     */
    public void setAttribute(String name, Object value) {
        if (value == null) {
            attributes.remove(name);
        } else {
            attributes.put(name, value);
        }
    }

   
}
TOP

Related Classes of de.danet.an.util.log4j.ApplLog4jFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.