01: package org.drools.agent;
02:
03: import java.io.File;
04: import java.util.Properties;
05:
06: import org.drools.RuleBase;
07: import org.drools.rule.Package;
08:
09: /**
10: * This will scan a directory for files to watch for a change.
11: * It will update the list of files only if they number of files in a directory changes.
12: *
13: * @author Michael Neale
14: */
15: public class DirectoryScanner extends PackageProvider {
16:
17: private File[] currentList;
18: private FileScanner scanner;
19: private File dir;
20:
21: void configure(Properties config) {
22: String d = config.getProperty(RuleAgent.DIRECTORY);
23:
24: //now check to see whats in them dir...
25: dir = new File(d);
26: if (!(dir.isDirectory() && dir.exists())) {
27: throw new IllegalArgumentException("The directory " + d
28: + "is not valid.");
29: }
30:
31: this .currentList = dir.listFiles();
32: scanner = new FileScanner();
33: scanner.setFiles(currentList);
34:
35: }
36:
37: Package[] loadPackageChanges() {
38: if (currentList.length != dir.listFiles().length) {
39: listener.info("Extra files detected in the directory "
40: + dir.getPath());
41: currentList = dir.listFiles();
42: scanner = new FileScanner();
43: scanner.setFiles(currentList);
44: }
45: return scanner.loadPackageChanges();
46: }
47:
48: public String toString() {
49: String s = "DirectoryScanner";
50: if (dir != null) {
51: s = s + " scanning dir: " + dir.getPath();
52: }
53: if (currentList != null) {
54: s = s + " found " + currentList.length + " file(s).";
55: }
56: return s;
57: }
58:
59: }
|