1 37 package net.sourceforge.cruisecontrol; 38 39 import net.sourceforge.cruisecontrol.taglib.CruiseControlLogFileFilter; 40 import net.sourceforge.cruisecontrol.taglib.CruiseControlSuccessfulLogFileFilter; 41 42 import java.io.File ; 43 import java.io.FileInputStream ; 44 import java.io.FilenameFilter ; 45 import java.io.IOException ; 46 import java.io.InputStream ; 47 import java.io.Serializable ; 48 import java.text.ParseException ; 49 import java.util.Arrays ; 50 import java.util.Collections ; 51 import java.util.zip.GZIPInputStream ; 52 53 54 62 public class LogFile implements Serializable { 63 65 public static final String LOG_SUFFIX = ".xml"; 66 public static final String LOG_COMPRESSED_SUFFIX = LOG_SUFFIX + ".gz"; 67 private static final FilenameFilter LOG_FILTER = new CruiseControlLogFileFilter(); 68 private static final FilenameFilter SUCCESSFUL_FILTER = new CruiseControlSuccessfulLogFileFilter(); 69 70 private File xmlFile; 71 72 77 public LogFile(File logDir, String logName) { 78 this.xmlFile = new File (logDir, logName + LOG_SUFFIX); 79 if (!xmlFile.exists()) { 80 xmlFile = new File (logDir, logName + LOG_COMPRESSED_SUFFIX); 81 } 82 } 83 84 88 public LogFile(File xmlFile) { 89 this.xmlFile = xmlFile; 90 } 91 92 99 public static LogFile getLatestLogFile(File logDir) { 100 File [] logs = logDir.listFiles(LOG_FILTER); 101 if (logs != null && logs.length > 0) { 102 return new LogFile((File ) Collections.max(Arrays.asList(logs))); 103 } else { 104 return null; 105 } 106 } 107 108 116 public static LogFile getLatestSuccessfulLogFile(File logDir) { 117 File [] logs = logDir.listFiles(SUCCESSFUL_FILTER); 118 if (logs != null && logs.length > 0) { 119 return new LogFile((File ) Collections.max(Arrays.asList(logs))); 120 } else { 121 return null; 122 } 123 } 124 125 130 public BuildInfo getBuildInfo() throws ParseException { 131 return new BuildInfo(this); 132 } 133 134 138 public File getFile() { 139 return xmlFile; 140 } 141 142 146 public boolean isCompressed() { 147 return getFile().getName().endsWith(LOG_COMPRESSED_SUFFIX); 148 } 149 150 158 public String getName() { 159 return extractLogNameFromFileName(getFile().getName()); 160 } 161 private String extractLogNameFromFileName(String fileName) { 162 return fileName.substring(0, fileName.lastIndexOf(LOG_SUFFIX)); 163 } 164 165 169 public File getLogDirectory() { 170 return getFile().getParentFile(); 171 } 172 173 179 public InputStream getInputStream() throws IOException { 180 InputStream in = new FileInputStream (xmlFile); 181 if (isCompressed()) { 182 in = new GZIPInputStream (in); 183 } 184 return in; 185 } 186 } 187 | Popular Tags |