KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > LogFile


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;
38
39 import net.sourceforge.cruisecontrol.taglib.CruiseControlLogFileFilter;
40 import net.sourceforge.cruisecontrol.taglib.CruiseControlSuccessfulLogFileFilter;
41
42 import java.io.File JavaDoc;
43 import java.io.FileInputStream JavaDoc;
44 import java.io.FilenameFilter JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.InputStream JavaDoc;
47 import java.io.Serializable JavaDoc;
48 import java.text.ParseException JavaDoc;
49 import java.util.Arrays JavaDoc;
50 import java.util.Collections JavaDoc;
51 import java.util.zip.GZIPInputStream JavaDoc;
52
53
54 /**
55  * Represents a XML log file.
56  * Build information that is based on the log name is in the <code>BuildInfo</code>
57  * class
58  *
59  * @see BuildInfo
60  * @author <a HREF="mailto:hak@2mba.dk">Hack Kampbjorn</a>
61  */

62 public class LogFile implements Serializable JavaDoc {
63     // NOTE: LogFile must be Serializable for Metrics tab (charts) to work
64

65     public static final String JavaDoc LOG_SUFFIX = ".xml";
66     public static final String JavaDoc LOG_COMPRESSED_SUFFIX = LOG_SUFFIX + ".gz";
67     private static final FilenameFilter JavaDoc LOG_FILTER = new CruiseControlLogFileFilter();
68     private static final FilenameFilter JavaDoc SUCCESSFUL_FILTER = new CruiseControlSuccessfulLogFileFilter();
69
70     private File JavaDoc xmlFile;
71
72     /**
73      * Creates a new instance of LogFile
74      * @param logDir directory with the XML log file
75      * @param logName name of the XML log file
76      */

77     public LogFile(File JavaDoc logDir, String JavaDoc logName) {
78         this.xmlFile = new File JavaDoc(logDir, logName + LOG_SUFFIX);
79         if (!xmlFile.exists()) {
80             xmlFile = new File JavaDoc(logDir, logName + LOG_COMPRESSED_SUFFIX);
81         }
82     }
83
84     /**
85      * Creates a new instance of LogFile
86      * @param xmlFile the XML log file
87      */

88     public LogFile(File JavaDoc xmlFile) {
89         this.xmlFile = xmlFile;
90     }
91
92     /**
93      * Gets the latest log file in a given directory. Since all of our logs contain a date/time string, this method
94      * is actually getting the log file that comes last alphabetically.
95      *
96      * @return The latest log file or <code>null</code> if there are no log
97      * files in the given directory.
98      */

99     public static LogFile getLatestLogFile(File JavaDoc logDir) {
100         File JavaDoc[] logs = logDir.listFiles(LOG_FILTER);
101         if (logs != null && logs.length > 0) {
102             return new LogFile((File JavaDoc) Collections.max(Arrays.asList(logs)));
103         } else {
104             return null;
105         }
106     }
107
108     /**
109      * Gets the latest successful log file in a given directory.
110      * Since all of our logs contain a date/time string, this method
111      * is actually getting the log file that comes last alphabetically.
112      *
113      * @return The latest log file or <code>null</code> if there are no
114      * sucessful log files in the given directory
115      */

116     public static LogFile getLatestSuccessfulLogFile(File JavaDoc logDir) {
117         File JavaDoc[] logs = logDir.listFiles(SUCCESSFUL_FILTER);
118         if (logs != null && logs.length > 0) {
119             return new LogFile((File JavaDoc) Collections.max(Arrays.asList(logs)));
120         } else {
121             return null;
122         }
123     }
124
125     /**
126      * Gets the build information for this log file like the label and build
127      * date.
128      * @return the log file's build information
129      */

130     public BuildInfo getBuildInfo() throws ParseException JavaDoc {
131         return new BuildInfo(this);
132     }
133
134     /**
135      * Gets the file object.
136      * @return the log file
137      */

138     public File JavaDoc getFile() {
139         return xmlFile;
140     }
141
142     /**
143      * Wether the log file is compressed or not.
144      * @return <code>true</code> if the file is compressed
145      */

146     public boolean isCompressed() {
147         return getFile().getName().endsWith(LOG_COMPRESSED_SUFFIX);
148     }
149
150     /**
151      * Gets the log's name.
152      * This is the file name without a file extension like <code>.xml<code> or
153      * <code>.xml.gz</code>. Use <code>getFile().getName()</code> to get the
154      * file name with extension.
155      *
156      * @return the name of the log
157      */

158     public String JavaDoc getName() {
159         return extractLogNameFromFileName(getFile().getName());
160     }
161     private String JavaDoc extractLogNameFromFileName(String JavaDoc fileName) {
162         return fileName.substring(0, fileName.lastIndexOf(LOG_SUFFIX));
163     }
164
165     /**
166      * Gets the log file's directory.
167      * @return the parent directory of the log file
168      */

169     public File JavaDoc getLogDirectory() {
170         return getFile().getParentFile();
171     }
172
173     /**
174      * Gets a stream with the log file's content.
175      *
176      * @throws java.io.IOException if there is an error reading the file
177      * @return the file content as a stream
178      */

179     public InputStream JavaDoc getInputStream() throws IOException JavaDoc {
180         InputStream JavaDoc in = new FileInputStream JavaDoc(xmlFile);
181         if (isCompressed()) {
182             in = new GZIPInputStream JavaDoc(in);
183         }
184         return in;
185     }
186 }
187
Popular Tags