001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2001, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol.sourcecontrols;
037:
038: import java.io.File;
039: import java.util.ArrayList;
040: import java.util.Date;
041: import java.util.List;
042:
043: import net.sourceforge.cruisecontrol.CruiseControlException;
044: import net.sourceforge.cruisecontrol.Modification;
045: import net.sourceforge.cruisecontrol.util.ValidationHelper;
046:
047: /**
048: * Scans a directory tree on a local drive rather than in a repository.
049: *
050: * @author <a href="mailto:alden@thoughtworks.com">Alden Almagro</a>
051: */
052: public class FileSystem extends FakeUserSourceControl {
053:
054: private List modifications;
055: private File folder;
056:
057: //TODO: change folder attribute to path. Can be file or directory.
058:
059: /**
060: * Set the root folder of the directories that we are going to scan
061: */
062: public void setFolder(String s) {
063: folder = new File(s);
064: }
065:
066: public void validate() throws CruiseControlException {
067: ValidationHelper.assertIsSet(folder, "folder", this .getClass());
068: ValidationHelper.assertTrue(folder.exists(), "folder "
069: + folder.getAbsolutePath()
070: + " must exist for FileSystem");
071: }
072:
073: /**
074: * For this case, we don't care about the quietperiod, only that
075: * one user is modifying the build.
076: *
077: * @param lastBuild date of last build
078: * @param now IGNORED
079: */
080: public List getModifications(Date lastBuild, Date now) {
081: modifications = new ArrayList();
082:
083: visit(folder, lastBuild.getTime());
084:
085: if (!modifications.isEmpty()) {
086: getSourceControlProperties().modificationFound();
087: }
088:
089: return modifications;
090: }
091:
092: /**
093: * Add a Modification to the list of modifications. A lot of default
094: * behavior is assigned here because we don't have a repository to query the
095: * modification. All modifications will be set to type "change" and
096: * userName "User".
097: */
098: private void addRevision(File revision) {
099: Modification mod = new Modification("filesystem");
100:
101: mod.userName = getUserName();
102:
103: Modification.ModifiedFile modfile = mod.createModifiedFile(
104: revision.getName(), revision.getParent());
105: modfile.action = "change";
106:
107: mod.modifiedTime = new Date(revision.lastModified());
108: mod.comment = "";
109: modifications.add(mod);
110: }
111:
112: /**
113: * Recursively visit all files below the specified one. Check for newer
114: * timestamps
115: */
116: private void visit(File file, long lastBuild) {
117: if ((!file.isDirectory()) && (file.lastModified() > lastBuild)) {
118: addRevision(file);
119: }
120:
121: if (file.isDirectory()) {
122: String[] children = file.list();
123: for (int i = 0; i < children.length; i++) {
124: visit(new File(file, children[i]), lastBuild);
125: }
126: }
127: }
128:
129: }
|