User: user57  
  Date: 01/09/07 16:22:14

  Modified:    src/main/org/jboss/logging Log4jService.java
  Log:
   o Added support to read the config file path from the system property
     org.jboss.logging.Log4jService.configfile.  This will only work when
     using the no args constructor.
   o Cleaned up some javadocs too.
  
  Revision  Changes    Path
  1.11      +235 -178  jboss/src/main/org/jboss/logging/Log4jService.java
  
  Index: Log4jService.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss/src/main/org/jboss/logging/Log4jService.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Log4jService.java 2001/08/30 03:57:44     1.10
  +++ Log4jService.java 2001/09/07 23:22:13     1.11
  @@ -23,183 +23,240 @@
   import org.apache.log4j.PropertyConfigurator;
   import org.apache.log4j.xml.DOMConfigurator;
   
  -/** This is a JMX MBean that provides three features:
  -1., It initalizes the log4j framework from the log4j properties format file
  -    specified by the ConfigurationPath attribute to that the log4j may be
  -    used by JBoss components.
  -2., It collects JMX notification events fired by the "service=Log" mbean
  -    and logs the msgs to log4j. This allows the Log4jService
  -    to replace all other JBoss logging services like ConsoleLogging and
  -    FileLogging.
  -    
  -3,  It uses the log name as the category to log under, allowing you to turn 
  -    individual components on and off using the log4j configuration file
  -    (automatically reloaded frequently).
  -
  -@author <a href="mailto:[EMAIL PROTECTED]";>Fulco Muriglio</a>
  -@author <a href="mailto:[EMAIL PROTECTED]";>Scott Stark</a>.
  -@author <a href="mailto:[EMAIL PROTECTED]";>David Jencks</a>
  -@version $Revision: 1.10 $
  -*/
  -public class Log4jService implements Log4jServiceMBean, NotificationListener,
  -    MBeanRegistration
  +/**
  + * This is a JMX MBean that provides three features:
  + * <ol>
  + * <li>It initalizes the log4j framework from the log4j properties format file
  + *     specified by the ConfigurationPath attribute to that the log4j may be
  + *     used by JBoss components.
  + * <li>It collects JMX notification events fired by the "service=Log" mbean
  + *     and logs the msgs to log4j. This allows the Log4jService
  + *     to replace all other JBoss logging services like ConsoleLogging and
  + *     FileLogging.
  + * <li>It uses the log name as the category to log under, allowing you to turn 
  + *     individual components on and off using the log4j configuration file
  + *     (automatically reloaded frequently).
  + * </ol>
  + * 
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Fulco Muriglio</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Scott Stark</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>David Jencks</a>
  + * @author <a href="mailto:[EMAIL PROTECTED]";>Jason Dillon</a>
  + * @version $Revision: 1.11 $
  + */
  +public class Log4jService
  +   implements Log4jServiceMBean, NotificationListener, MBeanRegistration
   {
  -
  -// Attributes ----------------------------------------------------
  -    private Category category;
  -    private String configurationPath;
  -    private int refreshPeriod;
  -    private boolean refreshFlag;
  -
  -// Constructors --------------------------------------------------
  -    public Log4jService()
  -    {
  -        this("log4j.properties", 60);
  -    }
  -    public Log4jService(String path)
  -    {
  -        this(path, 60);
  -    }
  -    /**
  -    @param path, the path to the log4j.properties format file
  -    @param refreshPeriod, the refreshPeriod in seconds to wait between each check.
  -    */
  -    public Log4jService(String path, int refreshPeriod)
  -    {
  -        this.configurationPath = path;
  -        this.refreshPeriod = refreshPeriod;
  -        this.refreshFlag = true;
  -    }
  -
  -    /** Get the log4j.properties format config file path
  -    */
  -    public String getConfigurationPath()
  -    {
  -        return configurationPath;
  -    }
  -    /** Set the log4j.properties format config file path
  -    */
  -    public void setConfigurationPath(String path)
  -    {
  -        this.configurationPath = path;
  -    }
  -    /** Get the refresh flag. This determines if the log4j.properties file
  -        is reloaded every refreshPeriod seconds or not.
  -    */
  -    public boolean getRefreshFlag()
  -    {
  -        return refreshFlag;
  -    }
  -    /** Set the refresh flag. This determines if the log4j.properties file
  -        is reloaded every refreshPeriod seconds or not.
  -    */
  -    public void setRefreshFlag(boolean flag)
  -    {
  -        this.refreshFlag = flag;
  -    }
  -    /** Configures the log4j framework using the current service properties
  -        and sets the service category to the log4j root Category. This method
  -        throws a FileNotFoundException exception if the current
  -        configurationPath cannot be located to avoid interaction problems
  -        between the log4j framework and the JBoss ConsoleLogging service.
  -    */
  -    public void start() throws Exception
  -    {
  -        // See if this is an xml configuration file
  -        boolean isXML = configurationPath.endsWith(".xml");
  -        // Make sure the config file can be found
  -        ClassLoader loader = Thread.currentThread().getContextClassLoader();
  -        URL url = loader.getResource(configurationPath);
  -        if( url == null )
  -            throw new FileNotFoundException("Failed to find logj4 props: 
"+configurationPath);
  -        if( refreshFlag )
  -        {
  -            // configurationPath is a file path
  -            String path = url.getFile();
  -            if( isXML )
  -                DOMConfigurator.configureAndWatch(path, 1000*refreshPeriod);
  -            else
  -                PropertyConfigurator.configureAndWatch(path, 1000*refreshPeriod);
  -        }
  -        else
  -        {
  -            if( isXML )
  -                DOMConfigurator.configure(url);
  -            else
  -                PropertyConfigurator.configure(url);
  -        }
  -        this.category = Category.getRoot();
  -        category.info("Started Log4jService, config="+url);
  -    }
  -    /** Stops the log4j framework by calling the Category.shutdown() method.
  -    @see org.apache.log4j.Category#shutdown()
  -    */
  -    public void stop()
  -    {
  -        Category.shutdown();
  -        if( category != null )
  -            category.info("Stopped Log4jService");
  -    }
  -
  -// Public --------------------------------------------------------
  -    /** This method recevies JMX notification events posted via a Logger
  -        instances fireNotification method and logs the msg through the
  -        log4j root Category.
  -
  -    @param n, the log event. This provides the log source as n.getUserData(),
  -        the log msg as n.getMessage(), and the type of message from n.getType().
  -    @see org.jboss.logging.Logger#fireNotification(String, Object, String)
  -    */
  -    public void handleNotification(Notification n, Object handback)
  -    {
  -        if( category == null )
  -            return;
  -
  -        String msg = n.getMessage();
  -        char type = n.getType().charAt(0);
  -        String source = (String) n.getUserData();
  -        if( source == null || source.length() == 0 )
  -            source = "Default";
  -        //get a category based on the source name, so we can turn on and off pieces 
of logging.
  -        Category localCategory = category.getInstance(source);
  -        switch( type )
  -        {
  -            case 'W':
  -                localCategory.warn(msg);
  -            break;
  -            case 'D':
  -                localCategory.debug(msg);
  -            break;
  -            case 'E':
  -                localCategory.error(msg);
  -            break;
  -            default:
  -                localCategory.info(msg);
  -            break;
  -        }
  -    }
  -
  -// --- Begin MBeanRegistration interface methods
  -    /** Initializes the MBean by registering as a addNotificationListener of the
  -        Log service and then invokes start() to configure the log4j framework.
  -    @return the name of this mbean.
  -    */
  -    public ObjectName preRegister(MBeanServer server, ObjectName name) throws 
Exception
  -    {
  -        start();
  -        // Receive notification events sent by the Logger mbean
  -        ObjectName logger = new ObjectName(server.getDefaultDomain(),"spine","Log");
  -        server.addNotificationListener(logger,this,null,null);
  -        return name == null ? new ObjectName(OBJECT_NAME) : name;
  -    }
  -    public void postRegister(Boolean b)
  -    {
  -    }
  -    public void preDeregister()
  -    {
  -    }
  -    public void postDeregister()
  -    {
  -    }
  -// --- End MBeanRegistration interface methods
  +   /**
  +    * The default path, either read from system properties or if not set
  +    * default to log4j.properties.
  +    *
  +    * <p>Note, this is a minor HACK to allow loading a different
  +    *    logging configuration file other that log4j.properties.  This should
  +    *    be fixed by exposing more configuration in the boot strapping
  +    *    xml snippet.
  +    */
  +   public static final String DEFAULT_PATH =
  +      System.getProperty(Log4jService.class.getName() + ".configfile",
  +                         "log4j.properties");
  +
  +   // Attributes ----------------------------------------------------
  +   
  +   private Category category;
  +   private String configurationPath;
  +   private int refreshPeriod;
  +   private boolean refreshFlag;
  +
  +   // Constructors --------------------------------------------------
  +   
  +   public Log4jService()
  +   {
  +      this(DEFAULT_PATH, 60);
  +   }
  +   
  +   public Log4jService(String path)
  +   {
  +      this(path, 60);
  +   }
  +   
  +   /**
  +    * @param path             The path to the log4j.properties format file
  +    * @param refreshPeriod    The refreshPeriod in seconds to wait between
  +    *                         each check.
  +    */
  +   public Log4jService(final String path, final int refreshPeriod)
  +   {
  +      this.configurationPath = path;
  +      this.refreshPeriod = refreshPeriod;
  +      this.refreshFlag = true;
  +   }
  +
  +   /**
  +    * Get the log4j.properties format config file path
  +    */
  +   public String getConfigurationPath()
  +   {
  +      return configurationPath;
  +   }
  +   
  +   /**
  +    * Set the log4j.properties format config file path
  +    */
  +   public void setConfigurationPath(String path)
  +   {
  +      this.configurationPath = path;
  +   }
  +   
  +   /**
  +    * Get the refresh flag. This determines if the log4j.properties file
  +    * is reloaded every refreshPeriod seconds or not.
  +    */
  +   public boolean getRefreshFlag()
  +   {
  +      return refreshFlag;
  +   }
  +   
  +   /**
  +    * Set the refresh flag. This determines if the log4j.properties file
  +    * is reloaded every refreshPeriod seconds or not.
  +    */
  +   public void setRefreshFlag(boolean flag)
  +   {
  +      this.refreshFlag = flag;
  +   }
  +   
  +   /**
  +    * Configures the log4j framework using the current service properties
  +    * and sets the service category to the log4j root Category. This method
  +    * throws a FileNotFoundException exception if the current
  +    * configurationPath cannot be located to avoid interaction problems
  +    * between the log4j framework and the JBoss ConsoleLogging service.
  +    */
  +   public void start() throws Exception
  +   {
  +      // See if this is an xml configuration file
  +      boolean isXML = configurationPath.endsWith(".xml");
  +      
  +      // Make sure the config file can be found
  +      ClassLoader loader = Thread.currentThread().getContextClassLoader();
  +      URL url = loader.getResource(configurationPath);
  +      if (url == null) {
  +         throw new FileNotFoundException
  +            ("Failed to find logj4 configuration: " + configurationPath);
  +      }
  +
  +      if (refreshFlag)
  +      {
  +         // configurationPath is a file path
  +         String path = url.getFile();
  +         if (isXML) {
  +            DOMConfigurator.configureAndWatch(path, 1000 * refreshPeriod);
  +         }
  +         else {
  +            PropertyConfigurator.configureAndWatch(path, 1000 * refreshPeriod);
  +         }
  +      }
  +      else
  +      {
  +         if (isXML) {
  +            DOMConfigurator.configure(url);
  +         }
  +         else {
  +            PropertyConfigurator.configure(url);
  +         }
  +      }
  +      
  +      this.category = Category.getRoot();
  +      category.info("Started Log4jService, config=" + url);
  +   }
  +   
  +   /**
  +    * Stops the log4j framework by calling the Category.shutdown() method.
  +    * @see org.apache.log4j.Category#shutdown()
  +    */
  +   public void stop()
  +   {
  +      Category.shutdown();
  +      if (category != null) {
  +         category.info("Stopped Log4jService");
  +      }
  +   }
  +
  +   // Public --------------------------------------------------------
  +   
  +   /**
  +    * This method recevies JMX notification events posted via a Logger
  +    * instances fireNotification method and logs the msg through the
  +    * log4j root Category.
  +    *
  +    * @param n    The log event. This provides the log source as
  +    *             n.getUserData(), the log msg as n.getMessage(), and the
  +    *             type of message from n.getType().
  +    *             
  +    * @see org.jboss.logging.Logger#fireNotification(String,Object,String)
  +    */
  +   public void handleNotification(Notification n, Object handback)
  +   {
  +      if (category == null) return;
  +
  +      String msg = n.getMessage();
  +      char type = n.getType().charAt(0);
  +      String source = (String) n.getUserData();
  +      if (source == null || source.length() == 0) {
  +         source = "Default";
  +      }
  +
  +      // get a category based on the source name, so we can turn on and
  +      // off pieces of logging.
  +      Category localCategory = category.getInstance(source);
  +      switch (type)
  +      {
  +       case 'W':
  +          localCategory.warn(msg);
  +          break;
  +       case 'D':
  +          localCategory.debug(msg);
  +          break;
  +       case 'E':
  +          localCategory.error(msg);
  +          break;
  +       default:
  +          localCategory.info(msg);
  +          break;
  +      }
  +   }
  +
  +   // --- Begin MBeanRegistration interface methods
  +   
  +   /**
  +    * Initializes the MBean by registering as a addNotificationListener of the
  +    * Log service and then invokes start() to configure the log4j framework.
  +    * 
  +    * @return the name of this mbean.
  +    */
  +   public ObjectName preRegister(MBeanServer server, ObjectName name)
  +      throws Exception
  +   {
  +      start();
  +      // Receive notification events sent by the Logger mbean
  +      ObjectName logger = new ObjectName(server.getDefaultDomain(),
  +                                         "spine",
  +                                         "Log");
  +      server.addNotificationListener(logger, this, null, null);
  +      return name == null ? new ObjectName(OBJECT_NAME) : name;
  +   }
  +   
  +   public void postRegister(Boolean b)
  +   {
  +   }
  +   
  +   public void preDeregister()
  +   {
  +   }
  +   
  +   public void postDeregister()
  +   {
  +   }
  +   
  +   // --- End MBeanRegistration interface methods
   }
  
  
  

_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to