KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Hashtable JavaDoc;
21 import org.apache.tools.ant.BuildEvent;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.SubBuildListener;
25 import org.apache.tools.ant.Task;
26 import org.apache.tools.ant.types.EnumeratedAttribute;
27 import org.apache.tools.ant.types.LogLevel;
28
29 /**
30  * Adds a listener to the current build process that records the
31  * output to a file.
32  * <p>Several recorders can exist at the same time. Each recorder is
33  * associated with a file. The filename is used as a unique identifier for
34  * the recorders. The first call to the recorder task with an unused filename
35  * will create a recorder (using the parameters provided) and add it to the
36  * listeners of the build. All subsequent calls to the recorder task using
37  * this filename will modify that recorders state (recording or not) or other
38  * properties (like logging level).</p>
39  * <p>Some technical issues: the file's print stream is flushed for &quot;finished&quot;
40  * events (buildFinished, targetFinished and taskFinished), and is closed on
41  * a buildFinished event.</p>
42  * @see RecorderEntry
43  * @version 0.5
44  * @since Ant 1.4
45  * @ant.task name="record" category="utility"
46  */

47 public class Recorder extends Task implements SubBuildListener {
48
49     //////////////////////////////////////////////////////////////////////
50
// ATTRIBUTES
51

52     /** The name of the file to record to. */
53     private String JavaDoc filename = null;
54     /**
55      * Whether or not to append. Need Boolean to record an unset state (null).
56      */

57     private Boolean JavaDoc append = null;
58     /**
59      * Whether to start or stop recording. Need Boolean to record an unset
60      * state (null).
61      */

62     private Boolean JavaDoc start = null;
63     /** The level to log at. A level of -1 means not initialized yet. */
64     private int loglevel = -1;
65     /** Strip task banners if true. */
66     private boolean emacsMode = false;
67     /** The list of recorder entries. */
68     private static Hashtable JavaDoc recorderEntries = new Hashtable JavaDoc();
69
70     //////////////////////////////////////////////////////////////////////
71
// CONSTRUCTORS / INITIALIZERS
72

73     /**
74      * Overridden so we can add the task as build listener.
75      *
76      * @since Ant 1.7
77      */

78     public void init() {
79         getProject().addBuildListener(this);
80     }
81
82     //////////////////////////////////////////////////////////////////////
83
// ACCESSOR METHODS
84

85     /**
86      * Sets the name of the file to log to, and the name of the recorder
87      * entry.
88      *
89      * @param fname File name of logfile.
90      */

91     public void setName(String JavaDoc fname) {
92         filename = fname;
93     }
94
95
96     /**
97      * Sets the action for the associated recorder entry.
98      *
99      * @param action The action for the entry to take: start or stop.
100      */

101     public void setAction(ActionChoices action) {
102         if (action.getValue().equalsIgnoreCase("start")) {
103             start = Boolean.TRUE;
104         } else {
105             start = Boolean.FALSE;
106         }
107     }
108
109
110     /**
111      * Whether or not the logger should append to a previous file.
112      * @param append if true, append to a previous file.
113      */

114     public void setAppend(boolean append) {
115         this.append = (append ? Boolean.TRUE : Boolean.FALSE);
116     }
117
118
119     /**
120      * Set emacs mode.
121      * @param emacsMode if true use emacs mode
122      */

123     public void setEmacsMode(boolean emacsMode) {
124         this.emacsMode = emacsMode;
125     }
126
127
128     /**
129      * Sets the level to which this recorder entry should log to.
130      * @param level the level to set.
131      * @see VerbosityLevelChoices
132      */

133     public void setLoglevel(VerbosityLevelChoices level) {
134         loglevel = level.getLevel();
135     }
136
137     //////////////////////////////////////////////////////////////////////
138
// CORE / MAIN BODY
139

140     /**
141      * The main execution.
142      * @throws BuildException on error
143      */

144     public void execute() throws BuildException {
145         if (filename == null) {
146             throw new BuildException("No filename specified");
147         }
148
149         getProject().log("setting a recorder for name " + filename,
150             Project.MSG_DEBUG);
151
152         // get the recorder entry
153
RecorderEntry recorder = getRecorder(filename, getProject());
154         // set the values on the recorder
155
recorder.setMessageOutputLevel(loglevel);
156         recorder.setEmacsMode(emacsMode);
157         if (start != null) {
158             if (start.booleanValue()) {
159                 recorder.reopenFile();
160                 recorder.setRecordState(start);
161             } else {
162                 recorder.setRecordState(start);
163                 recorder.closeFile();
164             }
165         }
166     }
167
168     //////////////////////////////////////////////////////////////////////
169
// INNER CLASSES
170

171     /**
172      * A list of possible values for the <code>setAction()</code> method.
173      * Possible values include: start and stop.
174      */

175     public static class ActionChoices extends EnumeratedAttribute {
176         private static final String JavaDoc[] VALUES = {"start", "stop"};
177
178         /**
179          * @see EnumeratedAttribute#getValues()
180          */

181         /** {@inheritDoc}. */
182         public String JavaDoc[] getValues() {
183             return VALUES;
184         }
185     }
186
187
188     /**
189      * A list of possible values for the <code>setLoglevel()</code> method.
190      * Possible values include: error, warn, info, verbose, debug.
191      */

192     public static class VerbosityLevelChoices extends LogLevel {
193     }
194
195
196     /**
197      * Gets the recorder that's associated with the passed in name. If the
198      * recorder doesn't exist, then a new one is created.
199      * @param name the name of the recoder
200      * @param proj the current project
201      * @return a recorder
202      * @throws BuildException on error
203      */

204     protected RecorderEntry getRecorder(String JavaDoc name, Project proj)
205          throws BuildException {
206         Object JavaDoc o = recorderEntries.get(name);
207         RecorderEntry entry;
208
209         if (o == null) {
210             // create a recorder entry
211
entry = new RecorderEntry(name);
212
213             if (append == null) {
214                 entry.openFile(false);
215             } else {
216                 entry.openFile(append.booleanValue());
217             }
218             entry.setProject(proj);
219             recorderEntries.put(name, entry);
220         } else {
221             entry = (RecorderEntry) o;
222         }
223         return entry;
224     }
225
226     /**
227      * Empty implementation required by SubBuildListener interface.
228      * @param event ignored.
229      * @since Ant 1.7
230      */

231     public void buildStarted(BuildEvent event) {
232     }
233
234     /**
235      * Empty implementation required by SubBuildListener interface.
236      * @param event ignored.
237      * @since Ant 1.7
238      */

239     public void subBuildStarted(BuildEvent event) {
240     }
241
242     /**
243      * Empty implementation required by SubBuildListener interface.
244      * @param event ignored.
245      * @since Ant 1.7
246      */

247     public void targetStarted(BuildEvent event) {
248     }
249
250     /**
251      * Empty implementation required by SubBuildListener interface.
252      * @param event ignored.
253      * @since Ant 1.7
254      */

255     public void targetFinished(BuildEvent event) {
256     }
257
258     /**
259      * Empty implementation required by SubBuildListener interface.
260      * @param event ignored.
261      * @since Ant 1.7
262      */

263     public void taskStarted(BuildEvent event) {
264     }
265
266     /**
267      * Empty implementation required by SubBuildListener interface.
268      * @param event ignored.
269      * @since Ant 1.7
270      */

271     public void taskFinished(BuildEvent event) {
272     }
273
274     /**
275      * Empty implementation required by SubBuildListener interface.
276      * @param event ignored.
277      * @since Ant 1.7
278      */

279     public void messageLogged(BuildEvent event) {
280     }
281
282     /**
283      * Cleans recorder registry.
284      * @param event ignored.
285      * @since Ant 1.7
286      */

287     public void buildFinished(BuildEvent event) {
288         cleanup();
289     }
290
291     /**
292      * Cleans recorder registry, if this is the subbuild the task has
293      * been created in.
294      * @param event ignored.
295      * @since Ant 1.7
296      */

297     public void subBuildFinished(BuildEvent event) {
298         if (event.getProject() == getProject()) {
299             cleanup();
300         }
301     }
302
303     /**
304      * cleans recorder registry and removes itself from BuildListener list.
305      *
306      * @since Ant 1.7
307      */

308     private void cleanup() {
309         recorderEntries.clear();
310         getProject().removeBuildListener(this);
311     }
312 }
313
314
Popular Tags