KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > monitor > Resource


1 /*
2
3  ============================================================================
4                    The Apache Software License, Version 1.1
5  ============================================================================
6  
7  Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  
9  Redistribution and use in source and binary forms, with or without modifica-
10  tion, are permitted provided that the following conditions are met:
11  
12  1. Redistributions of source code must retain the above copyright notice,
13     this list of conditions and the following disclaimer.
14  
15  2. Redistributions in binary form must reproduce the above copyright notice,
16     this list of conditions and the following disclaimer in the documentation
17     and/or other materials provided with the distribution.
18  
19  3. The end-user documentation included with the redistribution, if any, must
20     include the following acknowledgment: "This product includes software
21     developed by the Apache Software Foundation (http://www.apache.org/)."
22     Alternately, this acknowledgment may appear in the software itself, if
23     and wherever such third-party acknowledgments normally appear.
24  
25  4. The names "Jakarta", "Avalon", "Excalibur" and "Apache Software Foundation"
26     must not be used to endorse or promote products derived from this software
27     without prior written permission. For written permission, please contact
28     apache@apache.org.
29  
30  5. Products derived from this software may not be called "Apache", nor may
31     "Apache" appear in their name, without prior written permission of the
32     Apache Software Foundation.
33  
34  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  
45  This software consists of voluntary contributions made by many individuals
46  on behalf of the Apache Software Foundation. For more information on the
47  Apache Software Foundation, please see <http://www.apache.org/>.
48  
49 */

50 package org.apache.avalon.excalibur.monitor;
51
52 import java.beans.PropertyChangeListener JavaDoc;
53 import java.beans.PropertyChangeSupport JavaDoc;
54 import java.util.Collections JavaDoc;
55 import java.util.HashSet JavaDoc;
56 import java.util.Set JavaDoc;
57
58 /**
59  * Managed Resource. All resources must have a constructor that takes a String
60  * and converts it to the needed format (i.e. File). A Managed Resource in the
61  * Monitor section has only one property needed to be changed: last modified.
62  * The property name for the last modified event will be the same as the resource
63  * key. Implementations may add additional properties, but for most instances the
64  * last modified property will be enough.
65  *
66  * @author <a HREF="mailto:bloritsch@apache.org">Berin Loritsch</a>
67  * @version $Id: Resource.java,v 1.20 2003/03/22 12:46:50 leosimons Exp $
68  */

69 public abstract class Resource
70     implements Modifiable
71 {
72     protected static final String JavaDoc MODIFIED = "last-modified";
73
74     /**
75      * The set of listeners for this particular resource.
76      */

77     private final Set JavaDoc m_propertyListeners = Collections.synchronizedSet( new HashSet JavaDoc() );
78
79     /**
80      * @deprecated Developers should use the setter/getters
81      * associated with field.
82      */

83     protected PropertyChangeSupport JavaDoc m_eventSupport = new PropertyChangeSupport JavaDoc( this );
84
85     /**
86      * The resource key is the identifier of the resource.
87      * ie A FileResource would have the filename as the resourceKey.
88      */

89     private final String JavaDoc m_resourceKey;
90
91     /**
92      * @deprecated Developers should use the setter/getters
93      * associated with field.
94      */

95     protected long m_previousModified = 0L;
96
97     /**
98      * Required constructor. The {@link String} location is transformed by
99      * the specific resource monitor. For instance, a FileResource will be able
100      * to convert a string representation of a path to the proper File object.
101      */

102     public Resource( final String JavaDoc resourceKey ) throws Exception JavaDoc
103     {
104         if( null == resourceKey )
105         {
106             throw new NullPointerException JavaDoc( "resourceKey" );
107         }
108
109         m_resourceKey = resourceKey;
110     }
111
112     /**
113      * Return the key for the resource.
114      */

115     public final String JavaDoc getResourceKey()
116     {
117         return m_resourceKey;
118     }
119
120     /**
121      * The time this was last modified.
122      */

123     public abstract long lastModified();
124
125     /**
126      * Test whether this has been modified since time X
127      */

128     public void testModifiedAfter( final long time )
129     {
130         if( getPreviousModified() > time )
131         {
132             //The next line should be uncommented for complete
133
//backward compatability. Unfortunately it does not
134
//make sense to add it or else you could get multiple
135
//notifications about a change.
136
//fireAndSetModifiedTime( lastModified );
137
return;
138         }
139
140         final long lastModified = lastModified();
141         if( lastModified > getPreviousModified() || lastModified > time )
142         {
143             fireAndSetModifiedTime( lastModified );
144         }
145     }
146
147     /**
148      * Fire a modify event and set the lastModified time as appropraite.
149      *
150      * @param lastModified the time modified at
151      */

152     protected void fireAndSetModifiedTime( final long lastModified )
153     {
154         getEventSupport().firePropertyChange( Resource.MODIFIED,
155                                               new Long JavaDoc( getPreviousModified() ),
156                                               new Long JavaDoc( lastModified ) );
157         setPreviousModified( lastModified );
158     }
159
160     /**
161      * Abstract method to add the PropertyChangeListeners in another Resource to
162      * this one.
163      */

164     public void addPropertyChangeListenersFrom( final Resource other )
165     {
166         PropertyChangeListener JavaDoc[] listeners = (PropertyChangeListener JavaDoc[])
167             other.m_propertyListeners.toArray( new PropertyChangeListener JavaDoc[]{} );
168
169         for( int i = 0; i < listeners.length; i++ )
170         {
171             addPropertyChangeListener( listeners[ i ] );
172         }
173     }
174
175     /**
176      * This is the prefered method of registering a {@link PropertyChangeListener}.
177      * It automatically registers the listener for the last modified event.
178      */

179     public final void addPropertyChangeListener( final PropertyChangeListener JavaDoc listener )
180     {
181         getEventSupport().addPropertyChangeListener( listener );
182         m_propertyListeners.add( listener );
183     }
184
185     /**
186      * This is a convenience if you want to expose other properties for the Resource.
187      * It is protected now, but you may override it with public access later.
188      */

189     protected void addPropertyChangeListener( final String JavaDoc property,
190                                               final PropertyChangeListener JavaDoc listener )
191     {
192         getEventSupport().addPropertyChangeListener( property, listener );
193         m_propertyListeners.add( listener );
194     }
195
196     /**
197      * This is the prefered method of unregistering a {@link PropertyChangeListener}.
198      * It automatically registers the listener for the last modified event.
199      */

200     public final void removePropertyChangeListener( final PropertyChangeListener JavaDoc listener )
201     {
202         getEventSupport().removePropertyChangeListener( listener );
203         m_propertyListeners.remove( listener );
204     }
205
206     /**
207      * This is a convenience if you want to expose other properties for the Resource.
208      * It is protected now, but you may override it with public access later.
209      */

210     protected void removePropertyChangeListener( final String JavaDoc property,
211                                                  final PropertyChangeListener JavaDoc listener )
212     {
213         getEventSupport().removePropertyChangeListener( property, listener );
214         m_propertyListeners.remove( listener );
215     }
216
217     /**
218      * This is the preferred method of determining if a Resource has listeners.
219      */

220     public final boolean hasListeners()
221     {
222         return getEventSupport().hasListeners( getResourceKey() );
223     }
224
225     /**
226      * This cleanup method removes all listeners
227      */

228     public void removeAllPropertyChangeListeners()
229     {
230         PropertyChangeListener JavaDoc[] listeners = (PropertyChangeListener JavaDoc[])
231             m_propertyListeners.toArray( new PropertyChangeListener JavaDoc[]{} );
232
233         for( int i = 0; i < listeners.length; i++ )
234         {
235             removePropertyChangeListener( listeners[ i ] );
236         }
237     }
238
239     /**
240      * This is a convenience if you want to expose other properties for the Resource.
241      * It is protected now, but you may override it with public access later.
242      */

243     protected boolean hasListeners( final String JavaDoc 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 JavaDoc getEventSupport()
259     {
260         return m_eventSupport;
261     }
262
263     public String JavaDoc toString()
264     {
265         return m_resourceKey;
266     }
267 }
268
Popular Tags