01: package org.nanocontainer.deployer;
02:
03: import org.apache.commons.vfs.FileObject;
04: import org.apache.commons.vfs.FileSystemException;
05: import org.picocontainer.Startable;
06:
07: /**
08: * Component that polls a folder for children at regular intervals.
09: * @author Aslak Hellesøy
10: * @version $Revision: 2343 $
11: */
12: public class FolderContentPoller implements Startable {
13: private FolderContentHandler folderContentHandler;
14: private FileObject folder;
15:
16: private Runnable poller = new Runnable() {
17: public void run() {
18: while (!Thread.interrupted()) {
19: try {
20: // Have to "close" the folder to invalidate child cache
21: folder.close();
22: FileObject[] currentChildren = folder.getChildren();
23: folderContentHandler
24: .setCurrentChildren(currentChildren);
25: synchronized (FolderContentPoller.this ) {
26: FolderContentPoller.this .notify();
27: FolderContentPoller.this .wait(2000);
28: }
29: } catch (FileSystemException e) {
30: e.printStackTrace();
31: } catch (InterruptedException e) {
32: thread.interrupt();
33: }
34: }
35: }
36: };
37: private Thread thread;
38: private boolean started = false;
39:
40: public FolderContentPoller(FolderContentHandler folderChangeNotifier) {
41: this .folderContentHandler = folderChangeNotifier;
42: folder = folderChangeNotifier.getFolder();
43: }
44:
45: public void start() {
46: if (started)
47: throw new IllegalStateException("Already started");
48: thread = new Thread(poller);
49: thread.start();
50: started = true;
51: }
52:
53: public void stop() {
54: if (!started)
55: throw new IllegalStateException("Already stopped");
56: thread.interrupt();
57: started = true;
58: }
59:
60: }
|