1 37 package net.sourceforge.cruisecontrol; 38 39 import net.sourceforge.cruisecontrol.util.DateUtil; 40 import net.sourceforge.cruisecontrol.util.XMLLogHelper; 41 import org.jdom.Content; 42 import org.jdom.Document; 43 import org.jdom.Element; 44 import org.jdom.output.Format; 45 import org.jdom.output.XMLOutputter; 46 47 import java.io.BufferedOutputStream ; 48 import java.io.File ; 49 import java.io.FileOutputStream ; 50 import java.io.IOException ; 51 import java.io.OutputStream ; 52 import java.util.ArrayList ; 53 import java.util.Date ; 54 import java.util.Iterator ; 55 import java.util.List ; 56 import java.text.SimpleDateFormat ; 57 import java.text.ParseException ; 58 59 60 64 public class Log { 65 private static final org.apache.log4j.Logger LOG4J = 66 org.apache.log4j.Logger.getLogger(Log.class); 67 68 public static final int BEFORE_LENGTH = "logYYYYMMDDhhmmssL".length(); 69 private static final int AFTER_LENGTH = ".xml".length(); 70 71 private transient String logDir; 72 private transient String logXmlEncoding; 73 private transient File lastLogFile; 74 private transient Element buildLog; 75 private transient List loggers = new ArrayList (); 76 private transient List manipulators = new ArrayList (); 77 private transient String projectName; 78 79 82 public Log() { 83 reset(); 84 } 85 86 91 void setProjectName(String projectName) { 92 if (projectName == null) { 93 throw new NullPointerException ("null projectName."); 94 } 95 this.projectName = projectName; 96 if (logDir == null) { 97 logDir = "logs" + File.separatorChar + projectName; 98 } 99 } 100 101 105 public void validate() throws CruiseControlException { 106 if (projectName == null) { 107 throw new IllegalStateException ("projectName unset."); 109 } 110 if (logDir != null) { 111 checkLogDirectory(logDir); 112 } 113 114 for (Iterator i = loggers.iterator(); i.hasNext();) { 115 BuildLogger logger = (BuildLogger) i.next(); 116 logger.validate(); 117 } 118 119 for (Iterator i = manipulators.iterator(); i.hasNext();) { 120 Manipulator manipulator = (Manipulator) i.next(); 121 manipulator.validate(); 122 } 123 } 124 125 129 public void add(BuildLogger logger) { 130 loggers.add(logger); 131 } 132 133 136 public void add(Manipulator manipulator) { 137 manipulators.add(manipulator); 138 } 139 140 public BuildLogger[] getLoggers() { 141 return (BuildLogger[]) loggers.toArray(new BuildLogger[loggers.size()]); 142 } 143 144 public String getLogXmlEncoding() { 145 return logXmlEncoding; 146 } 147 148 public String getProjectName() { 149 return projectName; 150 } 151 152 157 public void setLogDir(String logDir) throws CruiseControlException { 158 setDir(logDir); 159 } 160 161 public void setDir(String logDir) throws CruiseControlException { 162 this.logDir = logDir; 163 } 164 165 169 public void setLogXmlEncoding(String logXmlEncoding) { 170 setEncoding(logXmlEncoding); 171 } 172 173 public void setEncoding(String logXmlEncoding) { 174 this.logXmlEncoding = logXmlEncoding; 175 } 176 177 public String getLogDir() { 178 return logDir; 179 } 180 181 184 public File getLastLogFile() { 185 return this.lastLogFile; 186 } 187 188 193 private void checkLogDirectory(String logDir) throws CruiseControlException { 194 File logDirectory = new File (logDir); 195 if (!logDirectory.exists()) { 196 LOG4J.info( 197 "log directory specified in config file does not exist; creating: " 198 + logDirectory.getAbsolutePath()); 199 if (!logDirectory.mkdirs()) { 200 throw new CruiseControlException( 201 "Can't create log directory specified in config file: " 202 + logDirectory.getAbsolutePath()); 203 } 204 } else if (!logDirectory.isDirectory()) { 205 throw new CruiseControlException( 206 "Log directory specified in config file is not a directory: " 207 + logDirectory.getAbsolutePath()); 208 } 209 } 210 211 214 public void writeLogFile(Date now) throws CruiseControlException { 215 216 for (int i = 0; i < loggers.size(); i++) { 218 BuildLogger nextLogger = (BuildLogger) loggers.get(i); 219 nextLogger.log(buildLog); 222 } 223 224 XMLLogHelper helper = new XMLLogHelper(buildLog); 226 227 String logFilename; 228 if (helper.isBuildSuccessful()) { 229 logFilename = formatLogFileName(now, helper.getLabel()); 230 } else { 231 logFilename = formatLogFileName(now); 232 } 233 234 this.lastLogFile = new File (logDir, logFilename); 235 if (LOG4J.isDebugEnabled()) { 236 LOG4J.debug("Project " + projectName + ": Writing log file [" 237 + lastLogFile.getAbsolutePath() + "]"); 238 } 239 240 Element logDirElement = new Element("property"); 242 logDirElement.setAttribute("name", "logdir"); 243 logDirElement.setAttribute("value", new File (logDir).getAbsolutePath()); 244 buildLog.getChild("info").addContent(logDirElement); 245 246 Element logFileElement = new Element("property"); 248 logFileElement.setAttribute("name", "logfile"); 249 logFileElement.setAttribute("value", logFilename); 250 buildLog.getChild("info").addContent(logFileElement); 251 252 OutputStream logStream = null; 255 try { 256 Format format = Format.getPrettyFormat(); 257 if (logXmlEncoding != null) { 258 format.setEncoding(logXmlEncoding); 259 } 260 XMLOutputter outputter = new XMLOutputter(format); 261 logStream = new BufferedOutputStream (new FileOutputStream (lastLogFile)); 262 outputter.output(new Document(buildLog), logStream); 263 } catch (IOException e) { 264 throw new CruiseControlException(e); 265 } finally { 266 if (logStream != null) { 267 try { 268 logStream.close(); 269 } catch (IOException e) { 270 } 272 } 273 } 274 275 callManipulators(); 276 } 277 278 281 protected void callManipulators() { 282 for (Iterator i = manipulators.iterator(); i.hasNext();) { 283 Manipulator manipulator = (Manipulator) i.next(); 284 manipulator.execute(getLogDir()); 285 } 286 } 287 288 public static String formatLogFileName(Date date) { 289 return formatLogFileName(date, null); 290 } 291 292 public static String formatLogFileName(Date date, String label) { 293 StringBuffer logFileName = new StringBuffer (); 294 logFileName.append("log"); 295 logFileName.append(DateUtil.getFormattedTime(date)); 296 if (label != null) { 297 logFileName.append("L"); 298 logFileName.append(label); 299 } 300 logFileName.append(".xml"); 301 302 return logFileName.toString(); 303 } 304 305 public void addContent(Content newContent) { 306 buildLog.addContent(newContent); 307 } 308 309 public Element getContent() { 310 return (Element) buildLog.clone(); 311 } 312 313 public boolean wasBuildSuccessful() { 314 return new XMLLogHelper(buildLog).isBuildSuccessful(); 315 } 316 317 321 public void reset() { 322 this.buildLog = new Element("cruisecontrol"); 323 } 324 325 public static boolean wasSuccessfulBuild(String filename) { 326 if (filename == null) { 327 return false; 328 } 329 boolean startsWithLog = filename.startsWith("log"); 330 boolean hasLabelSeparator = filename.indexOf('L') == BEFORE_LENGTH - 1; 331 boolean isXmlFile = filename.endsWith(".xml"); 332 return startsWithLog && hasLabelSeparator && isXmlFile; 333 } 334 335 public static Date parseDateFromLogFileName(String filename) throws ParseException { 336 SimpleDateFormat formatter = new SimpleDateFormat ("yyyyMMddHHmmss"); 337 return formatter.parse(filename.substring(3, BEFORE_LENGTH - 1)); 338 } 339 340 public static String parseLabelFromLogFileName(String filename) { 341 if (!Log.wasSuccessfulBuild(filename)) { 342 return ""; 343 } 344 return filename.substring(BEFORE_LENGTH, filename.length() - AFTER_LENGTH); 345 } 346 347 } 348 349 | Popular Tags |