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/common/src/java/org/apache/geronimo/common/task URLMonitorTask.java
Date Fri, 29 Aug 2003 21:13:17 GMT
jdillon     2003/08/29 14:13:17

  Added:       modules/common/src/java/org/apache/geronimo/common/task
                        URLMonitorTask.java
  Log:
   o A timer task to monitor urls based on the last modified attribute
  
  Revision  Changes    Path
  1.1                  incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/task/URLMonitorTask.java
  
  Index: URLMonitorTask.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.common.task;
  
  import java.net.URL;
  import java.net.URLConnection;
  
  import java.util.Timer;
  import java.util.TimerTask;
  import java.util.List;
  import java.util.ArrayList;
  import java.util.Collections;
  import java.util.EventObject;
  
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  
  import org.apache.geronimo.common.NullArgumentException;
  
  /**
   * A timer task to monitor a URL.
   *
   * @version $Revision: 1.1 $ $Date: 2003/08/29 21:13:16 $
   */
  public class URLMonitorTask
      extends TimerTask
  {
      private Log log = LogFactory.getLog(URLMonitorTask.class);
      
      protected URL url;
      
      private long lastChanged = -1;
      
      protected List listeners = Collections.synchronizedList(new ArrayList());
      
      public URLMonitorTask(final URL url)
      {
          if (url == null) {
              throw new NullArgumentException("url");
          }
          
          this.url = url;
      }
      
      public URL getURL()
      {
          return url;
      }
      
      public long getLastChanged()
      {
          return lastChanged;
      }
      
      public void run()
      {
          log.trace("Checking if URL has changed");
          
          boolean trace = log.isTraceEnabled();
          
          try {
              URLConnection connection = url.openConnection();
              if (trace) {
                  log.trace("Connection: " + connection);
              }
              
              long lastModified = connection.getLastModified();
              if (trace) {
                  log.trace("Last modified: " + lastModified + ", last changed: " + lastChanged);
              }
              
              if (lastChanged < lastModified) {
                  fireURLChanged(new Event(this, lastModified));
                  lastChanged = lastModified;
              }
          }
          catch (Exception e) {
              log.warn("Failed to check URL: " + url, e);
          }
      }
      
      
      /////////////////////////////////////////////////////////////////////////
      //                            Listener Support                         //
      /////////////////////////////////////////////////////////////////////////
      
      public static class Event
          extends EventObject
      {
          private URLMonitorTask task;
          private long lastModified;
          
          Event(final URLMonitorTask task, final long lastModified)
          {
              super(task);
              
              assert task != null;
              
              this.task = task;
              this.lastModified = lastModified;
          }
          
          public URLMonitorTask getURLMonitorTask()
          {
              return task;
          }
          
          public URL getURL()
          {
              return getURLMonitorTask().getURL();
          }
          
          public long getLastModified()
          {
              return lastModified;
          }
      }
      
      public static interface Listener
      {
          void doURLChanged(Event event);
      }
      
      public void addListener(final Listener listener) {
          if (listener == null) {
              throw new NullArgumentException("listener");
          }
          
          // only add the listener if it isn't already in the list
          if (!listeners.contains(listener)) {
              listeners.add(listener);
          }
      }
      
      public void removeThrowableListener(final Listener listener) {
          if (listener == null) {
              throw new NullArgumentException("listener");
          }
          
          listeners.remove(listener);
      }
      
      protected void fireURLChanged(final Event event) {
          assert event != null;
          
          Object[] list = listeners.toArray();
          
          for (int i=0; i<list.length; i++) {
              Listener listener = (Listener)list[i];
              assert listener != null;
              
              listener.doURLChanged(event);
          }
      }
  }
  
  
  

Mime
View raw message