KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > idaremedia > antx > capture > CopyLoggedTask


1 /**
2  * $Id: CopyLoggedTask.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2002-2005 iDare Media, Inc. All rights reserved.
4  *
5  * Originally written by iDare Media, Inc. for release into the public domain. This
6  * library, source form and binary form, is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation; either version 2.1 of the License, or (at your option) any
9  * later version.<p>
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU LGPL (GNU Lesser General Public License) for more details.<p>
14  *
15  * You should have received a copy of the GNU Lesser General Public License along with this
16  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite
17  * 330, Boston, MA 02111-1307 USA. The LGPL can be found online at
18  * http://www.fsf.org/copyleft/lesser.html<p>
19  *
20  * This product has been influenced by several projects within the open-source community.
21  * The JWare developers wish to acknowledge the open-source community's support. For more
22  * information regarding the open-source products used within JWare, please visit the
23  * JWare website.
24  *----------------------------------------------------------------------------------------*
25  * WEBSITE- http://www.jware.info EMAIL- inquiries@jware.info
26  *----------------------------------------------------------------------------------------*
27  **/

28
29 package com.idaremedia.antx.capture;
30
31 import java.io.BufferedOutputStream JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.FileOutputStream JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.io.OutputStream JavaDoc;
36
37 import org.apache.tools.ant.BuildException;
38 import org.apache.tools.ant.Project;
39
40 import com.idaremedia.antx.AntX;
41 import com.idaremedia.antx.parameters.FlexInformationSaver;
42 import com.idaremedia.antx.parameters.TransformHelper;
43
44 /**
45  * Helper task that transfers captured logs to a file, project property, or project
46  * reference. Copy-to destinations can be specified in a single &lt;copylogged&gt;
47  * declaration. The source of copied information is either the ant logs (default) or
48  * the system output streams (see "{@linkplain LogsUsingTask#setFrom from}" attribute).
49  * <p>
50  * <b>Example Usage:</b><pre>
51  * &lt;capturelogs importantfrom="warning"&gt;
52  * &lt;show message="Whoa"/&gt;
53  * &lt;show message="Whoa" level="error"/&gt;
54  * &lt;copylogged toproperty="one.whoa"/&gt;
55  * &lt;assert equals="Whoa" property="one.whoa"/&gt;
56  * &lt;copylogged toproperty="two.whoa" important="no"/&gt;
57  * &lt;assert startswith="WhoaWhoa" property="two.whoa"/&gt;
58  * &lt;/capturelogs&gt;
59  *
60  * &lt;capturelogs importantfrom="info"&gt;
61  * &lt;javadoc &#46;&#46;&#46;/&gt;
62  * &lt;copylogged tofile="${scratch.root}/javadoc.out"/&gt;
63  * &lt;/capturelogs&gt;
64  *
65  * &lt;captureoutput&gt;
66  * &lt;<i>Your Tasks Here</i>&#46;&#46;&#46;/&gt;
67  * &lt;copylogged <b>from="stdio"</b> toreference="tasks.stdio.out"/&gt;
68  * &lt;/captureoutput&gt;
69  *</pre>
70  *
71  * @since JWare/AntX 0.3
72  * @author ssmc, &copy;2002-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
73  * @version 0.5
74  * @.safety single
75  * @.group impl,helper
76  * @see CaptureLogsTask
77  **/

78
79 public final class CopyLoggedTask extends LogsUsingTask
80     implements FlexInformationSaver
81 {
82     /**
83      * Initializes a new CopyLoggedTask instance.
84      **/

85     public CopyLoggedTask()
86     {
87         super(AntX.capture+"CopyLoggedTask:");
88     }
89
90
91     /**
92      * Initializes a CV-labeled CopyLoggedTask instance.
93      * @param iam CV-label (non-null)
94      **/

95     public CopyLoggedTask(String JavaDoc iam)
96     {
97         super(iam);
98     }
99
100 // ---------------------------------------------------------------------------------------
101
// Parameters:
102
// ---------------------------------------------------------------------------------------
103

104     /**
105      * Sets the target file for the copied logs. If the given
106      * path is relative, the output file will be determined
107      * relative to this task's project's base directory.
108      * @param fileRef the output file's path or file URL (non-null)
109      **/

110     public void setToFile(String JavaDoc fileRef)
111     {
112         require_(fileRef!=null,"setToFil- nonzro fileref");
113         fileRef = TransformHelper.toPath(fileRef,getProject());
114         m_toFile= new File JavaDoc(fileRef);
115
116     }
117
118
119     /**
120      * Sets the target file for the copied logs. File reference
121      * is used as-is.
122      * @param outputFile the output file (non-null)
123      * @since JWare/AntX 0.4
124      **/

125     public final void setToProjectFile(File JavaDoc outputFile)
126     {
127         require_(outputFile!=null,"setToProjFil- nonzro file");
128         m_toFile= outputFile;
129     }
130
131
132     /**
133      * Returns this task's target file for output. Returns
134      * <i>null</i> if never set.
135      * @since JWare/AntX 0.4
136      **/

137     public final String JavaDoc getToFilePath()
138     {
139         File JavaDoc f = getToFile();
140         return f!=null ? f.getPath() : null;
141     }
142
143
144     /**
145      * Returns this target file for output. Returns <i>null</i>
146      * if never set.
147      **/

148     public File JavaDoc getToFile()
149     {
150         return m_toFile;
151     }
152
153
154     /**
155      * Marks whether this task should try to append its information
156      * to its output stream.
157      * @param append <i>true</i> if should try to append
158      **/

159     public void setAppend(boolean append)
160     {
161         m_tryAppend= append;
162     }
163
164
165     /**
166      * Returns <i>true</i> if this task will try to append any new
167      * information to an existing output sink. By default is
168      * <i>false</i>.
169      **/

170     public final boolean willTryAppend()
171     {
172         return m_tryAppend;
173     }
174
175
176     /**
177      * Sets the property to which logs copied.
178      * @param property the property to create (non-null)
179      **/

180     public void setToProperty(String JavaDoc property)
181     {
182         require_(property!=null,"setPropty- nonzro nam");
183         m_updateProperty = property;
184     }
185
186
187     /**
188      * Returns the name of the property this task will set with
189      * the captured log information. Returns <i>null</i> if
190      * never set.
191      * @see #setToProperty setToProperty
192      **/

193     public String JavaDoc getToProperty()
194     {
195         return m_updateProperty;
196     }
197
198
199     /**
200      * Sets the reference to which logs copied.
201      * @param name the name of the (new) reference to update (non-null)
202      **/

203     public void setToReference(String JavaDoc name)
204     {
205         require_(name!=null,"setRef- nonzro nam");
206         m_updateReference = name;
207     }
208
209
210     /**
211      * Returns the name of the reference this task will update with
212      * the captured log information. Returns <i>null</i> if
213      * never set.
214      * @see #setToReference setToReference
215      **/

216     public String JavaDoc getToReference()
217     {
218         return m_updateReference;
219     }
220
221
222 // ---------------------------------------------------------------------------------------
223
// Execution:
224
// ---------------------------------------------------------------------------------------
225

226     /**
227      * Returns an output stream base on this task's <i>file</i>
228      * and <i>append</i> parameters. Will return <i>null</i> if the
229      * file parameter never set. The returned stream is the raw
230      * file output stream; it is <em>not</em> a buffered stream.
231      **/

232     protected OutputStream JavaDoc getOutputStream()
233     {
234         File JavaDoc f = getToFile();
235         if (f!=null) {
236             try {
237                 return new FileOutputStream JavaDoc(f.getPath(), willTryAppend());
238             } catch(IOException JavaDoc ioX) {
239                 String JavaDoc ermsg = uistrs().get("task.echo.unable.use.file",
240                                             f.getPath(), ioX.getMessage());
241                 log(ermsg,Project.MSG_ERR);
242                 throw new BuildException(ermsg,getLocation());
243             }
244         }
245         return null;
246     }
247
248
249
250     /**
251      * Tries to copy this nearest task's enclosing logs recorder current
252      * contents to a file and/or project property.
253      * @throws BuildException if neither a file nor a property specified
254      * or unable to write information to file
255      **/

256     public void execute()
257     {
258         verifyCanExecute_("execute");
259
260         String JavaDoc info = getVUTLog();
261
262         if (getToProperty()!=null) {
263             checkIfProperty_(getToProperty(), true);
264             getProject().setNewProperty(getToProperty(),info);
265         }
266
267         if (getToReference()!=null) {
268             checkIfReference_(getToReference(),true);
269             getProject().addReference(getToReference(),info);
270         }
271
272         OutputStream JavaDoc fos = getOutputStream();
273         if (fos!=null) {
274             try {
275                 BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(fos, 512);
276                 byte[] raw = info.getBytes();
277                 bos.write(raw,0,raw.length);
278                 bos.flush();
279                 raw = null;
280             } catch(IOException JavaDoc ioX) {
281                 String JavaDoc ermsg = uistrs().get("task.echo.unable.use.file",
282                                             getToFile().getPath(),
283                                             ioX.getMessage());
284                 log(ermsg,Project.MSG_ERR);
285                 throw new BuildException(ermsg,getLocation());
286             } finally {
287                 try { fos.close(); } catch(IOException JavaDoc igx) {/*burp*/}
288             }
289         }
290
291         if (willReset()) {
292             LogsRecorder r = getRecorder(true);
293             if (r!=null) {
294                 r.clearLogs();
295             }
296         }
297     }
298
299
300     /**
301      * Verify this task is enclosed by at least one {@linkplain LogsRecorder
302      * logs recording} taskset and has a fully defined condition.
303      * @throws BuildException if neither a value or a sequence of values defined
304      **/

305     protected void verifyCanExecute_(String JavaDoc calr)
306     {
307         super.verifyCanExecute_(calr);
308
309         String JavaDoc pn = getToProperty();
310
311         if (getToFile()==null && pn==null) {
312             String JavaDoc ermsg = getAntXMsg("task.needs.this.attr",getTaskName(),
313                                       "tofile|toproperty");
314             log(ermsg,Project.MSG_ERR);
315             throw new BuildException(ermsg,getLocation());
316         }
317         if (pn!=null) {
318             checkIfProperty_(pn,true);
319         }
320     }
321
322     private String JavaDoc m_updateProperty, m_updateReference;
323     private File JavaDoc m_toFile;
324     private boolean m_tryAppend;//NB:false=>overwrite existin'
325
}
326
327 /* end-of-CopyLoggedTask.java */
328
Popular Tags