mx4j/src/core/mx4j/log CommonsLogger.java,1.1,1.2 FileLogger.java,1.3,1.4 Log.java,1.5,1.6 Log4JLogger.java,1.5,1.6 Logger.java,1.3,1.4 LoggerBroadcaster.java,1.5,1.6 LoggerBroadcasterMBean.java,1.3,1.4 MBeanLogger.java,1.5,1.6
Simone Bordet <[email protected]> Sat, 04 Sep 2004 15:44:10 +0000
| Newsgroups | gmane.comp.java.mx4j.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/mx4j/mx4j/src/core/mx4j/log In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13665/src/core/mx4j/log Modified Files: CommonsLogger.java FileLogger.java Log.java Log4JLogger.java Logger.java LoggerBroadcaster.java LoggerBroadcasterMBean.java MBeanLogger.java Log Message: Updated copyright notice to be: Copyright (C) The MX4J Contributors Index: LoggerBroadcasterMBean.java =================================================================== RCS file: /cvsroot/mx4j/mx4j/src/core/mx4j/log/LoggerBroadcasterMBean.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** LoggerBroadcasterMBean.java 4 Mar 2002 15:52:01 -0000 1.3 --- LoggerBroadcasterMBean.java 4 Sep 2004 15:44:08 -0000 1.4 *************** *** 1,4 **** /* ! * Copyright (C) MX4J. * All rights reserved. * --- 1,4 ---- /* ! * Copyright (C) The MX4J Contributors. * All rights reserved. * *************** *** 9,30 **** package mx4j.log; - import javax.management.NotificationListener; - import javax.management.NotificationFilter; import javax.management.ListenerNotFoundException; /** * Management interface for the LoggerBroadcaster MBean. * - * @see LoggerBroadcaster * @author <a href="mailto:[email protected]">Simone Bordet</a> * @version $Revision$ */ public interface LoggerBroadcasterMBean { ! public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback); ! public void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws ListenerNotFoundException; ! public void start(); ! public void start(String category); ! public void stop(); ! public void stop(String category); } --- 9,35 ---- package mx4j.log; import javax.management.ListenerNotFoundException; + import javax.management.NotificationFilter; + import javax.management.NotificationListener; /** * Management interface for the LoggerBroadcaster MBean. * * @author <a href="mailto:[email protected]">Simone Bordet</a> * @version $Revision$ + * @see LoggerBroadcaster */ public interface LoggerBroadcasterMBean { ! public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback); ! ! public void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws ListenerNotFoundException; ! ! public void start(); ! ! public void start(String category); ! ! public void stop(); ! ! public void stop(String category); } Index: FileLogger.java =================================================================== RCS file: /cvsroot/mx4j/mx4j/src/core/mx4j/log/FileLogger.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** FileLogger.java 4 Mar 2002 15:52:01 -0000 1.3 --- FileLogger.java 4 Sep 2004 15:44:08 -0000 1.4 *************** *** 1,4 **** /* ! * Copyright (C) MX4J. * All rights reserved. * --- 1,4 ---- /* ! * Copyright (C) The MX4J Contributors. * All rights reserved. * *************** *** 19,30 **** public class FileLogger extends Logger { ! public FileLogger(String location) ! { ! // TODO ! } ! protected void log(int priority, Object message, Throwable t) ! { ! // TODO ! } } --- 19,30 ---- public class FileLogger extends Logger { ! public FileLogger(String location) ! { ! // TODO ! } ! protected void log(int priority, Object message, Throwable t) ! { ! // TODO ! } } Index: LoggerBroadcaster.java =================================================================== RCS file: /cvsroot/mx4j/mx4j/src/core/mx4j/log/LoggerBroadcaster.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** LoggerBroadcaster.java 7 Sep 2002 19:54:59 -0000 1.5 --- LoggerBroadcaster.java 4 Sep 2004 15:44:08 -0000 1.6 *************** *** 1,4 **** /* ! * Copyright (C) MX4J. * All rights reserved. * --- 1,4 ---- /* ! * Copyright (C) The MX4J Contributors. * All rights reserved. * *************** *** 9,21 **** package mx4j.log; - import javax.management.NotificationListener; - import javax.management.NotificationFilter; import javax.management.ListenerNotFoundException; import javax.management.MBeanNotificationInfo; import javax.management.Notification; import javax.management.NotificationBroadcasterSupport; ! import javax.management.MBeanRegistration; import javax.management.ObjectName; - import javax.management.MBeanServer; /** --- 9,21 ---- package mx4j.log; import javax.management.ListenerNotFoundException; import javax.management.MBeanNotificationInfo; + import javax.management.MBeanRegistration; + import javax.management.MBeanServer; import javax.management.Notification; import javax.management.NotificationBroadcasterSupport; ! import javax.management.NotificationFilter; ! import javax.management.NotificationListener; import javax.management.ObjectName; /** *************** *** 31,162 **** public class LoggerBroadcaster extends NotificationBroadcasterSupport implements MBeanRegistration, LoggerBroadcasterMBean { ! private long m_sequence; ! private boolean m_registered; ! private int m_recursionLevel; ! public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception ! { ! return name; ! } ! public void postRegister(Boolean registrationDone) ! { ! if (!registrationDone.booleanValue()) {return;} ! m_registered = true; ! } ! public void preDeregister() throws Exception {} ! public void postDeregister() {m_registered = false;} ! public void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws ListenerNotFoundException ! { ! // PENDING: for JMX 1.2 this has to be changed ! super.removeNotificationListener(listener); ! } ! public void sendNotification(Notification n) ! { ! // Since I use a broadcaster to log, it happens that log requests of the broadcaster itself are redirected ! // to the broadcaster, which broadcast to itself generating other log requests, an endless loop. ! // Here I stop reentrant calls: maxRecursionLevel == 1 means that initial log requests are broadcasted, but log ! // requests happening during this broadcast aren't; maxRecursionLevel == 2 means that initial log requests are ! // broadcasted, and also log requests happening during this broadcast, while log requests happening during ! // broadcast of log requests due to broacasting of the initial log request aren't. ! int maxRecursionLevel = 1; ! synchronized (this) ! { ! if (m_recursionLevel < maxRecursionLevel) ! { ! ++m_recursionLevel; ! super.sendNotification(n); ! --m_recursionLevel; ! } ! } ! } ! public void start() ! { ! Logger logger = createLoggerPrototype(); ! Log.redirectTo(logger); ! } ! public void start(String category) ! { ! Logger logger = createLoggerPrototype(); ! Log.redirectTo(logger, category); ! } ! public void stop() ! { ! Log.redirectTo(null); ! } ! public void stop(String category) ! { ! Log.redirectTo(null, category); ! } ! private boolean isRegistered() {return m_registered;} ! public MBeanNotificationInfo[] getNotificationInfo() ! { ! String[] types = new String[] {"mx4j.logger.trace", ! "mx4j.logger.debug", ! "mx4j.logger.info", ! "mx4j.logger.warn", ! "mx4j.logger.error", ! "mx4j.logger.fatal"}; ! MBeanNotificationInfo notifs = new MBeanNotificationInfo(types, "javax.management.Notification", "MX4J Logger MBean notifications"); ! return new MBeanNotificationInfo[] {notifs}; ! } ! protected Logger createLoggerPrototype() ! { ! return new LoggerNotifier(this); ! } ! public static class LoggerNotifier extends Logger ! { ! private static LoggerBroadcaster m_loggerBroadcaster; ! private LoggerNotifier(LoggerBroadcaster mbean) {m_loggerBroadcaster = mbean;} ! public LoggerNotifier() {} ! protected void log(int priority, Object message, Throwable t) ! { ! // Notify listeners ! notify(priority, message, t); ! } ! private void notify(int priority, Object message, Throwable t) ! { ! if (m_loggerBroadcaster.isRegistered()) ! { ! long sequence = 0; ! synchronized (this) {sequence = ++m_loggerBroadcaster.m_sequence;} ! String type = null; ! switch (priority) ! { ! case TRACE: type = "mx4j.logger.trace"; break; ! case DEBUG: type = "mx4j.logger.debug"; break; ! case INFO: type = "mx4j.logger.info"; break; ! case WARN: type = "mx4j.logger.warn"; break; ! case ERROR: type = "mx4j.logger.error"; break; ! case FATAL: type = "mx4j.logger.fatal"; break; ! default: type = "mx4j.logger." + priority; break; ! } ! String msg = message == null ? "" : message.toString(); ! // TODO: the source must be the object name of the MBean if the listener was registered through MBeanServer ! Notification n = new Notification(type, this, sequence, msg); ! if (t != null) ! { ! n.setUserData(t); ! } ! m_loggerBroadcaster.sendNotification(n); ! } ! } ! } } --- 31,198 ---- public class LoggerBroadcaster extends NotificationBroadcasterSupport implements MBeanRegistration, LoggerBroadcasterMBean { ! private long m_sequence; ! private boolean m_registered; ! private int m_recursionLevel; ! public ObjectName preRegister(MBeanServer server, ObjectName name) throws Exception ! { ! return name; ! } ! public void postRegister(Boolean registrationDone) ! { ! if (!registrationDone.booleanValue()) ! { ! return; ! } ! m_registered = true; ! } ! public void preDeregister() throws Exception ! { ! } ! public void postDeregister() ! { ! m_registered = false; ! } ! public void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws ListenerNotFoundException ! { ! // PENDING: for JMX 1.2 this has to be changed ! super.removeNotificationListener(listener); ! } ! public void sendNotification(Notification n) ! { ! // Since I use a broadcaster to log, it happens that log requests of the broadcaster itself are redirected ! // to the broadcaster, which broadcast to itself generating other log requests, an endless loop. ! // Here I stop reentrant calls: maxRecursionLevel == 1 means that initial log requests are broadcasted, but log ! // requests happening during this broadcast aren't; maxRecursionLevel == 2 means that initial log requests are ! // broadcasted, and also log requests happening during this broadcast, while log requests happening during ! // broadcast of log requests due to broacasting of the initial log request aren't. ! int maxRecursionLevel = 1; ! synchronized (this) ! { ! if (m_recursionLevel < maxRecursionLevel) ! { ! ++m_recursionLevel; ! super.sendNotification(n); ! --m_recursionLevel; ! } ! } ! } ! public void start() ! { ! Logger logger = createLoggerPrototype(); ! Log.redirectTo(logger); ! } ! public void start(String category) ! { ! Logger logger = createLoggerPrototype(); ! Log.redirectTo(logger, category); ! } ! public void stop() ! { ! Log.redirectTo(null); ! } ! public void stop(String category) ! { ! Log.redirectTo(null, category); ! } ! private boolean isRegistered() ! { ! return m_registered; ! } ! public MBeanNotificationInfo[] getNotificationInfo() ! { ! String[] types = new String[]{"mx4j.logger.trace", ! "mx4j.logger.debug", ! "mx4j.logger.info", ! "mx4j.logger.warn", ! "mx4j.logger.error", ! "mx4j.logger.fatal"}; ! MBeanNotificationInfo notifs = new MBeanNotificationInfo(types, "javax.management.Notification", "MX4J Logger MBean notifications"); ! return new MBeanNotificationInfo[]{notifs}; ! } ! protected Logger createLoggerPrototype() ! { ! return new LoggerNotifier(this); ! } ! public static class LoggerNotifier extends Logger ! { ! private static LoggerBroadcaster m_loggerBroadcaster; ! private LoggerNotifier(LoggerBroadcaster mbean) ! { ! m_loggerBroadcaster = mbean; ! } ! public LoggerNotifier() ! { ! } ! protected void log(int priority, Object message, Throwable t) ! { ! // Notify listeners ! notify(priority, message, t); ! } ! private void notify(int priority, Object message, Throwable t) ! { ! if (m_loggerBroadcaster.isRegistered()) ! { ! long sequence = 0; ! synchronized (this) ! { ! sequence = ++m_loggerBroadcaster.m_sequence; ! } ! String type = null; ! switch (priority) ! { ! case TRACE: ! type = "mx4j.logger.trace"; ! break; ! case DEBUG: ! type = "mx4j.logger.debug"; ! break; ! case INFO: ! type = "mx4j.logger.info"; ! break; ! case WARN: ! type = "mx4j.logger.warn"; ! break; ! case ERROR: ! type = "mx4j.logger.error"; ! break; ! case FATAL: ! type = "mx4j.logger.fatal"; ! break; ! default: ! type = "mx4j.logger." + priority; ! break; ! } ! ! String msg = message == null ? "" : message.toString(); ! ! // TODO: the source must be the object name of the MBean if the listener was registered through MBeanServer ! Notification n = new Notification(type, this, sequence, msg); ! if (t != null) ! { ! n.setUserData(t); ! } ! ! m_loggerBroadcaster.sendNotification(n); ! } ! } ! } } Index: CommonsLogger.java =================================================================== RCS file: /cvsroot/mx4j/mx4j/src/core/mx4j/log/CommonsLogger.java,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** CommonsLogger.java 29 Aug 2003 13:01:28 -0000 1.1 --- CommonsLogger.java 4 Sep 2004 15:44:08 -0000 1.2 *************** *** 1,4 **** /* ! * Copyright (C) MX4J. * All rights reserved. * --- 1,4 ---- /* ! * Copyright (C) The MX4J Contributors. * All rights reserved. * Index: Logger.java =================================================================== RCS file: /cvsroot/mx4j/mx4j/src/core/mx4j/log/Logger.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** Logger.java 4 Mar 2002 15:52:01 -0000 1.3 --- Logger.java 4 Sep 2004 15:44:08 -0000 1.4 *************** *** 1,4 **** /* ! * Copyright (C) MX4J. * All rights reserved. * --- 1,4 ---- /* ! * Copyright (C) The MX4J Contributors. * All rights reserved. * *************** *** 17,66 **** public class Logger { ! public static final int TRACE = 0; ! public static final int DEBUG = TRACE + 10; ! public static final int INFO = DEBUG + 10; ! public static final int WARN = INFO + 10; ! public static final int ERROR = WARN + 10; ! public static final int FATAL = ERROR + 10; ! private int m_priority = WARN; ! private String m_category; ! protected Logger() {} ! public void setPriority(int priority) {m_priority = priority;} ! public int getPriority() {return m_priority;} ! public String getCategory() {return m_category;} ! protected void setCategory(String category) {m_category = category;} ! public final boolean isEnabledFor(int priority) ! { ! return priority >= getPriority(); ! } ! public final void fatal(Object message) {log(FATAL, message, null);} ! public final void fatal(Object message, Throwable t) {log(FATAL, message, t);} ! public final void error(Object message) {log(ERROR, message, null);} ! public final void error(Object message, Throwable t) {log(ERROR, message, t);} ! public final void warn(Object message) {log(WARN, message, null);} ! public final void warn(Object message, Throwable t) {log(WARN, message, t);} ! public final void info(Object message) {log(INFO, message, null);} ! public final void info(Object message, Throwable t) {log(INFO, message, t);} ! public final void debug(Object message) {log(DEBUG, message, null);} ! public final void debug(Object message, Throwable t) {log(DEBUG, message, t);} ! public final void trace(Object message) {log(TRACE, message, null);} ! public final void trace(Object message, Throwable t) {log(TRACE, message, t);} ! protected void log(int priority, Object message, Throwable t) ! { ! if (isEnabledFor(priority)) ! { ! System.out.println(message); ! if (t != null) ! { ! t.printStackTrace(System.out); ! } ! } ! } } --- 17,129 ---- public class Logger { ! public static final int TRACE = 0; ! public static final int DEBUG = TRACE + 10; ! public static final int INFO = DEBUG + 10; ! public static final int WARN = INFO + 10; ! public static final int ERROR = WARN + 10; ! public static final int FATAL = ERROR + 10; ! private int m_priority = WARN; ! private String m_category; ! protected Logger() ! { ! } ! public void setPriority(int priority) ! { ! m_priority = priority; ! } ! public int getPriority() ! { ! return m_priority; ! } ! public String getCategory() ! { ! return m_category; ! } ! protected void setCategory(String category) ! { ! m_category = category; ! } ! public final boolean isEnabledFor(int priority) ! { ! return priority >= getPriority(); ! } ! ! public final void fatal(Object message) ! { ! log(FATAL, message, null); ! } ! ! public final void fatal(Object message, Throwable t) ! { ! log(FATAL, message, t); ! } ! ! public final void error(Object message) ! { ! log(ERROR, message, null); ! } ! ! public final void error(Object message, Throwable t) ! { ! log(ERROR, message, t); ! } ! ! public final void warn(Object message) ! { ! log(WARN, message, null); ! } ! ! public final void warn(Object message, Throwable t) ! { ! log(WARN, message, t); ! } ! ! public final void info(Object message) ! { ! log(INFO, message, null); ! } ! ! public final void info(Object message, Throwable t) ! { ! log(INFO, message, t); ! } ! ! public final void debug(Object message) ! { ! log(DEBUG, message, null); ! } ! ! public final void debug(Object message, Throwable t) ! { ! log(DEBUG, message, t); ! } ! ! public final void trace(Object message) ! { ! log(TRACE, message, null); ! } ! ! public final void trace(Object message, Throwable t) ! { ! log(TRACE, message, t); ! } ! ! protected void log(int priority, Object message, Throwable t) ! { ! if (isEnabledFor(priority)) ! { ! System.out.println(message); ! if (t != null) ! { ! t.printStackTrace(System.out); ! } ! } ! } } Index: Log4JLogger.java =================================================================== RCS file: /cvsroot/mx4j/mx4j/src/core/mx4j/log/Log4JLogger.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** Log4JLogger.java 27 May 2004 01:12:10 -0000 1.5 --- Log4JLogger.java 4 Sep 2004 15:44:08 -0000 1.6 *************** *** 1,4 **** /* ! * Copyright (C) MX4J. * All rights reserved. * --- 1,4 ---- /* ! * Copyright (C) The MX4J Contributors. * All rights reserved. * *************** *** 16,63 **** * Log that redirects log calls to a Log4J Logger. <p> * - * @see org.apache.log4j.Logger at <a href="http://jakarta.apache.org/log4j">Apache's Log4J</a> * @author <a href="mailto:[email protected]">Simone Bordet</a> * @author <a href="mailto:[email protected]">Brian Scully</a> * @version $Revision$ */ public class Log4JLogger extends mx4j.log.Logger { ! private Logger m_logger; ! /** ! * This constructor is called to create instances of this Logger, following the prototype pattern. ! */ ! public Log4JLogger() {} ! protected void setCategory(String category) ! { ! super.setCategory(category); ! m_logger = Logger.getLogger(getCategory()); ! } ! protected void log(int priority, Object message, Throwable t) ! { ! // Convert MX4J priority to log4j priority ! Level l = convertPriority(priority); ! m_logger.log(l, message, t); ! } ! /** ! * Converts MX4J priority to Log4J priority ! */ ! protected Level convertPriority(int mx4jPriority) ! { ! Level log4jPriority = Level.DEBUG; ! switch (mx4jPriority) ! { ! case mx4j.log.Logger.FATAL: log4jPriority = Level.FATAL; break; ! case mx4j.log.Logger.ERROR: log4jPriority = Level.ERROR; break; ! case mx4j.log.Logger.WARN: log4jPriority = Level.WARN; break; ! case mx4j.log.Logger.INFO: log4jPriority = Level.INFO; break; ! case mx4j.log.Logger.DEBUG: log4jPriority = Level.DEBUG; break; ! case mx4j.log.Logger.TRACE: log4jPriority = Level.DEBUG; break; ! default: log4jPriority = Level.INFO; break; ! } ! return log4jPriority; ! } } --- 16,79 ---- * Log that redirects log calls to a Log4J Logger. <p> * * @author <a href="mailto:[email protected]">Simone Bordet</a> * @author <a href="mailto:[email protected]">Brian Scully</a> * @version $Revision$ + * @see org.apache.log4j.Logger at <a href="http://jakarta.apache.org/log4j">Apache's Log4J</a> */ public class Log4JLogger extends mx4j.log.Logger { ! private Logger m_logger; ! /** ! * This constructor is called to create instances of this Logger, following the prototype pattern. ! */ ! public Log4JLogger() ! { ! } ! protected void setCategory(String category) ! { ! super.setCategory(category); ! m_logger = Logger.getLogger(getCategory()); ! } ! protected void log(int priority, Object message, Throwable t) ! { ! // Convert MX4J priority to log4j priority ! Level l = convertPriority(priority); ! m_logger.log(l, message, t); ! } ! /** ! * Converts MX4J priority to Log4J priority ! */ ! protected Level convertPriority(int mx4jPriority) ! { ! Level log4jPriority = Level.DEBUG; ! switch (mx4jPriority) ! { ! case mx4j.log.Logger.FATAL: ! log4jPriority = Level.FATAL; ! break; ! case mx4j.log.Logger.ERROR: ! log4jPriority = Level.ERROR; ! break; ! case mx4j.log.Logger.WARN: ! log4jPriority = Level.WARN; ! break; ! case mx4j.log.Logger.INFO: ! log4jPriority = Level.INFO; ! break; ! case mx4j.log.Logger.DEBUG: ! log4jPriority = Level.DEBUG; ! break; ! case mx4j.log.Logger.TRACE: ! log4jPriority = Level.DEBUG; ! break; ! default: ! log4jPriority = Level.INFO; ! break; ! } ! return log4jPriority; ! } } Index: Log.java =================================================================== RCS file: /cvsroot/mx4j/mx4j/src/core/mx4j/log/Log.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** Log.java 29 Aug 2003 13:06:59 -0000 1.5 --- Log.java 4 Sep 2004 15:44:08 -0000 1.6 *************** *** 1,4 **** /* ! * Copyright (C) MX4J. * All rights reserved. * --- 1,4 ---- /* ! * Copyright (C) The MX4J Contributors. * All rights reserved. * *************** *** 11,17 **** import java.security.AccessController; import java.security.PrivilegedAction; - import java.util.Map; import java.util.HashMap; ! import javax.management.RuntimeOperationsException; --- 11,16 ---- import java.security.AccessController; import java.security.PrivilegedAction; import java.util.HashMap; ! import java.util.Map; import javax.management.RuntimeOperationsException; *************** *** 36,233 **** public class Log { ! private static Logger m_prototype; ! private static Map m_prototypeMap = new HashMap(); ! private static Map m_loggerCache = new HashMap(); ! private static int m_defaultPriority; ! static ! { ! // Do not require callers up in the stack to have this permission ! String priority = (String)AccessController.doPrivileged(new PrivilegedAction() ! { ! public Object run() ! { ! return System.getProperty(MX4JSystemKeys.MX4J_LOG_PRIORITY); ! } ! }); ! if ("trace".equalsIgnoreCase(priority)) {m_defaultPriority = Logger.TRACE;} ! else if ("debug".equalsIgnoreCase(priority)) {m_defaultPriority = Logger.DEBUG;} ! else if ("info".equalsIgnoreCase(priority)) {m_defaultPriority = Logger.INFO;} ! else if ("warn".equalsIgnoreCase(priority)) {m_defaultPriority = Logger.WARN;} ! else if ("error".equalsIgnoreCase(priority)) {m_defaultPriority = Logger.ERROR;} ! else if ("fatal".equalsIgnoreCase(priority)) {m_defaultPriority = Logger.FATAL;} ! else {m_defaultPriority = Logger.INFO;} ! String prototype = (String)AccessController.doPrivileged(new PrivilegedAction() ! { ! public Object run() ! { ! return System.getProperty(MX4JSystemKeys.MX4J_LOG_PROTOTYPE); ! } ! }); ! if (prototype != null && prototype.trim().length() > 0) ! { ! try ! { ! ClassLoader cl = Thread.currentThread().getContextClassLoader(); ! Class cls = cl.loadClass(prototype); ! redirectTo((Logger)cls.newInstance()); ! } ! catch (Exception x) ! { ! x.printStackTrace(); ! // Do nothing else: the user will see the exception trace ! // and understand that the property was wrong ! } ! } ! } ! private Log() {} ! /** ! * Sets the default priority for all loggers. ! * @see #setDefaultPriority ! */ ! public static void setDefaultPriority(int priority) ! { ! switch (priority) ! { ! case Logger.TRACE: m_defaultPriority = Logger.TRACE; break; ! case Logger.DEBUG: m_defaultPriority = Logger.DEBUG; break; ! case Logger.INFO: m_defaultPriority = Logger.INFO; break; ! case Logger.WARN: m_defaultPriority = Logger.WARN; break; ! case Logger.ERROR: m_defaultPriority = Logger.ERROR; break; ! case Logger.FATAL: m_defaultPriority = Logger.FATAL; break; ! default: m_defaultPriority = Logger.WARN; break; ! } ! } ! /** ! * Returns the default priority. ! * @see #setDefaultPriority ! */ ! public static int getDefaultPriority() ! { ! return m_defaultPriority; ! } ! /** ! * Returns a new instance of a Logger associated with the given <code>category</code>; ! * if {@link #redirectTo} has been called then a new instance of the prototype Logger, associated with the given ! * <code>category<code>, is returned. This requires the prototype Logger class to have a public parameterless ! * constructor. ! */ ! public static Logger getLogger(String category) ! { ! if (category == null) {throw new RuntimeOperationsException(new IllegalArgumentException("Category cannot be null"));} ! synchronized (m_loggerCache) ! { ! Logger logger = (Logger)m_loggerCache.get(category); ! if (logger == null) ! { ! // Try to see if a delegate for this category overrides other settings ! Logger prototype = null; ! synchronized (m_prototypeMap) ! { ! prototype = (Logger)m_prototypeMap.get(category); ! } ! if (prototype == null) ! { ! // Try to see if a prototype for all categories has been set ! if (m_prototype != null) ! { ! logger = createLogger(m_prototype, category); ! } ! else ! { ! logger = createLogger(null, category); ! } ! } ! else ! { ! logger = createLogger(prototype, category); ! } ! // cache it ! m_loggerCache.put(category, logger); ! } ! return logger; ! } ! } ! private static Logger createLogger(Logger prototype, String category) ! { ! Logger logger = null; ! try ! { ! logger = prototype == null ? new Logger() : (Logger)prototype.getClass().newInstance(); ! } ! catch (Exception x) ! { ! x.printStackTrace(); ! logger = new Logger(); ! } ! logger.setCategory(category); ! logger.setPriority(m_defaultPriority); ! return logger; ! } ! /** ! * Tells to the log service to use the given <code>delegate</code> Logger to perform logging. <br> ! * Use a null delegate to remove redirection. ! * @see #getLogger ! */ ! public static void redirectTo(Logger prototype) ! { ! m_prototype = prototype; ! // Clear the cache, as we want requests for new loggers to be generated again. ! synchronized (m_loggerCache) ! { ! m_loggerCache.clear(); ! } ! } ! /** ! * Tells to the log service to use the given <code>delegate</code> Logger to perform logging for the given ! * category (that cannot be null). <br> ! * Settings made using this method overrides the ones made with {@link #redirectTo(Logger) redirectTo}, meaning ! * that it is possible to redirect all the log to a certain delegate but certain categories. ! * Use a null delegate to remove redirection for the specified category. ! * @see #getLogger ! */ ! public static void redirectTo(Logger prototype, String category) ! { ! if (category == null) {throw new RuntimeOperationsException(new IllegalArgumentException("Category cannot be null"));} ! if (prototype == null) ! { ! // Remove the redirection ! synchronized (m_prototypeMap) ! { ! m_prototypeMap.remove(category); ! } ! // Clear the cache for this category ! synchronized (m_loggerCache) ! { ! m_loggerCache.remove(category); ! } ! } ! else ! { ! // Put or replace ! synchronized (m_prototypeMap) ! { ! m_prototypeMap.put(category, prototype); ! } ! // Clear the cache for this category ! synchronized (m_loggerCache) ! { ! m_loggerCache.remove(category); ! } ! } ! } } --- 35,279 ---- public class Log { ! private static Logger m_prototype; ! private static Map m_prototypeMap = new HashMap(); ! private static Map m_loggerCache = new HashMap(); ! private static int m_defaultPriority; ! static ! { ! // Do not require callers up in the stack to have this permission ! String priority = (String)AccessController.doPrivileged(new PrivilegedAction() ! { ! public Object run() ! { ! return System.getProperty(MX4JSystemKeys.MX4J_LOG_PRIORITY); ! } ! }); ! if ("trace".equalsIgnoreCase(priority)) ! { ! m_defaultPriority = Logger.TRACE; ! } ! else if ("debug".equalsIgnoreCase(priority)) ! { ! m_defaultPriority = Logger.DEBUG; ! } ! else if ("info".equalsIgnoreCase(priority)) ! { ! m_defaultPriority = Logger.INFO; ! } ! else if ("warn".equalsIgnoreCase(priority)) ! { ! m_defaultPriority = Logger.WARN; ! } ! else if ("error".equalsIgnoreCase(priority)) ! { ! m_defaultPriority = Logger.ERROR; ! } ! else if ("fatal".equalsIgnoreCase(priority)) ! { ! m_defaultPriority = Logger.FATAL; ! } ! else ! { ! m_defaultPriority = Logger.INFO; ! } ! String prototype = (String)AccessController.doPrivileged(new PrivilegedAction() ! { ! public Object run() ! { ! return System.getProperty(MX4JSystemKeys.MX4J_LOG_PROTOTYPE); ! } ! }); ! if (prototype != null && prototype.trim().length() > 0) ! { ! try ! { ! ClassLoader cl = Thread.currentThread().getContextClassLoader(); ! Class cls = cl.loadClass(prototype); ! redirectTo((Logger)cls.newInstance()); ! } ! catch (Exception x) ! { ! x.printStackTrace(); ! // Do nothing else: the user will see the exception trace ! // and understand that the property was wrong ! } ! } ! } ! private Log() ! { ! } ! /** ! * Sets the default priority for all loggers. ! * ! * @see #setDefaultPriority ! */ ! public static void setDefaultPriority(int priority) ! { ! switch (priority) ! { ! case Logger.TRACE: ! m_defaultPriority = Logger.TRACE; ! break; ! case Logger.DEBUG: ! m_defaultPriority = Logger.DEBUG; ! break; ! case Logger.INFO: ! m_defaultPriority = Logger.INFO; ! break; ! case Logger.WARN: ! m_defaultPriority = Logger.WARN; ! break; ! case Logger.ERROR: ! m_defaultPriority = Logger.ERROR; ! break; ! case Logger.FATAL: ! m_defaultPriority = Logger.FATAL; ! break; ! default: ! m_defaultPriority = Logger.WARN; ! break; ! } ! } ! /** ! * Returns the default priority. ! * ! * @see #setDefaultPriority ! */ ! public static int getDefaultPriority() ! { ! return m_defaultPriority; ! } ! /** ! * Returns a new instance of a Logger associated with the given <code>category</code>; ! * if {@link #redirectTo} has been called then a new instance of the prototype Logger, associated with the given ! * <code>category<code>, is returned. This requires the prototype Logger class to have a public parameterless ! * constructor. ! */ ! public static Logger getLogger(String category) ! { ! if (category == null) ! { ! throw new RuntimeOperationsException(new IllegalArgumentException("Category cannot be null")); ! } ! synchronized (m_loggerCache) ! { ! Logger logger = (Logger)m_loggerCache.get(category); ! if (logger == null) ! { ! // Try to see if a delegate for this category overrides other settings ! Logger prototype = null; ! synchronized (m_prototypeMap) ! { ! prototype = (Logger)m_prototypeMap.get(category); ! } ! if (prototype == null) ! { ! // Try to see if a prototype for all categories has been set ! if (m_prototype != null) ! { ! logger = createLogger(m_prototype, category); ! } ! else ! { ! logger = createLogger(null, category); ! } ! } ! else ! { ! logger = createLogger(prototype, category); ! } ! // cache it ! m_loggerCache.put(category, logger); ! } ! return logger; ! } ! } ! private static Logger createLogger(Logger prototype, String category) ! { ! Logger logger = null; ! try ! { ! logger = prototype == null ? new Logger() : (Logger)prototype.getClass().newInstance(); ! } ! catch (Exception x) ! { ! x.printStackTrace(); ! logger = new Logger(); ! } ! logger.setCategory(category); ! logger.setPriority(m_defaultPriority); ! return logger; ! } ! /** ! * Tells to the log service to use the given <code>delegate</code> Logger to perform logging. <br> ! * Use a null delegate to remove redirection. ! * ! * @see #getLogger ! */ ! public static void redirectTo(Logger prototype) ! { ! m_prototype = prototype; ! // Clear the cache, as we want requests for new loggers to be generated again. ! synchronized (m_loggerCache) ! { ! m_loggerCache.clear(); ! } ! } ! /** ! * Tells to the log service to use the given <code>delegate</code> Logger to perform logging for the given ! * category (that cannot be null). <br> ! * Settings made using this method overrides the ones made with {@link #redirectTo(Logger) redirectTo}, meaning ! * that it is possible to redirect all the log to a certain delegate but certain categories. ! * Use a null delegate to remove redirection for the specified category. ! * ! * @see #getLogger ! */ ! public static void redirectTo(Logger prototype, String category) ! { ! if (category == null) ! { ! throw new RuntimeOperationsException(new IllegalArgumentException("Category cannot be null")); ! } ! if (prototype == null) ! { ! // Remove the redirection ! synchronized (m_prototypeMap) ! { ! m_prototypeMap.remove(category); ! } ! // Clear the cache for this category ! synchronized (m_loggerCache) ! { ! m_loggerCache.remove(category); ! } ! } ! else ! { ! // Put or replace ! synchronized (m_prototypeMap) ! { ! m_prototypeMap.put(category, prototype); ! } ! // Clear the cache for this category ! synchronized (m_loggerCache) ! { ! m_loggerCache.remove(category); ! } ! } ! } } Index: MBeanLogger.java =================================================================== RCS file: /cvsroot/mx4j/mx4j/src/core/mx4j/log/MBeanLogger.java,v retrieving revision 1.5 retrieving revision 1.6 diff -C2 -d -r1.5 -r1.6 *** MBeanLogger.java 7 Sep 2002 19:54:59 -0000 1.5 --- MBeanLogger.java 4 Sep 2004 15:44:08 -0000 1.6 *************** *** 1,4 **** /* ! * Copyright (C) MX4J. * All rights reserved. * --- 1,4 ---- /* ! * Copyright (C) The MX4J Contributors. * All rights reserved. * *************** *** 9,18 **** package mx4j.log; - import javax.management.MBeanServer; - import javax.management.ObjectName; import javax.management.MBeanException; import javax.management.MBeanInfo; import javax.management.MBeanOperationInfo; import javax.management.MBeanParameterInfo; import javax.management.ServiceNotFoundException; --- 9,18 ---- package mx4j.log; import javax.management.MBeanException; import javax.management.MBeanInfo; import javax.management.MBeanOperationInfo; import javax.management.MBeanParameterInfo; + import javax.management.MBeanServer; + import javax.management.ObjectName; import javax.management.ServiceNotFoundException; *************** *** 30,88 **** public class MBeanLogger extends Logger { ! private MBeanServer m_server; ! private ObjectName m_name; ! public MBeanLogger(MBeanServer server, ObjectName objectName) throws MBeanException ! { ! if (server == null) {throw new MBeanException(new IllegalArgumentException("MBeanServer cannot be null"));} ! if (objectName == null) {throw new MBeanException(new IllegalArgumentException("ObjectName cannot be null"));} ! m_server = server; ! m_name = objectName; ! boolean found = false; ! try ! { ! MBeanInfo info = m_server.getMBeanInfo(m_name); ! MBeanOperationInfo[] opers = info.getOperations(); ! if (opers != null) ! { ! for (int i = 0; i < opers.length; ++i) ! { ! MBeanOperationInfo oper = opers[i]; ! if (oper.getName().equals("log")) ! { ! MBeanParameterInfo[] params = oper.getSignature(); ! if (params.length == 3) ! { ! if (params[0].getType().equals("int") && ! params[1].getType().equals("java.lang.Object") && ! params[2].getType().equals("java.lang.Throwable")) ! { ! found = true; ! break; ! } ! } ! } ! } ! } ! } ! catch (Exception x) ! { ! x.printStackTrace(); ! } ! if (!found) {throw new MBeanException(new ServiceNotFoundException("MBean does not have an operation log(int,Object,Throwable)"));} ! } ! protected void log(int priority, Object message, Throwable t) ! { ! try ! { ! m_server.invoke(m_name, "log", new Object[] {new Integer(priority), message, t}, new String[] {"int", "java.lang.Object", "java.lang.Throwable"}); ! } ! catch (Exception x) ! { ! x.printStackTrace(); ! } ! } } --- 30,97 ---- public class MBeanLogger extends Logger { ! private MBeanServer m_server; ! private ObjectName m_name; ! public MBeanLogger(MBeanServer server, ObjectName objectName) throws MBeanException ! { ! if (server == null) ! { ! throw new MBeanException(new IllegalArgumentException("MBeanServer cannot be null")); ! } ! if (objectName == null) ! { ! throw new MBeanException(new IllegalArgumentException("ObjectName cannot be null")); ! } ! m_server = server; ! m_name = objectName; ! boolean found = false; ! try ! { ! MBeanInfo info = m_server.getMBeanInfo(m_name); ! MBeanOperationInfo[] opers = info.getOperations(); ! if (opers != null) ! { ! for (int i = 0; i < opers.length; ++i) ! { ! MBeanOperationInfo oper = opers[i]; ! if (oper.getName().equals("log")) ! { ! MBeanParameterInfo[] params = oper.getSignature(); ! if (params.length == 3) ! { ! if (params[0].getType().equals("int") && ! params[1].getType().equals("java.lang.Object") && ! params[2].getType().equals("java.lang.Throwable")) ! { ! found = true; ! break; ! } ! } ! } ! } ! } ! } ! catch (Exception x) ! { ! x.printStackTrace(); ! } ! if (!found) ! { ! throw new MBeanException(new ServiceNotFoundException("MBean does not have an operation log(int,Object,Throwable)")); ! } ! } ! protected void log(int priority, Object message, Throwable t) ! { ! try ! { ! m_server.invoke(m_name, "log", new Object[]{new Integer(priority), message, t}, new String[]{"int", "java.lang.Object", "java.lang.Throwable"}); ! } ! catch (Exception x) ! { ! x.printStackTrace(); ! } ! } } ------------------------------------------------------- This SF.Net email is sponsored by BEA Weblogic Workshop FREE Java Enterprise J2EE developer tools! Get your free copy of BEA WebLogic Workshop 8.1 today. http://ads.osdn.com/?ad_id=5047&alloc_id=10808&op=click