geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject cvs commit: incubator-geronimo/modules/core/src/java/org/apache/geronimo/core/logging/log4j Log4jService.java
Date Mon, 01 Sep 2003 19:31:33 GMT
jdillon     2003/09/01 12:31:33

  Added:       modules/core/src/java/org/apache/geronimo/core/logging
                        AbstractLoggingService.java LoggingService.java
               modules/core/src/java/org/apache/geronimo/core/logging/log4j
                        Log4jService.java
  Log:
   o A Log4j logging service + support for alternatives
  
  Revision  Changes    Path
  1.1                  incubator-geronimo/modules/core/src/java/org/apache/geronimo/core/logging/AbstractLoggingService.java
  
  Index: AbstractLoggingService.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  package org.apache.geronimo.core.logging;
  
  import java.net.URL;
  
  import java.util.Timer;
  
  import org.apache.geronimo.common.NullArgumentException;
  import org.apache.geronimo.common.task.URLMonitorTask;
  
  import org.apache.geronimo.management.AbstractManagedObject;
  
  /**
   * An abstract logging service.
   *
   * <p>Sub-classes only need to provide a {@link #configure(URL)}.
   *
   * @version $Revision: 1.1 $ $Date: 2003/09/01 19:31:33 $
   */
  public abstract class AbstractLoggingService
      extends AbstractManagedObject
      implements LoggingService
  {
      /** The default refresh period (60 seconds) */
      public static final int DEFAULT_REFRESH_PERIOD = 60;
      
      /** The URL to the configuration file. */
      protected URL configURL;
      
      /** The time (in seconds) between checking for new config. */
      protected int refreshPeriod;
      
      /** The URL watch timer (in daemon mode). */
      protected Timer timer = new Timer(true);
      
      /** A monitor to check when the config URL changes. */
      protected URLMonitorTask monitor;
      
      /**
       * Initialize <code>AbstractLoggingService</code>.
       *
       * @param url       The configuration URL.
       * @param period    The refresh period (in seconds).
       */
      protected AbstractLoggingService(final URL url, final int period)
      {
          setRefreshPeriod(period);
          setConfigurationURL(url);
      }
      
      /**
       * Initialize <code>AbstractLoggingService</code> using the default
       * refresh period.
       *
       * @param url   The configuration URL.
       */
      protected AbstractLoggingService(final URL url)
      {
          this(url, DEFAULT_REFRESH_PERIOD);
      }
      
      public int getRefreshPeriod()
      {
          return refreshPeriod;
      }
      
      public void setRefreshPeriod(final int period)
      {
          if (period < 1) {
              throw new IllegalArgumentException("Refresh period must be > 0");
          }
          
          this.refreshPeriod = period;
      }
      
      public URL getConfigurationURL()
      {
          return configURL;
      }
      
      public void setConfigurationURL(final URL url)
      {
          if (url == null) {
              throw new NullArgumentException("url");
          }
          
          this.configURL = url;
      }
      
      public void reconfigure()
      {
          configure(configURL);
      }
      
      
      ///////////////////////////////////////////////////////////////////////////
      //                    AbstractManagedObject Overrides                    //
      ///////////////////////////////////////////////////////////////////////////
      
      protected void doStart() throws Exception
      {
          monitor = new URLMonitorTask(configURL);
          monitor.addListener(new URLMonitorTask.Listener() {
              public void doURLChanged(final URLMonitorTask.Event event) {
                  configure(event.getURL());
              }
          });
          monitor.run();
          timer.schedule(monitor, 1000 * refreshPeriod, 1000 * refreshPeriod);
      }
      
      protected void doStop() throws Exception
      {
          monitor.cancel();
          monitor = null;
      }
  }
  
  
  
  1.1                  incubator-geronimo/modules/core/src/java/org/apache/geronimo/core/logging/LoggingService.java
  
  Index: LoggingService.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  package org.apache.geronimo.core.logging;
  
  import java.net.URL;
  
  /**
   * Interface for the logging services.
   *
   * <p>Currently assumes URL based configuration.
   *
   * @jmx:mbean
   *      extends="org.apache.geronimo.management.StateManageable, org.apache.geronimo.management.ManagedObject"
   *
   * @version $Revision: 1.1 $ $Date: 2003/09/01 19:31:33 $
   */
  public interface LoggingService
      extends LoggingServiceMBean
  {
      /**
       * Get the refresh period.
       *
       * @return The refresh period (in seconds).
       *
       * @jmx:managed-attribute
       */
      int getRefreshPeriod();
      
      /**
       * Set the refresh period.
       *
       * @param period    The refresh period (in seconds).
       *
       * @throws IllegalArgumentException     Refresh period must be > 0
       *
       * @jmx:managed-attribute
       */
      void setRefreshPeriod(int period);
      
      /**
       * Get the logging configuration URL.
       *
       * @return The logging configuration URL.
       *
       * @jmx:managed-attribute
       */
      URL getConfigurationURL();
      
      /**
       * Set the logging configuration URL.
       *
       * @param url   The logging configuration URL.
       *
       * @jmx:managed-attribute
       */
      void setConfigurationURL(URL url);
      
      /**
       * Force the logging system to reconfigure.
       *
       * @jmx:managed-operation
       */
      void reconfigure();
      
      /**
       * Force the logging system to configure from the given URL.
       *
       * @param url   The URL to configure the logging system from.
       *
       * @jmx:managed-operation
       */
      void configure(URL url);
  }
  
  
  
  1.1                  incubator-geronimo/modules/core/src/java/org/apache/geronimo/core/logging/log4j/Log4jService.java
  
  Index: Log4jService.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  package org.apache.geronimo.core.logging.log4j;
  
  import java.net.URL;
  
  import org.apache.log4j.Level;
  import org.apache.log4j.Logger;
  Overrides
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  import org.apache.geronimo.common.NullArgumentException;
  
  import org.apache.geronimo.common.propertyeditor.PropertyEditors;
  import org.apache.geronimo.common.propertyeditor.TextPropertyEditorSupport;
  
  import org.apache.geronimo.common.log.log4j.XLevel;
  import org.apache.geronimo.common.log.log4j.URLConfigurator;
  
  import org.apache.geronimo.core.logging.AbstractLoggingService;
  
  /**
   * A Log4j logging service.
   *
   * @jmx:mbean
   *      extends="org.apache.geronimo.core.logging.LoggingServiceMBean"
   *
   * @version $Revision: 1.1 $ $Date: 2003/09/01 19:31:33 $
   */
  public class Log4jService
      extends AbstractLoggingService
      implements Log4jServiceMBean
  {
      private static final Log log = LogFactory.getLog(Log4jService.class);
      
      /**
       * Construct a <code>Log4jService</code>.
       * 
       * @param url       The configuration URL.
       * @param period    The refresh period (in seconds).
       *
       * @jmx:managed-constructor
       */
      public Log4jService(final URL url, final int period)
      {
          super(url, period);
      }
      
      /**
       * Construct a <code>Log4jService</code>.
       * 
       * @param url   The configuration URL.
       *
       * @jmx:managed-constructor
       */
      public Log4jService(final URL url)
      {
          super(url);
      }
      
      /**
       * Force the logging system to configure from the given URL.
       *
       * @param url   The URL to configure from.
       */
      public void configure(final URL url)
      {
          if (url == null) {
              throw new NullArgumentException("url");
          }
          
          URLConfigurator.configure(url);
      }
      
      
      ///////////////////////////////////////////////////////////////////////////
      //                    Log4j Level Accessors & Mutators                   //
      ///////////////////////////////////////////////////////////////////////////
      
      /**
       * A property editor for Log4j Levels.
       */
      public static class LevelEditor
          extends TextPropertyEditorSupport
      {
          public Object getValue()
          {
              return XLevel.toLevel(getAsText().trim());
          }
      }
      
      /**
       * A property editor for Log4j Loggers.
       */
      public static class LoggerEditor
          extends TextPropertyEditorSupport
      {
          public Object getValue()
          {
              return Logger.getLogger(getAsText().trim());
          }
      }
      
      /** Install property editors for Logger and Level. */
      static
      {
          PropertyEditors.registerEditor(Logger.class, LoggerEditor.class);
          PropertyEditors.registerEditor(Level.class, LevelEditor.class);
      }
      
      /**
       * Sets the level for a logger of the give name.
       *
       * @param logger    The logger to change level
       * @param level     The level to change the logger to.
       *
       * @jmx:managed-operation
       */
      public void setLoggerLevel(final Logger logger, final Level level)
      {
          if (logger == null) {
              throw new NullArgumentException("logger");
          }
          if (level == null) {
              throw new NullArgumentException("level");
          }
          
          logger.setLevel(level);
          log.info("Changed logger '" + logger.getName() + "' level: " + level);
      }
      
      /**
       * Gets the level of the logger of the give name.
       *
       * @param logger    The logger to inspect.
       *
       * @jmx:managed-operation
       */
      public String getLoggerLevel(final Logger logger)
      {
          if (logger == null) {
              throw new NullArgumentException("logger");
          }
          
          Level level = logger.getLevel();
          
          if (level != null) {
              return level.toString();
          }
          
          return null;
      }
      
      
      ///////////////////////////////////////////////////////////////////////////
      //                         AbstractManagedObject                         //
      ///////////////////////////////////////////////////////////////////////////
      
      protected void doStart() throws Exception
      {
          super.doStart();
          
          // Make sure the root Logger has loaded
          Logger.getRootLogger();
      }
  }
  
  
  

Mime
View raw message