Skip to content

Commit 81eef5d

Browse files
author
Niall Pemberton
committed
IO-132 - add File Listener/Monitor
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@645221 13f79535-47bb-0310-9956-ffa450edef68
1 parent 500778f commit 81eef5d

8 files changed

Lines changed: 1713 additions & 0 deletions

File tree

Lines changed: 246 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,246 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.io.monitor;
18+
19+
import java.io.File;
20+
import java.io.Serializable;
21+
22+
/**
23+
* {@link FilesystemEntry} represents the state of a file or directory, capturing
24+
* the following {@link File} attributes at a point in time:
25+
* <ul>
26+
* <li>File Name (see {@link File#getName()})</li>
27+
* <li>Exists - whether the file exists or not (see {@link File#exists()})</li>
28+
* <li>Directory - whether the file is a directory or not (see {@link File#isDirectory()})</li>
29+
* <li>Last Modified Date/Time (see {@link File#lastModified()})</li>
30+
* <li>Children - contents of a directory (see {@link File#listFiles(java.io.FileFilter)})</li>
31+
* </ul>
32+
* <p>
33+
* <h3>Custom Implementations</h3>
34+
* If the state of additional {@link File} attributes is required then create a custom
35+
* {@link FilesystemEntry} with properties for those attributes. Override the
36+
* {@link #newChildInstance(File)} to return a new instance of the appropriate type.
37+
* You may also want to override the {@link #refresh()} and/or {@link #hasChanged()}
38+
* methods.
39+
*
40+
*/
41+
public class FilesystemEntry implements Serializable {
42+
43+
private FilesystemEntry parent;
44+
private FilesystemEntry[] children;
45+
private File file;
46+
private String name;
47+
private boolean exists;
48+
private boolean directory;
49+
private long lastModified;
50+
51+
/**
52+
* Construct a new monitor for a specified {@link File}.
53+
*
54+
* @param file The file being monitored
55+
*/
56+
public FilesystemEntry(File file) {
57+
this((FilesystemEntry)null, file);
58+
}
59+
60+
/**
61+
* Construct a new monitor for a specified {@link File}.
62+
*
63+
* @param parent The parent
64+
* @param file The file being monitored
65+
*/
66+
public FilesystemEntry(FilesystemEntry parent, File file) {
67+
if (file == null) {
68+
throw new IllegalArgumentException("File is missing");
69+
}
70+
this.file = file;
71+
this.parent = parent;
72+
this.name = file.getName();
73+
}
74+
75+
/**
76+
* Refresh the attributes from the underlying {@link File}.
77+
* <p>
78+
* This implementation refreshes the <code>name</code>, <code>exists</code>
79+
* <code>directory</code> and <code>lastModified</code> properties.
80+
*/
81+
public void refresh() {
82+
name = file.getName();
83+
exists = file.exists();
84+
if (exists) {
85+
directory = file.isDirectory();
86+
lastModified = file.lastModified();
87+
}
88+
}
89+
90+
/**
91+
* Create a new child instance.
92+
* <p>
93+
* Custom implementations should override this method to return
94+
* a new instance of the appropriate type.
95+
*
96+
* @param file The child file
97+
* @return a new child instance
98+
*/
99+
public FilesystemEntry newChildInstance(File file) {
100+
return new FilesystemEntry(this, file);
101+
}
102+
103+
/**
104+
* Indicate whether the file has changed or not.
105+
* <p>
106+
* This implementation compares the <code>lastModified<code>
107+
* value of the {@link File} with the stored value.
108+
*
109+
* @return whether the file has changed or not
110+
*/
111+
public boolean hasChanged() {
112+
return (lastModified != file.lastModified());
113+
}
114+
115+
/**
116+
* Return the parent entry.
117+
*
118+
* @return the parent entry
119+
*/
120+
public FilesystemEntry getParent() {
121+
return parent;
122+
}
123+
124+
/**
125+
* Return the level
126+
*
127+
* @return the level
128+
*/
129+
public int getLevel() {
130+
return parent == null ? 0 : parent.getLevel() + 1;
131+
}
132+
133+
/**
134+
* Return the directory's files.
135+
*
136+
* @return This directory's files or an empty
137+
* array if the file is not a directory or the
138+
* directory is empty
139+
*/
140+
public FilesystemEntry[] getChildren() {
141+
return children != null ? children : FilesystemObserver.EMPTY_ENTRIES;
142+
}
143+
144+
/**
145+
* Set the directory's files.
146+
*
147+
* @param children This directory's files, may be null
148+
*/
149+
public void setChildren(FilesystemEntry[] children) {
150+
this.children = children;
151+
}
152+
153+
/**
154+
* Return the file being monitored.
155+
*
156+
* @return the file being monitored
157+
*/
158+
public File getFile() {
159+
return file;
160+
}
161+
162+
/**
163+
* Set the file being monitored.
164+
*
165+
* @param file the file being monitored
166+
*/
167+
public void setFile(File file) {
168+
this.file = file;
169+
}
170+
171+
/**
172+
* Return the file name.
173+
*
174+
* @return the file name
175+
*/
176+
public String getName() {
177+
return name;
178+
}
179+
180+
/**
181+
* Set the file name.
182+
*
183+
* @param name the file name
184+
*/
185+
public void setName(String name) {
186+
this.name = name;
187+
}
188+
189+
/**
190+
* Return the last modified time from the last time it
191+
* was checked.
192+
*
193+
* @return the last modified time
194+
*/
195+
public long getLastModified() {
196+
return lastModified;
197+
}
198+
199+
/**
200+
* Return the last modified time from the last time it
201+
* was checked.
202+
*
203+
* @param lastModified The last modified time
204+
*/
205+
public void setLastModified(long lastModified) {
206+
this.lastModified = lastModified;
207+
}
208+
209+
/**
210+
* Indicate whether the file existed the last time it
211+
* was checked.
212+
*
213+
* @return whether the file existed
214+
*/
215+
public boolean isExists() {
216+
return exists;
217+
}
218+
219+
/**
220+
* Set whether the file existed the last time it
221+
* was checked.
222+
*
223+
* @param exists whether the file exists or not
224+
*/
225+
public void setExists(boolean exists) {
226+
this.exists = exists;
227+
}
228+
229+
/**
230+
* Indicate whether the file is a directory or not.
231+
*
232+
* @return whether the file is a directory or not
233+
*/
234+
public boolean isDirectory() {
235+
return directory;
236+
}
237+
238+
/**
239+
* Set whether the file is a directory or not.
240+
*
241+
* @param directory whether the file is a directory or not
242+
*/
243+
public void setDirectory(boolean directory) {
244+
this.directory = directory;
245+
}
246+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.io.monitor;
18+
import java.io.File;
19+
20+
/**
21+
* A listener that receives events of file system modifications.
22+
* <p>
23+
* Register {@link FilesystemListener}s with a {@link FilesystemObserver}.
24+
*
25+
* @see FilesystemObserver
26+
* @version $Id$
27+
* @since Commons IO 2.0
28+
*/
29+
public interface FilesystemListener {
30+
31+
/**
32+
* File system observer started checking event.
33+
*
34+
* @param observer The file system observer
35+
*/
36+
void onStart(final FilesystemObserver observer);
37+
38+
/**
39+
* Directory created Event.
40+
*
41+
* @param directory The directory created
42+
*/
43+
void onDirectoryCreate(final File directory);
44+
45+
/**
46+
* Directory changed Event.
47+
*
48+
* @param directory The directory changed
49+
*/
50+
void onDirectoryChange(final File directory);
51+
52+
/**
53+
* Directory deleted Event.
54+
*
55+
* @param directory The directory deleted
56+
*/
57+
void onDirectoryDelete(final File directory);
58+
59+
/**
60+
* File created Event.
61+
*
62+
* @param file The file created
63+
*/
64+
void onFileCreate(final File file);
65+
66+
/**
67+
* File changed Event.
68+
*
69+
* @param file The file changed
70+
*/
71+
void onFileChange(final File file);
72+
73+
/**
74+
* File deleted Event.
75+
*
76+
* @param file The file deleted
77+
*/
78+
void onFileDelete(final File file);
79+
80+
/**
81+
* File system observer finished checking event.
82+
*
83+
* @param observer The file system observer
84+
*/
85+
void onStop(final FilesystemObserver observer);
86+
}

0 commit comments

Comments
 (0)