1 50 package org.apache.avalon.excalibur.monitor.impl; 51 52 import java.util.Collection ; 53 import java.util.HashMap ; 54 import java.util.Map ; 55 56 import org.apache.avalon.excalibur.monitor.Monitor; 57 import org.apache.avalon.excalibur.monitor.Resource; 58 import org.apache.avalon.framework.logger.AbstractLogEnabled; 59 60 69 public abstract class AbstractMonitor 70 implements Monitor 71 { 72 75 private Map m_resources = new HashMap (); 76 77 82 public final void addResources( final Resource[] resources ) 83 { 84 for( int i = 0; i < resources.length; i++ ) 85 { 86 addResource( resources[ i ] ); 87 } 88 } 89 90 94 public final void addResource( final Resource resource ) 95 { 96 synchronized( m_resources ) 97 { 98 final String resourceKey = resource.getResourceKey(); 99 if( m_resources.containsKey( resourceKey ) ) 100 { 101 final Resource original = 102 (Resource)m_resources.get( resourceKey ); 103 original.addPropertyChangeListenersFrom( resource ); 104 } 105 else 106 { 107 m_resources.put( resourceKey, resource ); 108 } 109 } 110 } 111 112 115 public Resource getResource( final String key ) 116 { 117 synchronized( m_resources ) 118 { 119 return (Resource)m_resources.get( key ); 120 } 121 } 122 123 126 public final void removeResource( final String key ) 127 { 128 synchronized( m_resources ) 129 { 130 final Resource resource = 131 (Resource)m_resources.remove( key ); 132 resource.removeAllPropertyChangeListeners(); 133 } 134 } 135 136 139 public final void removeResource( final Resource resource ) 140 { 141 removeResource( resource.getResourceKey() ); 142 } 143 144 149 protected Resource[] getResources() 150 { 151 final Collection collection = m_resources.values(); 152 return (Resource[])collection.toArray( new Resource[ collection.size() ] ); 153 } 154 155 158 protected void scanAllResources() 159 { 160 final long currentTestTime = System.currentTimeMillis(); 161 final Resource[] resources = getResources(); 162 for( int i = 0; i < resources.length; i++ ) 163 { 164 resources[ i ].testModifiedAfter( currentTestTime ); 165 } 166 } 167 } 168 | Popular Tags |