mwomack     2002/09/19 22:30:01

  Added:       src/java/org/apache/log4j PluginSkeleton.java
                        PluginRegistry.java Plugin.java
  Log:
  Checkin of Plugin design/implementation.  Plugins can be used to generically extend 
functionality of log4j.  Each plugin is attached to specific LoggerRepository to 
affect/act upon.
  
  Revision  Changes    Path
  1.1                  jakarta-log4j/src/java/org/apache/log4j/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;
  
  import java.util.Vector;
  import org.apache.log4j.spi.LoggingEvent;
  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/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;
  
  import java.util.Hashtable;
  import java.util.Enumeration;
  import org.apache.log4j.spi.LoggerRepository;
  
  /**
    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();
    
    /**
      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);
        }
        
        // existing plugin exists with the 
        Plugin existingPlugin = (Plugin)pluginMap.get(plugin.getName());
        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(plugin.getName(), 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) {
      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(plugin);
        
        // if no more plugins, remove the plugin map from
        // repository map
        if (pluginMap.isEmpty())
          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;
          
        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);
      }
    }
  }
  
  
  1.1                  jakarta-log4j/src/java/org/apache/log4j/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;
  
  import org.apache.log4j.spi.LoggingEvent;
  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.
    
    <p>Contributors: Nicko Cadell
    
    @author Mark Womack
    @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]>

Reply via email to