KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * $Id: InterpretLoggedTask.java 180 2007-03-15 12:56:38Z ssmc $
3  * Copyright 2004-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 (LGPL) as published
8  * by the Free Software Foundation; either version 2.1 of the License, or (at your option)
9  * any 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 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 GNU 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.File JavaDoc;
32 import java.io.FileReader JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.Reader JavaDoc;
35 import java.io.StringReader JavaDoc;
36 import java.util.Properties JavaDoc;
37
38 import org.apache.tools.ant.AntTypeDefinition;
39 import org.apache.tools.ant.BuildException;
40 import org.apache.tools.ant.ComponentHelper;
41 import org.apache.tools.ant.Project;
42
43 import com.idaremedia.antx.AntX;
44 import com.idaremedia.antx.apis.Requester;
45 import com.idaremedia.antx.ownhelpers.ConditionalInnerNameValuePair;
46 import com.idaremedia.antx.ownhelpers.ConditionalParameters;
47 import com.idaremedia.antx.parameters.IsA;
48 import com.idaremedia.antx.parameters.TransformHelper;
49
50 /**
51  * Helper task that evaluates a captured log for some pass/fail criteria and sets a set
52  * of project properties in response.
53  * <p>
54  * <b>Example Usage:</b><pre>
55  * &lt;typedef name="javadoclog-checker"
56  * classname="com.idaremedia.antx.capture.JavadocLogInterpreter"/&gt;
57  * &#8230;
58  * &lt;capturelogs importantfrom="warning"&gt;
59  * &lt;javadoc.../&gt;
60  * &lt;<b>evaluatelogged</b> prefix="_javadoc."
61  * interpreter="javadoclog-checker"/&gt;
62  * &lt;/capturelogs&gt;
63  * &lt;domatch property="_javadoc.result"&gt;
64  * &lt;equals value="clean"&gt;
65  * ...
66  * &lt;otherwise&gt;
67  * ...
68  * &lt;/domatch&gt;
69  * </pre>
70  *
71  * @since JWare/AntX 0.5
72  * @author ssmc, &copy;2004-2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
73  * @version 0.5
74  * @.safety single
75  * @.group api,helper
76  **/

77
78 public final class InterpretLoggedTask extends LogsUsingTask
79 {
80     /**
81      * Initializes a new log interpretation task.
82      **/

83     public InterpretLoggedTask()
84     {
85         super(AntX.capture+"InterpretLoggedTask:");
86     }
87
88
89
90     /**
91      * Initializes this interpretation task's target log file.
92      * Either a file URL or an actual file path can be defined. This log
93      * file is used <em>instead of</em> any enclosing log recorder's
94      * captured information.
95      * @param fileref location of file (non-null)
96      * @.impl Used for testing task.
97      **/

98     public void setLogFile(String JavaDoc fileref)
99     {
100         require_(fileref!=null,"setLogFile- nonzro fileref");
101         m_fileRef = fileref;
102         m_logFile = null;
103     }
104
105
106
107     /**
108      * Sets the prefix of the property to set with the final conclusion. The
109      * actually name of the property set will be
110      * "<span class="src">&lt;prefix&gt;&#46;result</span>" and the value
111      * will be one of: "clean", "problem", or "failure". If anything other than
112      * a clean log is detected, this task will also set two count properties
113      * "<span class="src">&lt;prefix&gt;&#46;errorcount</span>" and
114      * "<span class="src">&lt;prefix&gt;&#46;warningcount</span>". If a prefix
115      * is never set explicitly the value "<span class="src">_logcheck.</span>"
116      * will be used.
117      * @param propertyPrefix property name prefix (non-null)
118      **/

119     public void setPrefix(String JavaDoc propertyPrefix)
120     {
121         require_(propertyPrefix!=null && propertyPrefix.length()>0,
122                  "setPrefix- nonzro property prefix");
123         if (propertyPrefix.endsWith(".")) {
124             m_propertiesPrefix = propertyPrefix;
125         } else {
126             m_propertiesPrefix = propertyPrefix+".";
127         }
128     }
129
130
131
132     /**
133      * Returns the name of the property this task will update with its
134      * conclusion. Never returns <i>null</i> or the empty string.
135      **/

136     public final String JavaDoc getUpdateProperty()
137     {
138         return m_propertiesPrefix+"result";
139     }
140
141
142
143     /**
144      * Returns the name of the property this task will update with the
145      * number of errors. Never returns <i>null</i> or the empty
146      * string.
147      **/

148     public final String JavaDoc getErrorCountProperty()
149     {
150         return m_propertiesPrefix+"errorcount";
151     }
152
153
154
155     /**
156      * Returns the name of the property this task will update with the
157      * number of warnings. Never returns <i>null</i> or the empty
158      * string.
159      **/

160     public final String JavaDoc getWarningCountProperty()
161     {
162         return m_propertiesPrefix+"warningcount";
163     }
164
165
166
167     /**
168      * Tells this task the type of the result set elements. Will default
169      * to regular (write-once) project properties unless set to something
170      * else.
171      * @param resulttype type of results (variable|property|reference)
172      **/

173     public void setResultType(String JavaDoc resulttype)
174     {
175         require_(resulttype!=null,"setResultType- nonzro typename");
176         IsA isa = IsA.from(resulttype);
177         if (isa==null) {
178             String JavaDoc err = getAntXMsg("task.illegal.param.value",getTaskName(),
179                             resulttype, "resulttype");
180             log(err, Project.MSG_ERR);
181             throw new BuildException(err,getLocation());
182         }
183         m_resulttype = isa;
184     }
185
186
187
188     /**
189      * Returns the preferred type of the result this task's interpreter
190      * sets. Never returns <i>null</i>. Defaults IsA.PROPERTY.
191      **/

192     public final IsA getResultType()
193     {
194         return m_resulttype;
195     }
196
197
198
199     /**
200      * Tells this task the maximum number of errors to tolerate before
201      * a failure is recorded. Note that <em>any</em> errors will force
202      * this task to conclude there was at least a <i>problem</i>; the
203      * max threshold indicates when a problem turns into a failure.
204      * @param maxErrors the maximum number of errors to tolerate (anything
205      * over is a failure).
206      **/

207     public void setMaxErrors(int maxErrors)
208     {
209         require_(maxErrors>=0,"setMaxErrors- zero or more value");
210         m_maxErrors = maxErrors;
211     }
212
213
214
215     /**
216      * Returns the maximum number of errors this task will tolerate before
217      * recording a failure. This value is zero (0) unless defined otherwise.
218      **/

219     public final int getMaxErrors()
220     {
221         return m_maxErrors;
222     }
223
224
225
226     /**
227      * Tells this task the maximum number of warnings to tolerate before
228      * a failure is recorded. Note that <em>any</em> warnings will force
229      * this task to conclude there was at least a <i>problem</i>; the
230      * max threshold indicates when a problem turns into a failure.
231      * @param maxWarnings the maximum number of errors to tolerate (anything
232      * over is a failure).
233      **/

234     public void setMaxWarnings(int maxWarnings)
235     {
236         require_(maxWarnings>=0,"setMaxWarnings- zero or more value");
237         m_maxWarnings = maxWarnings;
238     }
239
240
241
242     /**
243      * Returns the maximum number of warnings this task will tolerate before
244      * recording a failure. This value is ten (10) unless defined otherwise.
245      **/

246     public final int getMaxWarnings()
247     {
248         return m_maxWarnings;
249     }
250
251
252
253     /**
254      * Tells this task the name of the actual log interpreter algorithm to
255      * use. The incoming string is either a typedef'ed application interpreter
256      * or the fully qualified class name of the interpreter.
257      * @param typename name of the interpreter as declared with &lt;typedef&gt;
258      **/

259     public void setInterpreter(String JavaDoc typename)
260     {
261         require_(typename!=null,"setInterprter- nonzro typename");
262
263         ComponentHelper ch = ComponentHelper.getComponentHelper(getProject());
264         AntTypeDefinition atd = ch.getDefinition(typename);
265         if (atd!=null) {
266             Object JavaDoc impl = atd.create(getProject());
267             if (impl instanceof LogInterpreter) {
268                 m_logInterpreter = (LogInterpreter)impl;
269             }
270         }
271         if (m_logInterpreter==null) {
272             try {
273                 Class JavaDoc c = Class.forName(typename);
274                 if (LogInterpreter.class.isAssignableFrom(c)) {
275                     m_logInterpreter = (LogInterpreter)c.newInstance();
276                 }
277             } catch(Exception JavaDoc anyX) {
278                 log(anyX.getMessage(),Project.MSG_ERR);
279             }
280         }
281         if (m_logInterpreter==null) {
282             String JavaDoc err = getAntXMsg("task.bad.custimpl.class1",typename,
283                 LogInterpreter.class.getName());
284             log(err, Project.MSG_ERR);
285             throw new BuildException(err, getLocation());
286         }
287     }
288
289
290
291     /**
292      * Adds a single interpreter parameter. The new parameter
293      * is merged with any existing parameters.
294      * @param arg the parameter information (non-null)
295      **/

296     public void addConfiguredParameter(ConditionalInnerNameValuePair arg)
297     {
298         if (m_interpreterArgs==null) {
299             m_interpreterArgs = new ConditionalParameters(getProject());
300         }
301         m_interpreterArgs.addConfiguredParameter(arg);
302     }
303
304
305
306     /**
307      * Ensure both a non-existing result property and a log interpreter have
308      * been specified for this task.
309      * @throws BuildException if incomplete task configuration.
310      **/

311     protected void verifyCanExecute_(String JavaDoc calr)
312     {
313         super.verifyCanExecute_(calr);
314
315         if (m_logInterpreter==null) {
316             String JavaDoc err = getAntXMsg("task.needs.this.attr",getTaskName(),
317                 "interpreter");
318             log(err, Project.MSG_ERR);
319             throw new BuildException(err, getLocation());
320         }
321
322         switch(getResultType().getIndex()) {
323             case IsA.PROPERTY_INDEX: {
324                 checkIfProperty_(getUpdateProperty(),false);
325                 break;
326             }
327             case IsA.REFERENCE_INDEX: {
328                 checkIfReference_(getUpdateProperty(),true);
329                 break;
330             }
331         }
332
333         if (m_fileRef!=null) {
334             m_logFile = new File JavaDoc(TransformHelper.toPath(m_fileRef,getProject()));
335         }
336     }
337
338
339
340     /**
341      * Returns instructions that should be passed to interpreter. Never
342      * returns <i>null</i>.
343      **/

344     private Properties JavaDoc getInstructionsNoNull()
345     {
346         Properties JavaDoc ini;
347         if (m_interpreterArgs==null) {
348             ini = new Properties JavaDoc();
349         } else {
350             ini = m_interpreterArgs.copyOfSimpleKeyValues(getProject(),true);
351         }
352         return ini;
353     }
354
355
356
357     /**
358      * Evaluates either a named log file or the information captured by
359      * the nearest logs recorder and updates one or more result
360      * project properties.
361      * @throws BuildException if task improperly defined, unable to read log file,
362      * or interpreter signals error.
363      */

364     public void execute()
365     {
366         verifyCanExecute_("exec");
367
368         Reader JavaDoc inputr = null;
369         
370         if (m_logFile!=null) {
371             if (!m_logFile.exists()) {
372                 getProject().setNewProperty(getUpdateProperty(),LogInterpreter.CLEAN);
373                 return;
374             }
375             try {
376                 inputr = new FileReader JavaDoc(m_logFile);
377             } catch(IOException JavaDoc ioX) {
378                 throw new BuildException(ioX);
379             }
380         } else {
381             inputr = new StringReader JavaDoc(getVUTLog());
382         }
383
384         m_logInterpreter.interpret(inputr, new ConfigWrap(this,getInstructionsNoNull()));
385     }
386
387
388
389     /**
390      * Returns <i>true</i> always. We can accept log files as well as
391      * log recorders for our input data.
392      */

393     protected boolean isSourceFlexible()
394     {
395         return true;
396     }
397
398
399     private String JavaDoc m_fileRef;
400     private File JavaDoc m_logFile;
401     private String JavaDoc m_propertiesPrefix = "_logcheck.";
402     private int m_maxErrors = 0;
403     private int m_maxWarnings = 10;
404     private LogInterpreter m_logInterpreter;
405     private ConditionalParameters m_interpreterArgs;
406     private IsA m_resulttype= IsA.PROPERTY;
407 }
408
409
410
411
412 /**
413  * Adapter of InterpretParameters to InterpretLoggedTask's interface.
414  *
415  * @since JWare/AntX 0.5
416  * @author ssmc, &copy;2005 <a HREF="http://www.jware.info">iDare&nbsp;Media,&nbsp;Inc.</a>
417  * @version 0.5
418  * @.safety single
419  * @.group impl,helper
420  **/

421 class ConfigWrap extends Requester.ForComponent implements InterpretParameters {
422     private final InterpretLoggedTask m_task;
423     private final Properties JavaDoc m_argv;
424     ConfigWrap(InterpretLoggedTask task, Properties JavaDoc argv) {
425         super(task);
426         m_task = task;
427         m_argv = argv;
428     }
429     public String JavaDoc getErrorCountProperty() {
430         return m_task.getErrorCountProperty();
431     }
432     public int getMaxErrors() {
433         return m_task.getMaxErrors();
434     }
435     public int getMaxWarnings() {
436         return m_task.getMaxWarnings();
437     }
438     public String JavaDoc getUpdateProperty() {
439         return m_task.getUpdateProperty();
440     }
441     public String JavaDoc getWarningCountProperty() {
442         return m_task.getWarningCountProperty();
443     }
444     public boolean updateProperties() {
445         return true;
446     }
447     public IsA getResultType() {
448         return m_task.getResultType();
449     }
450     public Properties JavaDoc getInstructions() {
451         return m_argv;
452     }
453 }
454
455 /* end-of-InterpretLoggedTask.java */
Popular Tags