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.IOException;
039: import java.util.ArrayList;
040: import java.util.Date;
041: import java.util.Iterator;
042: import java.util.List;
043: import java.util.Map;
044: import java.util.StringTokenizer;
045:
046: import net.sourceforge.cruisecontrol.CruiseControlException;
047: import net.sourceforge.cruisecontrol.Modification;
048: import net.sourceforge.cruisecontrol.SourceControl;
049: import net.sourceforge.cruisecontrol.util.ManagedCommandline;
050: import net.sourceforge.cruisecontrol.util.ValidationHelper;
051:
052: import org.apache.log4j.Logger;
053:
054: /**
055: * This class implements the SourceControl methods for an AlienBrain
056: * repository. It does this by taking advantage of the AlienBrain command-
057: * line utility. Obviously, the command line utility must be installed
058: * and working in order for this class to work.
059: *
060: * This class is based very heavily on P4.java.
061: *
062: * @author <a href="mailto:scottj+cc@escherichia.net">Scott Jacobs</a>
063: */
064: public class AlienBrain extends AlienBrainCore implements SourceControl {
065:
066: private static final Logger LOG = Logger
067: .getLogger(AlienBrain.class);
068: /*
069: * The difference between January 1, 1601 0:00:00 UTC and January 1,
070: * 1970 0:00:00 UTC in milliseconds.
071: * ((369 years * 365 days) + 89 leap days) * 24h * 60m * 60s * 1000ms
072: */
073: private static final long FILETIME_EPOCH_DIFF = 11644473600000L;
074: /* 100-ns intervals per ms */
075: private static final long HUNDRED_NANO_PER_MILLI_RATIO = 10000L;
076: private static final String AB_NO_MODIFICATIONS = "No files or folders found!";
077: private static final String AB_MODIFICATION_SUMMARY_PREFIX = "Total of ";
078:
079: private final SourceControlProperties properties = new SourceControlProperties();
080:
081: /**
082: * Any properties that have been set in this sourcecontrol.
083: */
084: public Map getProperties() {
085: return properties.getPropertiesAndReset();
086: }
087:
088: public void setProperty(String propertyName) {
089: properties.assignPropertyName(propertyName);
090: }
091:
092: public void validate() throws CruiseControlException {
093: ValidationHelper
094: .assertIsSet(getPath(), "path", this .getClass());
095: }
096:
097: /**
098: * Get a List of Modifications detailing all the changes between now and
099: * the last build
100: *
101: *@param lastBuild
102: *@param now
103: *@return List of Modification objects
104: */
105: public List getModifications(Date lastBuild, Date now) {
106: List mods = new ArrayList();
107: try {
108: validate();
109: mods = getModificationsFromAlienBrain(lastBuild, now);
110: } catch (Exception e) {
111: LOG.error("Log command failed to execute succesfully", e);
112: }
113:
114: if (!mods.isEmpty()) {
115: properties.modificationFound();
116: }
117:
118: return mods;
119: }
120:
121: /**
122: * Convert a Java Date into an AlienBrain SCIT timestamp.
123: * AlienBrain provides a 64-bit modification timestamp that is in windows
124: * FILETIME format, which is a 65-bit value representing the number of
125: * 100-nanosecond intervals since January 1, 1601 (UTC).
126: */
127: public static long dateToFiletime(Date date) {
128: long milliSecsSinceUnixEpoch = date.getTime();
129: long milliSecsSinceFiletimeEpoch = milliSecsSinceUnixEpoch
130: + FILETIME_EPOCH_DIFF;
131: return milliSecsSinceFiletimeEpoch
132: * HUNDRED_NANO_PER_MILLI_RATIO;
133: }
134:
135: /**
136: * Convert an AlienBrain SCIT timestamp into a Java Date.
137: * AlienBrain provides a 64-bit modification timestamp that is in windows
138: * FILETIME format, which is a 64-bit value representing the number of
139: * 100-nanosecond intervals since January 1, 1601 (UTC).
140: */
141: public static Date filetimeToDate(long filetime) {
142: long milliSecsSinceFiletimeEpoch = filetime
143: / HUNDRED_NANO_PER_MILLI_RATIO;
144: long milliSecsSinceUnixEpoch = milliSecsSinceFiletimeEpoch
145: - FILETIME_EPOCH_DIFF;
146: return new Date(milliSecsSinceUnixEpoch);
147: }
148:
149: /**
150: * Construct a ManagedCommandline which will run the AlienBrain command-line
151: * client in such a way that it will return a list of modifications.
152: *
153: *@param lastBuild
154: *@param now
155: */
156: protected ManagedCommandline buildGetModificationsCommand(
157: Date lastBuild, Date now) {
158: ManagedCommandline cmdLine = buildCommonCommand();
159: cmdLine.createArguments("find", getPath());
160: cmdLine.createArguments("-regex", "SCIT > "
161: + dateToFiletime(lastBuild));
162: cmdLine.createArguments("-format",
163: "#SCIT#|#DbPath#|#Changed By#|#CheckInComment#");
164:
165: return cmdLine;
166: }
167:
168: /**
169: * Run the AlienBrain command-line client and return a list of
170: * Modifications since lastBuild, if any.
171: *@param lastBuild
172: *@param now
173: */
174: protected List getModificationsFromAlienBrain(Date lastBuild,
175: Date now) throws IOException, CruiseControlException {
176:
177: if (getBranch() != null) {
178: setActiveBranch(getBranch());
179: }
180:
181: ManagedCommandline cmdLine = buildGetModificationsCommand(
182: lastBuild, now);
183: LOG.debug("Executing: " + cmdLine.toString());
184: cmdLine.execute();
185:
186: return parseModifications(cmdLine.getStdoutAsList());
187: }
188:
189: /**
190: * Turn a stream containing the results of running the AlienBrain
191: * command-line client into a list of Modifications.
192: */
193: protected List parseModifications(List modifications) {
194: List mods = new ArrayList();
195:
196: for (Iterator it = modifications.iterator(); it.hasNext();) {
197: String line = (String) it.next();
198: line = line.trim();
199: if (line.equals(AB_NO_SESSION)) {
200: LOG.error(AB_NO_SESSION);
201: continue;
202: } else if (line.equals(AB_NO_MODIFICATIONS)) {
203: continue;
204: } else if (line.startsWith(AB_MODIFICATION_SUMMARY_PREFIX)) {
205: continue;
206: } else if (line.startsWith("|")) {
207: //Folders don't seem to always have a checked-in time, so
208: //fake one.
209: line = "0" + line;
210: }
211:
212: Modification m = parseModificationDescription(line);
213: mods.add(m);
214: }
215: return mods;
216: }
217:
218: /**
219: * Turns a string, most likely provided from the AlienBrain command-line
220: * client, into a Modification.
221: */
222: protected static Modification parseModificationDescription(
223: String description) {
224: Modification m = new Modification("AlienBrain");
225:
226: StringTokenizer st = new StringTokenizer(description, "|");
227:
228: m.modifiedTime = AlienBrain.filetimeToDate(Long.parseLong(st
229: .nextToken()));
230: m.createModifiedFile(st.nextToken(), null);
231: m.userName = st.nextToken();
232: while (st.hasMoreTokens()) {
233: m.comment += st.nextToken();
234: }
235:
236: return m;
237: }
238: }
|