1 50 package org.apache.avalon.excalibur.monitor; 51 52 import java.beans.PropertyChangeListener ; 53 import java.beans.PropertyChangeSupport ; 54 import java.util.Collections ; 55 import java.util.HashSet ; 56 import java.util.Set ; 57 58 69 public abstract class Resource 70 implements Modifiable 71 { 72 protected static final String MODIFIED = "last-modified"; 73 74 77 private final Set m_propertyListeners = Collections.synchronizedSet( new HashSet () ); 78 79 83 protected PropertyChangeSupport m_eventSupport = new PropertyChangeSupport ( this ); 84 85 89 private final String m_resourceKey; 90 91 95 protected long m_previousModified = 0L; 96 97 102 public Resource( final String resourceKey ) throws Exception 103 { 104 if( null == resourceKey ) 105 { 106 throw new NullPointerException ( "resourceKey" ); 107 } 108 109 m_resourceKey = resourceKey; 110 } 111 112 115 public final String getResourceKey() 116 { 117 return m_resourceKey; 118 } 119 120 123 public abstract long lastModified(); 124 125 128 public void testModifiedAfter( final long time ) 129 { 130 if( getPreviousModified() > time ) 131 { 132 return; 138 } 139 140 final long lastModified = lastModified(); 141 if( lastModified > getPreviousModified() || lastModified > time ) 142 { 143 fireAndSetModifiedTime( lastModified ); 144 } 145 } 146 147 152 protected void fireAndSetModifiedTime( final long lastModified ) 153 { 154 getEventSupport().firePropertyChange( Resource.MODIFIED, 155 new Long ( getPreviousModified() ), 156 new Long ( lastModified ) ); 157 setPreviousModified( lastModified ); 158 } 159 160 164 public void addPropertyChangeListenersFrom( final Resource other ) 165 { 166 PropertyChangeListener [] listeners = (PropertyChangeListener []) 167 other.m_propertyListeners.toArray( new PropertyChangeListener []{} ); 168 169 for( int i = 0; i < listeners.length; i++ ) 170 { 171 addPropertyChangeListener( listeners[ i ] ); 172 } 173 } 174 175 179 public final void addPropertyChangeListener( final PropertyChangeListener listener ) 180 { 181 getEventSupport().addPropertyChangeListener( listener ); 182 m_propertyListeners.add( listener ); 183 } 184 185 189 protected void addPropertyChangeListener( final String property, 190 final PropertyChangeListener listener ) 191 { 192 getEventSupport().addPropertyChangeListener( property, listener ); 193 m_propertyListeners.add( listener ); 194 } 195 196 200 public final void removePropertyChangeListener( final PropertyChangeListener listener ) 201 { 202 getEventSupport().removePropertyChangeListener( listener ); 203 m_propertyListeners.remove( listener ); 204 } 205 206 210 protected void removePropertyChangeListener( final String property, 211 final PropertyChangeListener listener ) 212 { 213 getEventSupport().removePropertyChangeListener( property, listener ); 214 m_propertyListeners.remove( listener ); 215 } 216 217 220 public final boolean hasListeners() 221 { 222 return getEventSupport().hasListeners( getResourceKey() ); 223 } 224 225 228 public void removeAllPropertyChangeListeners() 229 { 230 PropertyChangeListener [] listeners = (PropertyChangeListener []) 231 m_propertyListeners.toArray( new PropertyChangeListener []{} ); 232 233 for( int i = 0; i < listeners.length; i++ ) 234 { 235 removePropertyChangeListener( listeners[ i ] ); 236 } 237 } 238 239 243 protected boolean hasListeners( final String property ) 244 { 245 return getEventSupport().hasListeners( property ); 246 } 247 248 protected final long getPreviousModified() 249 { 250 return m_previousModified; 251 } 252 253 protected final void setPreviousModified( final long previousModified ) 254 { 255 m_previousModified = previousModified; 256 } 257 258 protected final PropertyChangeSupport getEventSupport() 259 { 260 return m_eventSupport; 261 } 262 263 public String toString() 264 { 265 return m_resourceKey; 266 } 267 } 268 | Popular Tags |