01: /*-
02: * See the file LICENSE for redistribution information.
03: *
04: * Copyright (c) 2002,2008 Oracle. All rights reserved.
05: *
06: * $Id: ClassEnhancerTask.java,v 1.9.2.2 2008/01/07 15:14:20 cwl Exp $
07: */
08:
09: package com.sleepycat.persist.model;
10:
11: import java.io.File;
12: import java.io.IOException;
13: import java.util.ArrayList;
14: import java.util.List;
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<FileSet> fileSets = new ArrayList<FileSet>();
53: private boolean verbose;
54:
55: public void execute() throws BuildException {
56: if (fileSets.size() == 0) {
57: throw new BuildException(
58: "At least one fileset must be specified");
59: }
60: try {
61: int nFiles = 0;
62: ClassEnhancer enhancer = new ClassEnhancer();
63: enhancer.setVerbose(verbose);
64: for (FileSet fileSet : fileSets) {
65: DirectoryScanner scanner = fileSet
66: .getDirectoryScanner(getProject());
67: String[] fileNames = scanner.getIncludedFiles();
68: for (String fileName : fileNames) {
69: File file = new File(scanner.getBasedir(), fileName);
70: try {
71: nFiles += enhancer.enhanceFile(file);
72: } catch (IOException e) {
73: throw new BuildException(e);
74: }
75: }
76: }
77: if (nFiles > 0) {
78: System.out.println("Enhanced: " + nFiles + " files");
79: }
80: } catch (RuntimeException e) {
81: e.printStackTrace();
82: throw e;
83: }
84: }
85:
86: public void addConfiguredFileset(FileSet files) {
87: fileSets.add(files);
88: }
89:
90: public void setVerbose(boolean verbose) {
91: this.verbose = verbose;
92: }
93: }
|