001: /*
002: * All content copyright (c) 2003-2007 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.tctest.service;
006:
007: import java.io.File;
008: import java.util.ArrayList;
009: import java.util.Arrays;
010: import java.util.Collection;
011: import java.util.List;
012: import org.apache.commons.logging.Log;
013: import org.apache.commons.logging.LogFactory;
014:
015: public class DirectoryMonitor implements DirectoryMonitorMBean {
016: private static Log log = LogFactory.getLog(DirectoryMonitor.class);
017: private long scanRate = 1000L;
018: private String directory = ".";
019: private Thread scanThread;
020: private boolean started;
021: private String fileExts;
022:
023: private List list = new ArrayList();
024:
025: public void setScanRate(long rate) {
026: scanRate = rate;
027: }
028:
029: public long getScanRate() {
030: return scanRate;
031: }
032:
033: public void setDirectory(String dir) {
034: directory = dir;
035: }
036:
037: public String getDirectory() {
038: return directory;
039: }
040:
041: public void setExtensionList(String list) {
042: this .fileExts = list;
043: }
044:
045: public String getExtensionList() {
046: return this .fileExts;
047: }
048:
049: public void start() {
050: if (started)
051: return;
052:
053: synchronized (list) {
054: list.add(new Integer(list.hashCode()));
055: list.notifyAll();
056: }
057:
058: started = true;
059: scanThread = new Thread(new ScannerThread(this ));
060: scanThread.start();
061: log.debug("**** Directory Monitor - Started with scan rate "
062: + this .getScanRate() + " *****");
063: }
064:
065: public boolean isStarted() {
066: return started;
067: }
068:
069: public void stop() {
070: if (!started)
071: return;
072:
073: synchronized (this ) {
074: started = false;
075: }
076: log.debug("Directory Monitor - requesting stop.");
077: }
078:
079: public void takeAction(Collection files) {
080: log.debug("********* Scanner Detected " + files.size()
081: + " Files ********* ");
082: }
083:
084: private class ScannerThread extends Thread {
085: private DirectoryMonitor monitor;
086:
087: public ScannerThread(DirectoryMonitor m) {
088: monitor = m;
089: }
090:
091: public void run() {
092: File dir = null;
093: String[] exts = null;
094:
095: dir = new File(monitor.getDirectory());
096: if (!dir.isDirectory()) {
097: log.warn(" Specified directory is invalid.");
098: }
099:
100: if (monitor.getExtensionList() != null) {
101: exts = monitor.getExtensionList().equals("*") ? null
102: : monitor.getExtensionList().split(",");
103: }
104: // scan for files - log when files are found.
105: for (;;) {
106: try {
107: log.debug("Scanning ... ");
108: Collection files = Arrays.asList(dir.listFiles());
109:
110: // if files are found, call Monitor.act();
111: if (files.size() > 0) {
112: monitor.takeAction(files);
113: } else {
114: log.debug("... found nothing");
115: }
116:
117: sleep(monitor.getScanRate());
118: } catch (InterruptedException e) {
119: System.err.println(" Error while scanning - " + e);
120: }
121:
122: if (!monitor.isStarted()) {
123: log.debug("Stopping Directory Monitor.");
124: break;
125: }
126: }
127: }
128: }
129: }
|