You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by mw...@apache.org on 2005/01/30 07:40:01 UTC

cvs commit: logging-log4j/src/java/org/apache/log4j/watchdog FileWatchdog.java TimedURLWatchdog.java Watchdog.java WatchdogSkeleton.java

mwomack     2005/01/29 22:40:01

  Added:       src/java/org/apache/log4j/watchdog FileWatchdog.java
                        TimedURLWatchdog.java Watchdog.java
                        WatchdogSkeleton.java
  Log:
  Added new watchdog base interface/classes and specific FileWatchdog.
  
  Revision  Changes    Path
  1.1                  logging-log4j/src/java/org/apache/log4j/watchdog/FileWatchdog.java
  
  Index: FileWatchdog.java
  ===================================================================
  /*
   * Copyright 1999,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.log4j.watchdog;
  
  import java.io.File;
  import java.net.URL;
  
  /**
   * Implements a watchdog to watch a file.  When the file changes, determined by
   * a change in the file's modification date, the contents of the file are use to
   * reconfigure the log4j environment.
   */
  public class FileWatchdog extends TimedURLWatchdog {
  
    /** The file being watched. */
    private File watchedFile;
    
    /**
     * Sets up the reference to the file being watched, then calls the version
     * in the super class.
     */
    public void activateOptions() {
  
      if (watchedURL == null) {
        getLogger().error("watchdog \"{}\" not configured with URL to watch",
          this.getName());
        return;
      }
  
      watchedFile = new File(watchedURL.getFile());
      super.activateOptions();
    }
    
    /**
     * @return The modification time of the file.
     */
    public long getModificationTime() {
      return watchedFile.lastModified();
    }
  }
  
  
  
  1.1                  logging-log4j/src/java/org/apache/log4j/watchdog/TimedURLWatchdog.java
  
  Index: TimedURLWatchdog.java
  ===================================================================
  /*
   * Copyright 1999,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.log4j.watchdog;
  
  import java.net.URL;
  
  import org.apache.log4j.LogManager;
  import org.apache.log4j.scheduler.Job;
  import org.apache.log4j.scheduler.Scheduler;
  import org.apache.log4j.watchdog.WatchdogSkeleton;
  
  /**
    Implements functionality of a watchdog that periodically checks a URL for
    updated configuration data.
    
    This class can be used as the base class for any Watchdog that needs to
    watch a single URL for changes.  Subclasses must implement the
    getModificationTime method to return the current modification time of the
    watched source (URL), since this can be source specific.  When a change in
    modification time is detected, the log4j environment will be reconfigured,
    using the URL as the source for the new configuration data.
    
    The URL will be checked peridoically.  This period of time is defined by the
    <code>interval</code> property.
  
    @author Mark Womack <mw...@apache.org>
    @since 1.3
  */
  public abstract class TimedURLWatchdog extends WatchdogSkeleton implements Job {
    
    /** Default interval of time between checks, in milliseconds. */
    public static long DEFAULT_INTERVAL = 60000;
    
    /** The url to watch. */
    protected URL watchedURL;
    
    /** The interval of time between checks. */
    protected long interval = DEFAULT_INTERVAL;
    
    /** The last time the url was modified. */
    private long lastModTime = -1;
    
    /**
     * Set the URL to watch.
     *
     * @param urlToWatch The URL to watch.
     */
    public void setURL(URL urlToWatch) {
      this.watchedURL = urlToWatch;
    }
    
    /**
     * Returns the URL that will be watched.
     *
     * @return The URL being watched.
     */
    public URL getURL() {
      return watchedURL;
    }
    
    /**
     * Sets the interval of time, in milliseconds, between checks on the
     * URL.
     *
     * @param interval An interval of time, in milliseconds.
     */
    public void setInterval(long interval) {
      this.interval = interval;
    }
    
    /**
     * Returns the interval of time, in milliseconds, between checks on the URL.
     *
     * @return An interval of time, in milliseconds.
     */
    public long getInterval() {
      return interval;
    }
    
    /**
     * Reconfigures the log4j environment using the URL as the source of the
     * configuration data.
     */
    public void reconfigure() {
      reconfigureByURL(watchedURL);
    }
    
    /**
     * Returns the current modification time for the watched URL.  Subclasses
     * must implement specifically for the type of source they are watching.
     *
     * @return The current modification time of the URL.
     */
    public abstract long getModificationTime();
    
    /**
     * Implements the Job interface for the Scheduler.  When this method is called
     * by the Scheduler it checks the current modification time of the watched
     * source with the last recorded modification time.  If the modification times
     * are different, then the log4j environment is reconfigured using the
     * watched source for the configuration data.
     */
    public void execute() {
      long newModTime = getModificationTime();
      
      if (lastModTime != newModTime) {
        reconfigure();
        lastModTime = newModTime;
      }
    }
    
    /**
     * Called to activate the watchdog and start the watching of the source.
     */
    public void activateOptions() {
      if (watchedURL == null) {
        this.getLogger().error("{} watchdog not configured with URL to watch",
          this.getName());
        return;
      }
      
      // get the current modification time of the watched source
      lastModTime = getModificationTime();
      
      // schedule this Wathdog as a Job with the Scheduler
      getLoggerRepository().getScheduler().schedule(this,
        System.currentTimeMillis() + interval, interval);
    }
    
    /**
     * Shutdown this watchdog.  Since implemented as a scheduled Job, this method
     * simply removes the watchdog from the Scheduler.
     */
    public void shutdown() {
      getLoggerRepository().getScheduler().delete(this);
    }
  }
  
  
  
  1.1                  logging-log4j/src/java/org/apache/log4j/watchdog/Watchdog.java
  
  Index: Watchdog.java
  ===================================================================
  /*
   * Copyright 1999,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.log4j.watchdog;
  
  import org.apache.log4j.plugins.Plugin;
  
  /**
    Defines the required interface for all Watchdog objects.
  
    <p>A watchdog is an entity that monitors a source of configuration data.
    If the source indicates that the configuration data has been changed, then
    the new configuration data is read and the log4j environment is
    reconfigured using the new data.
  
    <p>Examples of watchdogs are FileWatchdog and SocketWatchdog.  FileWatchdog
    monitors a configuration file for updates, using the updated file to
    reconfigure log4j.  SocketWatchdog monitors a socket port, using the data
    stream from the socket to reconfigure log4j.
    
    <p>Watchdogs are implemented as instances of the Plugin interface and can
    be started and stopped like any other Plugin object.
    
    <p>Watchdogs are not specific to any Configurator class.  Any Configurator
    can be used with any Watchdog.  When reconfiguring, the Watchdog should
    create a new instance of the defined Configurator class and call the
    appropriate Configurator method to reconfigure the log4j environment.
  
    @author Mark Womack <mw...@apache.org>
    @since 1.3
  */
  public interface Watchdog extends Plugin {
  
    /**
     * Sets the Configurator class used for reconfiguration.
     *
     * @param configuratorClassName Fully qualified class name for
     *   Configurator class.
     */
    public void setConfigurator(String configuratorClassName);
    
    /**
     * Returns the configurator class used for reconfiguration.
     *
     * @return Fully qualified class name for Configurator class.
     */
    public String getConfigurator();
  
    /**
     * Called to reconfigure the log4j environment when the monitored data
     * source has been updated.
     */
    public void reconfigure();
    
  }
  
  
  
  1.1                  logging-log4j/src/java/org/apache/log4j/watchdog/WatchdogSkeleton.java
  
  Index: WatchdogSkeleton.java
  ===================================================================
  /*
   * Copyright 1999,2004 The Apache Software Foundation.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
   * 
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package org.apache.log4j.watchdog;
  
  import org.apache.log4j.Logger;
  import org.apache.log4j.LogManager;
  import org.apache.log4j.PropertyConfigurator;
  import org.apache.log4j.spi.Configurator;
  import org.apache.log4j.plugins.Plugin;
  import org.apache.log4j.plugins.PluginSkeleton;
  import org.apache.log4j.helpers.OptionConverter;
  
  import java.io.InputStream;
  import java.net.URL;
  
  /**
    Implements the required interface for Watchdog objects.
  
    <p>This class provides an implementation of the Watchdog interface that
    forms the base class used for specific Watchdog classes implemented in the
    log4j framework.  Using this class for all Watchdog implementations is not
    required.  Developers may choose to implement their own version of the
    Watchdog interface (which extends the base Plugin interface) if they so 
    choose.
    
    <p>This implementation provides two helper methods, reconfigureByURL and
    reconfigureByInputStream.  Either of these methods can be called from the
    implemented reconfigure method, as required by the specific type of data
    source being monitored.  The reconfigureByURL method is meant for sources
    that can be described by a URL (ie file, url).  The rconfigureByInputStream
    method is meant for sources where onl a stream of data is available
    (ie socket).
    
    <p>Subclasses of this implementation are required to implement their own
    version of the abstract reconfigure method.
  
    @author Mark Womack <mw...@apache.org>
    @since 1.3
  */
  public abstract class WatchdogSkeleton 
  extends PluginSkeleton implements Watchdog {
    
    protected String configuratorClassName;
    
    /**
     * Sets the Configurator class used for reconfiguration.
     *
     * @param configuratorClassName Fully qualified class name for Configurator
     * class.
     */
     public void setConfigurator(String configuratorClassName) {
       this.configuratorClassName = configuratorClassName;
     }
    
    /**
     * Returns the configurator class used for reconfiguration.
     *
     * @return Fully qualified class name for Configurator class.
     */
     public String getConfigurator() {
       return configuratorClassName;
     }
  
    /**
     * Called to reconfigure the log4j environment when the monitored data
     * source has been updated.  Must be implemented by subclasses.
     */
    public abstract void reconfigure();
    
    /**
     * Helper method to get an instance of the configurator class.
     *
     * @return An instance of the Configurator class to use
     * for reconfiguration.
     */
    protected Configurator getConfiguratorInstance() {
      // create an instance of the configurator class
      Configurator configurator = null;
      
      // if we were configured with a configurator class name, use it
      if (configuratorClassName != null) {
        configurator = (Configurator) OptionConverter.instantiateByClassName(
        	configuratorClassName, Configurator.class, null);
      }
      // otherwise, default to PropertyConfigurator
      else {
        configurator = new PropertyConfigurator();
      }
      
      return configurator;
    }
    
    /**
     * Helper method to reconfigure using a URL.
     * The input parameter, configurationURL, should be a URL pointing to
     * the configuration data in a format expected by the configurator.
     *
     * @param srcURL The url that contains the data to be used for
     *   reconfiguration.
     */
    protected void reconfigureByURL(URL srcURL) {
      if (this.getLogger().isDebugEnabled()) {
        this.getLogger().debug("watchdog \"{}\" reconfiguring from url: {}",
          this.getName(), srcURL);
      }
      
      // create an instance of the configurator class
      Configurator configurator = getConfiguratorInstance();
      
      // if able to create configurator, then reconfigure using input stream
      if (configurator != null) {
        configurator.doConfigure(srcURL, this.getLoggerRepository());
      }
  	  else {
  	    getLogger().error(
          "watchdog \"{}\" could not create configurator, ignoring new configuration settings",
          this.getName());
  	  }
    }
    
    /**
     * Helper method to reconfigure using an InputStream.
     * The input parameter, configurationStream, should be a stream
     * of configuration data in a format expected by the configurator.
     *
     * @param srcStream The input stream that contains the data to be used
     *   reconfiguration.
     */
     protected void reconfigureByInputStream(InputStream srcStream) {
      if (this.getLogger().isDebugEnabled()) {
        this.getLogger().debug("watchdog \"{}\" reconfiguring from InputStream");
      }
      
      // create an instance of the configurator class
      Configurator configurator = getConfiguratorInstance();
      
      // if able to create configurator, then reconfigure using input stream
      if (configurator != null) {
        try {
          configurator.doConfigure(srcStream, this.getLoggerRepository());
  	    } catch (Exception e) {
  	  	  getLogger().error(
          "watchdog " + this.getName() + " error working with configurator," +
          " ignoring new configuration settings", e);
  	    }
  	  }
  	  else {
  	  	  getLogger().error(
            "watchdog \"{}\" could not create configurator, ignoring new configuration settings",
            this.getName());
  	  }
    }  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org