KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sourceforge.cruisecontrol;
2
3 import java.io.BufferedReader JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.FileReader JavaDoc;
6 import java.io.IOException JavaDoc;
7
8 /**
9  * This class has the logic to determine current build status message.
10  *
11  * <br/><br/>Note the use of the <code>READ_*_LINES</code> constants for the
12  * <code>maxReadLines</code> parameter:
13  * <ul>
14  * <li>READ_ONLY_STATUS_LINES only returns the first two lines that contain the
15  * current summary build status and time.</li>
16  * <li>READ_ALL_LINES returns the entire contents for the build status file.
17  * This includes any output from other loggers, such as the XmlLoggerWithStatus.</li>
18  * </ul>
19  * You can always pass a number to get a specific number of lines.
20  *
21  * @author <a HREF="mailto:jeffjensen@upstairstechnology.com">Jeff Jensen </a>
22  */

23 public class BuildStatus {
24     /** Constant meaning to read all lines from the build status file. */
25     public static final int READ_ALL_LINES = -2;
26
27     /**
28      * Constant meaning to read only the project status and time lines from the
29      * build status file.
30      */

31     public static final int READ_ONLY_STATUS_LINES = 2;
32
33     protected BuildStatus() {
34     }
35
36     /**
37      * Generate the current build status string formatted for plain text.
38      *
39      * @param isSingleProject
40      * Specify true if this is a single project config, or false if
41      * it is a multi project config.
42      * @param dir
43      * The dir containing the build status file.
44      * @param projectName
45      * The name of the project to get the build status for.
46      * @param statusFileName
47      * The name of the status file.
48      * @param maxReadLines
49      * The maximim number of lines to read from the file. Use the
50      * READ_ALL_LINES (value of zero) and READ_ONLY_STATUS_LINES
51      * (value of 2) constants when applicable.
52      * @return The build status string formatted for plain text usage or the
53      * text <code>(build status file not found)</code> if the
54      * status file cannot be not found.
55      */

56     public static String JavaDoc getStatusPlain(boolean isSingleProject, String JavaDoc dir,
57         String JavaDoc projectName, String JavaDoc statusFileName, int maxReadLines) {
58         String JavaDoc status = genStatus(isSingleProject, dir, projectName,
59             statusFileName, false, maxReadLines);
60
61         return status;
62     }
63
64     /**
65      * Generate the current build status string formatted for HTML.
66      *
67      * @param isSingleProject
68      * Specify true if this is a single project config, or false if
69      * it is a multi project config.
70      * @param dir
71      * The dir containing the build status file.
72      * @param projectName
73      * The name of the project to get the build status for.
74      * @param statusFileName
75      * The name of the status file.
76      * @param maxReadLines
77      * The maximim number of lines to read from the file. Use the
78      * READ_ALL_LINES (value of zero) and READ_ONLY_STATUS_LINES
79      * (value of 2) constants when applicable.
80      * @return The build status string formatted for plain text usage or the
81      * text <code>(build status file not found)</code> if the
82      * status file cannot be not found.
83      */

84     public static String JavaDoc getStatusHtml(boolean isSingleProject, String JavaDoc dir,
85         String JavaDoc projectName, String JavaDoc statusFileName, int maxReadLines) {
86         String JavaDoc status = genStatus(isSingleProject, dir, projectName,
87             statusFileName, true, maxReadLines);
88
89         return status;
90     }
91
92     /**
93      * Generate the current build status string.
94      *
95      * @param isSingleProject
96      * Specify true if this is a single project config, or false if
97      * it is a multi project config.
98      * @param dir
99      * The dir containing the build status file.
100      * @param projectName
101      * The name of the project to get the build status for.
102      * @param statusFileName
103      * The name of the status file.
104      * @param insertBreaks
105      * If true, inserts XHTML br tag between content lines from the
106      * status file.
107      * @param maxReadLines
108      * The maximim number of lines to read from the file. Use the
109      * READ_ALL_LINES (value of zero) and READ_ONLY_STATUS_LINES
110      * (value of 2) constants when applicable.
111      * @return The build status string formatted as per the insertBreaks param
112      * or the text <code>(build status file not found)</code> if the
113      * status file is not found.
114      */

115     private static String JavaDoc genStatus(boolean isSingleProject, String JavaDoc dir,
116         String JavaDoc projectName, String JavaDoc statusFileName, boolean insertBreaks,
117         int maxReadLines) {
118
119         File JavaDoc statusFile = getFile(isSingleProject, dir, projectName,
120             statusFileName);
121
122         String JavaDoc status;
123         if (statusFile.exists()) {
124             status = getStatus(statusFile, insertBreaks, maxReadLines);
125         } else {
126             status = "(build status file not found)";
127         }
128
129         return status;
130     }
131
132     /**
133      * Get the status from the build status file. Need to consider a &lt;br&gt;
134      * a new line to account for XmlLoggerWithStatus output.
135      *
136      * @param statusFile
137      * The status file to get the status info from.
138      * @param insertBreaks
139      * true to insert HTML break elements at newlines.
140      * @param maxReadLines
141      * The maximim number of lines to read from the file. Use the
142      * READ_ALL_LINES (value of zero) and READ_ONLY_STATUS_LINES
143      * (value of 2) constants when applicable.
144      * @return The status string from the specified status file.
145      */

146     private static String JavaDoc getStatus(File JavaDoc statusFile, boolean insertBreaks,
147         int maxReadLines) {
148         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(101);
149         BufferedReader JavaDoc br = null;
150
151         try {
152             br = new BufferedReader JavaDoc(new FileReader JavaDoc(statusFile));
153             String JavaDoc line = br.readLine();
154             int linesRead = 1;
155
156             while (line != null && readMoreLines(linesRead, maxReadLines)) {
157               
158                 if (line.indexOf("<br>") == -1) {
159                     addLine(line, sb, insertBreaks);
160                 } else {
161                     int startIndex = 0;
162                     for (int endIndex = line.indexOf("<br>");
163                              endIndex != -1 && readMoreLines(linesRead, maxReadLines);
164                              linesRead++) {
165                         String JavaDoc substring = line.substring(startIndex, endIndex);
166                         if (substring.length() > 0) {
167                             addLine(substring, sb, insertBreaks);
168                         } else {
169                             linesRead--;
170                         }
171                         startIndex = endIndex + "<br>".length();
172                         endIndex = line.indexOf("<br>", startIndex);
173                     }
174                     String JavaDoc substring = line.substring(startIndex);
175                     if (substring.length() > 0 && readMoreLines(linesRead, maxReadLines)) {
176                         addLine(substring, sb, insertBreaks);
177                     }
178                 }
179
180                 line = br.readLine();
181                 linesRead++;
182             }
183         } catch (IOException JavaDoc e) {
184             throw new CruiseControlWebAppException(
185                 "Error reading status file: " + statusFile.getName() + " : "
186                     + e.getMessage(), e);
187         } finally {
188             try {
189                 if (br != null) {
190                     br.close();
191                 }
192             } catch (IOException JavaDoc e) {
193                 // skip action on close error
194
}
195             br = null;
196         }
197
198         return sb.toString();
199     }
200
201     private static boolean readMoreLines(int linesRead, int maxReadLines) {
202         return linesRead <= maxReadLines || maxReadLines == READ_ALL_LINES;
203     }
204
205     private static void addLine(String JavaDoc line, StringBuffer JavaDoc sb, boolean insertBreaks) {
206         sb.append(line);
207         sb.append('\n');
208         if (insertBreaks) {
209             sb.append("<br/>");
210         }
211     }
212
213     /**
214      * Get the status file to read the status info from.
215      *
216      * @param isSingleProject
217      * Specify true if this is a single project config, or false if
218      * it is a multi project config.
219      * @param dir
220      * The dir containing the build status file.
221      * @param projectName
222      * The name of the project to get the build status for.
223      * @param statusFileName
224      * The name of the status file.
225      * @return The status file.
226      */

227     private static File JavaDoc getFile(boolean isSingleProject, String JavaDoc dir,
228         String JavaDoc projectName, String JavaDoc statusFileName) {
229         String JavaDoc statusFileDir = null;
230
231         if (isSingleProject) {
232             statusFileDir = dir;
233         } else {
234             statusFileDir = dir + System.getProperty("file.separator")
235                 + projectName;
236         }
237
238         File JavaDoc statusFile = new File JavaDoc(statusFileDir, statusFileName);
239
240         if (statusFile.isDirectory()) {
241             final String JavaDoc msg = "CruiseControl: currentBuildStatusFile "
242                 + statusFile.getAbsolutePath() + " is a directory."
243                 + " Edit the web.xml to provide the path to the correct file.";
244             throw new CruiseControlWebAppException(msg);
245         }
246
247         return statusFile;
248     }
249 }
250
Popular Tags