001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2001-2003, 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.BufferedReader;
039: import java.io.IOException;
040: import java.io.InputStream;
041: import java.io.InputStreamReader;
042: import java.text.ParseException;
043: import java.text.SimpleDateFormat;
044: import java.util.ArrayList;
045: import java.util.Collections;
046: import java.util.Date;
047: import java.util.List;
048: import java.util.Map;
049:
050: import net.sourceforge.cruisecontrol.CruiseControlException;
051: import net.sourceforge.cruisecontrol.Modification;
052: import net.sourceforge.cruisecontrol.util.IO;
053: import net.sourceforge.cruisecontrol.util.StreamLogger;
054:
055: import org.apache.log4j.Logger;
056:
057: /**
058: * This defines a child element for the ModificationSet element.
059: *
060: * @author Matt Harp
061: */
062: public class SSCM implements
063: net.sourceforge.cruisecontrol.SourceControl {
064:
065: private static final Logger LOG = Logger.getLogger(SSCM.class);
066: private final SimpleDateFormat formatter = new SimpleDateFormat(
067: "yyyyMMddHHmmss");
068:
069: private SSCMCLIStringParam strparamBranch = new SSCMCLIStringParam(
070: "branch", "-b", false);
071: private SSCMCLIStringParam strparamRepository = new SSCMCLIStringParam(
072: "repository", "-p", false);
073: private SSCMCLIStringParam strparamFile = new SSCMCLIStringParam(
074: "file", "", false);
075: private SSCMCLIStringParam strparamServerConnect = new SSCMCLIStringParam(
076: "serverconnect", "-z", false);
077: private SSCMCLIStringParam strparamServerLogin = new SSCMCLIStringParam(
078: "serverlogin", "-y", false);
079: private SSCMCLIBoolParam fparamSearchRegExp = new SSCMCLIBoolParam(
080: "searchregexp", "-x", false);
081: private SSCMCLIBoolParam fparamRecursive = new SSCMCLIBoolParam(
082: "recursive", "-r", false);
083:
084: private SourceControlProperties properties = new SourceControlProperties();
085:
086: public void validate() throws CruiseControlException { /* nothing is required */
087: }
088:
089: public void setBranch(String str) {
090: strparamBranch.setData(str);
091: }
092:
093: public void setRepository(String str) {
094: strparamRepository.setData(str);
095: }
096:
097: public void setFile(String str) {
098: strparamFile.setData(str);
099: }
100:
101: public void setServerConnect(String str) {
102: strparamServerConnect.setData(str);
103: }
104:
105: public void setServerLogin(String str) {
106: strparamServerLogin.setData(str);
107: }
108:
109: public void setSearchRegExp(String str) {
110: if (str.equals("1")) {
111: fparamSearchRegExp.setData(null);
112: }
113: }
114:
115: public void setRecursive(String str) {
116: if (str.equals("1")) {
117: fparamRecursive.setData(null);
118: }
119: }
120:
121: public List getModifications(Date lastBuild, Date now) {
122: java.util.List paramList = new java.util.ArrayList();
123: if (!strparamFile.isSet()) {
124: strparamFile.setData("/");
125: }
126: paramList.add(strparamFile);
127: paramList.add(strparamBranch);
128: paramList.add(strparamRepository);
129: paramList.add(fparamRecursive);
130: paramList.add(fparamSearchRegExp);
131: paramList.add(strparamServerLogin);
132: paramList.add(strparamServerConnect);
133:
134: List listMods = executeCLICommand(paramList,
135: buildDateTimeRangeCLIParam(lastBuild, now));
136:
137: if (listMods == null) {
138: listMods = Collections.EMPTY_LIST;
139: }
140:
141: if (!listMods.isEmpty()) {
142: properties.modificationFound();
143: }
144:
145: return listMods;
146: }
147:
148: public Map getProperties() {
149: return properties.getPropertiesAndReset();
150: }
151:
152: public void setProperty(String property) {
153: properties.assignPropertyName(property);
154: }
155:
156: protected List executeCLICommand(java.util.List paramList,
157: String strDTRangeParam) {
158: List listMods = null;
159: StringBuffer strbufferCmdLine = new StringBuffer("sscm cc ");
160:
161: // Next, we just iterate through the list, adding entries.
162: boolean fAllRequirementsMet = true;
163: for (int i = 0; i < paramList.size() && fAllRequirementsMet; ++i) {
164: SSCMCLIParam param = (SSCMCLIParam) paramList.get(i);
165: if (param != null) {
166: if (param.checkRequired()) {
167: String str = param.getFormatted();
168: if (str != null) {
169: strbufferCmdLine.append(str);
170: strbufferCmdLine.append(' ');
171: }
172: } else {
173: fAllRequirementsMet = false;
174: LOG.error("Required parameter '"
175: + param.getParamName() + "' is missing!");
176: }
177: }
178: }
179:
180: if (fAllRequirementsMet) {
181: strbufferCmdLine.append(' ');
182: strbufferCmdLine.append(strDTRangeParam);
183: strbufferCmdLine.append(' ');
184:
185: LOG.debug("\n" + strbufferCmdLine + "\n");
186:
187: try {
188: Process process = Runtime.getRuntime().exec(
189: strbufferCmdLine.toString());
190: process.getOutputStream().close();
191: Thread stderr = new Thread(StreamLogger.getWarnPumper(
192: LOG, process));
193: stderr.start();
194:
195: InputStream input = process.getInputStream();
196: listMods = parseCLIOutput(input);
197:
198: process.waitFor();
199: stderr.join();
200:
201: IO.close(process);
202: } catch (IOException e) {
203: LOG
204: .error(
205: "Problem trying to execute command line process",
206: e);
207: } catch (InterruptedException e) {
208: LOG
209: .error(
210: "Problem trying to execute command line process",
211: e);
212: }
213: }
214:
215: return listMods;
216: }
217:
218: protected List parseCLIOutput(InputStream input) throws IOException {
219: List listMods = new ArrayList();
220: BufferedReader reader = new BufferedReader(
221: new InputStreamReader(input));
222:
223: String line = reader.readLine();
224:
225: // -meh. Kind of lame, but total-0 will work.
226: if (!"total-0".equals(line)) {
227: while ((line = reader.readLine()) != null) {
228: Modification mod = parseOutputLine(line);
229: if (mod != null) {
230: listMods.add(mod);
231: }
232: }
233: }
234:
235: return listMods;
236: }
237:
238: protected Modification parseOutputLine(String str) {
239: LOG.debug("Output-" + str + "-\n");
240:
241: if (str == null || str.length() == 0) {
242: return null;
243: }
244: Modification mod = new Modification("sscm");
245: Modification.ModifiedFile modfile = mod.createModifiedFile(
246: null, null);
247:
248: boolean fValid = false;
249: String strToken = "><";
250: int iLeft = 1;
251:
252: // Repository
253: int iRight = str.indexOf(strToken, iLeft);
254: if (iRight > iLeft) {
255: modfile.folderName = str.substring(iLeft, iRight);
256: iLeft = iRight + strToken.length();
257:
258: // Filename
259: iRight = str.indexOf(strToken, iLeft);
260: if (iRight > iLeft) {
261: modfile.fileName = str.substring(iLeft, iRight);
262: iLeft = iRight + strToken.length();
263:
264: // Revision
265: iRight = str.indexOf(strToken, iLeft);
266: if (iRight > iLeft) {
267: mod.revision = str.substring(iLeft, iRight);
268: iLeft = iRight + strToken.length();
269:
270: // Event
271: iRight = str.indexOf(strToken, iLeft);
272: if (iRight > iLeft) {
273: modfile.action = str.substring(iLeft, iRight);
274: iLeft = iRight + strToken.length();
275:
276: // Date
277: iRight = str.indexOf(strToken, iLeft);
278: if (iRight > iLeft) {
279: mod.modifiedTime = buildDateTimeFromCLIOutput(str
280: .substring(iLeft, iRight));
281: iLeft = iRight + strToken.length();
282:
283: // Comment
284: iRight = str.indexOf(strToken, iLeft);
285: if (iRight >= iLeft) {
286: mod.comment = str.substring(iLeft,
287: iRight);
288: iLeft = iRight + strToken.length();
289:
290: // User
291: iRight = str.indexOf(strToken, iLeft);
292: if (iRight > iLeft) {
293: mod.userName = str.substring(iLeft,
294: iRight);
295: iLeft = iRight + strToken.length();
296:
297: // Email
298: iRight = str.indexOf(">", iLeft);
299: if (iRight >= iLeft) {
300: mod.emailAddress = str
301: .substring(iLeft,
302: iRight);
303: fValid = true;
304: }
305: }
306: }
307: }
308: }
309: }
310: }
311: }
312:
313: if (!fValid) {
314: mod = null;
315: LOG.debug("Invalid output; skipping this entry");
316: }
317: return (mod);
318: }
319:
320: protected String buildDateTimeRangeCLIParam(Date lastBuild, Date now) {
321: String strLast = formatter.format(lastBuild);
322: String strNow = formatter.format(now);
323: return "-d" + strLast + ":" + strNow;
324: }
325:
326: protected Date buildDateTimeFromCLIOutput(String str) {
327: Date dt;
328: try {
329: dt = formatter.parse(str);
330: } catch (ParseException e) {
331: dt = null;
332: LOG.error("Unable to parse DateTime from Surround", e);
333: }
334: return dt;
335: }
336:
337: public abstract static class SSCMCLIParam {
338: public SSCMCLIParam(String strParamNameIN, String strParamIN,
339: boolean fIsRequiredIN) {
340: strParamName = strParamNameIN;
341: strParam = strParamIN;
342: fIsRequired = fIsRequiredIN;
343: fIsSet = false;
344: }
345:
346: public String getParamName() {
347: return (strParamName);
348: }
349:
350: public String getParam() {
351: return (strParam);
352: }
353:
354: public void setRequired(boolean f) {
355: fIsRequired = f;
356: }
357:
358: public boolean isRequired() {
359: return fIsRequired;
360: }
361:
362: public boolean isSet() {
363: return fIsSet;
364: }
365:
366: public boolean checkRequired() {
367: return !(isRequired() && !isSet());
368: }
369:
370: public abstract String getFormatted();
371:
372: public abstract void setData(Object obj);
373:
374: protected void setSet(boolean f) {
375: fIsSet = f;
376: }
377:
378: private String strParamName;
379: private String strParam;
380: private boolean fIsRequired;
381: private boolean fIsSet;
382: }
383:
384: public static class SSCMCLIBoolParam extends SSCMCLIParam {
385: public SSCMCLIBoolParam(String strParamNameIN,
386: String strParamIN, boolean fIsRequiredIN) {
387: super (strParamNameIN, strParamIN, fIsRequiredIN);
388: }
389:
390: public void setData(Object obj) {
391: fData = true;
392: setSet(true);
393: }
394:
395: public String getFormatted() {
396: String str = null;
397: if (isSet() && fData) {
398: str = getParam();
399: }
400: return str;
401: }
402:
403: private boolean fData;
404: }
405:
406: public static class SSCMCLIStringParam extends SSCMCLIParam {
407: public SSCMCLIStringParam(String strParamNameIN,
408: String strParamIN, boolean fIsRequiredIN) {
409: super (strParamNameIN, strParamIN, fIsRequiredIN);
410: }
411:
412: public void setData(Object obj) {
413: strData = (String) obj;
414: setSet(true);
415: }
416:
417: public String getFormatted() {
418: String str = null;
419: if (isSet()) {
420: str = getParam() + strData;
421: }
422: return str;
423: }
424:
425: private String strData;
426: }
427:
428: }
|