KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > sourcecontrols > FileSystem


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.sourcecontrols;
38
39 import java.io.File JavaDoc;
40 import java.util.ArrayList JavaDoc;
41 import java.util.Date JavaDoc;
42 import java.util.Hashtable JavaDoc;
43 import java.util.List JavaDoc;
44 import java.util.Map JavaDoc;
45
46 import net.sourceforge.cruisecontrol.CruiseControlException;
47 import net.sourceforge.cruisecontrol.Modification;
48 import net.sourceforge.cruisecontrol.util.ValidationHelper;
49
50 /**
51  * Scans a directory tree on a local drive rather than in a repository.
52  *
53  * @author <a HREF="mailto:alden@thoughtworks.com">Alden Almagro</a>
54  */

55 public class FileSystem extends FakeUserSourceControl {
56
57     private Hashtable JavaDoc properties = new Hashtable JavaDoc();
58     private String JavaDoc property;
59
60     private List JavaDoc modifications;
61     private File JavaDoc folder;
62     //TODO: change folder attribute to path. Can be file or directory.
63

64     /**
65      * Set the root folder of the directories that we are going to scan
66      */

67     public void setFolder(String JavaDoc s) {
68         folder = new File JavaDoc(s);
69     }
70
71     public void setProperty(String JavaDoc property) {
72         this.property = property;
73     }
74
75     public Map JavaDoc getProperties() {
76         return properties;
77     }
78
79     public void validate() throws CruiseControlException {
80         ValidationHelper.assertIsSet(folder, "folder", this.getClass());
81         ValidationHelper.assertTrue(folder.exists(),
82             "folder " + folder.getAbsolutePath() + " must exist for FileSystem");
83     }
84
85     /**
86      * For this case, we don't care about the quietperiod, only that
87      * one user is modifying the build.
88      *
89      * @param lastBuild date of last build
90      * @param now IGNORED
91      */

92     public List JavaDoc getModifications(Date JavaDoc lastBuild, Date JavaDoc now) {
93         modifications = new ArrayList JavaDoc();
94         
95         visit(folder, lastBuild.getTime());
96
97         return modifications;
98     }
99
100     /**
101      * Add a Modification to the list of modifications. A lot of default
102      * behavior is assigned here because we don't have a repository to query the
103      * modification. All modifications will be set to type "change" and
104      * userName "User".
105      */

106     private void addRevision(File JavaDoc revision) {
107         Modification mod = new Modification("filesystem");
108
109         mod.userName = getUserName();
110
111         Modification.ModifiedFile modfile = mod.createModifiedFile(revision.getName(), revision.getParent());
112         modfile.action = "change";
113
114         mod.modifiedTime = new Date JavaDoc(revision.lastModified());
115         mod.comment = "";
116         modifications.add(mod);
117
118         if (property != null) {
119             properties.put(property, "true");
120         }
121     }
122
123     /**
124      * Recursively visit all files below the specified one. Check for newer
125      * timestamps
126      */

127     private void visit(File JavaDoc file, long lastBuild) {
128         if ((!file.isDirectory()) && (file.lastModified() > lastBuild)) {
129             addRevision(file);
130         }
131
132         if (file.isDirectory()) {
133             String JavaDoc[] children = file.list();
134             for (int i = 0; i < children.length; i++) {
135                 visit(new File JavaDoc(file, children[i]), lastBuild);
136             }
137         }
138     }
139
140 }
Popular Tags