KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > vladium > emma > emmajavaTask


1 /* Copyright (C) 2003 Vladimir Roubtsov. All rights reserved.
2  *
3  * This program and the accompanying materials are made available under
4  * the terms of the Common Public License v1.0 which accompanies this distribution,
5  * and is available at http://www.eclipse.org/legal/cpl-v10.html
6  *
7  * $Id: emmajavaTask.java,v 1.1.1.1.2.2 2004/07/16 23:32:04 vlad_r Exp $
8  */

9 package com.vladium.emma;
10
11 import java.io.File JavaDoc;
12
13 import com.vladium.util.IProperties;
14 import com.vladium.util.Strings;
15 import com.vladium.emma.ant.*;
16 import com.vladium.emma.instr.FilterCfg;
17 import com.vladium.emma.instr.FilterCfg.filterElement;
18 import com.vladium.emma.report.ReportCfg;
19 import com.vladium.emma.report.IReportEnums.DepthAttribute;
20 import com.vladium.emma.report.IReportEnums.UnitsTypeAttribute;
21 import com.vladium.emma.report.ReportCfg.Element_HTML;
22 import com.vladium.emma.report.ReportCfg.Element_TXT;
23 import com.vladium.emma.report.ReportCfg.Element_XML;
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Project;
27 import org.apache.tools.ant.taskdefs.Java;
28 import org.apache.tools.ant.types.Commandline;
29 import org.apache.tools.ant.types.Path;
30 import org.apache.tools.ant.types.Reference;
31
32 // ----------------------------------------------------------------------------
33
/**
34  * @author Vlad Roubtsov, (C) 2003
35  */

36 public
37 class emmajavaTask extends Java
38 {
39     // public: ................................................................
40

41     
42     public void init () throws BuildException
43     {
44         super.init ();
45
46         m_verbosityCfg = new VerbosityCfg ();
47         m_genericCfg = new GenericCfg (this);
48         m_filterCfg = new FilterCfg (this);
49         m_reportCfg = new ReportCfg (project, this);
50         setEnabled (true);
51     }
52
53     
54     public void execute () throws BuildException
55     {
56         log (IAppConstants.APP_VERBOSE_BUILD_ID, Project.MSG_VERBOSE);
57         
58         if (getClasspath () == null)
59             throw (BuildException) SuppressableTask.newBuildException (getTaskName ()
60                 + ": this task requires 'classpath' attribute to be set", location).fillInStackTrace ();
61             
62
63         if (isEnabled ())
64         {
65             // fork:
66
if (m_forkUserOverride && ! m_fork)
67                 log (getTaskName () + ": 'fork=\"false\"' attribute setting ignored (this task always forks)", Project.MSG_WARN);
68             
69             super.setFork (true); // always fork
70

71             // add emma libs to the parent task's classpath [to support non-extdir deployment]:
72
final Path libClasspath = m_libClasspath;
73             if ((libClasspath != null) && (libClasspath.size () > 0))
74             {
75                 super.createClasspath ().append (libClasspath);
76             }
77             
78             // classname|jar (1/2):
79
super.setClassname ("emmarun");
80             
81             // <emmajava> extensions:
82
{
83                 // report types:
84
{
85                     String JavaDoc reportTypes = Strings.toListForm (m_reportCfg.getReportTypes (), ',');
86                     if ((reportTypes == null) || (reportTypes.length () == 0)) reportTypes = "txt";
87                     
88                     super.createArg ().setValue ("-r");
89                     super.createArg ().setValue (reportTypes);
90                 }
91                 
92                 // full classpath scan flag:
93
{
94                     if (m_scanCoveragePath)
95                     {
96                         super.createArg ().setValue ("-f");
97                     }
98                 }
99                 
100                 // dump raw data flag and options:
101
{
102                     if (m_dumpSessionData)
103                     {
104                         super.createArg ().setValue ("-raw");
105                         
106                         if (m_outFile != null)
107                         {
108                             super.createArg ().setValue ("-out");
109                             super.createArg ().setValue (m_outFile.getAbsolutePath ());
110                         }
111                         
112                         if (m_outFileMerge != null)
113                         {
114                             super.createArg ().setValue ("-merge");
115                             super.createArg ().setValue (m_outFileMerge.booleanValue () ? "y" : "n");
116                         }
117                     }
118                     else
119                     {
120                         if (m_outFile != null)
121                             log (getTaskName () + ": output file attribute ignored ('fullmetadata=\"true\"' not specified)", Project.MSG_WARN);
122                         
123                         if (m_outFileMerge != null)
124                             log (getTaskName () + ": merge attribute setting ignored ('fullmetadata=\"true\"' not specified)", Project.MSG_WARN);
125                     }
126                 }
127                 
128                 // instr filter:
129
{
130                     final String JavaDoc [] specs = m_filterCfg.getFilterSpecs ();
131                     if ((specs != null) && (specs.length > 0))
132                     {
133                         super.createArg ().setValue ("-ix");
134                         super.createArg ().setValue (Strings.toListForm (specs, ','));
135                     }
136                 }
137                 
138                 // sourcepath:
139
{
140                     final Path srcpath = m_reportCfg.getSourcepath ();
141                     if (srcpath != null)
142                     {
143                         super.createArg ().setValue ("-sp");
144                         super.createArg ().setValue (Strings.toListForm (srcpath.list (), ','));
145                     }
146                 }
147                 
148                 // all other generic settings:
149
{
150                     final IProperties reportSettings = m_reportCfg.getReportSettings ();
151                     final IProperties genericSettings = m_genericCfg.getGenericSettings ();
152                     
153                     // TODO: another options is to read this file in the forked JVM [use '-props' pass-through]
154
// the best option depends on how ANT resolves relative file names
155
final IProperties fileSettings = m_genericCfg.getFileSettings ();
156                     
157                     // verbosity settings use dedicated attributes and hence are more specific
158
// than anything generic:
159
final IProperties verbositySettings = m_verbosityCfg.getSettings ();
160                     
161                     // (1) file settings have lower priority than any explicitly named overrides
162
// (2) named report settings override generic named settings
163
// (3) verbosity settings use dedicated attributes (not overlapping with report
164
// cfg) and hence are more specific than anything generic
165
final IProperties settings = IProperties.Factory.combine (reportSettings,
166                                                  IProperties.Factory.combine (verbositySettings,
167                                                  IProperties.Factory.combine (genericSettings,
168                                                                               fileSettings)));
169                     
170                     final String JavaDoc [] argForm = settings.toAppArgsForm ("-D");
171                     if (argForm.length > 0)
172                     {
173                         for (int a = 0; a < argForm.length; ++ a)
174                             super.createArg ().setValue (argForm [a]);
175                     }
176                 }
177             }
178             
179             // [assertion: getClasspath() is not null]
180

181             // classpath:
182
super.createArg ().setValue ("-cp");
183             super.createArg ().setPath (getClasspath ());
184                         
185             // classname|jar (2/2):
186
if (getClassname () != null)
187                 super.createArg ().setValue (getClassname ());
188             else if (getJar () != null)
189             {
190                 super.createArg ().setValue ("-jar");
191                 super.createArg ().setValue (getJar ().getAbsolutePath ());
192             }
193             else
194                 throw (BuildException) SuppressableTask.newBuildException (getTaskName ()
195                     + "either 'jar' or 'classname' attribute must be set", location).fillInStackTrace ();
196                 
197             // main class args:
198
if (m_appArgs != null)
199             {
200                 final String JavaDoc [] args = m_appArgs.getArguments ();
201                 for (int a = 0; a < args.length; ++ a)
202                 {
203                     super.createArg ().setValue (args [a]); // note: spaces etc are escaped correctly by ANT libs
204
}
205             }
206         }
207         else
208         {
209             // fork:
210
super.setFork (m_fork);
211             
212             // [assertion: getClasspath() is not null]
213

214             // classpath:
215
super.createClasspath ().append (getClasspath ()); // can't use setClasspath() for obvious reasons
216

217             // classname|jar:
218
if (getClassname () != null)
219                 super.setClassname (getClassname ());
220             else if (getJar () != null)
221                 super.setJar (getJar ());
222             else
223                 throw (BuildException) SuppressableTask.newBuildException (getTaskName ()
224                     + "either 'jar' or 'classname' attribute must be set", location).fillInStackTrace ();
225             
226             // main class args:
227
if (m_appArgs != null)
228             {
229                 final String JavaDoc [] args = m_appArgs.getArguments ();
230                 for (int a = 0; a < args.length; ++ a)
231                 {
232                     super.createArg ().setValue (args [a]); // note: spaces etc are escaped correctly by ANT libs
233
}
234             }
235         }
236         
237         super.execute ();
238     }
239
240     
241     
242     // <java> overrides [ANT 1.4]:
243

244     public void setClassname (final String JavaDoc classname)
245     {
246         if (getJar () != null)
247             throw (BuildException) SuppressableTask.newBuildException (getTaskName ()
248                 + "'jar' and 'classname' attributes cannot be set at the same time", location).fillInStackTrace ();
249             
250         m_classname = classname;
251     }
252     
253     public void setJar (final File JavaDoc file)
254     {
255         if (getClassname () != null)
256             throw (BuildException) SuppressableTask.newBuildException (getTaskName ()
257                 + "'jar' and 'classname' attributes cannot be set at the same time", location).fillInStackTrace ();
258             
259         m_jar = file;
260     }
261     
262     
263     public void setClasspath (final Path path)
264     {
265         if (m_classpath == null)
266             m_classpath = path;
267         else
268             m_classpath.append (path);
269     }
270     
271     public void setClasspathRef (final Reference ref)
272     {
273         createClasspath ().setRefid (ref);
274     }
275     
276     public Path createClasspath ()
277     {
278         if (m_classpath == null)
279             m_classpath = new Path (project);
280         
281         return m_classpath.createPath ();
282     }
283     
284     /**
285      * This is already deprecated in ANT v1.4. However, it is still supported by
286      * the parent task so I do likewise.
287      */

288     public void setArgs (final String JavaDoc args)
289     {
290         throw (BuildException) SuppressableTask.newBuildException (getTaskName ()
291             + ": disallows using <java>'s deprecated 'args' attribute", location).fillInStackTrace ();
292     }
293
294     /**
295      * Not overridable.
296      */

297     public final void setFork (final boolean fork)
298     {
299         m_fork = fork;
300         m_forkUserOverride = true;
301     }
302     
303     /**
304      * Not overridable [due to limitations in ANT's Commandline].
305      */

306     public final Commandline.Argument createArg ()
307     {
308         if (m_appArgs == null)
309             m_appArgs = new Commandline ();
310         
311         return m_appArgs.createArgument ();
312     }
313     
314     // <java> overrides [ANT 1.5]:
315

316     // [nothing at this point]
317

318     
319     // <emmajava> extensions:
320

321     public void setEnabled (final boolean enabled)
322     {
323         m_enabled = enabled;
324     }
325     
326     // .properties file attribute:
327

328     public final void setProperties (final File JavaDoc file)
329     {
330         m_genericCfg.setProperties (file);
331     }
332
333     // generic property element:
334

335     public final PropertyElement createProperty ()
336     {
337         return m_genericCfg.createProperty ();
338     }
339     
340     // verbosity attribute:
341

342     public void setVerbosity (final VerbosityCfg.VerbosityAttribute verbosity)
343     {
344         m_verbosityCfg.setVerbosity (verbosity);
345     }
346     
347     // verbosity class filter attribute:
348

349     public void setVerbosityfilter (final String JavaDoc filter)
350     {
351         m_verbosityCfg.setVerbosityfilter (filter);
352     }
353     
354     // lib classpath attribute [to support non-extdir deployment]:
355

356     public final void setLibclasspath (final Path classpath)
357     {
358         if (m_libClasspath == null)
359             m_libClasspath = classpath;
360         else
361             m_libClasspath.append (classpath);
362     }
363     
364     public final void setLibclasspathRef (final Reference ref)
365     {
366         if (m_libClasspath == null)
367             m_libClasspath = new Path (project);
368         
369         m_libClasspath.createPath ().setRefid (ref);
370     }
371     
372     // -f flag:
373

374     public void setFullmetadata (final boolean full)
375     {
376         m_scanCoveragePath = full; // defaults to false TODO: maintain the default in a central location
377
}
378     
379     // -raw flag:
380

381     public void setDumpsessiondata (final boolean dump)
382     {
383         m_dumpSessionData = dump;
384     }
385     
386     // -out option:
387

388     // sessiondatafile|outfile attribute:
389

390     public void setSessiondatafile (final File JavaDoc file)
391     {
392         if (m_outFile != null)
393             throw (BuildException) SuppressableTask.newBuildException (getTaskName ()
394                 + ": session data file attribute already set", location).fillInStackTrace ();
395             
396         m_outFile = file;
397     }
398     
399     public void setOutfile (final File JavaDoc file)
400     {
401         if (m_outFile != null)
402             throw (BuildException) SuppressableTask.newBuildException (getTaskName ()
403                 + ": session data file attribute already set", location).fillInStackTrace ();
404             
405         m_outFile = file;
406     }
407
408 // public void setTofile (final File file)
409
// {
410
// if (m_outFile != null)
411
// throw (BuildException) SuppressableTask.newBuildException (getTaskName ()
412
// + ": session data file attribute already set", location).fillInStackTrace ();
413
//
414
// m_outFile = file;
415
// }
416
//
417
// public void setFile (final File file)
418
// {
419
// if (m_outFile != null)
420
// throw (BuildException) SuppressableTask.newBuildException (getTaskName ()
421
// + ": session data file attribute already set", location).fillInStackTrace ();
422
//
423
// m_outFile = file;
424
// }
425

426     
427     // merge attribute:
428

429     public void setMerge (final boolean merge)
430     {
431         m_outFileMerge = merge ? Boolean.TRUE : Boolean.FALSE;
432     }
433     
434     // instr filter attribute/element:
435

436     public final void setFilter (final String JavaDoc filter)
437     {
438         m_filterCfg.setFilter (filter);
439     }
440     
441     public final filterElement createFilter ()
442     {
443         return m_filterCfg.createFilter ();
444     }
445
446     
447     // TODO: should what's below go inside <report></report> ?
448

449     // sourcepath attribute/element:
450

451     public final void setSourcepath (final Path path)
452     {
453         m_reportCfg.setSourcepath (path);
454     }
455     
456     public final void setSourcepathRef (final Reference ref)
457     {
458         m_reportCfg.setSourcepathRef (ref);
459     }
460     
461     public final Path createSourcepath ()
462     {
463         return m_reportCfg.createSourcepath ();
464     }
465     
466     
467     // generator elements:
468

469     public final Element_TXT createTxt ()
470     {
471         return m_reportCfg.createTxt ();
472     }
473     
474     public final Element_HTML createHtml ()
475     {
476         return m_reportCfg.createHtml ();
477     }
478     
479     public final Element_XML createXml ()
480     {
481         return m_reportCfg.createXml ();
482     }
483     
484     
485     // report properties [defaults for all report types]:
486

487     public final void setUnits (final UnitsTypeAttribute units)
488     {
489         m_reportCfg.setUnits (units);
490     }
491
492     public final void setDepth (final DepthAttribute depth)
493     {
494         m_reportCfg.setDepth (depth);
495     }
496     
497     public final void setColumns (final String JavaDoc columns)
498     {
499         m_reportCfg.setColumns (columns);
500     }
501     
502     public final void setSort (final String JavaDoc sort)
503     {
504         m_reportCfg.setSort (sort);
505     }
506     
507     public final void setMetrics (final String JavaDoc metrics)
508     {
509         m_reportCfg.setMetrics (metrics);
510     }
511     
512     // these are not supported anymore
513

514 // public final void setOutdir (final File dir)
515
// {
516
// m_reportCfg.setOutdir (dir);
517
// }
518
//
519
// public final void setDestdir (final File dir)
520
// {
521
// m_reportCfg.setDestdir (dir);
522
// }
523

524       // should be set at this level [and conflicts with raw data opts]:
525

526 // public void setOutfile (final String fileName)
527
// {
528
// m_reportCfg.setOutfile (fileName);
529
// }
530

531     public void setEncoding (final String JavaDoc encoding)
532     {
533         m_reportCfg.setEncoding (encoding);
534     }
535     
536     // protected: .............................................................
537

538     
539     protected String JavaDoc getClassname ()
540     {
541         return m_classname;
542     }
543     
544     protected File JavaDoc getJar ()
545     {
546         return m_jar;
547     }
548     
549     protected Path getClasspath ()
550     {
551         return m_classpath;
552     }
553     
554     // extended functionality:
555

556     protected boolean isEnabled ()
557     {
558         return m_enabled;
559     }
560
561     // package: ...............................................................
562

563     // private: ...............................................................
564

565     // <java> overrides:
566

567     private Path m_classpath;
568     private String JavaDoc m_classname;
569     private File JavaDoc m_jar;
570     private Commandline m_appArgs;
571     private boolean m_fork, m_forkUserOverride;
572     
573     // <emmajava> extensions:
574

575     private boolean m_enabled;
576     private Path m_libClasspath;
577     private /*final*/ VerbosityCfg m_verbosityCfg;
578     private /*final*/ GenericCfg m_genericCfg;
579     private /*final*/ FilterCfg m_filterCfg;
580     private /*final*/ ReportCfg m_reportCfg;
581     private boolean m_scanCoveragePath; // defaults to false
582
private boolean m_dumpSessionData; //defaults to false
583
private File JavaDoc m_outFile;
584     private Boolean JavaDoc m_outFileMerge;
585
586 } // end of class
587
// ----------------------------------------------------------------------------
Popular Tags