KickJava   Java API By Example, From Geeks To Geeks.

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


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, 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 net.sourceforge.cruisecontrol.CruiseControlException;
40 import net.sourceforge.cruisecontrol.Modification;
41 import net.sourceforge.cruisecontrol.util.StreamPumper;
42 import org.apache.log4j.Logger;
43
44 import java.io.BufferedReader JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.InputStream JavaDoc;
47 import java.io.InputStreamReader JavaDoc;
48 import java.text.ParseException JavaDoc;
49 import java.text.SimpleDateFormat JavaDoc;
50 import java.util.ArrayList JavaDoc;
51 import java.util.Collections JavaDoc;
52 import java.util.Date JavaDoc;
53 import java.util.Hashtable JavaDoc;
54 import java.util.List JavaDoc;
55 import java.util.Map JavaDoc;
56
57 /**
58  * This defines a child element for the ModificationSet element.
59  *
60  * @author Matt Harp
61  */

62 public class SSCM implements net.sourceforge.cruisecontrol.SourceControl {
63
64     private static final Logger LOG = Logger.getLogger(SSCM.class);
65     private final SimpleDateFormat JavaDoc formatter = new SimpleDateFormat JavaDoc("yyyyMMddHHmmss");
66
67     private SSCMCLIStringParam strparamBranch = new SSCMCLIStringParam("branch", "-b", false);
68     private SSCMCLIStringParam strparamRepository = new SSCMCLIStringParam("repository", "-p", false);
69     private SSCMCLIStringParam strparamFile = new SSCMCLIStringParam("file", "", false);
70     private SSCMCLIStringParam strparamServerConnect = new SSCMCLIStringParam("serverconnect", "-z", false);
71     private SSCMCLIStringParam strparamServerLogin = new SSCMCLIStringParam("serverlogin", "-y", false);
72     private SSCMCLIBoolParam fparamSearchRegExp = new SSCMCLIBoolParam("searchregexp", "-x", false);
73     private SSCMCLIBoolParam fparamRecursive = new SSCMCLIBoolParam("recursive", "-r", false);
74
75     private Hashtable JavaDoc hashProperties = new Hashtable JavaDoc();
76     private String JavaDoc strProperty = null;
77     
78     public void validate() throws CruiseControlException { /* nothing is required */ }
79
80     public void setBranch(String JavaDoc str) {
81         strparamBranch.setData(str);
82     }
83
84     public void setRepository(String JavaDoc str) {
85         strparamRepository.setData(str);
86     }
87
88     public void setFile(String JavaDoc str) {
89         strparamFile.setData(str);
90     }
91
92     public void setServerConnect(String JavaDoc str) {
93         strparamServerConnect.setData(str);
94     }
95
96     public void setServerLogin(String JavaDoc str) {
97         strparamServerLogin.setData(str);
98     }
99
100     public void setSearchRegExp(String JavaDoc str) {
101         if (str.equals("1")) {
102             fparamSearchRegExp.setData(null);
103         }
104     }
105
106     public void setRecursive(String JavaDoc str) {
107         if (str.equals("1")) {
108             fparamRecursive.setData(null);
109         }
110     }
111
112     public List JavaDoc getModifications(Date JavaDoc lastBuild, Date JavaDoc now) {
113         java.util.List JavaDoc paramList = new java.util.ArrayList JavaDoc();
114         if (!strparamFile.isSet()) {
115             strparamFile.setData("/");
116         }
117         paramList.add(strparamFile);
118         paramList.add(strparamBranch);
119         paramList.add(strparamRepository);
120         paramList.add(fparamRecursive);
121         paramList.add(fparamSearchRegExp);
122         paramList.add(strparamServerLogin);
123         paramList.add(strparamServerConnect);
124
125         List JavaDoc listMods = executeCLICommand(paramList, buildDateTimeRangeCLIParam(lastBuild, now));
126
127         if (listMods == null) {
128             listMods = Collections.EMPTY_LIST;
129         }
130         if (listMods.size() > 0 && strProperty != null) {
131             hashProperties.put(strProperty, "true");
132         }
133
134         return listMods;
135     }
136
137     public Map JavaDoc getProperties() {
138         return (hashProperties);
139     }
140
141     public void setProperty(String JavaDoc property) {
142         strProperty = property;
143     }
144
145     protected List JavaDoc executeCLICommand(java.util.List JavaDoc paramList, String JavaDoc strDTRangeParam) {
146         List JavaDoc listMods = null;
147         StringBuffer JavaDoc strbufferCmdLine = new StringBuffer JavaDoc("sscm cc ");
148
149         // Next, we just iterate through the list, adding entries.
150
boolean fAllRequirementsMet = true;
151         for (int i = 0; i < paramList.size() && fAllRequirementsMet; ++i) {
152             SSCMCLIParam param = (SSCMCLIParam) paramList.get(i);
153             if (param != null) {
154                 if (param.checkRequired()) {
155                     String JavaDoc str = param.getFormatted();
156                     if (str != null) {
157                         strbufferCmdLine.append(str);
158                         strbufferCmdLine.append(' ');
159                     }
160                 } else {
161                     fAllRequirementsMet = false;
162                     LOG.error("Required parameter '" + param.getParamName() + "' is missing!");
163                 }
164             }
165         }
166
167         if (fAllRequirementsMet) {
168             strbufferCmdLine.append(' ');
169             strbufferCmdLine.append(strDTRangeParam);
170             strbufferCmdLine.append(' ');
171
172             LOG.debug("\n" + strbufferCmdLine + "\n");
173
174             try {
175                 Process JavaDoc process = Runtime.getRuntime().exec(strbufferCmdLine.toString());
176                 new Thread JavaDoc(new StreamPumper(process.getErrorStream())).start();
177
178                 InputStream JavaDoc input = process.getInputStream();
179                 listMods = parseCLIOutput(input);
180
181                 process.waitFor();
182
183                 process.getInputStream().close();
184                 process.getOutputStream().close();
185                 process.getErrorStream().close();
186             } catch (IOException JavaDoc e) {
187                 LOG.error("Problem trying to execute command line process", e);
188             } catch (InterruptedException JavaDoc e) {
189                 LOG.error("Problem trying to execute command line process", e);
190             }
191         }
192
193         return listMods;
194     }
195
196     protected List JavaDoc parseCLIOutput(InputStream JavaDoc input) throws IOException JavaDoc {
197         List JavaDoc listMods = new ArrayList JavaDoc();
198         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(input));
199
200         String JavaDoc line = reader.readLine();
201
202         // -meh. Kind of lame, but total-0 will work.
203
if (!"total-0".equals(line)) {
204             while ((line = reader.readLine()) != null) {
205                 Modification mod = parseOutputLine(line);
206                 if (mod != null) {
207                     listMods.add(mod);
208                 }
209             }
210         }
211
212         return listMods;
213     }
214
215     protected Modification parseOutputLine(String JavaDoc str) {
216         LOG.debug("Output-" + str + "-\n");
217
218         if (str == null || str.length() == 0) {
219             return null;
220         }
221         Modification mod = new Modification("sscm");
222         Modification.ModifiedFile modfile = mod.createModifiedFile(null, null);
223
224         boolean fValid = false;
225         String JavaDoc strToken = "><";
226         int iLeft = 1;
227
228         // Repository
229
int iRight = str.indexOf(strToken, iLeft);
230         if (iRight > iLeft) {
231             modfile.folderName = str.substring(iLeft, iRight);
232             iLeft = iRight + strToken.length();
233
234             // Filename
235
iRight = str.indexOf(strToken, iLeft);
236             if (iRight > iLeft) {
237                 modfile.fileName = str.substring(iLeft, iRight);
238                 iLeft = iRight + strToken.length();
239
240                 // Revision
241
iRight = str.indexOf(strToken, iLeft);
242                 if (iRight > iLeft) {
243                     mod.revision = str.substring(iLeft, iRight);
244                     iLeft = iRight + strToken.length();
245
246                     // Event
247
iRight = str.indexOf(strToken, iLeft);
248                     if (iRight > iLeft) {
249                         modfile.action = str.substring(iLeft, iRight);
250                         iLeft = iRight + strToken.length();
251
252                         // Date
253
iRight = str.indexOf(strToken, iLeft);
254                         if (iRight > iLeft) {
255                             mod.modifiedTime = buildDateTimeFromCLIOutput(str.substring(iLeft, iRight));
256                             iLeft = iRight + strToken.length();
257
258                             // Comment
259
iRight = str.indexOf(strToken, iLeft);
260                             if (iRight >= iLeft) {
261                                 mod.comment = str.substring(iLeft, iRight);
262                                 iLeft = iRight + strToken.length();
263
264                                 // User
265
iRight = str.indexOf(strToken, iLeft);
266                                 if (iRight > iLeft) {
267                                     mod.userName = str.substring(iLeft, iRight);
268                                     iLeft = iRight + strToken.length();
269
270                                     // Email
271
iRight = str.indexOf(">", iLeft);
272                                     if (iRight > iLeft) {
273                                         mod.emailAddress = str.substring(iLeft, iRight);
274                                         fValid = true;
275
276                                         if (strProperty != null) {
277                                             hashProperties.put(strProperty, "true");
278                                         }
279                                     }
280                                 }
281                             }
282                         }
283                     }
284                 }
285             }
286         }
287
288         if (!fValid) {
289             mod = null;
290             LOG.debug("Invalid output; skipping this entry");
291         }
292         return (mod);
293     }
294
295     protected String JavaDoc buildDateTimeRangeCLIParam(Date JavaDoc lastBuild, Date JavaDoc now) {
296         String JavaDoc strLast = formatter.format(lastBuild);
297         String JavaDoc strNow = formatter.format(now);
298         return "-d" + strLast + ":" + strNow;
299     }
300
301     protected Date JavaDoc buildDateTimeFromCLIOutput(String JavaDoc str) {
302         Date JavaDoc dt;
303         try {
304             dt = formatter.parse(str);
305         } catch (ParseException JavaDoc e) {
306             dt = null;
307             LOG.error("Unable to parse DateTime from Surround", e);
308         }
309         return dt;
310     }
311
312     public abstract static class SSCMCLIParam {
313         public SSCMCLIParam(String JavaDoc strParamNameIN, String JavaDoc strParamIN, boolean fIsRequiredIN) {
314             strParamName = strParamNameIN;
315             strParam = strParamIN;
316             fIsRequired = fIsRequiredIN;
317             fIsSet = false;
318         }
319
320         public String JavaDoc getParamName() {
321             return (strParamName);
322         }
323
324         public String JavaDoc getParam() {
325             return (strParam);
326         }
327
328         public void setRequired(boolean f) {
329             fIsRequired = f;
330         }
331
332         public boolean isRequired() {
333             return fIsRequired;
334         }
335
336         public boolean isSet() {
337             return fIsSet;
338         }
339
340         public boolean checkRequired() {
341             return !(isRequired() && !isSet());
342         }
343
344         public abstract String JavaDoc getFormatted();
345
346         public abstract void setData(Object JavaDoc obj);
347
348         protected void setSet(boolean f) {
349             fIsSet = f;
350         }
351
352         private String JavaDoc strParamName;
353         private String JavaDoc strParam;
354         private boolean fIsRequired;
355         private boolean fIsSet;
356     }
357
358     public static class SSCMCLIBoolParam extends SSCMCLIParam {
359         public SSCMCLIBoolParam(String JavaDoc strParamNameIN, String JavaDoc strParamIN, boolean fIsRequiredIN) {
360             super(strParamNameIN, strParamIN, fIsRequiredIN);
361         }
362
363         public void setData(Object JavaDoc obj) {
364             fData = true;
365             setSet(true);
366         }
367
368         public String JavaDoc getFormatted() {
369             String JavaDoc str = null;
370             if (isSet() && fData) {
371                 str = getParam();
372             }
373             return str;
374         }
375
376         private boolean fData;
377     }
378
379     public static class SSCMCLIStringParam extends SSCMCLIParam {
380         public SSCMCLIStringParam(String JavaDoc strParamNameIN, String JavaDoc strParamIN, boolean fIsRequiredIN) {
381             super(strParamNameIN, strParamIN, fIsRequiredIN);
382         }
383
384         public void setData(Object JavaDoc obj) {
385             strData = (String JavaDoc) obj;
386             setSet(true);
387         }
388
389         public String JavaDoc getFormatted() {
390             String JavaDoc str = null;
391             if (isSet()) {
392                 str = getParam() + strData;
393             }
394             return str;
395         }
396
397         private String JavaDoc strData;
398     }
399
400 }
401
402
Popular Tags