KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nanocontainer > deployer > FolderContentPoller


1 package org.nanocontainer.deployer;
2
3 import org.apache.commons.vfs.FileObject;
4 import org.apache.commons.vfs.FileSystemException;
5 import org.picocontainer.Startable;
6
7 /**
8  * Component that polls a folder for children at regular intervals.
9  * @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 JavaDoc poller = new Runnable JavaDoc() {
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.setCurrentChildren(currentChildren);
24                     synchronized(FolderContentPoller.this) {
25                         FolderContentPoller.this.notify();
26                         FolderContentPoller.this.wait(2000);
27                     }
28                 } catch (FileSystemException e) {
29                     e.printStackTrace();
30                 } catch (InterruptedException JavaDoc e) {
31                     thread.interrupt();
32                 }
33             }
34         }
35     };
36     private Thread JavaDoc thread;
37     private boolean started = false;
38
39
40     public FolderContentPoller(FolderContentHandler folderChangeNotifier) {
41         this.folderContentHandler = folderChangeNotifier;
42         folder = folderChangeNotifier.getFolder();
43     }
44
45     public void start() {
46         if(started) throw new IllegalStateException JavaDoc("Already started");
47         thread = new Thread JavaDoc(poller);
48         thread.start();
49         started = true;
50     }
51
52     public void stop() {
53         if(!started) throw new IllegalStateException JavaDoc("Already stopped");
54         thread.interrupt();
55         started = true;
56     }
57
58 }
Popular Tags