KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > publishers > XSLTLogPublisher


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.publishers;
38
39 import net.sourceforge.cruisecontrol.CruiseControlException;
40 import net.sourceforge.cruisecontrol.Publisher;
41 import net.sourceforge.cruisecontrol.util.ValidationHelper;
42 import net.sourceforge.cruisecontrol.util.XMLLogHelper;
43 import org.apache.log4j.Logger;
44 import org.jdom.Element;
45 import org.jdom.transform.JDOMSource;
46
47 import javax.xml.transform.Transformer JavaDoc;
48 import javax.xml.transform.TransformerException JavaDoc;
49 import javax.xml.transform.TransformerFactory JavaDoc;
50 import javax.xml.transform.stream.StreamResult JavaDoc;
51 import javax.xml.transform.stream.StreamSource JavaDoc;
52 import java.io.File JavaDoc;
53 import java.io.FileInputStream JavaDoc;
54 import java.io.FileOutputStream JavaDoc;
55 import java.io.IOException JavaDoc;
56 import java.io.OutputStream JavaDoc;
57
58 /**
59  * Publisher Plugin which performs an xslt transform of the
60  * jdom Element representation of the build log to an output file of choice.
61  *
62  * @author David Cole
63  */

64 public class XSLTLogPublisher implements Publisher {
65     private static final Logger LOG = Logger.getLogger(XSLTLogPublisher.class);
66
67     private String JavaDoc directory;
68     private String JavaDoc xsltFile;
69     private boolean publishOnFail = true;
70     private String JavaDoc outFileName;
71
72     /**
73      * Full path to the xslt file used in the transform
74      *
75      * @param fileName (required)
76      */

77     public void setXsltFile(String JavaDoc fileName) {
78         this.xsltFile = fileName;
79     }
80
81     /**
82      * Name of the output file where the transformed
83      * log contents is to be written.
84      * <br>
85      * If ommitted, a default will be provided matching the
86      * build label name
87      *
88      * @param name
89      */

90     public void setOutFileName(String JavaDoc name) {
91         this.outFileName = name;
92     }
93
94     /**
95      * Directory where the transformed log will is to be written
96      *
97      * @param directory
98      */

99     public void setDirectory(String JavaDoc directory) {
100         this.directory = directory;
101     }
102
103     /**
104      * If true then publish the log contents even if the build failed.
105      * If false then only publish the log if the build was successful
106      * <br>
107      * Defaults to true
108      *
109      * @param pof
110      */

111     public void setPublishOnFail(boolean pof) {
112         this.publishOnFail = pof;
113     }
114
115     /**
116      * Called after the configuration is read to make sure that all the mandatory parameters
117      * were specified..
118      *
119      * @throws CruiseControlException if there was a configuration error.
120      */

121     public void validate() throws CruiseControlException {
122         ValidationHelper.assertIsSet(xsltFile, "xsltFile", this.getClass());
123         ValidationHelper.assertIsSet(directory, "directory", this.getClass());
124     }
125
126     /**
127      * Perform the log transform and publish the results.
128      */

129     public void publish(Element cruisecontrolLog) throws CruiseControlException {
130         XMLLogHelper helper = new XMLLogHelper(cruisecontrolLog);
131         Boolean JavaDoc buildSuccess = null;
132         try {
133             buildSuccess = (helper.isBuildSuccessful()) ? Boolean.TRUE : Boolean.FALSE;
134         } catch (NullPointerException JavaDoc ne) {
135             //Do Nothing - leave buildSuccess = null
136
}
137
138         //if the cruisecontrollog element or the buildSuccess attribute
139
//turn out to be null, then there is nothing to do, so just return
140
if (cruisecontrolLog == null || buildSuccess == null) {
141             return;
142         }
143
144         //if the build failed and we are not supposed to publish on fail the return immediately
145
if (!buildSuccess.booleanValue() && !publishOnFail) {
146             LOG.info("Build failed and publishOnFail is false: Not publishing log.");
147             return;
148         }
149
150         //If the outFileName attribute is null then construct the outFileName based
151
//upon the build label that was created
152
if (outFileName == null) {
153             String JavaDoc label = helper.getCruiseControlInfoProperty("label");
154             if (label == null || label.trim().length() == 0) {
155                 throw new CruiseControlException("The Label property is not set in the log file..."
156                         + "unable to publish the log.");
157             }
158             LOG.debug(
159                     "Using the cruise control info label property to construct the file name which is set to: "
160                             + label);
161             outFileName = label + ".log";
162         }
163
164         //Make sure that the directory exists and is a directory
165
//Attempt to create an empty directory if necessary
166
File JavaDoc dir = new File JavaDoc(directory);
167         dir.mkdirs();
168         if (!dir.isDirectory()) {
169             throw new CruiseControlException(
170                     "Unable to locate or create the output directory (" + directory + "): Failed to publish log file.");
171         }
172
173         String JavaDoc filePath = directory + File.separator + outFileName;
174         LOG.info("Publishing log file to: " + filePath);
175         writeFile(cruisecontrolLog, filePath);
176         LOG.info("Log file successfully published to the file at path: " + filePath);
177     }
178
179     /**
180      * Performs the transform of the cruisecontrolLog, writing the results to the locations
181      * specified by the path parameter
182      * @param cruisecontrolLog
183      * @param path
184      * @throws CruiseControlException
185      */

186     protected void writeFile(Element cruisecontrolLog, String JavaDoc path) throws CruiseControlException {
187         FileInputStream JavaDoc xslFileStream = null;
188         OutputStream JavaDoc out = null;
189         try {
190             //Make sure that the xsltFile exists
191
try {
192                 xslFileStream = new FileInputStream JavaDoc(this.xsltFile);
193             } catch (IOException JavaDoc ioe) {
194                 throw new CruiseControlException("Error reading the xsltFile: " + this.xsltFile, ioe);
195             }
196
197             //construct a FileWriter to the outputFile path location
198
try {
199                 out = new FileOutputStream JavaDoc(path);
200             } catch (IOException JavaDoc ioe) {
201                 throw new CruiseControlException("Unable to write to the file location: " + path);
202             }
203
204             //Prepare the transformer
205
TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
206             Transformer JavaDoc transformer = tFactory.newTransformer(new StreamSource JavaDoc(xslFileStream));
207
208             //cruisecontrolLog.get
209
XMLLogHelper helper = new XMLLogHelper(cruisecontrolLog);
210             String JavaDoc logFileName = helper.getLogFileName();
211             LOG.info(
212                     "Transforming the log file: " + logFileName + " to: " + path + " using the xslt: " + this.xsltFile);
213
214             //perform the transform, writing out the results to the output location
215
transformer.transform(new JDOMSource(cruisecontrolLog), new StreamResult JavaDoc(out));
216
217         } catch (TransformerException JavaDoc te) {
218             throw new CruiseControlException("An error occurred during the transformation process", te);
219         } catch (Exception JavaDoc ioe) {
220             throw new CruiseControlException("An unexpected exception occurred, unable to publish the log file.", ioe);
221         } finally {
222             try {
223                 if (xslFileStream != null) {
224                     xslFileStream.close();
225                 }
226             } catch (IOException JavaDoc e) {
227                 //Do nothing
228
}
229             try {
230                 if (out != null) {
231                     out.close();
232                 }
233             } catch (IOException JavaDoc e) {
234                 //Do Nothing
235
}
236         }
237     }
238 }
239
Popular Tags