You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@avalon.apache.org by do...@apache.org on 2002/09/07 14:14:02 UTC

cvs commit: jakarta-avalon-excalibur/monitor/src/test/org/apache/avalon/excalibur/monitor/test MonitorTestCaseListener.java MonitorTestCase.java MonitorTestCase.xtest

donaldp     2002/09/07 05:14:02

  Modified:    monitor/src/java/org/apache/avalon/excalibur/monitor
                        ActiveMonitor.java Monitor.java
                        MonitorableURLSource.java PassiveMonitor.java
                        Resource.java ResourceOutputStream.java
                        ResourceWriter.java SourceResource.java
               monitor/src/java/org/apache/avalon/excalibur/monitor/impl
                        AbstractMonitor.java ActiveMonitor.java
               monitor/src/test/org/apache/avalon/excalibur/monitor/test
                        MonitorTestCase.java MonitorTestCase.xtest
  Added:       monitor/src/java/org/apache/avalon/excalibur/monitor
                        MonitorUtil.java
               monitor/src/java/org/apache/avalon/excalibur/monitor/impl
                        PassiveMonitor.java
               monitor/src/test/org/apache/avalon/excalibur/monitor/test
                        MonitorTestCaseListener.java
  Log:
  Decouple the majority of Monitor from Avalon/Framework. The old Active/PassiveMonitor are now just shallwo classes that wrap the underying classes.
  
  Revision  Changes    Path
  1.14      +16 -67    jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/ActiveMonitor.java
  
  Index: ActiveMonitor.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/ActiveMonitor.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- ActiveMonitor.java	7 Sep 2002 07:15:56 -0000	1.13
  +++ ActiveMonitor.java	7 Sep 2002 12:14:01 -0000	1.14
  @@ -7,7 +7,6 @@
    */
   package org.apache.avalon.excalibur.monitor;
   
  -import java.lang.reflect.Constructor;
   import org.apache.avalon.framework.activity.Startable;
   import org.apache.avalon.framework.configuration.Configurable;
   import org.apache.avalon.framework.configuration.Configuration;
  @@ -38,10 +37,8 @@
    */
   public final class ActiveMonitor
       extends org.apache.avalon.excalibur.monitor.impl.ActiveMonitor
  -    implements Monitor, LogEnabled, Configurable, Startable, ThreadSafe
  +    implements LogEnabled, Configurable, Startable, ThreadSafe
   {
  -    private static final Class[] c_constructorParams = new Class[]{String.class};
  -
       private Logger m_logger;
   
       public void enableLogging( final Logger logger )
  @@ -49,83 +46,35 @@
           m_logger = logger;
       }
   
  -    private Logger getLogger()
  -    {
  -        return m_logger;
  -    }
  -
       /**
        * Configure the ActiveMonitor.
        */
  -    public final void configure( Configuration conf )
  +    public final void configure( final Configuration config )
           throws ConfigurationException
       {
  -        final Configuration thread = conf.getChild( "thread" );
  +        final Configuration thread = config.getChild( "thread" );
           final long frequency =
               thread.getAttributeAsLong( "frequency", 1000L * 60L );
           final int priority =
               thread.getAttributeAsInteger( "priority", Thread.MIN_PRIORITY );
  -        if( getLogger().isDebugEnabled() )
  -        {
  -            getLogger().debug( "Active monitor will sample all resources every " +
  -                               frequency + " milliseconds with a thread priority of " +
  -                               priority + "(Minimum = " + Thread.MIN_PRIORITY +
  -                               ", Normal = " + Thread.NORM_PRIORITY +
  -                               ", Maximum = " + Thread.MAX_PRIORITY + ")." );
  -        }
  -
  -        final Configuration[] resources =
  -            conf.getChild( "init-resources" ).getChildren( "resource" );
  -        configureResources( resources );
   
           setFrequency( frequency );
           setPriority( priority );
  -    }
   
  -    private void configureResources( final Configuration[] initialResources )
  -    {
  -        for( int i = 0; i < initialResources.length; i++ )
  +        if( m_logger.isDebugEnabled() )
           {
  -            final Configuration initialResource = initialResources[ i ];
  -            final String key =
  -                initialResource.getAttribute( "key", "** Unspecified key **" );
  -            final String className =
  -                initialResource.getAttribute( "class", "** Unspecified class **" );
  -
  -            try
  -            {
  -                final Resource resource = createResource( className, key );
  -                addResource( resource );
  -
  -                if( getLogger().isDebugEnabled() )
  -                {
  -                    final String message =
  -                        "Initial Resource: \"" + key + "\" Initialized.";
  -                    getLogger().debug( message );
  -                }
  -            }
  -            catch( final Exception e )
  -            {
  -                if( getLogger().isWarnEnabled() )
  -                {
  -                    final String message =
  -                        "Initial Resource: \"" + key +
  -                        "\" Failed (" + className + ").";
  -                    getLogger().warn( message, e );
  -                }
  -            }
  +            m_logger.debug( "Active monitor will sample all resources every " +
  +                            frequency + " milliseconds with a thread priority of " +
  +                            priority + "(Minimum = " + Thread.MIN_PRIORITY +
  +                            ", Normal = " + Thread.NORM_PRIORITY +
  +                            ", Maximum = " + Thread.MAX_PRIORITY + ")." );
           }
  -    }
   
  -    private Resource createResource( final String className,
  -                                     final String key )
  -        throws Exception
  -    {
  -        final ClassLoader loader = Thread.currentThread().getContextClassLoader();
  -        final Class clazz = loader.loadClass( className );
  -        final Constructor initializer =
  -            clazz.getConstructor( ActiveMonitor.c_constructorParams );
  -        final Resource resource = (Resource)initializer.newInstance( new Object[]{key} );
  -        return resource;
  +        final Configuration[] resourcesConfig =
  +            config.getChild( "init-resources" ).getChildren( "resource" );
  +        final Resource[] resources =
  +            MonitorUtil.configureResources( resourcesConfig, m_logger );
  +        addResources( resources );
  +
       }
   }
  
  
  
  1.9       +2 -3      jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/Monitor.java
  
  Index: Monitor.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/Monitor.java,v
  retrieving revision 1.8
  retrieving revision 1.9
  diff -u -r1.8 -r1.9
  --- Monitor.java	13 Jun 2002 17:24:52 -0000	1.8
  +++ Monitor.java	7 Sep 2002 12:14:01 -0000	1.9
  @@ -11,8 +11,7 @@
   
   /**
    * The Monitor is used to actively check a set of resources to see if they have
  - * changed.  It will be implemented as a Component, that can be retrieved from
  - * the ComponentLocator.
  + * changed.
    *
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
    * @version $Id$
  
  
  
  1.6       +5 -8      jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/MonitorableURLSource.java
  
  Index: MonitorableURLSource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/MonitorableURLSource.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- MonitorableURLSource.java	12 Jun 2002 09:24:14 -0000	1.5
  +++ MonitorableURLSource.java	7 Sep 2002 12:14:01 -0000	1.6
  @@ -10,17 +10,15 @@
   import org.apache.excalibur.source.impl.URLSource;
   
   /**
  - * This adds the <code>Monitorable</code> interface to the URLSource.
  + * This adds the {@link Monitorable} interface to the {@link URLSource}.
    *
    * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
    * @version CVS $Revision$ $Date$
    */
  -
   public class MonitorableURLSource
       extends URLSource
       implements Monitorable
   {
  -
       /**
        * Constructor
        */
  @@ -34,15 +32,14 @@
       public Resource getResource()
           throws Exception
       {
  -        this.checkInfos();
  -        if( null == this.file )
  +        checkInfos();
  +        if( null != file )
           {
  -            return new FileResource( this.file.getAbsolutePath() );
  +            return new FileResource( file.getAbsolutePath() );
           }
           else
           {
               return new SourceResource( this );
           }
       }
  -
   }
  
  
  
  1.11      +15 -104   jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/PassiveMonitor.java
  
  Index: PassiveMonitor.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/PassiveMonitor.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- PassiveMonitor.java	13 Jun 2002 17:24:52 -0000	1.10
  +++ PassiveMonitor.java	7 Sep 2002 12:14:01 -0000	1.11
  @@ -7,14 +7,11 @@
    */
   package org.apache.avalon.excalibur.monitor;
   
  -import java.lang.reflect.Constructor;
  -import java.util.Collections;
  -import java.util.HashMap;
  -import java.util.Map;
   import org.apache.avalon.framework.configuration.Configurable;
   import org.apache.avalon.framework.configuration.Configuration;
   import org.apache.avalon.framework.configuration.ConfigurationException;
  -import org.apache.avalon.framework.logger.AbstractLogEnabled;
  +import org.apache.avalon.framework.logger.LogEnabled;
  +import org.apache.avalon.framework.logger.Logger;
   import org.apache.avalon.framework.thread.ThreadSafe;
   
   /**
  @@ -37,109 +34,23 @@
    * @version $Id$
    */
   public final class PassiveMonitor
  -    extends AbstractLogEnabled
  -    implements Monitor, ThreadSafe, Configurable
  +    extends org.apache.avalon.excalibur.monitor.impl.PassiveMonitor
  +    implements LogEnabled, Configurable, ThreadSafe
   {
  -    private static final Class[] m_constructorParams = new Class[]{String.class};
  -    private Map m_resources = new HashMap();
  -    private Map m_lastModified = Collections.synchronizedMap( new HashMap() );
  +    private Logger m_logger;
   
  -    public final void configure( final Configuration conf )
  -        throws ConfigurationException
  -    {
  -        ClassLoader loader = Thread.currentThread().getContextClassLoader();
  -        Configuration[] initialResources = conf.getChild( "init-resources" ).getChildren( "resource" );
  -
  -        for( int i = 0; i < initialResources.length; i++ )
  -        {
  -            String key = initialResources[ i ].getAttribute( "key", "*** KEY NOT SPECIFIED ***" );
  -            String className = initialResources[ i ].getAttribute( "class", "*** CLASSNAME NOT SPECIFIED ***" );
  -
  -            try
  -            {
  -                Class clazz = loader.loadClass( className );
  -                Constructor initializer = clazz.getConstructor( PassiveMonitor.m_constructorParams );
  -                this.addResource( (Resource)initializer.newInstance( new Object[]{key} ) );
  -
  -                if( getLogger().isDebugEnabled() )
  -                {
  -                    getLogger().debug( "Initial Resource: \"" + key + "\" Initialized." );
  -                }
  -            }
  -            catch( Exception e )
  -            {
  -                if( getLogger().isWarnEnabled() )
  -                {
  -                    getLogger().warn( "Initial Resource: \"" + key +
  -                                      "\" Failed (" + className + ").", e );
  -                }
  -            }
  -        }
  -    }
  -
  -    /**
  -     * Add a resource to monitor.  The resource key referenced in the other
  -     * interfaces is derived from the resource object.
  -     */
  -    public final void addResource( final Resource resource )
  +    public void enableLogging( final Logger logger )
       {
  -
  -        synchronized( m_resources )
  -        {
  -            if( m_resources.containsKey( resource.getResourceKey() ) )
  -            {
  -                Resource original = (Resource)m_resources.get( resource.getResourceKey() );
  -                original.addPropertyChangeListenersFrom( resource );
  -            }
  -            else
  -            {
  -                m_resources.put( resource.getResourceKey(), resource );
  -            }
  -        }
  +        m_logger = logger;
       }
   
  -    /**
  -     * Find a monitored resource.  If no resource is available, return null
  -     */
  -    public final Resource getResource( final String key )
  -    {
  -        synchronized( m_resources )
  -        {
  -            Resource resource = (Resource)m_resources.get( key );
  -
  -            if( resource != null )
  -            {
  -                Long lastModified = (Long)m_lastModified.get( key );
  -
  -                if( lastModified != null )
  -                {
  -                    resource.testModifiedAfter( lastModified.longValue() );
  -                }
  -
  -                m_lastModified.put( key, new Long( System.currentTimeMillis() ) );
  -            }
  -
  -            return resource;
  -        }
  -    }
  -
  -    /**
  -     * Remove a monitored resource by key.
  -     */
  -    public final void removeResource( final String key )
  -    {
  -        synchronized( m_resources )
  -        {
  -            Resource resource = (Resource)m_resources.remove( key );
  -            resource.removeAllPropertyChangeListeners();
  -        }
  -    }
  -
  -    /**
  -     * Remove a monitored resource by reference.
  -     */
  -    public final void removeResource( final Resource resource )
  +    public final void configure( final Configuration config )
  +        throws ConfigurationException
       {
  -        this.removeResource( resource.getResourceKey() );
  +        final Configuration[] initialResources =
  +            config.getChild( "init-resources" ).getChildren( "resource" );
  +        final Resource[] resources =
  +            MonitorUtil.configureResources( initialResources, m_logger );
  +        addResources( resources );
       }
   }
  
  
  
  1.10      +6 -7      jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/Resource.java
  
  Index: Resource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/Resource.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- Resource.java	7 Sep 2002 07:15:56 -0000	1.9
  +++ Resource.java	7 Sep 2002 12:14:01 -0000	1.10
  @@ -35,7 +35,7 @@
       protected long m_previousModified = 0L;
   
       /**
  -     * Required constructor.  The <code>String</code> location is transformed by
  +     * Required constructor.  The {@link String} location is transformed by
        * the specific resource monitor.  For instance, a FileResource will be able
        * to convert a string representation of a path to the proper File object.
        */
  @@ -62,8 +62,7 @@
        */
       public void testModifiedAfter( long time )
       {
  -        long lastModified = this.lastModified();
  -
  +        final long lastModified = lastModified();
           if( lastModified > m_previousModified || lastModified > time )
           {
               m_eventSupport.firePropertyChange( Resource.MODIFIED,
  @@ -84,7 +83,7 @@
   
           for( int i = 0; i < listeners.length; i++ )
           {
  -            this.addPropertyChangeListener( listeners[ i ] );
  +            addPropertyChangeListener( listeners[ i ] );
           }
       }
   
  @@ -133,7 +132,7 @@
        */
       public final boolean hasListeners()
       {
  -        return m_eventSupport.hasListeners( this.getResourceKey() );
  +        return m_eventSupport.hasListeners( getResourceKey() );
       }
   
       /**
  @@ -146,7 +145,7 @@
   
           for( int i = 0; i < listeners.length; i++ )
           {
  -            this.removePropertyChangeListener( listeners[ i ] );
  +            removePropertyChangeListener( listeners[ i ] );
           }
       }
   
  
  
  
  1.7       +3 -2      jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/ResourceOutputStream.java
  
  Index: ResourceOutputStream.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/ResourceOutputStream.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ResourceOutputStream.java	13 May 2002 12:17:40 -0000	1.6
  +++ ResourceOutputStream.java	7 Sep 2002 12:14:01 -0000	1.7
  @@ -21,7 +21,8 @@
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
    * @version $Id$
    */
  -final class ResourceOutputStream extends FilterOutputStream
  +final class ResourceOutputStream
  +    extends FilterOutputStream
   {
       private final StreamResource m_resource;
   
  
  
  
  1.7       +3 -2      jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/ResourceWriter.java
  
  Index: ResourceWriter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/ResourceWriter.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- ResourceWriter.java	13 May 2002 12:17:40 -0000	1.6
  +++ ResourceWriter.java	7 Sep 2002 12:14:01 -0000	1.7
  @@ -21,7 +21,8 @@
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
    * @version $Id$
    */
  -final class ResourceWriter extends FilterWriter
  +final class ResourceWriter
  +    extends FilterWriter
   {
       private final StreamResource m_resource;
   
  
  
  
  1.7       +2 -3      jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/SourceResource.java
  
  Index: SourceResource.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/SourceResource.java,v
  retrieving revision 1.6
  retrieving revision 1.7
  diff -u -r1.6 -r1.7
  --- SourceResource.java	23 Aug 2002 05:33:59 -0000	1.6
  +++ SourceResource.java	7 Sep 2002 12:14:01 -0000	1.7
  @@ -18,7 +18,6 @@
   import org.apache.excalibur.source.SourceValidity;
   
   /**
  - *
    * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
    * @version $Id$
    */
  @@ -49,7 +48,7 @@
        */
       public long lastModified()
       {
  -        if( m_validity == null )
  +        if( null == m_validity )
           {
               return System.currentTimeMillis();
           }
  
  
  
  1.1                  jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/MonitorUtil.java
  
  Index: MonitorUtil.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.monitor;
  
  import org.apache.avalon.framework.configuration.Configuration;
  import org.apache.avalon.framework.logger.Logger;
  import java.util.ArrayList;
  import java.lang.reflect.Constructor;
  
  /**
   * A class that contains a few utility methods for working
   * creating resource sets from Avalons configuration objects.
   *
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/09/07 12:14:01 $
   */
  class MonitorUtil
  {
      private static final Class[] c_constructorParams =
          new Class[]{String.class};
  
      public static Resource[] configureResources( final Configuration[] resources,
                                                   final Logger logger )
      {
          final ArrayList results = new ArrayList();
          for( int i = 0; i < resources.length; i++ )
          {
              final Configuration initialResource = resources[ i ];
              final String key =
                  initialResource.getAttribute( "key", "** Unspecified key **" );
              final String className =
                  initialResource.getAttribute( "class", "** Unspecified class **" );
  
              try
              {
                  final Resource resource = createResource( className, key );
                  results.add( resource );
  
                  if( logger.isDebugEnabled() )
                  {
                      final String message =
                          "Initial Resource: \"" + key + "\" Initialized.";
                      logger.debug( message );
                  }
              }
              catch( final Exception e )
              {
                  if( logger.isWarnEnabled() )
                  {
                      final String message =
                          "Initial Resource: \"" + key +
                          "\" Failed (" + className + ").";
                      logger.warn( message, e );
                  }
              }
          }
  
          return (Resource[])results.toArray( new Resource[ results.size() ] );
      }
  
      private static Resource createResource( final String className,
                                              final String key )
          throws Exception
      {
          final ClassLoader loader = Thread.currentThread().getContextClassLoader();
          final Class clazz = loader.loadClass( className );
          final Constructor initializer =
              clazz.getConstructor( c_constructorParams );
          return (Resource)initializer.newInstance( new Object[]{key} );
      }
  }
  
  
  
  1.3       +2 -2      jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/impl/AbstractMonitor.java
  
  Index: AbstractMonitor.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/impl/AbstractMonitor.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- AbstractMonitor.java	7 Sep 2002 07:26:35 -0000	1.2
  +++ AbstractMonitor.java	7 Sep 2002 12:14:02 -0000	1.3
  @@ -68,7 +68,7 @@
       /**
        * Find a monitored resource.  If no resource is available, return null
        */
  -    public final Resource getResource( final String key )
  +    public Resource getResource( final String key )
       {
           synchronized( m_resources )
           {
  
  
  
  1.2       +19 -13    jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/impl/ActiveMonitor.java
  
  Index: ActiveMonitor.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/impl/ActiveMonitor.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ActiveMonitor.java	7 Sep 2002 07:15:56 -0000	1.1
  +++ ActiveMonitor.java	7 Sep 2002 12:14:02 -0000	1.2
  @@ -69,15 +69,16 @@
           m_priority = priority;
       }
   
  -    public final void start()
  +    public void start()
           throws Exception
       {
  +        m_keepRunning = true;
           m_monitorThread.setDaemon( true );
           m_monitorThread.setPriority( m_priority );
           m_monitorThread.start();
       }
   
  -    public final void stop()
  +    public void stop()
           throws Exception
       {
           m_keepRunning = false;
  @@ -89,19 +90,11 @@
           while( m_keepRunning )
           {
               long currentTestTime = System.currentTimeMillis();
  -            long sleepTillTime = currentTestTime + m_frequency;
  +            final long sleepTillTime = currentTestTime + m_frequency;
   
               while( (currentTestTime = System.currentTimeMillis()) < sleepTillTime )
               {
  -                try
  -                {
  -                    Thread.sleep( sleepTillTime - currentTestTime );
  -                }
  -                catch( InterruptedException e )
  -                {
  -                    // ignore interrupted exception and keep sleeping until it's
  -                    // time to wake up
  -                }
  +                delay( sleepTillTime - currentTestTime );
               }
   
               final Resource[] resources = getResources();
  @@ -109,6 +102,19 @@
               {
                   resources[ i ].testModifiedAfter( currentTestTime );
               }
  +        }
  +    }
  +
  +    private void delay( final long delay )
  +    {
  +        try
  +        {
  +            Thread.sleep( delay );
  +        }
  +        catch( InterruptedException e )
  +        {
  +            // ignore interrupted exception and keep sleeping until it's
  +            // time to wake up
           }
       }
   }
  
  
  
  1.1                  jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/impl/PassiveMonitor.java
  
  Index: PassiveMonitor.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.monitor.impl;
  
  import org.apache.avalon.excalibur.monitor.Resource;
  import java.util.Map;
  import java.util.Collections;
  import java.util.HashMap;
  
  /**
   * A passive monitor will check the reosurce each time it
   * is accessed.
   *
   * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
   * @author <a href="mailto:peter at apache.org">Peter Donald</a>
   * @version $Revision: 1.1 $ $Date: 2002/09/07 12:14:02 $
   */
  public class PassiveMonitor
      extends AbstractMonitor
  {
      private Map m_lastModified = Collections.synchronizedMap( new HashMap() );
  
      /**
       * Find a monitored resource.  If no resource is available, return null
       */
      public final Resource getResource( final String key )
      {
          final Resource resource = super.getResource( key );
          if( resource != null )
          {
              final Long lastModified = (Long)m_lastModified.get( key );
  
              if( lastModified != null )
              {
                  resource.testModifiedAfter( lastModified.longValue() );
              }
  
              m_lastModified.put( key,
                                  new Long( System.currentTimeMillis() ) );
          }
  
          return resource;
      }
  }
  
  
  
  1.12      +52 -117   jakarta-avalon-excalibur/monitor/src/test/org/apache/avalon/excalibur/monitor/test/MonitorTestCase.java
  
  Index: MonitorTestCase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/monitor/src/test/org/apache/avalon/excalibur/monitor/test/MonitorTestCase.java,v
  retrieving revision 1.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- MonitorTestCase.java	13 Jun 2002 13:06:27 -0000	1.11
  +++ MonitorTestCase.java	7 Sep 2002 12:14:02 -0000	1.12
  @@ -8,8 +8,6 @@
   
   package org.apache.avalon.excalibur.monitor.test;
   
  -import java.beans.PropertyChangeEvent;
  -import java.beans.PropertyChangeListener;
   import java.io.File;
   import java.io.FileWriter;
   import java.io.OutputStream;
  @@ -21,15 +19,16 @@
   import org.apache.avalon.framework.component.Component;
   import org.apache.avalon.framework.component.ComponentException;
   import org.apache.avalon.framework.component.ComponentSelector;
  -import org.apache.avalon.framework.logger.AbstractLogEnabled;
   
   /**
    * Junit TestCase for all the monitors in Excalibur.
    *
    * @author <a href="mailto:bloritsch@apache.org">Berin Loritsch</a>
  + * @author <a href="mailto:peter at apache.org">Peter Donald</a>
    * @version $Id$
    */
  -public class MonitorTestCase extends ExcaliburTestCase
  +public class MonitorTestCase
  +    extends ExcaliburTestCase
   {
       /**
        * The constructor for the MonitorTest
  @@ -49,10 +48,10 @@
           {
               selector = (ComponentSelector)manager.lookup( Monitor.ROLE + "Selector" );
               activeMonitor = (Monitor)selector.select( "active" );
  -
  +            getLogger().info( "Aquired Active monitor" );
               internalTestProcedure( activeMonitor, true );
           }
  -        catch( ComponentException ce )
  +        catch( final ComponentException ce )
           {
               if( getLogger().isDebugEnabled() )
               {
  @@ -64,7 +63,6 @@
           finally
           {
               assertTrue( "The monitor selector could not be retrieved.", null != selector );
  -
               selector.release( (Component)activeMonitor );
               manager.release( selector );
           }
  @@ -80,7 +78,7 @@
           {
               selector = (ComponentSelector)manager.lookup( Monitor.ROLE + "Selector" );
               passiveMonitor = (Monitor)selector.select( "passive" );
  -
  +            getLogger().info( "Aquired Passive monitor" );
               internalTestProcedure( passiveMonitor, false );
           }
           catch( ComponentException ce )
  @@ -101,114 +99,49 @@
           }
       }
   
  -    private void internalTestProcedure( Monitor testMonitor, boolean active )
  +    private void internalTestProcedure( final Monitor testMonitor,
  +                                        final boolean active )
       {
           try
           {
  -            long sleepTo;
  -            File thirdWheel = new File( "test.txt" );
  -
  +            final File thirdWheel = new File( "test.txt" );
               thirdWheel.createNewFile();
  +            thirdWheel.setLastModified( System.currentTimeMillis() );
  +            final MonitorTestCaseListener listener = new MonitorTestCaseListener();
  +            listener.enableLogging( getLogEnabledLogger() );
   
  -            MonitorTestCaseListener listener = new MonitorTestCaseListener();
  -            listener.enableLogging( this.getLogEnabledLogger() );
  -
  -            FileResource resource = new FileResource( "test.txt" );
  +            final FileResource resource = new FileResource( "test.txt" );
               resource.addPropertyChangeListener( listener );
   
               testMonitor.addResource( resource );
  -
  -            thirdWheel.setLastModified( System.currentTimeMillis() );
  +            longDelay();
   
               if( active )
               {
  -                FileWriter externalWriter = new FileWriter( thirdWheel );
  +                final FileWriter externalWriter = new FileWriter( thirdWheel );
                   externalWriter.write( "External Writer modification" );
                   externalWriter.flush();
                   externalWriter.close();
   
  -                sleepTo = System.currentTimeMillis() + 1000L;
  -
  -                while( System.currentTimeMillis() < sleepTo && ( !listener.hasBeenModified() ) )
  -                {
  -                    try
  -                    {
  -                        Thread.sleep( 10 );  // sleep 10 millis per iteration
  -                    }
  -                    catch( final InterruptedException ie )
  -                    {
  -                        // ignore and keep waiting
  -                    }
  -                }
  -
  -                assertTrue( "File not changed", listener.hasBeenModified() );
  +                getLogger().info( "Checking for modification on active monitor" );
  +                checkForModification( listener );
               }
   
  -            listener.reset();
  -
  -            OutputStream out = resource.setResourceAsStream();
  +            final OutputStream out = resource.setResourceAsStream();
               out.write( "Test line 1\n".getBytes() );
  -
  -            try
  -            {
  -                Thread.sleep( 10 ); // sleep 10 millis at a time
  -            }
  -            catch( final InterruptedException ie )
  -            {
  -                // ignore and keep waiting
  -            }
  -
  +            delay();
               out.flush();
               out.close();
   
  -            sleepTo = System.currentTimeMillis() + 1000L;
  +            checkForModification( listener );
   
  -            while( System.currentTimeMillis() < sleepTo && ( !listener.hasBeenModified() ) )
  -            {
  -                try
  -                {
  -                    Thread.sleep( 10 ); // sleep 10 millis at a time
  -                }
  -                catch( final InterruptedException ie )
  -                {
  -                    // ignore and keep waiting
  -                }
  -            }
  -
  -            assertTrue( "File not changed", listener.hasBeenModified() );
  -            listener.reset();
  -
  -            Writer write = resource.setResourceAsWriter();
  +            final Writer write = resource.setResourceAsWriter();
               write.write( "Test line 2\n" );
  -
  -            try
  -            {
  -                Thread.sleep( 10 ); // sleep 10 millis at a time
  -            }
  -            catch( final InterruptedException ie )
  -            {
  -                // ignore and keep waiting
  -            }
  -
  +            delay();
               write.flush();
               write.close();
   
  -            sleepTo = System.currentTimeMillis() + 1000L;
  -
  -            while( System.currentTimeMillis() < sleepTo && ( !listener.hasBeenModified() ) )
  -            {
  -                try
  -                {
  -                    Thread.sleep( 10 ); // sleep 10 millis at a time
  -                }
  -                catch( final InterruptedException ie )
  -                {
  -                    // ignore and keep waiting
  -                }
  -            }
  -
  -            assertTrue( "File not changed", listener.hasBeenModified() );
  -            listener.reset();
  +            checkForModification( listener );
   
               resource.removePropertyChangeListener( listener );
               testMonitor.removeResource( resource );
  @@ -225,40 +158,42 @@
           }
       }
   
  -    public static class MonitorTestCaseListener
  -        extends AbstractLogEnabled
  -        implements PropertyChangeListener
  +    private void delay()
       {
  -        private volatile boolean m_hasChanged = false;
  +        delay( 10 );
  +    }
   
  -        public boolean hasBeenModified()
  +    /**
  +     * Some filesystems are not sensitive enough so you need
  +     * to delay for a long enough period of time (ie 1 second).
  +     */
  +    private void longDelay()
  +    {
  +        delay( 1000 );
  +    }
  +
  +    private void delay( final int time )
  +    {
  +        try
           {
  -            return m_hasChanged;
  +            Thread.sleep( time ); // sleep 10 millis at a time
           }
  -
  -        public void reset()
  +        catch( final InterruptedException ie )
           {
  -            m_hasChanged = false;
  +            // ignore and keep waiting
           }
  +    }
   
  -        public void propertyChange( final PropertyChangeEvent propertyChangeEvent )
  +    private void checkForModification( final MonitorTestCaseListener listener )
  +    {
  +        final long sleepTo = System.currentTimeMillis() + 1000L;
  +        while( System.currentTimeMillis() < sleepTo &&
  +            (!listener.hasBeenModified()) )
           {
  -            m_hasChanged = true;
  -
  -            if( getLogger().isInfoEnabled() )
  -            {
  -                getLogger().info( "NOTIFICATION LATENCY: " + ( System.currentTimeMillis() -
  -                                                               ( (Long)propertyChangeEvent.getNewValue() ).longValue() ) +
  -                                  "ms" );
  -                getLogger().info( "Received notification for " +
  -                                  ( (FileResource)propertyChangeEvent.getSource() ).getResourceKey() );
  -                getLogger().info( propertyChangeEvent.getPropertyName() +
  -                                  "\n  IS::" + (Long)propertyChangeEvent.getNewValue() +
  -                                  "\n  WAS::" + (Long)propertyChangeEvent.getOldValue() +
  -                                  "\n  TIME SINCE LAST MOD::" +
  -                                  ( ( (Long)propertyChangeEvent.getNewValue() ).longValue() -
  -                                    ( (Long)propertyChangeEvent.getOldValue() ).longValue() ) );
  -            }
  +            delay();
           }
  +        assertTrue( "File not changed", listener.hasBeenModified() );
  +        listener.reset();
       }
  +
   }
  
  
  
  1.6       +2 -2      jakarta-avalon-excalibur/monitor/src/test/org/apache/avalon/excalibur/monitor/test/MonitorTestCase.xtest
  
  Index: MonitorTestCase.xtest
  ===================================================================
  RCS file: /home/cvs/jakarta-avalon-excalibur/monitor/src/test/org/apache/avalon/excalibur/monitor/test/MonitorTestCase.xtest,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- MonitorTestCase.xtest	17 Dec 2001 06:38:25 -0000	1.5
  +++ MonitorTestCase.xtest	7 Sep 2002 12:14:02 -0000	1.6
  @@ -26,10 +26,10 @@
        </targets>
   
        <categories>
  -       <category name="test" log-level="INFO">
  +       <category name="test" log-level="DEBUG">
            <log-target id-ref="root"/>
          </category>
  -       <category name="" log-level="INFO">
  +       <category name="" log-level="DEBUG">
            <log-target id-ref="root"/>
          </category>
        </categories>
  
  
  
  1.1                  jakarta-avalon-excalibur/monitor/src/test/org/apache/avalon/excalibur/monitor/test/MonitorTestCaseListener.java
  
  Index: MonitorTestCaseListener.java
  ===================================================================
  /*
   * Copyright (C) The Apache Software Foundation. All rights reserved.
   *
   * This software is published under the terms of the Apache Software License
   * version 1.1, a copy of which has been included with this distribution in
   * the LICENSE.txt file.
   */
  package org.apache.avalon.excalibur.monitor.test;
  
  import org.apache.avalon.framework.logger.AbstractLogEnabled;
  import org.apache.avalon.excalibur.monitor.FileResource;
  import java.beans.PropertyChangeListener;
  import java.beans.PropertyChangeEvent;
  
  class MonitorTestCaseListener
      extends AbstractLogEnabled
      implements PropertyChangeListener
  {
      private volatile boolean m_hasChanged = false;
  
      public boolean hasBeenModified()
      {
          return m_hasChanged;
      }
  
      public void reset()
      {
          m_hasChanged = false;
      }
  
      public void propertyChange( final PropertyChangeEvent propertyChangeEvent )
      {
          m_hasChanged = true;
  
          if( getLogger().isInfoEnabled() )
          {
              getLogger().info( "NOTIFICATION LATENCY: " + ( System.currentTimeMillis() -
                                                             ( (Long)propertyChangeEvent.getNewValue() ).longValue() ) +
                                "ms" );
              getLogger().info( "Received notification for " +
                                ( (FileResource)propertyChangeEvent.getSource() ).getResourceKey() );
              getLogger().info( propertyChangeEvent.getPropertyName() +
                                "\n  IS::" + (Long)propertyChangeEvent.getNewValue() +
                                "\n  WAS::" + (Long)propertyChangeEvent.getOldValue() +
                                "\n  TIME SINCE LAST MOD::" +
                                ( ( (Long)propertyChangeEvent.getNewValue() ).longValue() -
                                  ( (Long)propertyChangeEvent.getOldValue() ).longValue() ) );
          }
      }
  }
  
  
  

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>