1 50 package org.apache.avalon.excalibur.monitor; 51 52 import java.io.File ; 53 import java.util.Collections ; 54 import java.util.HashMap ; 55 import java.util.HashSet ; 56 import java.util.Iterator ; 57 import java.util.Map ; 58 import java.util.Set ; 59 60 68 public class DirectoryResource 69 extends Resource 70 { 71 public static final String ADDED = "AddedFiles"; 72 public static final String REMOVED = "DeletedFiles"; 73 public static final String MODIFIED = "ModifiedFiles"; 74 75 private final File m_dir; 76 private Set m_files; 77 private Map m_times; 78 79 public DirectoryResource( final String resourceKey ) 80 throws Exception 81 { 82 super( resourceKey ); 83 m_dir = new File ( resourceKey ); 84 if( !m_dir.isDirectory() ) 85 { 86 final String message = m_dir + " is not a directory."; 87 throw new IllegalArgumentException ( message ); 88 } 89 90 m_files = new HashSet (); 91 m_times = new HashMap (); 92 93 final File [] files = m_dir.listFiles(); 94 for( int i = 0; i < files.length; i++ ) 95 { 96 final File file = files[ i ]; 97 m_files.add( file ); 98 m_times.put( file, new Long ( file.lastModified() ) ); 99 } 100 setPreviousModified( System.currentTimeMillis() ); 101 } 102 103 106 public void testModifiedAfter( final long time ) 107 { 108 if( getPreviousModified() > time ) 109 { 110 return; 111 } 112 113 final HashSet existingFiles = new HashSet (); 114 final HashSet modifiedFiles = new HashSet (); 115 final HashSet addedFiles = new HashSet (); 116 117 final File [] 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 file = files[ i ]; 126 final long newModTime = file.lastModified(); 127 if( m_files.contains( file ) ) 128 { 129 existingFiles.add( file ); 130 131 final Long oldModTime = (Long )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 ( 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( fileCount == lastCount && 152 0 == addedCount && 153 0 == modifiedCount ) 154 { 155 return; 156 } 157 158 final HashSet deletedFiles = new HashSet (); 159 160 if( fileCount != lastCount + addedCount ) 163 { 164 final Iterator iterator = m_files.iterator(); 166 while( iterator.hasNext() ) 167 { 168 final File file = (File )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 |