KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > RecorderEntry


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs;
19
20 import java.io.FileOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.PrintStream JavaDoc;
23 import org.apache.tools.ant.BuildEvent;
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.BuildLogger;
26 import org.apache.tools.ant.DefaultLogger;
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.SubBuildListener;
29 import org.apache.tools.ant.util.StringUtils;
30
31 /**
32  * This is a class that represents a recorder. This is the listener to the
33  * build process.
34  *
35  * @since Ant 1.4
36  */

37 public class RecorderEntry implements BuildLogger, SubBuildListener {
38
39     //////////////////////////////////////////////////////////////////////
40
// ATTRIBUTES
41

42     /** The name of the file associated with this recorder entry. */
43     private String JavaDoc filename = null;
44     /** The state of the recorder (recorder on or off). */
45     private boolean record = true;
46     /** The current verbosity level to record at. */
47     private int loglevel = Project.MSG_INFO;
48     /** The output PrintStream to record to. */
49     private PrintStream JavaDoc out = null;
50     /** The start time of the last know target. */
51     private long targetStartTime = 0L;
52     /** Strip task banners if true. */
53     private boolean emacsMode = false;
54     /** project instance the recorder is associated with */
55     private Project project;
56
57     //////////////////////////////////////////////////////////////////////
58
// CONSTRUCTORS / INITIALIZERS
59

60     /**
61      * @param name The name of this recorder (used as the filename).
62      */

63     protected RecorderEntry(String JavaDoc name) {
64         targetStartTime = System.currentTimeMillis();
65         filename = name;
66     }
67
68     //////////////////////////////////////////////////////////////////////
69
// ACCESSOR METHODS
70

71     /**
72      * @return the name of the file the output is sent to.
73      */

74     public String JavaDoc getFilename() {
75         return filename;
76     }
77
78     /**
79      * Turns off or on this recorder.
80      *
81      * @param state true for on, false for off, null for no change.
82      */

83     public void setRecordState(Boolean JavaDoc state) {
84         if (state != null) {
85             flush();
86             record = state.booleanValue();
87         }
88     }
89
90     /**
91      * @see org.apache.tools.ant.BuildListener#buildStarted(BuildEvent)
92      */

93     /** {@inheritDoc}. */
94     public void buildStarted(BuildEvent event) {
95         log("> BUILD STARTED", Project.MSG_DEBUG);
96     }
97
98     /**
99      * @see org.apache.tools.ant.BuildListener#buildFinished(BuildEvent)
100      */

101     /** {@inheritDoc}. */
102     public void buildFinished(BuildEvent event) {
103         log("< BUILD FINISHED", Project.MSG_DEBUG);
104
105         if (record && out != null) {
106             Throwable JavaDoc error = event.getException();
107
108             if (error == null) {
109                 out.println(StringUtils.LINE_SEP + "BUILD SUCCESSFUL");
110             } else {
111                 out.println(StringUtils.LINE_SEP + "BUILD FAILED"
112                             + StringUtils.LINE_SEP);
113                 error.printStackTrace(out);
114             }
115         }
116         cleanup();
117     }
118
119     /**
120      * Cleans up any resources held by this recorder entry at the end
121      * of a subbuild if it has been created for the subbuild's project
122      * instance.
123      *
124      * @param event the buildFinished event
125      *
126      * @since Ant 1.6.2
127      */

128     public void subBuildFinished(BuildEvent event) {
129         if (event.getProject() == project) {
130             cleanup();
131         }
132     }
133
134     /**
135      * Empty implementation to satisfy the BuildListener interface.
136      *
137      * @param event the buildStarted event
138      *
139      * @since Ant 1.6.2
140      */

141     public void subBuildStarted(BuildEvent event) {
142     }
143
144     /**
145      * @see org.apache.tools.ant.BuildListener#targetStarted(BuildEvent)
146      */

147     /** {@inheritDoc}. */
148     public void targetStarted(BuildEvent event) {
149         log(">> TARGET STARTED -- " + event.getTarget(), Project.MSG_DEBUG);
150         log(StringUtils.LINE_SEP + event.getTarget().getName() + ":",
151             Project.MSG_INFO);
152         targetStartTime = System.currentTimeMillis();
153     }
154
155     /**
156      * @see org.apache.tools.ant.BuildListener#targetFinished(BuildEvent)
157      */

158     /** {@inheritDoc}. */
159     public void targetFinished(BuildEvent event) {
160         log("<< TARGET FINISHED -- " + event.getTarget(), Project.MSG_DEBUG);
161
162         String JavaDoc time = formatTime(System.currentTimeMillis() - targetStartTime);
163
164         log(event.getTarget() + ": duration " + time, Project.MSG_VERBOSE);
165         flush();
166     }
167
168     /**
169      * @see org.apache.tools.ant.BuildListener#taskStarted(BuildEvent)
170      */

171     /** {@inheritDoc}. */
172     public void taskStarted(BuildEvent event) {
173         log(">>> TASK STARTED -- " + event.getTask(), Project.MSG_DEBUG);
174     }
175
176     /**
177      * @see org.apache.tools.ant.BuildListener#taskFinished(BuildEvent)
178      */

179     /** {@inheritDoc}. */
180     public void taskFinished(BuildEvent event) {
181         log("<<< TASK FINISHED -- " + event.getTask(), Project.MSG_DEBUG);
182         flush();
183     }
184
185     /**
186      * @see org.apache.tools.ant.BuildListener#messageLogged(BuildEvent)
187      */

188     /** {@inheritDoc}. */
189     public void messageLogged(BuildEvent event) {
190         log("--- MESSAGE LOGGED", Project.MSG_DEBUG);
191
192         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
193
194         if (event.getTask() != null) {
195             String JavaDoc name = event.getTask().getTaskName();
196
197             if (!emacsMode) {
198                 String JavaDoc label = "[" + name + "] ";
199                 int size = DefaultLogger.LEFT_COLUMN_SIZE - label.length();
200
201                 for (int i = 0; i < size; i++) {
202                     buf.append(" ");
203                 }
204                 buf.append(label);
205             }
206         }
207         buf.append(event.getMessage());
208
209         log(buf.toString(), event.getPriority());
210     }
211
212
213     /**
214      * The thing that actually sends the information to the output.
215      *
216      * @param mesg The message to log.
217      * @param level The verbosity level of the message.
218      */

219     private void log(String JavaDoc mesg, int level) {
220         if (record && (level <= loglevel) && out != null) {
221             out.println(mesg);
222         }
223     }
224
225     private void flush() {
226         if (record && out != null) {
227             out.flush();
228         }
229     }
230
231     /**
232      * @see BuildLogger#setMessageOutputLevel(int)
233      */

234     /** {@inheritDoc}. */
235     public void setMessageOutputLevel(int level) {
236         if (level >= Project.MSG_ERR && level <= Project.MSG_DEBUG) {
237             loglevel = level;
238         }
239     }
240
241     /**
242      * @see BuildLogger#setOutputPrintStream(PrintStream)
243      */

244     /** {@inheritDoc}. */
245     public void setOutputPrintStream(PrintStream JavaDoc output) {
246         closeFile();
247         out = output;
248     }
249
250
251     /**
252      * @see BuildLogger#setEmacsMode(boolean)
253      */

254     /** {@inheritDoc}. */
255     public void setEmacsMode(boolean emacsMode) {
256         this.emacsMode = emacsMode;
257     }
258
259
260     /**
261      * @see BuildLogger#setErrorPrintStream(PrintStream)
262      */

263     /** {@inheritDoc}. */
264     public void setErrorPrintStream(PrintStream JavaDoc err) {
265         setOutputPrintStream(err);
266     }
267
268
269     private static String JavaDoc formatTime(long millis) {
270         long seconds = millis / 1000;
271         long minutes = seconds / 60;
272
273
274         if (minutes > 0) {
275             return Long.toString(minutes) + " minute"
276                  + (minutes == 1 ? " " : "s ")
277                  + Long.toString(seconds % 60) + " second"
278                  + (seconds % 60 == 1 ? "" : "s");
279         } else {
280             return Long.toString(seconds) + " second"
281                  + (seconds % 60 == 1 ? "" : "s");
282         }
283
284     }
285
286     /**
287      * Set the project associated with this recorder entry.
288      *
289      * @param project the project instance
290      *
291      * @since 1.6.2
292      */

293     public void setProject(Project project) {
294         this.project = project;
295         if (project != null) {
296             project.addBuildListener(this);
297         }
298     }
299
300     /**
301      * @since 1.6.2
302      */

303     public void cleanup() {
304         closeFile();
305         if (project != null) {
306             project.removeBuildListener(this);
307         }
308         project = null;
309     }
310
311     /**
312      * Initially opens the file associated with this recorder.
313      * Used by Recorder.
314      * @param append Indicates if output must be appended to the logfile or that
315      * the logfile should be overwritten.
316      * @throws BuildException
317      * @since 1.6.3
318      */

319     void openFile(boolean append) throws BuildException {
320         openFileImpl(append);
321     }
322
323     /**
324      * Closes the file associated with this recorder.
325      * Used by Recorder.
326      * @since 1.6.3
327      */

328     void closeFile() {
329         if (out != null) {
330             out.close();
331             out = null;
332         }
333     }
334
335     /**
336      * Re-opens the file associated with this recorder.
337      * Used by Recorder.
338      * @throws BuildException
339      * @since 1.6.3
340      */

341     void reopenFile() throws BuildException {
342         openFileImpl(true);
343     }
344
345     private void openFileImpl(boolean append) throws BuildException {
346         if (out == null) {
347             try {
348                 out = new PrintStream JavaDoc(new FileOutputStream JavaDoc(filename, append));
349             } catch (IOException JavaDoc ioe) {
350                 throw new BuildException("Problems opening file using a "
351                                          + "recorder entry", ioe);
352             }
353         }
354     }
355
356 }
357
Popular Tags