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 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 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 e) { 31 thread.interrupt(); 32 } 33 } 34 } 35 }; 36 private Thread 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 ("Already started"); 47 thread = new Thread (poller); 48 thread.start(); 49 started = true; 50 } 51 52 public void stop() { 53 if(!started) throw new IllegalStateException ("Already stopped"); 54 thread.interrupt(); 55 started = true; 56 } 57 58 } | Popular Tags |