KickJava   Java API By Example, From Geeks To Geeks.

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


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 net.sourceforge.cruisecontrol.CruiseControlException;
40 import net.sourceforge.cruisecontrol.Modification;
41 import net.sourceforge.cruisecontrol.SourceControl;
42 import net.sourceforge.cruisecontrol.sourcecontrols.accurev.AccurevCommand;
43 import net.sourceforge.cruisecontrol.sourcecontrols.accurev.AccurevCommandline;
44 import net.sourceforge.cruisecontrol.sourcecontrols.accurev.AccurevInputParser;
45 import net.sourceforge.cruisecontrol.sourcecontrols.accurev.DateTimespec;
46 import net.sourceforge.cruisecontrol.sourcecontrols.accurev.Runner;
47 import org.apache.log4j.Logger;
48
49 import java.io.BufferedReader JavaDoc;
50 import java.io.IOException JavaDoc;
51 import java.io.InputStream JavaDoc;
52 import java.io.InputStreamReader JavaDoc;
53 import java.util.ArrayList JavaDoc;
54 import java.util.Date JavaDoc;
55 import java.util.Hashtable JavaDoc;
56 import java.util.List JavaDoc;
57 import java.util.Map JavaDoc;
58 import java.util.StringTokenizer JavaDoc;
59
60 /**
61  * This class handles all Accurev aspects of determining the modifications since the last good
62  * build.
63  *
64  * @author <a HREF="mailto:jason_chown@scee.net">Jason Chown </a>
65  * @author <a HREF="mailto:Nicola_Orru@scee.net">Nicola Orru'</a>
66  */

67 public class Accurev implements SourceControl, AccurevInputParser {
68     private static final Logger LOG = Logger.getLogger(Accurev.class);
69     private String JavaDoc stream;
70     private boolean verbose;
71     private Hashtable JavaDoc properties = new Hashtable JavaDoc();
72     private String JavaDoc propertyOnDelete;
73     private ArrayList JavaDoc modifications;
74     private Runner runner;
75
76     /**
77      * Sets the Accurev stream to search for changes
78      *
79      * @param stream the name of the stream
80      */

81     public void setStream(String JavaDoc stream) {
82         this.stream = stream;
83     }
84
85     /**
86      * Enables/disables verbose logging
87      *
88      * @param verbose set to true to enable verbose logging
89      */

90     public void setVerbose(boolean verbose) {
91         this.verbose = verbose;
92     }
93
94     /**
95      * Choose a property to be set if the project has modifications if we have a change that only
96      * requires repackaging, i.e. jsp, we don't need to recompile everything, just rejar.
97      *
98      * @param propertyName the name of the property
99      */

100     public void setProperty(String JavaDoc propertyName) {
101         if (propertyName != null) {
102             properties.put(propertyName, "true");
103         }
104     }
105
106     /**
107      * Choose a property to be set if the project has deletions
108      *
109      * @param propertyName the name of the property
110      */

111     public void setPropertyOnDelete(String JavaDoc propertyName) {
112         propertyOnDelete = propertyName;
113         addPropertyOnDelete();
114     }
115
116     public Map JavaDoc getProperties() {
117         return properties;
118     }
119
120     public void validate() throws CruiseControlException {
121         if (stream == null) {
122             throw new CruiseControlException("'stream' is a required attribute for Accurev");
123         }
124     }
125
126     /**
127      * Calls "accurev hist -s [stream] -t "[now] - [lastBuild]" or something like that ; )
128      *
129      * @param lastBuild the date and time of the last successful build
130      * @param now the current date and time
131      * @return the List of all detected modifications
132      */

133     public List JavaDoc getModifications(Date JavaDoc lastBuild, Date JavaDoc now) {
134         LOG.info("Accurev: getting modifications for " + stream);
135         AccurevCommandline hist = AccurevCommand.HIST.create();
136         if (runner != null) {
137             hist.setRunner(runner);
138         }
139         hist.setVerbose(verbose);
140         hist.setInputParser(this);
141         hist.setStream(stream);
142         hist.setTransactionRange(new DateTimespec(lastBuild), new DateTimespec(now));
143         hist.run();
144         return modifications;
145     }
146
147     /**
148      * Parse the output from Accurev. These are lines of the form: <code>
149      * transaction &lt;id>; &lt;verb&gt;; YYYY/MM/DD hh:mm:ss ; user: &lt;user&gt;
150      * # &lt;comment&gt;
151      * \.\PathTo\FileChanged.cpp &lt;version&gt;
152      * </code>
153      * <p/>
154      * Where <verb>can be promote, chstream or purge. There can be multiple lines of comments and
155      * files.
156      *
157      * @param input the output of the "accurev hist" command run
158      * @return true at the end
159      * @throws IOException
160      */

161     public boolean parseStream(InputStream JavaDoc input) throws IOException JavaDoc, CruiseControlException {
162         modifications = new ArrayList JavaDoc();
163         Modification modification = null;
164         BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(input));
165         while (true) {
166             String JavaDoc line = reader.readLine();
167             if (line == null) {
168                 break;
169             }
170             LOG.debug(line);
171             if (line.startsWith("transaction")) {
172                 // transaction <id>; <verb>; YYYY/MM/DD hh:mm:ss ; user: <user>
173
modification = new Modification();
174                 String JavaDoc[] parts = getParts(line);
175                 modification.comment = "";
176                 modification.revision = parts[0].substring(parts[0].indexOf(' ') + 1);
177                 modification.type = parts[1].trim();
178                 modification.modifiedTime = DateTimespec.parse(parts[2].trim());
179                 modification.userName = parts[3].substring(6).trim();
180                 modifications.add(modification);
181             } else if (line.startsWith(" #")) {
182                 // # Comment
183
if (modification != null) {
184                     modification.comment += line.substring(3) + "\n";
185                 } else {
186                     LOG.warn("Comment outside modification - skipping");
187                 }
188                 // Accurev is returning always \\ instead of File.separatorChar
189
} else if (line.startsWith(" \\.\\") || line.startsWith(" /./")) {
190                 // ...but just for the sake of paranoia...
191
final char separator = line.charAt(2);
192                 final int lastSlash = line.lastIndexOf(separator);
193                 int lastSpace = line.lastIndexOf(' ');
194                 lastSpace = line.lastIndexOf(' ', lastSpace - 1);
195                 if (lastSpace > lastSlash) {
196                     String JavaDoc fileName = line.substring(lastSlash + 1, lastSpace);
197                     String JavaDoc folderName = ((lastSlash > 5) ? line.substring(5, lastSlash) : line.substring(5)).replace(
198                             separator, '/');
199                     Modification.ModifiedFile modfile = modification.createModifiedFile(fileName, folderName);
200                     modfile.action = "change";
201                 }
202             }
203         }
204         return true;
205     }
206
207     private String JavaDoc[] getParts(String JavaDoc line) {
208         List JavaDoc partsList = new ArrayList JavaDoc();
209         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(line, ";");
210         while (tokenizer.hasMoreTokens()) {
211             partsList.add(tokenizer.nextToken());
212         }
213         String JavaDoc[] parts = new String JavaDoc[partsList.size()];
214         partsList.toArray(parts);
215         return parts;
216     }
217
218     private void addPropertyOnDelete() {
219         if (propertyOnDelete != null) {
220             properties.put(propertyOnDelete, "true");
221             LOG.debug("setting property " + propertyOnDelete + " to be true");
222         }
223     }
224
225     public void setRunner(Runner runner) {
226         this.runner = runner;
227   }
228 }
Popular Tags