mwomack 2002/12/10 23:25:14 Added: src/java/org/apache/log4j/plugins Receiver.java PluginSkeleton.java PluginRegistry.java Plugin.java Log: Moved from o.a.log4j to o.a.log4j.plugins package. Fixed bug in PluginRegistry where stopped plugin was not removed from plugin map. Revision Changes Path 1.1 jakarta-log4j/src/java/org/apache/log4j/plugins/Receiver.java Index: Receiver.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software * License version 1.1, a copy of which has been included with this * distribution in the LICENSE.txt file. */ package org.apache.log4j.plugins; import org.apache.log4j.Logger; import org.apache.log4j.spi.LoggingEvent; /** Defines the base class for Receiver plugins. <p>Just as Appenders send logging events outside of the log4j environment (to files, to smtp, to sockets, etc), Receivers bring logging events inside the log4j environment. <p>Receivers are primarily meant to support the receiving of remote logging events from another process. For example, SocketAppender "appends" a logging event to a socket, configured for a specific host and port number. On the receiving side of the socket can be a SocketReceiver object. The SocketReceiver object receives the logging event, and then "posts" it to the log4j environment (LoggerRepository) on the receiving machine, to be handled by the configured appenders, etc. The various settings in this environment (Logger levels, Appender filters & thresholds) are applied to the received logging event. <p>Receivers can also be used to "import" log messages from other logging packages into the log4j environment. <p>Receivers can be configured to post events to a given LoggerRepository. If a repository is not specified, then the default repository value returned by LogManager.getLoggerRepository() should be used. <p>Subclasses of Receiver must implement the isActive(), activateOptions(), and shutdown() methods. The doPost() method is provided to standardize the "import" of remote events into the repository. @author Mark Womack @since 1.3 */ public abstract class Receiver extends PluginSkeleton { /** Posts the logging event to a logger in the configured logger repository. */ public void doPost(LoggingEvent event) { // get the "local" logger for this event from the // configured repository. Logger localLogger = getLoggerRepository().getLogger(event.getLoggerName()); // if the logger level is greater or equal to the level // of the event, use the logger to append the event. if(event.getLevel().isGreaterOrEqual(localLogger.getEffectiveLevel())) { // set the logger for the event // event.logger = localLogger; // call the loggers appenders to process the event localLogger.callAppenders(event); } } } 1.1 jakarta-log4j/src/java/org/apache/log4j/plugins/PluginSkeleton.java Index: PluginSkeleton.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software * License version 1.1, a copy of which has been included with this * distribution in the LICENSE.txt file. */ package org.apache.log4j.plugins; import org.apache.log4j.LogManager; import org.apache.log4j.spi.LoggerRepository; /** A convienent abstract class for receiver classes that implements the basic methods of the Plugin interface. Subclasses are required to implement the isActive(), activateOptions(), and shutdown() methods. <p>Developers are not required to subclass PluginSkeleton to develop their own plugins (they are only required to implement the Plugin interface), but it provides a convienent base class to start from. Contributors: Nicko Cadell @author Mark Womack @since 1.3 */ public abstract class PluginSkeleton implements Plugin { protected String name = ""; protected LoggerRepository repository; /** Gets the name of the plugin. */ public String getName() { return name; } /** Sets the name of the plugin. */ public void setName(String _name) { name = _name; } /** Gets the logger repository for this plugin. If not explicity set, returns the value of LogManager.getLoggerRepository(). */ public LoggerRepository getLoggerRepository() { if (repository != null) { return repository; } else { return LogManager.getLoggerRepository(); } } /** Sets the logger repository used by this plugin. This repository will be used by the plugin functionality. */ public void setLoggerRepository(LoggerRepository _repository) { repository = _repository; } } 1.1 jakarta-log4j/src/java/org/apache/log4j/plugins/PluginRegistry.java Index: PluginRegistry.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software * License version 1.1, a copy of which has been included with this * distribution in the LICENSE.txt file. */ package org.apache.log4j.plugins; import java.util.Hashtable; import java.util.Enumeration; import org.apache.log4j.LogManager; import org.apache.log4j.spi.LoggerRepository; import org.apache.log4j.spi.LoggerRepositoryEventListener; /** This is a registry for Plugin instances. It provides methods to start and stop plugin objects individually and to stop all plugins for a repository. @author Mark Womack @since 1.3 */ public class PluginRegistry { private static Hashtable repositoryMap = new Hashtable(); private static RepositoryListener listener = new RepositoryListener(); /** Starts a Plugin with default logger repository. */ public static Plugin startPlugin(Plugin plugin) { return startPlugin(plugin, LogManager.getLoggerRepository()); } /** Starts a plugin with a given logger repository. */ public static Plugin startPlugin(Plugin plugin, LoggerRepository repository) { // make sure the plugin has reference to repository plugin.setLoggerRepository(repository); // put plugin into the repository's reciever map synchronized(repositoryMap) { // get plugin map for repository Hashtable pluginMap = (Hashtable)repositoryMap.get(repository); // if the plugin map does not exist, create one if (pluginMap == null) { pluginMap = new Hashtable(); repositoryMap.put(repository, pluginMap); repository.addLoggerRepositoryEventListener(listener); } // existing plugin exists with the String name = plugin.getName(); if (name == null) { name = ""; } Plugin existingPlugin = (Plugin)pluginMap.get(name); if (existingPlugin != null) { boolean isEqual = existingPlugin.equals(plugin); // if the plugins are equivalent and the existing one // is still active, just return the existing one now if (isEqual && existingPlugin.isActive()) { return existingPlugin; } else { existingPlugin.shutdown(); } } // put the new plugin into the map pluginMap.put(name, plugin); // start the new plugin plugin.activateOptions(); return plugin; } } /** Stops a plugin in the default logger repository. */ public static Plugin stopPlugin(Plugin plugin) { return stopPlugin(plugin.getName(), LogManager.getLoggerRepository()); } /** Stops a plugin in the default logger repository. */ public static Plugin stopPlugin(String pluginName) { return stopPlugin(pluginName, LogManager.getLoggerRepository()); } /** Stops a plugin in the given logger repository. */ public static Plugin stopPlugin(Plugin plugin, LoggerRepository repository) { return stopPlugin(plugin.getName(), repository); } /** Stops a plugin in the given logger repository. */ public static Plugin stopPlugin(String pluginName, LoggerRepository repository) { if (pluginName == null) { pluginName = ""; } synchronized(repositoryMap) { Hashtable pluginMap = (Hashtable)repositoryMap.get(repository); if (pluginMap == null) return null; Plugin plugin = (Plugin)pluginMap.get(pluginName); if (plugin == null) return null; // shutdown the plugin plugin.shutdown(); // remove it from the plugin map pluginMap.remove(pluginName); // if no more plugins, remove the plugin map from // repository map if (pluginMap.isEmpty()) { repository.removeLoggerRepositoryEventListener(listener); repositoryMap.remove(repository); } // return it for future use return plugin; } } /** Stops all plugins in the default logger repository. */ public static void stopAllPlugins() { stopAllPlugins(LogManager.getLoggerRepository()); } /** Stops all plugins in the given logger repository. */ public static void stopAllPlugins(LoggerRepository repository) { synchronized(repositoryMap) { Hashtable pluginMap = (Hashtable)repositoryMap.get(repository); if (pluginMap == null) return; // remove the listener for this repository repository.removeLoggerRepositoryEventListener(listener); Enumeration enum = pluginMap.elements(); while(enum.hasMoreElements()) { Plugin plugin = (Plugin)enum.nextElement(); plugin.shutdown(); } // since no more plugins, remove plugin map from // the repository repositoryMap.remove(repository); } } /** Internal class used to handle listener events from repositories. */ private static class RepositoryListener implements LoggerRepositoryEventListener { /** Stops all plugins associated with the repository being reset. */ public void configurationResetEvent(LoggerRepository repository) { PluginRegistry.stopAllPlugins(repository); } /** Called when the repository configuration is changed. */ public void configurationChangedEvent(LoggerRepository repository) { // do nothing with this event } /** Stops all plugins associated with the repository being shutdown. */ public void shutdownEvent(LoggerRepository repository) { PluginRegistry.stopAllPlugins(repository); } } } 1.1 jakarta-log4j/src/java/org/apache/log4j/plugins/Plugin.java Index: Plugin.java =================================================================== /* * Copyright (C) The Apache Software Foundation. All rights reserved. * * This software is published under the terms of the Apache Software * License version 1.1, a copy of which has been included with this * distribution in the LICENSE.txt file. */ package org.apache.log4j.plugins; import org.apache.log4j.spi.LoggerRepository; import org.apache.log4j.spi.OptionHandler; /** Defines the required interface for all Plugin objects. <p>A plugin implements some specific functionality to extend the log4j framework. Each plugin is associated with a specific LoggerRepository, which it then uses/acts upon. If no repository is specified, the default repository from LogManager.getLoggerRepository() is used. <p>Examples of plugins are Receiver and Watchdog. Receiver plugins allow for remote logging events to be received and processed by a repository as if the event was sent locally. Watchdog plugins allow for a repository to be reconfigured when some "watched" configuration data changes. @author Mark Womack @author Nicko Cadell @since 1.3 */ public interface Plugin extends OptionHandler { /** Gets the name of the plugin. */ public String getName(); /** Sets the name of the plugin. */ public void setName(String _name); /** Gets the logger repository for this plugin. If not explicity set, returns the value of LogManager.getLoggerRepository(). */ public LoggerRepository getLoggerRepository(); /** Sets the logger repository used by this plugin. This repository will be used by the plugin functionality. */ public void setLoggerRepository(LoggerRepository _repository); /** True if the plugin is active and running. */ public boolean isActive(); /** Call when the plugin should be stopped. */ public void shutdown(); }
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>