KickJava   Java API By Example, From Geeks To Geeks.

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


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2004, 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 java.io.File JavaDoc;
40 import java.io.Serializable JavaDoc;
41 import java.text.DateFormat JavaDoc;
42 import java.text.ParseException JavaDoc;
43 import java.text.SimpleDateFormat JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.Collections JavaDoc;
46 import java.util.Date JavaDoc;
47 import java.util.List JavaDoc;
48
49 import net.sourceforge.cruisecontrol.taglib.CruiseControlLogFileFilter;
50 import net.sourceforge.cruisecontrol.taglib.CruiseControlSuccessfulLogFileFilter;
51
52 /**
53  * Contains various information about a specific build. The
54  * information is extracted from the name of the log file
55  * generated by the build.
56  *
57  * @author <a HREF="mailto:robertdw@users.sourceforge.net">Robert Watkins</a>
58  * @author <a HREF="mailto:hak@2mba.dk">Hack Kampbjorn</a>
59  */

60 public class BuildInfo implements Comparable JavaDoc, Serializable JavaDoc {
61     public static final String JavaDoc LOG_PREFIX = "log";
62     public static final char LABEL_SEPARATOR = 'L';
63     public static final String JavaDoc LOG_DATE_PATTERN = "yyyyMMddHHmmss";
64     private final Date JavaDoc buildDate;
65     private final String JavaDoc dateStamp;
66     private final String JavaDoc label;
67     private final LogFile logFile;
68
69     // Convenience constructor used by the testcasess
70
BuildInfo(String JavaDoc infoText) throws ParseException JavaDoc {
71         this(new File JavaDoc(infoText));
72     }
73     public BuildInfo(File JavaDoc logFile) throws ParseException JavaDoc {
74         this(new LogFile(logFile));
75     }
76     public BuildInfo(LogFile logFile) throws ParseException JavaDoc {
77         this.logFile = logFile;
78         dateStamp = deriveDateStamp();
79         buildDate = deriveDate();
80         label = deriveLabel();
81     }
82
83     private String JavaDoc deriveLabel() {
84         String JavaDoc infoText = logFile.getName();
85         boolean buildSuccessful = new CruiseControlSuccessfulLogFileFilter().isSuccessful(infoText);
86         String JavaDoc theLabel;
87         if (buildSuccessful) {
88             int labelStartIndex = (LOG_PREFIX + LOG_DATE_PATTERN + LABEL_SEPARATOR).length();
89             theLabel = infoText.substring(labelStartIndex);
90         } else {
91             theLabel = null;
92         }
93         return theLabel;
94     }
95
96     private String JavaDoc deriveDateStamp() throws ParseException JavaDoc {
97         String JavaDoc infoText = logFile.getName();
98         try {
99             return infoText.substring(LOG_PREFIX.length(), LOG_PREFIX.length() + LOG_DATE_PATTERN.length());
100         } catch (StringIndexOutOfBoundsException JavaDoc e) {
101             throw new IllegalStateException JavaDoc("infoText has wrong format: " + infoText + " " + e.getMessage());
102         }
103     }
104
105     private Date JavaDoc deriveDate() throws ParseException JavaDoc {
106         String JavaDoc infoText = logFile.getName();
107         Date JavaDoc theDate;
108         final DateFormat JavaDoc logDateFormat = new SimpleDateFormat JavaDoc(LOG_DATE_PATTERN);
109         try {
110             theDate = logDateFormat.parse(dateStamp);
111         } catch (ParseException JavaDoc e) {
112             throw new ParseException JavaDoc("Invalid format: " + infoText + ". Format must be logyyyyMMddHHmmSS.xml or "
113                                      + "logyyyyMMddHHmmSSLlabel.xml", e.getErrorOffset());
114         }
115         return theDate;
116     }
117
118     public Date JavaDoc getBuildDate() {
119         return buildDate;
120     }
121
122     /**
123      * Gets the date stamp of the log name.
124      * @return the build date as a stamp.
125      */

126     public String JavaDoc getDateStamp() {
127         return dateStamp;
128     }
129
130     public String JavaDoc getLabel() {
131         return label;
132     }
133     
134     public boolean isSuccessful() {
135         return getLabel() != null;
136     }
137
138     /**
139      * Gets the log's name with a file extension.
140      */

141     public String JavaDoc getLogName() {
142         return logFile.getName();
143     }
144
145     public static BuildInfoSummary loadFromDir(File JavaDoc logDir) throws CruiseControlWebAppException {
146         File JavaDoc [] logFileNames = logDir.listFiles(new CruiseControlLogFileFilter());
147         if (logFileNames == null) {
148             throw new CruiseControlWebAppException("Could not access the directory " + logDir.getAbsolutePath());
149         } else if (logFileNames.length == 0) {
150             throw new CruiseControlWebAppException("Configuration problem? No logs found in logDir: "
151                                              + logDir.getAbsolutePath());
152         }
153         List JavaDoc buildInfoList = new ArrayList JavaDoc(logFileNames.length);
154         for (int i = 0; i < logFileNames.length; i++) {
155             File JavaDoc file = logFileNames[i];
156             try {
157                 buildInfoList.add(new BuildInfo(file));
158             } catch (ParseException JavaDoc e) {
159                 throw new CruiseControlWebAppException("Could not parse log file name " + file.getName()
160                                            + ". Is the filter broken?", e);
161             }
162         }
163         Collections.sort(buildInfoList);
164         return new BuildInfoSummary(buildInfoList);
165     }
166
167     /**
168      * Return a comparision based on the build time.
169      * @see java.lang.Comparable#compareTo(java.lang.Object)
170      */

171     public int compareTo(Object JavaDoc arg0) {
172         BuildInfo other = (BuildInfo) arg0;
173         return this.buildDate.compareTo(other.buildDate);
174     }
175 }
176
Popular Tags