KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > persist > model > ClassEnhancerTask


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: ClassEnhancerTask.java,v 1.9 2006/10/30 21:14:33 bostic Exp $
7  */

8
9 package com.sleepycat.persist.model;
10
11 import java.io.File JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16 import org.apache.tools.ant.BuildException;
17 import org.apache.tools.ant.DirectoryScanner;
18 import org.apache.tools.ant.Task;
19 import org.apache.tools.ant.types.FileSet;
20
21 /**
22  * An {@code ant} task for running the {@link ClassEnhancer}.
23  *
24  * <p>{@code ClassEnhancerTask} objects are thread-safe. Multiple threads may
25  * safely call the methods of a shared {@code ClassEnhancerTask} object.</p>
26  *
27  * <p>The class enhancer task element has no attributes. It may contain one or
28  * more nested {@code fileset} elements specifying the classes to be enhanced.
29  * The class files are replaced when they are enhanced, without changing the
30  * file modification date. For example:</p>
31  *
32  * <pre class="code">
33  * {@literal <taskdef name="enhance-persistent-classes"}
34  * {@literal classname="com.sleepycat.persist.model.ClassEnhancerTask"}
35  * {@literal classpath="${je.home}/lib/je-<version>.jar"/>}
36  *
37  * {@literal <target name="main">}
38  * {@literal <enhance-persistent-classes verbose="no">}
39  * {@literal <fileset dir="classes"/>}
40  * {@literal </enhance-persistent-classes>}
41  * {@literal </target>}</pre>
42  *
43  * <p>The verbose attribute may be specified as "true", "yes" or "on" (like
44  * other Ant boolean attributes) to print the name of each class file that is
45  * enhanced. The total number of class files enhanced will always be
46  * printed.</p>
47  *
48  * @author Mark Hayes
49  */

50 public class ClassEnhancerTask extends Task {
51
52     private List JavaDoc<FileSet> fileSets = new ArrayList JavaDoc<FileSet>();
53     private boolean verbose;
54
55     public void execute() throws BuildException {
56         if (fileSets.size() == 0) {
57             throw new BuildException("At least one fileset must be specified");
58         }
59         try {
60             int nFiles = 0;
61             ClassEnhancer enhancer = new ClassEnhancer();
62             enhancer.setVerbose(verbose);
63             for (FileSet fileSet : fileSets) {
64                 DirectoryScanner scanner =
65                     fileSet.getDirectoryScanner(getProject());
66                 String JavaDoc[] fileNames = scanner.getIncludedFiles();
67                 for (String JavaDoc fileName : fileNames) {
68                     File JavaDoc file = new File JavaDoc(scanner.getBasedir(), fileName);
69                     try {
70                         nFiles += enhancer.enhanceFile(file);
71                     } catch (IOException JavaDoc e) {
72                         throw new BuildException(e);
73                     }
74                 }
75             }
76             if (nFiles > 0) {
77                 System.out.println("Enhanced: " + nFiles + " files");
78             }
79         } catch (RuntimeException JavaDoc e) {
80             e.printStackTrace();
81             throw e;
82         }
83     }
84
85     public void addConfiguredFileset(FileSet files) {
86         fileSets.add(files);
87     }
88
89     public void setVerbose(boolean verbose) {
90         this.verbose = verbose;
91     }
92 }
93
Popular Tags