001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2008 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id$
023: */
024: package com.bostechcorp.cbesb.runtime.file;
025:
026: import java.io.File;
027: import java.io.FilenameFilter;
028: import java.util.Vector;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: public class DirectoryScanner {
034:
035: private final Log log = LogFactory.getLog(this .getClass());
036:
037: private static final String MATCH_MODE_GLOB = "glob";
038: // private static final String MATCH_MODE_REGEX = "regexpr";
039:
040: protected File dir;
041: protected String matchMode;
042: protected String pattern;
043: protected boolean twoPass;
044: protected long twoPassInterval;
045: protected FilenameFilter filter;
046:
047: public File getDir() {
048: return dir;
049: }
050:
051: public void setDir(File dir) {
052: this .dir = dir;
053: }
054:
055: public String getMatchMode() {
056: return matchMode;
057: }
058:
059: public void setMatchMode(String matchMode) {
060: this .matchMode = matchMode;
061: }
062:
063: public String getPattern() {
064: return pattern;
065: }
066:
067: public void setPattern(String pattern) {
068: this .pattern = pattern;
069: }
070:
071: public boolean isTwoPass() {
072: return twoPass;
073: }
074:
075: public void setTwoPass(boolean twoPass) {
076: this .twoPass = twoPass;
077: }
078:
079: public long getTwoPassInterval() {
080: return twoPassInterval;
081: }
082:
083: public void setTwoPassInterval(long twoPassInterval) {
084: this .twoPassInterval = twoPassInterval;
085: }
086:
087: public String[] doScan() {
088: if (log.isDebugEnabled())
089: log.debug("Scanning directory: " + dir.getAbsolutePath());
090:
091: String[] matchedFiles = dir.list(getFilenameFilter());
092: if (twoPass) {
093: Vector<FileInfo> twoPassList = new Vector<FileInfo>();
094: for (int i = 0; i < matchedFiles.length; i++) {
095: File file = new File(dir, matchedFiles[i]);
096: FileInfo fileInfo = new FileInfo(file);
097: twoPassList.add(fileInfo);
098: }
099: try {
100: if (log.isDebugEnabled())
101: log.debug("Two pass - sleeping " + twoPassInterval
102: + " ms");
103: Thread.sleep(twoPassInterval);
104: } catch (InterruptedException e) {
105: log.warn("Two pass interval was interrupted: ", e);
106: }
107: Vector<File> fileList = new Vector<File>();
108: for (int i = 0; i < twoPassList.size(); i++) {
109: if (!twoPassList.get(i).hasChanged()) {
110: fileList.add(twoPassList.get(i).file);
111: }
112: }
113: matchedFiles = new String[fileList.size()];
114: for (int i = 0; i < fileList.size(); i++) {
115: matchedFiles[i] = fileList.get(i).getName();
116: }
117: }
118:
119: if (log.isDebugEnabled())
120: log.debug("Found " + matchedFiles.length
121: + " files to process.");
122: return matchedFiles;
123: }
124:
125: private FilenameFilter getFilenameFilter() {
126: if (filter == null) {
127: if (MATCH_MODE_GLOB.equalsIgnoreCase(matchMode)) {
128: GlobFilenameFilter globFilter = new GlobFilenameFilter();
129: globFilter.setPattern(pattern);
130: filter = globFilter;
131: } else {
132: RegexFilenameFilter regexFilter = new RegexFilenameFilter();
133: regexFilter.setPattern(pattern);
134: filter = regexFilter;
135: }
136: }
137: return filter;
138: }
139:
140: private class FileInfo {
141: protected File file;
142: protected long startModifiedTime;
143: protected long startSize;
144:
145: public FileInfo(File file) {
146: this .file = file;
147: startModifiedTime = file.lastModified();
148: startSize = file.length();
149: }
150:
151: public boolean hasChanged() {
152: return file.lastModified() != startModifiedTime
153: || file.length() != startSize;
154: }
155: }
156: }
|