donaldp 2002/09/07 00:15:56
Modified: monitor/src/java/org/apache/avalon/excalibur/monitor
ActiveMonitor.java Resource.java
Added: monitor/src/java/org/apache/avalon/excalibur/monitor/impl
AbstractMonitor.java ActiveMonitor.java
Log:
Extract an active monitor implementation and decouple it from framework. Leave the
old class extending from new ActiveMonitor for backwards compatability.
Revision Changes Path
1.13 +58 -126
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.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- ActiveMonitor.java 13 Jun 2002 17:24:52 -0000 1.12
+++ ActiveMonitor.java 7 Sep 2002 07:15:56 -0000 1.13
@@ -8,13 +8,12 @@
package org.apache.avalon.excalibur.monitor;
import java.lang.reflect.Constructor;
-import java.util.HashMap;
-import java.util.Map;
import org.apache.avalon.framework.activity.Startable;
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;
/**
@@ -38,16 +37,22 @@
* @version $Id$
*/
public final class ActiveMonitor
- extends AbstractLogEnabled
- implements Monitor, Startable, ThreadSafe, Configurable, Runnable
+ extends org.apache.avalon.excalibur.monitor.impl.ActiveMonitor
+ implements Monitor, LogEnabled, Configurable, Startable, ThreadSafe
{
- private static final Class[] m_constructorParams = new Class[]{String.class};
- private static final Resource[] m_arrayType = new Resource[]{};
- private final Thread m_monitorThread = new Thread( this );
- private long m_frequency;
- private int m_priority;
- private Map m_resources = new HashMap();
- private boolean m_keepRunning = true;
+ private static final Class[] c_constructorParams = new Class[]{String.class};
+
+ private Logger m_logger;
+
+ public void enableLogging( final Logger logger )
+ {
+ m_logger = logger;
+ }
+
+ private Logger getLogger()
+ {
+ return m_logger;
+ }
/**
* Configure the ActiveMonitor.
@@ -55,145 +60,72 @@
public final void configure( Configuration conf )
throws ConfigurationException
{
- m_frequency = conf.getChild( "thread" ).getAttributeAsLong( "frequency",
1000L * 60L );
- m_priority = conf.getChild( "thread" ).getAttributeAsInteger( "priority",
Thread.MIN_PRIORITY );
- ClassLoader loader = Thread.currentThread().getContextClassLoader();
- Configuration[] initialResources = conf.getChild( "init-resources"
).getChildren( "resource" );
-
+ final Configuration thread = conf.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 " +
- m_frequency + " milliseconds with a thread priority
of " +
- m_priority + "(Minimum = " + Thread.MIN_PRIORITY +
+ 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++ )
{
- String key = initialResources[ i ].getAttribute( "key", "*** KEY NOT
SPECIFIED ***" );
- String className = initialResources[ i ].getAttribute( "class", "***
CLASSNAME NOT SPECIFIED ***" );
+ final Configuration initialResource = initialResources[ i ];
+ final String key =
+ initialResource.getAttribute( "key", "** Unspecified key **" );
+ final String className =
+ initialResource.getAttribute( "class", "** Unspecified class **" );
try
{
- Class clazz = loader.loadClass( className );
- Constructor initializer = clazz.getConstructor(
ActiveMonitor.m_constructorParams );
- this.addResource( (Resource)initializer.newInstance( new
Object[]{key} ) );
+ final Resource resource = createResource( className, key );
+ addResource( resource );
if( getLogger().isDebugEnabled() )
{
- getLogger().debug( "Initial Resource: \"" + key + "\"
Initialized." );
+ final String message =
+ "Initial Resource: \"" + key + "\" Initialized.";
+ getLogger().debug( message );
}
}
- catch( Exception e )
+ catch( final Exception e )
{
if( getLogger().isWarnEnabled() )
{
- getLogger().warn( "Initial Resource: \"" + key +
- "\" Failed (" + className + ").", e );
+ final String message =
+ "Initial Resource: \"" + key +
+ "\" Failed (" + className + ").";
+ getLogger().warn( message, e );
}
}
}
}
- public final void start()
- throws Exception
- {
- m_monitorThread.setDaemon( true );
- m_monitorThread.setPriority( Thread.MIN_PRIORITY );
- m_monitorThread.start();
- }
-
- public final void stop()
+ private Resource createResource( final String className,
+ final String key )
throws Exception
{
- m_keepRunning = false;
- m_monitorThread.join();
- }
-
- /**
- * 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 )
- {
-
- 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 );
- }
- }
- }
-
- /**
- * Find a monitored resource. If no resource is available, return null
- */
- public final Resource getResource( final String key )
- {
- synchronized( m_resources )
- {
- return (Resource)m_resources.get( key );
- }
- }
-
- /**
- * 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 )
- {
- this.removeResource( resource.getResourceKey() );
- }
-
- public final void run()
- {
- while( m_keepRunning )
- {
- long currentTestTime = System.currentTimeMillis();
- 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
- }
- }
-
- Resource[] resources;
-
- synchronized( m_resources )
- {
- resources = (Resource[])m_resources.values().toArray(
ActiveMonitor.m_arrayType );
- }
-
- for( int i = 0; i < resources.length; i++ )
- {
- resources[ i ].testModifiedAfter( currentTestTime );
- }
- }
+ 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;
}
}
1.9 +5 -4
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.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- Resource.java 13 May 2002 12:17:40 -0000 1.8
+++ Resource.java 7 Sep 2002 07:15:56 -0000 1.9
@@ -27,11 +27,12 @@
public abstract class Resource
implements Modifiable
{
+ private static final Set m_propertyListeners = Collections.synchronizedSet( new
HashSet() );
protected static final String MODIFIED = "last-modified";
+
protected PropertyChangeSupport m_eventSupport = new PropertyChangeSupport(
this );
private final String m_resourceKey;
protected long m_previousModified = 0L;
- private static final Set m_propertyListeners = Collections.synchronizedSet( new
HashSet() );
/**
* Required constructor. The <code>String</code> location is transformed by
@@ -76,7 +77,7 @@
* Abstract method to add the PropertyChangeListeners in another Resource to
* this one.
*/
- protected void addPropertyChangeListenersFrom( Resource other )
+ public void addPropertyChangeListenersFrom( Resource other )
{
PropertyChangeListener[] listeners = (PropertyChangeListener[])
other.m_propertyListeners.toArray( new PropertyChangeListener[]{} );
@@ -138,7 +139,7 @@
/**
* This cleanup method removes all listeners
*/
- protected void removeAllPropertyChangeListeners()
+ public void removeAllPropertyChangeListeners()
{
PropertyChangeListener[] listeners = (PropertyChangeListener[])
m_propertyListeners.toArray( new PropertyChangeListener[]{} );
1.1
jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/impl/AbstractMonitor.java
Index: AbstractMonitor.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 java.util.HashMap;
import java.util.Map;
import java.util.Collection;
import org.apache.avalon.excalibur.monitor.Monitor;
import org.apache.avalon.excalibur.monitor.Resource;
/**
* The AbstractMonitor class is a useful base class which all Monitors
* can extend. The particular monitoring policy is defined by the particular
* implementation.
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @author <a href="mailto:peter at apache.org">Peter Donald</a>
* @version $Id: AbstractMonitor.java,v 1.1 2002/09/07 07:15:56 donaldp Exp $
*/
public abstract class AbstractMonitor
implements Monitor
{
/**
* The set of resources that the monitor is monitoring.
*/
private Map m_resources = new HashMap();
/**
* 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 )
{
synchronized( m_resources )
{
final String resourceKey = resource.getResourceKey();
if( m_resources.containsKey( resourceKey ) )
{
final Resource original =
(Resource)m_resources.get( resourceKey );
original.addPropertyChangeListenersFrom( resource );
}
else
{
m_resources.put( resourceKey, resource );
}
}
}
/**
* Find a monitored resource. If no resource is available, return null
*/
public final Resource getResource( final String key )
{
synchronized( m_resources )
{
return (Resource)m_resources.get( key );
}
}
/**
* Remove a monitored resource by key.
*/
public final void removeResource( final String key )
{
synchronized( m_resources )
{
final Resource resource =
(Resource)m_resources.remove( key );
resource.removeAllPropertyChangeListeners();
}
}
/**
* Remove a monitored resource by reference.
*/
public final void removeResource( final Resource resource )
{
removeResource( resource.getResourceKey() );
}
/**
* Return an array containing all the resources currently monitored.
*
* @return an array containing all the resources currently monitored.
*/
protected Resource[] getResources()
{
final Collection collection = m_resources.values();
return (Resource[])collection.toArray( new Resource[ collection.size() ] );
}
}
1.1
jakarta-avalon-excalibur/monitor/src/java/org/apache/avalon/excalibur/monitor/impl/ActiveMonitor.java
Index: ActiveMonitor.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;
/**
* The ActiveMonitor is used to actively check a set of resources to see if they have
* changed. It will poll the resources with a frequency as specified or if
* unspecified with the default (60 seconds).
*
* @author <a href="mailto:[EMAIL PROTECTED]">Berin Loritsch</a>
* @author <a href="mailto:peter at apache.org">Peter Donald</a>
* @version $Id: ActiveMonitor.java,v 1.1 2002/09/07 07:15:56 donaldp Exp $
*/
public class ActiveMonitor
extends AbstractMonitor
implements Runnable
{
private static final long DEFAULT_FREQUENCY = 1000L * 60L;
/**
* The frequency to scan resources for changes measured
* in milliseconds.
*/
private long m_frequency = DEFAULT_FREQUENCY;
/**
* The priority of the thread that monitors resources. Defaults
* to System specific {@link Thread#MIN_PRIORITY}.
*/
private int m_priority = Thread.MIN_PRIORITY;
/**
* The thread that does the monitoring.
*/
private final Thread m_monitorThread = new Thread( this );
/**
* Set to false to shutdown the thread.
*/
private boolean m_keepRunning = true;
/**
* Set the frequency with which the monitor
* checks the resources. This can be changed
* anytime and will be enabled the next time
* through the check.
*
* @param frequency the frequency to scan resources for changes
*/
public void setFrequency( final long frequency )
{
m_frequency = frequency;
}
/**
* Set the priority of the active monitors thread.
*
* @param priority the priority of the active monitors thread.
*/
public void setPriority( final int priority )
{
m_priority = priority;
}
public final void start()
throws Exception
{
m_monitorThread.setDaemon( true );
m_monitorThread.setPriority( m_priority );
m_monitorThread.start();
}
public final void stop()
throws Exception
{
m_keepRunning = false;
m_monitorThread.join();
}
public final void run()
{
while( m_keepRunning )
{
long currentTestTime = System.currentTimeMillis();
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
}
}
final Resource[] resources = getResources();
for( int i = 0; i < resources.length; i++ )
{
resources[ i ].testModifiedAfter( currentTestTime );
}
}
}
}
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>