KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.File JavaDoc;
53 import java.util.Collections JavaDoc;
54 import java.util.HashMap JavaDoc;
55 import java.util.HashSet JavaDoc;
56 import java.util.Iterator JavaDoc;
57 import java.util.Map JavaDoc;
58 import java.util.Set JavaDoc;
59
60 /**
61  * This is a Resource that monitors a directory. If any files
62  * are added, removed or modified in directory then it will
63  * send an event indicating the change.
64  *
65  * @author <a HREF="mailto:peter at apache.org">Peter Donald</a>
66  * @version $Revision: 1.7 $ $Date: 2003/03/22 12:46:50 $
67  */

68 public class DirectoryResource
69     extends Resource
70 {
71     public static final String JavaDoc ADDED = "AddedFiles";
72     public static final String JavaDoc REMOVED = "DeletedFiles";
73     public static final String JavaDoc MODIFIED = "ModifiedFiles";
74
75     private final File JavaDoc m_dir;
76     private Set JavaDoc m_files;
77     private Map JavaDoc m_times;
78
79     public DirectoryResource( final String JavaDoc resourceKey )
80         throws Exception JavaDoc
81     {
82         super( resourceKey );
83         m_dir = new File JavaDoc( resourceKey );
84         if( !m_dir.isDirectory() )
85         {
86             final String JavaDoc message = m_dir + " is not a directory.";
87             throw new IllegalArgumentException JavaDoc( message );
88         }
89
90         m_files = new HashSet JavaDoc();
91         m_times = new HashMap JavaDoc();
92
93         final File JavaDoc[] files = m_dir.listFiles();
94         for( int i = 0; i < files.length; i++ )
95         {
96             final File JavaDoc file = files[ i ];
97             m_files.add( file );
98             m_times.put( file, new Long JavaDoc( file.lastModified() ) );
99         }
100         setPreviousModified( System.currentTimeMillis() );
101     }
102
103     /**
104      * Test whether this has been modified since time X
105      */

106     public void testModifiedAfter( final long time )
107     {
108         if( getPreviousModified() > time )
109         {
110             return;
111         }
112
113         final HashSet JavaDoc existingFiles = new HashSet JavaDoc();
114         final HashSet JavaDoc modifiedFiles = new HashSet JavaDoc();
115         final HashSet JavaDoc addedFiles = new HashSet JavaDoc();
116
117         final File JavaDoc[] files = m_dir.listFiles();
118
119         int fileCount = 0;
120         if( null != files )
121         {
122             fileCount = files.length;
123             for( int i = 0; i < files.length; i++ )
124             {
125                 final File JavaDoc file = files[ i ];
126                 final long newModTime = file.lastModified();
127                 if( m_files.contains( file ) )
128                 {
129                     existingFiles.add( file );
130
131                     final Long JavaDoc oldModTime = (Long JavaDoc)m_times.get( file );
132                     if( oldModTime.longValue() != newModTime )
133                     {
134                         modifiedFiles.add( file );
135                     }
136                 }
137                 else
138                 {
139                     addedFiles.add( file );
140                 }
141                 m_times.put( file, new Long JavaDoc( newModTime ) );
142             }
143         }
144
145         final int lastCount = m_files.size();
146         final int addedCount = addedFiles.size();
147         final int modifiedCount = modifiedFiles.size();
148
149         //If no new files have been added and
150
//none deleted then nothing to do
151
if( fileCount == lastCount &&
152             0 == addedCount &&
153             0 == modifiedCount )
154         {
155             return;
156         }
157
158         final HashSet JavaDoc deletedFiles = new HashSet JavaDoc();
159
160         //If only new files were added and none were changed then
161
//we don't have to scan for deleted files
162
if( fileCount != lastCount + addedCount )
163         {
164             //Looks like we do have to scan for deleted files
165
final Iterator JavaDoc iterator = m_files.iterator();
166             while( iterator.hasNext() )
167             {
168                 final File JavaDoc file = (File JavaDoc)iterator.next();
169                 if( !existingFiles.contains( file ) )
170                 {
171                     deletedFiles.add( file );
172                     m_times.remove( file );
173                 }
174             }
175         }
176
177         final int deletedCount = deletedFiles.size();
178         if( 0 != deletedCount )
179         {
180             getEventSupport().firePropertyChange( REMOVED,
181                                                   Collections.EMPTY_SET,
182                                                   deletedFiles );
183         }
184         if( 0 != addedCount )
185         {
186             getEventSupport().firePropertyChange( ADDED,
187                                                   Collections.EMPTY_SET,
188                                                   addedFiles );
189         }
190
191         if( 0 != modifiedCount )
192         {
193             getEventSupport().firePropertyChange( MODIFIED,
194                                                   Collections.EMPTY_SET,
195                                                   modifiedFiles );
196         }
197
198         existingFiles.addAll( addedFiles );
199         m_files = existingFiles;
200     }
201
202     public long lastModified()
203     {
204         return getPreviousModified();
205     }
206 }
207
Popular Tags