User: user57  
  Date: 02/03/04 16:38:58

  Modified:    src/main/org/jboss/logging Log4jService.java
  Log:
   o Log4j can now be configured/reconfigured from a URL
  
  Revision  Changes    Path
  1.2       +62 -14    jboss-system/src/main/org/jboss/logging/Log4jService.java
  
  Index: Log4jService.java
  ===================================================================
  RCS file: /cvsroot/jboss/jboss-system/src/main/org/jboss/logging/Log4jService.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Log4jService.java 24 Feb 2002 10:16:53 -0000      1.1
  +++ Log4jService.java 5 Mar 2002 00:38:58 -0000       1.2
  @@ -12,7 +12,10 @@
   import java.io.FileNotFoundException;
   import java.io.PrintStream;
   import java.net.URL;
  -import java.util.ArrayList;
  +import java.net.URLConnection;
  +
  +import java.util.Timer;
  +import java.util.TimerTask;
   
   import javax.management.MBeanRegistration;
   import javax.management.MBeanServer;
  @@ -25,6 +28,7 @@
   
   import org.jboss.util.ThrowableHandler;
   import org.jboss.util.ThrowableListener;
  +import org.jboss.util.NullArgumentException;
   
   import org.jboss.logging.util.CategoryStream;
   
  @@ -39,7 +43,7 @@
    * <p>Installs CategoryStream adapters for System.out and System.err
    *    to catch and redirect calls to Log4j.
    *
  - * @version <tt>$Revision: 1.1 $</tt>
  + * @version <tt>$Revision: 1.2 $</tt>
    * @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>
  @@ -95,6 +99,9 @@
      /** The previous value of System.err. */
      private PrintStream err;
   
  +   /** The URL watch timer (in daemon mode). */
  +   private Timer timer = new Timer(true);
  +
      // Constructors --------------------------------------------------
      
      public Log4jService()
  @@ -161,20 +168,12 @@
            throw new FileNotFoundException
               ("Failed to find logj4 configuration: " + configurationPath);
         }
  +
         log.debug("Configuration URL: " + url);
         
  -      // configurationPath is a file path
  -      String path = url.getFile();
  -      boolean isXML = configurationPath.endsWith(".xml");
  -      log.debug("isXML: " + isXML);
  -      if (isXML)
  -      {
  -         DOMConfigurator.configureAndWatch(path, 1000 * refreshPeriod);
  -      }
  -      else
  -      {
  -         PropertyConfigurator.configureAndWatch(path, 1000 * refreshPeriod);
  -      }
  +      URLWatchTimerTask timerTask = new URLWatchTimerTask(url);
  +      timerTask.run();
  +      timer.schedule(timerTask, 1000 * refreshPeriod, 1000 * refreshPeriod);
   
         // Make sure Category has loaded
         Category category = Category.getRoot();
  @@ -286,6 +285,55 @@
                   // these could be red-herrings, so log them as trace
                   log.trace("unhandled throwable, status is unknown", t);
                   break;
  +         }
  +      }
  +   }
  +
  +   /**
  +    * A timer task to check when a URL changes (based on 
  +    * last modified time) and reconfigure Log4j.
  +    */
  +   private class URLWatchTimerTask
  +      extends TimerTask
  +   {
  +      private Category log = Category.getInstance(URLWatchTimerTask.class);
  +
  +      private long lastConfigured;
  +      private URL url;
  +
  +      public URLWatchTimerTask(final URL url)
  +      {
  +         if (url == null)
  +            throw new NullArgumentException("url");
  +
  +         this.url = url;
  +      }
  +
  +      public void run()
  +      {
  +         try {
  +            URLConnection conn = url.openConnection();
  +            long lastModified = conn.getLastModified();
  +            
  +            if (lastConfigured < lastModified) {
  +               reconfigure(conn);
  +            }
  +         }
  +         catch (Exception e) {
  +            log.warn("Failed to check URL: " + url, e);
  +         }
  +      }
  +
  +      private void reconfigure(URLConnection conn) 
  +      {
  +         boolean xml = conn.getContentType().equals("text/xml");
  +         log.debug("reconfiguring; xml=" + xml);
  +
  +         if (xml) {
  +            DOMConfigurator.configure(url);
  +         }
  +         else {
  +            PropertyConfigurator.configure(url);
            }
         }
      }
  
  
  

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

Reply via email to