User: ejort
Date: 02/02/08 13:35:38
Added: src/main/org/jboss/mx/logging DefaultLoggerAdapter.java
DefaultLoggerAdapterFactory.java
DefaultLoggerContextSelector.java Logger.java
LoggerAdapter.java LoggerAdapterFactory.java
LoggerAdapterFactorySupport.java
LoggerAdapterSupport.java LoggerContext.java
LoggerContextSelector.java LoggerManager.java
LoggerPermission.java NullLoggerAdapter.java
NullLoggerAdapterFactory.java package.html
Log:
Initial version of logging - might be over-engineered?
Revision Changes Path
1.1 jmx/src/main/org/jboss/mx/logging/DefaultLoggerAdapter.java
Index: DefaultLoggerAdapter.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mx.logging;
/**
* The default logger adapter.<p>
*
* This logs to System.err
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>
* @version $Revision: 1.1 $
*/
public final class DefaultLoggerAdapter
extends LoggerAdapterSupport
{
// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
/**
* The default logging level
*/
private int level = Logger.WARN;
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// LoggerAdapter Implementation ----------------------------------
public final String getName()
{
return "Default";
}
public final int getLevel()
{
return level;
}
public final void setLevel(int level)
{
this.level = level;
}
public final void log(int level, String message)
{
if (isEnabled(level) == false)
return;
doLog(level, message, null);
}
public final void log(int level, String message, Throwable throwable)
{
if (isEnabled(level) == false)
return;
doLog(level, message, throwable);
}
public final boolean isEnabled(int level)
{
return level >= this.level;
}
/**
* This is a logger implementation.
*
* @return this logger adapter.
*/
public Object getUnderlyingLogger()
{
return this;
}
// Y Overrides ---------------------------------------------------
// Protected -----------------------------------------------------
// Package Private -----------------------------------------------
// Private -------------------------------------------------------
/**
* Perform the actual logging to system error.
*
* @param level the logging level
* @param message the message
* @param throwable an error
*/
private static final void doLog(int level, String message, Throwable throwable)
{
synchronized(System.err)
{
if (level == Logger.FATAL)
System.err.print("FATAL ");
if (level == Logger.ERROR)
System.err.print("ERROR ");
if (level == Logger.WARN)
System.err.print("WARN ");
if (level == Logger.INFO)
System.err.print("INFO ");
if (level == Logger.DEBUG)
System.err.print("DEBUG ");
if (level == Logger.TRACE)
System.err.print("TRACE ");
System.err.println(message);
if (throwable != null)
throwable.printStackTrace(System.err);
}
}
// Inner classes -------------------------------------------------
}
1.1
jmx/src/main/org/jboss/mx/logging/DefaultLoggerAdapterFactory.java
Index: DefaultLoggerAdapterFactory.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mx.logging;
/**
* The default logger adapter factory.<p>
*
* This has singleton logger adapter that logs to System.err.
*
* @see DefaultLoggerAdapter
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>
* @version $Revision: 1.1 $
*/
public final class DefaultLoggerAdapterFactory
extends LoggerAdapterFactorySupport
{
// Constants -----------------------------------------------------
public static final DefaultLoggerAdapter adapter = new DefaultLoggerAdapter();
// Attributes ----------------------------------------------------
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// LoggerAdapterFactory Implementation --------------------------
public final LoggerAdapter getLoggerAdapter(String name)
{
return adapter;
}
public final Class getLoggerAdapterClass()
{
return adapter.getClass();
}
// Y Overrides ---------------------------------------------------
// Protected -----------------------------------------------------
// Package Private -----------------------------------------------
// Private -------------------------------------------------------
// Inner classes -------------------------------------------------
}
1.1
jmx/src/main/org/jboss/mx/logging/DefaultLoggerContextSelector.java
Index: DefaultLoggerContextSelector.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mx.logging;
import java.util.WeakHashMap;
/**
* The default context selector.<p>
*
* This has singleton context configured initially configured with the default
* logger adapter implementation.<p>
*
* Extra contexts can be registered keyed by classloader.<p>
*
* When new loggers are requested, the thread's context classloader is
* checked to discover if there is a special context. If that fails the
* parent is checked and that its parent, etc. If none of the classloaders
* has a special logging context, the default logging context is used.
*
* @see DefaultLoggerAdapter
* @see DefaultLoggerAdapterFactory
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>
* @version $Revision: 1.1 $
*/
public final class DefaultLoggerContextSelector
implements LoggerContextSelector
{
// Constants -----------------------------------------------------
/**
* The default logger context
*/
public static final LoggerContext defaultLoggerContext =
new LoggerContext(new DefaultLoggerAdapterFactory());
// Attributes ----------------------------------------------------
/**
* The association of classloader to logger context.
*/
private WeakHashMap contexts = new WeakHashMap();
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// Public --------------------------------------------------------
// LoggerContextSelector Implementation --------------------------
/**
* Associate a special logging context with a classloader.
*
* @param context the class loader for the context cannot be null
* @param loggerContext the logger context cannot be null
*/
public final void associate(Object context, LoggerContext loggerContext)
{
if (context == null)
throw new IllegalArgumentException("null context");
if ((context instanceof ClassLoader) == false)
throw new RuntimeException("Only classloaders are allowed as contexts");
if (loggerContext == null)
throw new IllegalArgumentException("null logger context");
synchronized (contexts)
{
contexts.put(context, loggerContext);
}
}
/**
* Remove an association of a special logging context with a classloader.
*
* @param context the class loader for the context to remove cannot be null
*/
public final void remove(Object context)
{
if (context == null)
throw new IllegalArgumentException("null context");
synchronized (contexts)
{
contexts.remove(context);
}
}
public final LoggerContext getLoggerContext()
{
LoggerContext result = null;
// Check for special context keep going up the class loader
// tree until we find one or run out of class loaders
synchronized (contexts)
{
ClassLoader cl = Thread.currentThread().getContextClassLoader();
while (cl != null)
{
result = (LoggerContext) contexts.get(cl);
if (result != null)
break;
cl = cl.getParent();
}
}
// No special context, use the default
if (result == null)
result = defaultLoggerContext;
return result;
}
// Y Overrides ---------------------------------------------------
// Protected -----------------------------------------------------
// Package Private -----------------------------------------------
// Private -------------------------------------------------------
// Inner classes -------------------------------------------------
}
1.1 jmx/src/main/org/jboss/mx/logging/Logger.java
Index: Logger.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mx.logging;
/**
* The logger.<p>
*
* It provides a standard interface based on log4j or
* java.util.logging package.<p>
*
* This does no work itself, instead it delegates to the real
* implementation via a LoggerAdapter.<p>
*
* The LoggerAdapter is initially set at creation, however it
* can be hot-swapped at any time.
*
* @see LoggerAdapter
* @see LoggerManager
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>
* @version $Revision: 1.1 $
*/
public class Logger
{
// Constants -----------------------------------------------------
/**
* No logging
*/
public static final int NONE = Integer.MAX_VALUE;
/**
* The fatal level
*/
public static final int FATAL = 50000;
/**
* The error level
*/
public static final int ERROR = 40000;
/**
* The warning level
*/
public static final int WARN = 30000;
/**
* The informational level
*/
public static final int INFO = 20000;
/**
* The debug level
*/
public static final int DEBUG = 10000;
/**
* The trace level
*/
public static final int TRACE = 9000;
/**
* All logging
*/
public static final int ALL = Integer.MIN_VALUE;
/**
* The log manager
*/
private static LoggerManager manager = LoggerManager.getLoggerManager();
// Attributes ----------------------------------------------------
/**
* The adapter to which this logger delegates.
*/
private LoggerAdapter adapter;
// Static --------------------------------------------------------
/**
* Create a new logger given a name.<p>
*
* It is common that the name is hierarchical like java class names.
* However this does not mean that the real logger will understand
* or use the hierarchy, or even the name.<p>
*
* @param name the name to assign to the logger, cannot be null
* @return the logger
*/
public static final Logger getLogger(String name)
{
// The name is checked by LoggerManager
return manager.getLogger(name);
}
/**
* Create a new logger given a class.<p>
*
* This is a convenience method to create a logger using a class name.
* This is very common usage.
*
* @param clazz the class, cannot be null
*/
public static final Logger getLogger(Class clazz)
{
if (clazz == null)
throw new IllegalArgumentException("Null class");
return getLogger(clazz.getName());
}
// Constructors --------------------------------------------------
/**
* Create a new logger that delegates to the given adapter.
*
* @param adapter the real logger cannot be null
*/
/*package*/ Logger(LoggerAdapter adapter)
{
if (adapter == null)
throw new IllegalArgumentException("Null adapter");
this.adapter = adapter;
}
// Public --------------------------------------------------------
/**
* Get the name of this logger. This may not be the name as that
* used to create the logger.
*
* @return the name of the logger.
*/
public String getName()
{
return adapter.getName();
}
/**
* Get the logging level.
*
* @return the level.
*/
public int getLevel()
{
return adapter.getLevel();
}
/**
* Set the logging level.
*
* @param level the new level.
* @exception SecurityException when not authorised
*/
public void setLevel(int level)
throws SecurityException
{
adapter.setLevel(level);
}
/**
* Log a message.
*
* @param the level to log
* @param message the message to log.
*/
public void log(int level, String message)
{
adapter.log(level, message);
}
/**
* Log a message with a throwable.
*
* @param the level to log
* @param message the message to log.
* @param throwable the throwable to log.
*/
public void log(int level, String message, Throwable throwable)
{
adapter.log(level, message, throwable);
}
/**
* Test whether a level is enabled.
*
* @param the level to test
* @return true when enabled, false otherwise.
*/
public boolean isEnabled(int level)
{
return adapter.isEnabled(level);
}
/**
* Log a fatal message.
*
* @param message the message to log.
*/
public void fatal(String message)
{
adapter.fatal(message);
}
/**
* Log a fatal message with a throwable.
*
* @param message the message to log.
* @param throwable the throwable to log.
*/
public void fatal(String message, Throwable throwable)
{
adapter.fatal(message, throwable);
}
/**
* Test whether fatal is enabled.
*
* @return true when fatal is enabled, false otherwise.
*/
public boolean isFatalEnabled()
{
return adapter.isFatalEnabled();
}
/**
* Log an error message.
*
* @param message the message to log.
*/
public void error(String message)
{
adapter.error(message);
}
/**
* Log an error message with a throwable.
*
* @param message the message to log.
* @param throwable the throwable to log.
*/
public void error(String message, Throwable throwable)
{
adapter.error(message, throwable);
}
/**
* Test whether error is enabled.
*
* @return true when error is enabled, false otherwise.
*/
public boolean isErrorEnabled()
{
return adapter.isErrorEnabled();
}
/**
* Log a warning message.
*
* @param message the message to log.
*/
public void warn(String message)
{
adapter.warn(message);
}
/**
* Log a warning message with a throwable.
*
* @param message the message to log.
* @param throwable the throwable to log.
*/
public void warn(String message, Throwable throwable)
{
adapter.warn(message, throwable);
}
/**
* Test whether warning is enabled.
*
* @return true when warning is enabled, false otherwise.
*/
public boolean isWarnEnabled()
{
return adapter.isWarnEnabled();
}
/**
* Log an informational message.
*
* @param message the message to log.
*/
public void info(String message)
{
adapter.info(message);
}
/**
* Log an informational message with a throwable.
*
* @param message the message to log.
* @param throwable the throwable to log.
*/
public void info(String message, Throwable throwable)
{
adapter.info(message, throwable);
}
/**
* Test whether info is enabled.
*
* @return true when info is enabled, false otherwise.
*/
public boolean isInfoEnabled()
{
return adapter.isInfoEnabled();
}
/**
* Log a debug message.
*
* @param message the message to log.
*/
public void debug(String message)
{
adapter.debug(message);
}
/**
* Log a debug message with a throwable.
*
* @param message the message to log.
* @param throwable the throwable to log.
*/
public void debug(String message, Throwable throwable)
{
adapter.debug(message, throwable);
}
/**
* Test whether debug is enabled.
*
* @return true when debug is enabled, false otherwise.
*/
public boolean isDebugEnabled()
{
return adapter.isDebugEnabled();
}
/**
* Log a trace message.
*
* @param message the message to log.
*/
public void trace(String message)
{
adapter.trace(message);
}
/**
* Log a trace message with a throwable.
*
* @param message the message to log.
* @param throwable the throwable to log.
*/
public void trace(String message, Throwable throwable)
{
adapter.trace(message, throwable);
}
/**
* Test whether trace is enabled.
*
* @return true when trace is enabled, false otherwise.
*/
public boolean isTraceEnabled()
{
return adapter.isTraceEnabled();
}
/**
* Retrieve the real logger.<p>
*
* Use this with caution. The aim of this method is to allow the
* real logger to be manipulated using methods not exposed by this
* class. No guarantees can be made that the object returned is
* useful.<p>
*
* For example if you are sure you are using log4j before 1.2, this
* method should return a Category.
*
* @return an object that might be of the expected logging class.
*/
public Object getUnderlyingLogger()
{
return adapter.getUnderlyingLogger();
}
// X Implementation ----------------------------------------------
// Y Overrides ---------------------------------------------------
// Protected -----------------------------------------------------
// Package Private -----------------------------------------------
/**
* Get the adapter of this logger.
*
* @return the adapter.
*/
LoggerAdapter getLoggerAdapter()
{
return adapter;
}
/**
* Set the adapter of this logger.
*
* @param the new adapter cannot be null
*/
void setLoggerAdapter(LoggerAdapter adapter)
{
if (adapter == null)
throw new IllegalArgumentException("Null adapter");
this.adapter = adapter;
}
// Private -------------------------------------------------------
// Inner classes -------------------------------------------------
}
1.1 jmx/src/main/org/jboss/mx/logging/LoggerAdapter.java
Index: LoggerAdapter.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mx.logging;
/**
* The interface implemented by logging implementations.<p>
*
* The adapter is either a logger or it can delegate or it can
* delegate to a more complete logging package like log4j or
* java.util.logging.<p>
*
* The LoggerAdapterSupport class can be used to help in implementing
* this interface.
*
* @see Logger
* @see LoggerManager
* @see LoggerAdapterSupport
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>
* @version $Revision: 1.1 $
*/
public interface LoggerAdapter
{
// Constants -----------------------------------------------------
// Public --------------------------------------------------------
/**
* Get the name of this logger.
*
* @return the name of the logger.
*/
public String getName();
/**
* Get the logging level.
*
* @return the level
*/
public int getLevel();
/**
* Set the logging level.
*
* @param level the new level.
* @exception SecurityException when not authorised
*/
public void setLevel(int level)
throws SecurityException;
/**
* Log a message.
*
* @param the level to log
* @param message the message to log.
*/
public void log(int level, String message);
/**
* Log a message with a throwable.
*
* @param the level to log
* @param message the message to log.
* @param throwable the throwable to log.
*/
public void log(int level, String message, Throwable throwable);
/**
* Test whether a level is enabled.
*
* @param the level to test
* @return true when enabled, false otherwise.
*/
public boolean isEnabled(int level);
/**
* Log a fatal message.
*
* @param message the message to log.
*/
public void fatal(String message);
/**
* Log a fatal message with a throwable.
*
* @param message the message to log.
* @param throwable the throwable to log.
*/
public void fatal(String message, Throwable throwable);
/**
* Test whether fatal is enabled.
*
* @return true when fatal is enabled, false otherwise.
*/
public boolean isFatalEnabled();
/**
* Log an error message.
*
* @param message the message to log.
*/
public void error(String message);
/**
* Log an error message with a throwable.
*
* @param message the message to log.
* @param throwable the throwable to log.
*/
public void error(String message, Throwable throwable);
/**
* Test whether error is enabled.
*
* @return true when error is enabled, false otherwise.
*/
public boolean isErrorEnabled();
/**
* Log a warning message.
*
* @param message the message to log.
*/
public void warn(String message);
/**
* Log a warning message with a throwable.
*
* @param message the message to log.
* @param throwable the throwable to log.
*/
public void warn(String message, Throwable throwable);
/**
* Test whether warning is enabled.
*
* @return true when warning is enabled, false otherwise.
*/
public boolean isWarnEnabled();
/**
* Log an informational message.
*
* @param message the message to log.
*/
public void info(String message);
/**
* Log an informational message with a throwable.
*
* @param message the message to log.
* @param throwable the throwable to log.
*/
public void info(String message, Throwable throwable);
/**
* Test whether info is enabled.
*
* @return true when info is enabled, false otherwise.
*/
public boolean isInfoEnabled();
/**
* Log a debug message.
*
* @param message the message to log.
*/
public void debug(String message);
/**
* Log a debug message with a throwable.
*
* @param message the message to log.
* @param throwable the throwable to log.
*/
public void debug(String message, Throwable throwable);
/**
* Test whether debug is enabled.
*
* @return true when debug is enabled, false otherwise.
*/
public boolean isDebugEnabled();
/**
* Log a trace message.
*
* @param message the message to log.
*/
public void trace(String message);
/**
* Log a trace message with a throwable.
*
* @param message the message to log.
* @param throwable the throwable to log.
*/
public void trace(String message, Throwable throwable);
/**
* Test whether trace is enabled.
*
* @return true when trace is enabled, false otherwise.
*/
public boolean isTraceEnabled();
/**
* Retrieve the real logger.<p>
*
* Use this with caution. The aim of this method is to allow the
* real logger to be manipulated using methods not exposed by this
* class. No guarantees can be made that the object returned is
* useful.<p>
*
* For example if you are sure you are using log4j before 1.2, this
* method should return a Category.
*
* @return an object that might be of the expected class.
*/
public Object getUnderlyingLogger();
// Inner classes -------------------------------------------------
}
1.1 jmx/src/main/org/jboss/mx/logging/LoggerAdapterFactory.java
Index: LoggerAdapterFactory.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mx.logging;
/**
* The interface is implemented to create adapters for each
* logging implementation.
*
* @see LoggerAdapter
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>
* @version $Revision: 1.1 $
*/
public interface LoggerAdapterFactory
{
// Constants -----------------------------------------------------
// Public --------------------------------------------------------
/**
* Create a new logger adapter given a name.
*
* @return the adapter
*/
public LoggerAdapter getLoggerAdapter(String name);
/**
* Retrieve the class of the adapter.
* This is used to check an adapter is up-to-date during hot-swapping.<p>
*
* The LoggerAdapterFactorySupport class helps to implement this
* this interface.
*
* @return the class of the adapter.
*/
public Class getLoggerAdapterClass();
/**
* Retrieve a context object for this factory.<p>
*
* Null should be returned when there is no context.
*
* @return the context object
*/
public Object getContext();
/**
* Set the configurator.<p>
*
* @param configurator the object used to configure the context
* @exception SecurityException when not authorised
*/
public void setConfiguration(Object configurator)
throws SecurityException;
/**
* Reset the configuration to the default.
*
* @exception SecurityException when not authorised
*/
public void resetConfiguration()
throws SecurityException;
// Inner classes -------------------------------------------------
}
1.1
jmx/src/main/org/jboss/mx/logging/LoggerAdapterFactorySupport.java
Index: LoggerAdapterFactorySupport.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mx.logging;
/**
* This class helps to implement the LoggerAdapterFactory interface.<p>
*
* @see LoggerAdapterFactory
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>
* @version $Revision: 1.1 $
*/
public abstract class LoggerAdapterFactorySupport
implements LoggerAdapterFactory
{
// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
/**
* The context object
*/
private Object context;
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
/**
* Constructor with no context object
*/
public LoggerAdapterFactorySupport()
{
this(null);
}
/**
* Constructor with the given context object
*
* @param context the context object
*/
public LoggerAdapterFactorySupport(Object context)
{
this.context = context;
}
// LoggerAdapterFactory Implementation ---------------------------
public Object getContext()
{
return context;
}
/**
* This operation is quietly ignored.
*/
public void setConfiguration(Object configurator)
throws SecurityException
{
}
/**
* This operation is quietly ignored.
*/
public void resetConfiguration()
throws SecurityException
{
}
// Y Overrides ---------------------------------------------------
// Protected -----------------------------------------------------
// Package Private -----------------------------------------------
// Private -------------------------------------------------------
// Inner classes -------------------------------------------------
}
1.1 jmx/src/main/org/jboss/mx/logging/LoggerAdapterSupport.java
Index: LoggerAdapterSupport.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mx.logging;
/**
* This class helps to implement the LoggerAdapter interface.<p>
*
* Only a small number of methods need implementing.<p>
*
* WARNING: The implemented methods are declared final for speed.
*
* @see LoggerAdapter
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>
* @version $Revision: 1.1 $
*/
public abstract class LoggerAdapterSupport
implements LoggerAdapter
{
// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// LoggerAdapter Implementation ----------------------------------
public final void fatal(String message)
{
log(Logger.FATAL, message);
}
public final void fatal(String message, Throwable throwable)
{
log(Logger.FATAL, message, throwable);
}
public final boolean isFatalEnabled()
{
return isEnabled(Logger.FATAL);
}
public final void error(String message)
{
log(Logger.ERROR, message);
}
public final void error(String message, Throwable throwable)
{
log(Logger.ERROR, message, throwable);
}
public final boolean isErrorEnabled()
{
return isEnabled(Logger.ERROR);
}
public final void warn(String message)
{
log(Logger.WARN, message);
}
public final void warn(String message, Throwable throwable)
{
log(Logger.WARN, message, throwable);
}
public final boolean isWarnEnabled()
{
return isEnabled(Logger.WARN);
}
public final void info(String message)
{
log(Logger.INFO, message);
}
public final void info(String message, Throwable throwable)
{
log(Logger.INFO, message, throwable);
}
public final boolean isInfoEnabled()
{
return isEnabled(Logger.INFO);
}
public final void debug(String message)
{
log(Logger.DEBUG, message);
}
public final void debug(String message, Throwable throwable)
{
log(Logger.DEBUG, message, throwable);
}
public final boolean isDebugEnabled()
{
return isEnabled(Logger.DEBUG);
}
public final void trace(String message)
{
log(Logger.TRACE, message);
}
public final void trace(String message, Throwable throwable)
{
log(Logger.TRACE, message, throwable);
}
public final boolean isTraceEnabled()
{
return isEnabled(Logger.TRACE);
}
// Y Overrides ---------------------------------------------------
// Protected -----------------------------------------------------
// Package Private -----------------------------------------------
// Private -------------------------------------------------------
// Inner classes -------------------------------------------------
}
1.1 jmx/src/main/org/jboss/mx/logging/LoggerContext.java
Index: LoggerContext.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mx.logging;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.WeakReference;
import java.security.AccessController;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/**
* A logger context.<p>
*
* The logger context holds the state for loggers in within its context.<p>
*
* It delegates most implementation to the registered LoggerAdapterFactory,
* this allows different logging implementations in different contexts. It
* allows the logging implementation to vary configuration according to
* context.<p>
*
* The processing performed within this class is the "hot-swapping" of
* implementations and the querying of the state.
*
* @see Logger
* @see LoggerAdapter
* @see LoggerAdapterFactory
* @see LoggerContextSelector
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>
* @version $Revision: 1.1 $
*/
public class LoggerContext
{
// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
/**
* The current logging adapter factory.
*/
private LoggerAdapterFactory factory;
/**
* The loggers by name. The loggers are stored in weak references
* for garbage collection.
*/
private final HashMap loggers = new HashMap();
// Static --------------------------------------------------------
/**
* The log manager
*/
private static final LoggerManager manager = LoggerManager.getLoggerManager();
/**
* The background thread waiting for loggers to be garbage collected
* When the first logger is created, the thread is started
*/
private static final GarbageMonitor garbageMonitor = new GarbageMonitor();
/**
* The reference queue for garbage collection
*/
private static final ReferenceQueue garbageCollected = new ReferenceQueue();
/**
* The logger info by weak reference, used for garbage collection.
*/
private static final HashMap monitored = new HashMap();
/**
* Monitor garbage collection information
*
* @param weak the weak reference that should be garbage collected
* @param context the context of the logger
* @param name the name of the logger
*/
private static final void monitor(WeakReference weak,
LoggerContext context,
String name)
{
LoggerInfo info = new LoggerInfo(context, name);
synchronized (monitored)
{
monitored.put(weak, info);
}
}
/**
* Remove a weak reference from the loggers
*
* @param weak the weak reference that has been garbage collected
*/
private static final void remove(WeakReference weak)
{
LoggerInfo info;
synchronized (monitored)
{
info = (LoggerInfo) monitored.remove(weak);
}
info.context.remove(info.name);
}
// Constructors --------------------------------------------------
/**
* Create a new context with the given LoggerAdapterFactory.<p>
*
* @param factory the factory
*/
public LoggerContext(LoggerAdapterFactory factory)
{
this.factory = factory;
Object context = factory.getContext();
if (context != null)
manager.associateContext(context, this);
}
// Public --------------------------------------------------------
/**
* Create a new logger given a name.<p>
*
* It is common that the name is hierarchical like java class names.
* However this does not mean that the real logger will understand
* or use the hierarchy, or even the name.<p>
*
* @param name the name to assign to the logger, cannot be null
* @return a logger
*/
public final Logger getLogger(String name)
{
if (name == null)
throw new IllegalArgumentException("null name");
Logger result;
WeakReference weak;
synchronized (loggers)
{
// Check to see if we already have a logger
weak = (WeakReference) loggers.get(name);
if (weak != null)
{
result = (Logger) weak.get();
if (result != null)
return result;
}
// Delegate the "creation" of a new logger
LoggerAdapter adapter = factory.getLoggerAdapter(name);
result = new Logger(adapter);
// Keep track of the loggers
weak = new WeakReference(result, garbageCollected);
loggers.put(name, weak);
}
// Monitor the logger for garbage collection
monitor(weak, this, name);
return result;
}
/**
* Retrieve the current logger adapter factory.
*
* @return the logger adapter factory
* @exception SecurityException when not authorised
*/
public final LoggerAdapterFactory getLoggerAdapterFactory()
throws SecurityException
{
return factory;
}
/**
* Register the adapter factory for this context.
* Any existing loggers are updated to use an adapter from this factory.<p>
*
* WARNING: New loggers cannot be retrieved during this operation.
*
* @param factory the factory creating new adapters
* @exception SecurityException when not authorised
*/
public final void setLoggerAdapterFactory(LoggerAdapterFactory factory)
throws SecurityException
{
if (factory == null)
throw new IllegalArgumentException("null factory");
// TODO AccessController.checkPermission(new LoggerPermission("control"));
synchronized (loggers)
{
Class oldDelegateClass = this.factory.getLoggerAdapterClass();
Class newDelegateClass = factory.getLoggerAdapterClass();
// No need to update adapters when using the same class
if (oldDelegateClass != newDelegateClass)
return;
// Remove any old context
Object context = this.factory.getContext();
if (context != null)
manager.removeContext(context);
// Switch the factory
this.factory = factory;
// Set the new context
context = factory.getContext();
if (context != null)
manager.associateContext(context, this);
// Update each logger to have the correct adapter
Iterator iterator = loggers.entrySet().iterator();
while (iterator.hasNext())
{
Map.Entry entry = (Map.Entry) iterator.next();
// See whether this logger is still used
WeakReference weak = (WeakReference) entry.getValue();
Logger logger = (Logger) weak.get();
if (logger == null)
iterator.remove();
// If we have the incorrect adapter, update it
LoggerAdapter adapter = logger.getLoggerAdapter();
if (adapter.getClass().equals(newDelegateClass) == false)
{
String name = (String) entry.getKey();
logger.setLoggerAdapter(factory.getLoggerAdapter(name));
}
}
}
}
/**
* Set the configurator for this context.<p>
*
* The configurator is implementation dependent.
*
* @param configurator the object used to configure the context
* @exception SecurityException when not authorised
*/
public final void setConfiguration(Object configurator)
throws SecurityException
{
factory.setConfiguration(configurator);
}
/**
* Reset the configuration to the default for this context.
*
* @exception SecurityException when not authorised
*/
public final void resetConfiguration()
throws SecurityException
{
factory.resetConfiguration();
}
/**
* Retrieve the list of loggers in this context.
*
* WARNING: The enumeration is NOT synchronized.
*
* @return an Enumeration of loggers
*/
public final Enumeration getLoggers()
{
return new LoggersEnumeration();
}
// X Implementation ----------------------------------------------
// Y Overrides ---------------------------------------------------
// Protected -----------------------------------------------------
// Package Private -----------------------------------------------
// Private -------------------------------------------------------
/**
* Remove a logger, used during garbage collection
*
* @param the name of the logger to remove
*/
private final void remove(String name)
{
synchronized (loggers)
{
loggers.remove(name);
}
}
// Inner classes -------------------------------------------------
/**
* An enumeration for the loggers.
*/
private class LoggersEnumeration
implements Enumeration
{
// Attributes --------------------------------------------------
/**
* The iterator for this enumeration
*/
private Iterator iterator;
/**
* The current logger, the logger return by nextElement()
*/
private Object current;
// Constructors ------------------------------------------------
/**
* Construct an enumeration using an iterator over the values
* parts of the name to loggers map.
*/
public LoggersEnumeration()
{
iterator = loggers.values().iterator();
next();
}
// Public ------------------------------------------------------
// Enumeration Implementation ----------------------------------
public final boolean hasMoreElements()
{
return (current != null);
}
public final Object nextElement()
{
Object result = current;
next();
return result;
}
// Y Overrides -------------------------------------------------
// Private -----------------------------------------------------
/**
* Retrieve the next logger after
* We need to skip over loggers that have been garbage collected.
*/
private final void next()
{
current = null;
while (iterator.hasNext())
{
WeakReference weak = (WeakReference) iterator.next();
current = weak.get();
if (current != null)
return;
}
}
}
/**
* Logger information used in garbage collection.
*/
private static class LoggerInfo
{
// Attributes --------------------------------------------------
/**
* The context of the logger
*/
public LoggerContext context;
/**
* The name of the logger
*/
private String name;
// Constructors ------------------------------------------------
/**
* Construct logger information
*
* @param context the logger context
* @param name the name of the logger
*/
public LoggerInfo(LoggerContext context, String name)
{
this.context = context;
this.name = name;
}
// Public ------------------------------------------------------
// X Implementation --------------------------------------------
// Y Overrides -------------------------------------------------
// Private -----------------------------------------------------
}
/**
* The garbage collection monitor.
*/
private static class GarbageMonitor
extends Thread
{
// Attributes --------------------------------------------------
// Constructors ------------------------------------------------
/**
* Construct a new Garbage Monitor
*/
public GarbageMonitor()
{
setDaemon(false);
start();
}
// Public ------------------------------------------------------
// X Implementation --------------------------------------------
// Thread Overrides --------------------------------------------
/**
* Waits for loggers to be garbage collected and tidys up
* the loggers in the contexts
*/
public final void run()
{
while (true)
{
try
{
WeakReference weak = (WeakReference) garbageCollected.remove();
remove(weak);
}
catch (InterruptedException ie)
{
break;
}
}
}
// Private -----------------------------------------------------
}
}
1.1 jmx/src/main/org/jboss/mx/logging/LoggerContextSelector.java
Index: LoggerContextSelector.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mx.logging;
/**
* The interface is implemented to control logging contexts.<p>
*
* The method for determining the context is entirely at the implementation's
* control.<p>
*
* The selector must be registered with the Logger Manager.
*
* @see LoggerManager
* @see LoggerContext
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>
* @version $Revision: 1.1 $
*/
public interface LoggerContextSelector
{
// Constants -----------------------------------------------------
// Public --------------------------------------------------------
/**
* Associate a special logging context with a context object.
*
* @param context the context object cannot be null
* @param loggerContext the logger context cannot be null
*/
public void associate(Object context, LoggerContext loggerContext);
/**
* Remove an association of a special logging context with a context object.
*
* @param context the context object to remove cannot be null
*/
public void remove(Object context);
/**
* Determine the logging context.
*
* @return the current logging context.
*/
public LoggerContext getLoggerContext();
// Inner classes -------------------------------------------------
}
1.1 jmx/src/main/org/jboss/mx/logging/LoggerManager.java
Index: LoggerManager.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mx.logging;
import java.security.AccessController;
import java.util.Enumeration;
/**
* The log manager.<p>
*
* Ideally, there should be only one of these. In practice
* there can be one per classloader provided they are diffent scopes.<p>
*
* This manager is mainly responsible for controlling logging contexts.
* This involves using a LoggerContextSelector to determine a LoggerContext.
* All requests are then delegated to that context.<p>
*
* These are main management areas.
* <ol>
* <li> Creating loggers.
* <li> Specifying configuration.
* <li> Querying.
* <li> A pluggable LoggerContextSelector.
* </ol>
*
* @see Logger
* @see LoggerContext
* @see LoggerContextSelector
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>
* @version $Revision: 1.1 $
*/
public class LoggerManager
{
// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
/**
* The logging context selector.
*/
private LoggerContextSelector selector;
// Static --------------------------------------------------------
/**
* The log manager singleton.
*/
private static LoggerManager manager = new LoggerManager();
/**
* Retrieve the log manager.<p>
*
* The log manager is a singleton, this method is used to obtain it.
*
* @return the log manager
*/
public static final LoggerManager getLoggerManager()
{
return manager;
}
// Constructors --------------------------------------------------
/**
* Create the log manager singleton.<p>
*
* The singleton will be contructed called when the LoggerManager is first
* loading. This will be either during initial configuration, or
* when the first Logger is created.<p>
*
* When the first call is due Logger creation, there is enough
* implementated in the default classes to get a simple logging
* configuration.
*
* @see DefaultLoggerContextSelector
* @see DefaultLoggerAdapter
*/
private LoggerManager()
{
selector = new DefaultLoggerContextSelector();
}
// Public --------------------------------------------------------
/**
* Create a new logger given a name.<p>
*
* It is common that the name is hierarchical like java class names.
* However this does not mean that the real logger will understand
* or use the hierarchy, or even the name.<p>
*
* @param name the name to assign to the logger, cannot be null
* @return a logger.
*/
public final Logger getLogger(String name)
{
if (name == null)
throw new IllegalArgumentException("null name");
return selector.getLoggerContext().getLogger(name);
}
/**
* Register the adapter factory for the current context
* Any existing loggers are updated to use an adapter from this factory.<p>
*
* WARNING: New loggers cannot be retrieved during this operation.
*
* @param factory the factory creating new adapters
* @exception SecurityException when not authorised
*/
public final void setLoggerAdapterFactory(LoggerAdapterFactory factory)
throws SecurityException
{
selector.getLoggerContext().setLoggerAdapterFactory(factory);
}
/**
* Set the configurator for the current context.<p>
*
* The configurator is implementation dependent.
*
* @param configurator the object used to configure the context
* @exception SecurityException when not authorised
*/
public final void setConfiguration(Object configurator)
throws SecurityException
{
selector.getLoggerContext().setConfiguration(configurator);
}
/**
* Reset the configuration to the default for the current context.
*
* @exception SecurityException when not authorised
*/
public final void resetConfiguration()
throws SecurityException
{
selector.getLoggerContext().resetConfiguration();
}
/**
* Retrieve the list of loggers.
* These are loggers from the current context.<p>
*
* WARNING: The enumeration is NOT synchronized.
*
* @return an Enumeration of loggers
*/
public final Enumeration getLoggers()
{
return selector.getLoggerContext().getLoggers();
}
/**
* Set the logging context selector.<p>
*
* @param selector the new Logging Context Selector, cannot be null
* @exception SecurityException when not authorised
*/
public final void setLoggerContextSelector(LoggerContextSelector seletor)
throws SecurityException
{
if (selector == null)
throw new IllegalArgumentException("null selector");
//TODO AccessController.checkPermission(new LoggerPermission("control"));
this.selector = selector;
}
// X Implementation ----------------------------------------------
// Y Overrides ---------------------------------------------------
// Protected -----------------------------------------------------
// Package Private -----------------------------------------------
/**
* Associate a special logging context with a context object.
*
* @param context the context object cannot be null
* @param loggerContext the logger context cannot be null
*/
void associateContext(Object context, LoggerContext loggerContext)
{
selector.associate(context, loggerContext);
}
/**
* Remove an association of a special logging context with a context object.
*
* @param context the context object to remove cannot be null
*/
void removeContext(Object context)
{
selector.remove(context);
}
// Private -------------------------------------------------------
// Inner classes -------------------------------------------------
}
1.1 jmx/src/main/org/jboss/mx/logging/LoggerPermission.java
Index: LoggerPermission.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mx.logging;
import java.security.BasicPermission;
/**
* The logging permission.<p>
*
* There is only one logging permission "control" that is required to
* to setLevel() on a Logger, setContextSelector() in the LogManager
* and setLoggerAdapterFactory() for a LoggerContext.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>
* @version $Revision: 1.1 $
*/
public final class LoggerPermission
extends BasicPermission
{
// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
/**
* Contruct a logging permission with the given name.
*
* @param name the name of the permission
*/
public LoggerPermission(String name)
throws IllegalArgumentException
{
super(name);
}
// X Implementation ----------------------------------------------
// Y Overrides ---------------------------------------------------
// Protected -----------------------------------------------------
// Package Private -----------------------------------------------
// Private -------------------------------------------------------
// Inner classes -------------------------------------------------
}
1.1 jmx/src/main/org/jboss/mx/logging/NullLoggerAdapter.java
Index: NullLoggerAdapter.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mx.logging;
/**
* The null logger adapter.<p>
*
* This does nothing
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>
* @version $Revision: 1.1 $
*/
public final class NullLoggerAdapter
extends LoggerAdapterSupport
{
// Constants -----------------------------------------------------
// Attributes ----------------------------------------------------
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// LoggerAdapter Implementation ----------------------------------
public final String getName()
{
return "Null";
}
public final int getLevel()
{
return Logger.NONE;
}
public final void setLevel(int level)
{
}
public final void log(int level, String message)
{
}
public final void log(int level, String message, Throwable throwable)
{
}
public final boolean isEnabled(int level)
{
return false;
}
/**
* This is a logger implementation.
*
* @return this logger adapter.
*/
public Object getUnderlyingLogger()
{
return this;
}
// Y Overrides ---------------------------------------------------
// Protected -----------------------------------------------------
// Package Private -----------------------------------------------
// Private -------------------------------------------------------
// Inner classes -------------------------------------------------
}
1.1 jmx/src/main/org/jboss/mx/logging/NullLoggerAdapterFactory.java
Index: NullLoggerAdapterFactory.java
===================================================================
/*
* JBoss, the OpenSource J2EE webOS
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.mx.logging;
/**
* A factory for the null logger.<p>
*
* This has singleton logger that does nothing.
*
* @see NullLoggerAdapter
*
* @author <a href="mailto:[EMAIL PROTECTED]">Adrian Brock</a>
* @version $Revision: 1.1 $
*/
public final class NullLoggerAdapterFactory
extends LoggerAdapterFactorySupport
{
// Constants -----------------------------------------------------
public static final NullLoggerAdapter adapter = new NullLoggerAdapter();
// Attributes ----------------------------------------------------
// Static --------------------------------------------------------
// Constructors --------------------------------------------------
// LoggerAdapterFactory Implementation --------------------------
public final LoggerAdapter getLoggerAdapter(String name)
{
return adapter;
}
public final Class getLoggerAdapterClass()
{
return adapter.getClass();
}
// Y Overrides ---------------------------------------------------
// Protected -----------------------------------------------------
// Package Private -----------------------------------------------
// Private -------------------------------------------------------
// Inner classes -------------------------------------------------
}
1.1 jmx/src/main/org/jboss/mx/logging/package.html
Index: package.html
===================================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<!--
JBoss, the OpenSource J2EE webOS
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published
by the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This library 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
Lesser General Public License for more details.
-->
<!-- $Id: package.html,v 1.1 2002/02/08 21:35:38 ejort Exp $ -->
</head>
<body bgcolor="white">
<p><em>The JBossMX logging framework</em>.
<h2>Package Specification</h2>
<ul>
<li>The logging framework provides a standard interface over a
number of different logging implementations.
</li>
<li>Different parts of an application can be assigned to
different contexts, each context can have its own logging
implementation and/or configuration.
</li>
<li>The logging implementation for each context is "hot-swapable".
</li>
<li>Some simple logging implementations are provided.
</li>
<li>Adapters for the more the common logging implementations are
provided. These include log4j (TODO complete list)
</li>
</ul>
<h2>Related Documentation</h2>
<ul>
<li>TODO: links to logging implementations.
</ul>
<h2>Package Status</h2>
<ul>
<li><font color="red"><b>ALPHA</b></font>
</ul>
<!-- Put @see and @since tags down here. -->
</body>
</html>
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development