1 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 ; 48 import javax.xml.transform.TransformerException ; 49 import javax.xml.transform.TransformerFactory ; 50 import javax.xml.transform.stream.StreamResult ; 51 import javax.xml.transform.stream.StreamSource ; 52 import java.io.File ; 53 import java.io.FileInputStream ; 54 import java.io.FileOutputStream ; 55 import java.io.IOException ; 56 import java.io.OutputStream ; 57 58 64 public class XSLTLogPublisher implements Publisher { 65 private static final Logger LOG = Logger.getLogger(XSLTLogPublisher.class); 66 67 private String directory; 68 private String xsltFile; 69 private boolean publishOnFail = true; 70 private String outFileName; 71 72 77 public void setXsltFile(String fileName) { 78 this.xsltFile = fileName; 79 } 80 81 90 public void setOutFileName(String name) { 91 this.outFileName = name; 92 } 93 94 99 public void setDirectory(String directory) { 100 this.directory = directory; 101 } 102 103 111 public void setPublishOnFail(boolean pof) { 112 this.publishOnFail = pof; 113 } 114 115 121 public void validate() throws CruiseControlException { 122 ValidationHelper.assertIsSet(xsltFile, "xsltFile", this.getClass()); 123 ValidationHelper.assertIsSet(directory, "directory", this.getClass()); 124 } 125 126 129 public void publish(Element cruisecontrolLog) throws CruiseControlException { 130 XMLLogHelper helper = new XMLLogHelper(cruisecontrolLog); 131 Boolean buildSuccess = null; 132 try { 133 buildSuccess = (helper.isBuildSuccessful()) ? Boolean.TRUE : Boolean.FALSE; 134 } catch (NullPointerException ne) { 135 } 137 138 if (cruisecontrolLog == null || buildSuccess == null) { 141 return; 142 } 143 144 if (!buildSuccess.booleanValue() && !publishOnFail) { 146 LOG.info("Build failed and publishOnFail is false: Not publishing log."); 147 return; 148 } 149 150 if (outFileName == null) { 153 String 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 File dir = new File (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 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 186 protected void writeFile(Element cruisecontrolLog, String path) throws CruiseControlException { 187 FileInputStream xslFileStream = null; 188 OutputStream out = null; 189 try { 190 try { 192 xslFileStream = new FileInputStream (this.xsltFile); 193 } catch (IOException ioe) { 194 throw new CruiseControlException("Error reading the xsltFile: " + this.xsltFile, ioe); 195 } 196 197 try { 199 out = new FileOutputStream (path); 200 } catch (IOException ioe) { 201 throw new CruiseControlException("Unable to write to the file location: " + path); 202 } 203 204 TransformerFactory tFactory = TransformerFactory.newInstance(); 206 Transformer transformer = tFactory.newTransformer(new StreamSource (xslFileStream)); 207 208 XMLLogHelper helper = new XMLLogHelper(cruisecontrolLog); 210 String logFileName = helper.getLogFileName(); 211 LOG.info( 212 "Transforming the log file: " + logFileName + " to: " + path + " using the xslt: " + this.xsltFile); 213 214 transformer.transform(new JDOMSource(cruisecontrolLog), new StreamResult (out)); 216 217 } catch (TransformerException te) { 218 throw new CruiseControlException("An error occurred during the transformation process", te); 219 } catch (Exception 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 e) { 227 } 229 try { 230 if (out != null) { 231 out.close(); 232 } 233 } catch (IOException e) { 234 } 236 } 237 } 238 } 239 | Popular Tags |