KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > util > XmlLoggerWithStatus


1 package net.sourceforge.cruisecontrol.util;
2
3 import java.io.File JavaDoc;
4 import java.io.FileWriter JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.Writer JavaDoc;
7 import java.text.SimpleDateFormat JavaDoc;
8 import java.util.Date JavaDoc;
9
10 import org.apache.tools.ant.BuildEvent;
11 import org.apache.tools.ant.XmlLogger;
12
13 /**
14  * Extends XmlLogger by adding information about currently running target to a
15  * file specified by system property <br>
16  * XmlLoggerWithStatus.file
17  *
18  * @author IgorSemenko (igor@semenko.com)
19  */

20 public class XmlLoggerWithStatus extends XmlLogger {
21
22     private Writer JavaDoc out;
23     private boolean inited;
24     private String JavaDoc targetFilter;
25
26     private static final SimpleDateFormat JavaDoc FORMATTER = new SimpleDateFormat JavaDoc("HH:mm:ss");
27
28     /**
29      * Reads current content of file defined by system property
30      * XmlLoggerWithStatus.file and creates a writer to it.
31      *
32      * NOTE: this code MAY NOT be placed into buildStarted event because
33      * properties are not available at that point yet.
34      */

35     public void init(BuildEvent event) {
36         if (!inited) {
37             String JavaDoc statusFileName = event.getProject().getProperty("XmlLoggerWithStatus.file");
38             if (statusFileName == null) {
39                 statusFileName = "buildstatus.txt";
40             }
41             this.targetFilter = event.getProject().getProperty("XmlLoggerWithStatus.filter");
42
43             //check directory
44
File JavaDoc parentDir = new File JavaDoc(statusFileName).getParentFile();
45             if (parentDir != null && !parentDir.exists()) {
46                 parentDir.mkdirs();
47             }
48
49             // create a writer
50
try {
51                 out = new FileWriter JavaDoc(statusFileName, true);
52             } catch (Exception JavaDoc e) {
53                 System.err.println("Error opening file " + statusFileName + " for appending");
54                 e.printStackTrace(System.err);
55             }
56             inited = true;
57         }
58     }
59
60     /**
61      * Closes writer to build status.
62      */

63     public void buildFinished(BuildEvent event) {
64         super.buildFinished(event);
65         if (out != null) {
66             try {
67                 out.close();
68             } catch (IOException JavaDoc ignored) {
69             }
70         }
71     }
72
73     /**
74      * Adds a line with started target and timestamp.
75      */

76     public void targetStarted(BuildEvent event) {
77         super.targetStarted(event);
78         init(event);
79         if (out != null) {
80             String JavaDoc name = event.getTarget().getName();
81             if (this.targetFilter != null && name.matches(this.targetFilter)){
82                 return;
83             }
84             StringBuffer JavaDoc content = new StringBuffer JavaDoc();
85             content.append(System.getProperty("line.separator"));
86             content.append(FORMATTER.format(new Date JavaDoc()));
87             content.append(" [");
88             content.append(name);
89             content.append("] ");
90             writeStatus(content);
91         }
92     }
93
94     /**
95      * Writes a line and flushes.
96      */

97     private void writeStatus(StringBuffer JavaDoc content) {
98         try {
99             out.write(content.toString());
100             out.flush();
101         } catch (IOException JavaDoc e) {
102             System.err.println("Error writing statusline to writer");
103             e.printStackTrace(System.err);
104         }
105     }
106
107 }
108
Popular Tags