KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > buildloggers > ClearCaseAuditLogger


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 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.buildloggers;
38
39 import java.io.BufferedReader JavaDoc;
40 import java.io.File JavaDoc;
41 import java.io.IOException JavaDoc;
42 import java.io.InputStreamReader JavaDoc;
43 import java.io.PrintWriter JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.StringTokenizer JavaDoc;
46
47 import net.sourceforge.cruisecontrol.BuildLogger;
48 import net.sourceforge.cruisecontrol.CruiseControlException;
49 import net.sourceforge.cruisecontrol.util.Commandline;
50 import net.sourceforge.cruisecontrol.util.StreamPumper;
51 import net.sourceforge.cruisecontrol.util.ValidationHelper;
52
53 import org.apache.log4j.Logger;
54 import org.jdom.Element;
55
56 /**
57  * This ClearCaseAuditLogger will parse a specified configuration record (created as the
58  * result of an audited build) and place it into CruiseControl's log.
59  *
60  * @author <a HREF="mailto:kevin.lee@buildmeister.com">Kevin A. Lee</a>
61  *
62  */

63 public class ClearCaseAuditLogger implements BuildLogger {
64
65     private static final Logger LOG = Logger.getLogger(ClearCaseAuditLogger.class);
66
67     private String JavaDoc doFiles;
68
69     /**
70      * set the list of comma separated files to retrieve the config recs of
71      * @param files comma separated list of derived objects
72      */

73     public void setDoFiles(String JavaDoc files) {
74         this.doFiles = files;
75     }
76     
77     /**
78      * check that enough attributes have been set
79      */

80     public void validate() throws CruiseControlException {
81         // check we have at least a configrecfile
82
ValidationHelper.assertIsSet(doFiles , "dofiles", this.getClass());
83     }
84
85     /**
86      * Merge the configuration records of a set of derived objects into the build log
87      * @param buildLog
88      * @throws CruiseControlException
89      */

90     public void log(Element buildLog) throws CruiseControlException {
91         String JavaDoc[] doList = splitOnComma(doFiles);
92         for (int i = 0; i < doList.length; i++) {
93             
94             // add an element for audit
95
Element auditElement = new Element("audit");
96             auditElement.setAttribute("name", doList[i]);
97             buildLog.addContent(auditElement);
98         
99             Commandline commandLine = buildConfigRecCommand(doList[i]);
100             LOG.debug("Executing: " + commandLine);
101             try {
102                 Process JavaDoc p = Runtime.getRuntime().exec(commandLine.getCommandline());
103                 StreamPumper errorPumper =
104                     new StreamPumper(p.getErrorStream(), new PrintWriter JavaDoc(System.err, true));
105                  new Thread JavaDoc(errorPumper).start();
106                  try {
107                      InputStreamReader JavaDoc isr = new InputStreamReader JavaDoc(p.getInputStream());
108                      BufferedReader JavaDoc br = new BufferedReader JavaDoc(isr);
109                      String JavaDoc line;
110                      while ((line = br.readLine()) != null) {
111                          if (line.startsWith("---")) {
112                              // ignore
113
} else if (line.startsWith("MVFS")) {
114                              // ignore
115
} else {
116                              Element doElement = new Element("do");
117                              // removing leading characters
118
line = line.substring(line.indexOf(File.separator), line.length());
119                              if (line.indexOf("@") > 0) {
120                                  doElement.setAttribute("name", line.substring(0, line.indexOf("@")));
121                              } else {
122                                  doElement.setAttribute("name", line.substring(0, line.indexOf("<") - 1));
123                              }
124                              // do we have a element version or another do version
125
if (line.endsWith(">")) {
126                                  // element version
127
doElement.setAttribute("type", "version");
128                                  if (line.indexOf("@") > 0) {
129                                      doElement.setAttribute("version", line.substring(line.indexOf("@")
130                                          + 2, line.lastIndexOf("<") - 1));
131                                  } else {
132                                      doElement.setAttribute("version", line.substring(line.indexOf("<")
133                                          + 1, line.lastIndexOf(">") - 1));
134                                  }
135                              } else {
136                                  // do version
137
doElement.setAttribute("type", "do");
138                                  doElement.setAttribute("version", line.substring(line.indexOf("@")
139                                      + 2, line.length()));
140                              }
141                              auditElement.addContent(doElement);
142                         }
143                      }
144                  } catch (IOException JavaDoc ioe) {
145                      LOG.error("Error executing ClearCase catcr command", ioe);
146                  }
147                  p.waitFor();
148                  p.getInputStream().close();
149                  p.getOutputStream().close();
150                  p.getErrorStream().close();
151              } catch (Exception JavaDoc e) {
152                  LOG.error("Error executing ClearCase catcr command", e);
153              }
154         }
155     }
156     
157     private String JavaDoc[] splitOnComma(String JavaDoc doFiles) {
158         // replacing doFiles.split(",") for jdk 1.3
159
ArrayList JavaDoc parts = new ArrayList JavaDoc();
160         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(doFiles, ",");
161         while (tokenizer.hasMoreTokens()) {
162             parts.add(tokenizer.nextToken());
163         }
164         return (String JavaDoc[]) parts.toArray(new String JavaDoc[]{});
165     }
166
167     /*
168      * build a command line for retrieving a configuration record
169      */

170     protected Commandline buildConfigRecCommand(String JavaDoc file) {
171         Commandline commandLine = new Commandline();
172         commandLine.setExecutable("cleartool");
173         commandLine.createArgument().setValue("catcr");
174         commandLine.createArgument().setValue("-union");
175         commandLine.createArgument().setValue(file);
176         return commandLine;
177     }
178     
179 }
180
Popular Tags