1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002-2009 Oracle. All rights reserved. 5 * 6 * $Id$ 7 */ 8 9package com.sleepycat.persist.model; 10 11import java.io.File; 12import java.io.IOException; 13import java.util.ArrayList; 14import java.util.List; 15 16import org.apache.tools.ant.BuildException; 17import org.apache.tools.ant.DirectoryScanner; 18import org.apache.tools.ant.Task; 19import 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>Note that in the BDB Java Edition product, the {@code ClassEnhancerTask} 28 * class is included in {@code je-<version>.jar}. However, in the BDB 29 * (C-based) product, it is not included in {@code db.jar} because the build is 30 * not dependent on the Ant libraries. Therefore, in the BDB product, the 31 * application must compile the {@code 32 * java/src/com/sleepycat/persist/model/ClassEnhancerTask.java} source file and 33 * ensure that the compiled class is available to the Ant task. For example 34 * the following Ant task definitions could be used.</p> 35 * 36 * <p>For BDB Java Edition product:</p> 37 * <pre class="code"> 38 * {@literal <taskdef name="enhance-persistent-classes"} 39 * {@literal classname="com.sleepycat.persist.model.ClassEnhancerTask"} 40 * {@literal classpath="${je.home}/lib/je-<version>.jar"/>}</pre> 41 * 42 * <p>For BDB (C-based Edition) product:</p> 43 * <pre class="code"> 44 * {@literal <taskdef name="enhance-persistent-classes"} 45 * {@literal classname="com.sleepycat.persist.model.ClassEnhancerTask"} 46 * {@literal classpath="/path-to-jar/db.jar:/path-to-ClassEnhancerTask-class"/>}</pre> 47 * 48 * <p>The class enhancer task element has no attributes. It may contain one or 49 * more nested {@code fileset} elements specifying the classes to be enhanced. 50 * The class files are replaced when they are enhanced, without changing the 51 * file modification date. For example:</p> 52 * 53 * <pre class="code"> 54 * {@literal <target name="main">} 55 * {@literal <enhance-persistent-classes verbose="no">} 56 * {@literal <fileset dir="classes"/>} 57 * {@literal </enhance-persistent-classes>} 58 * {@literal </target>}</pre> 59 * 60 * <p>The verbose attribute may be specified as "true", "yes" or "on" (like 61 * other Ant boolean attributes) to print the name of each class file that is 62 * enhanced. The total number of class files enhanced will always be 63 * printed.</p> 64 * 65 * @author Mark Hayes 66 */ 67public class ClassEnhancerTask extends Task { 68 69 private List<FileSet> fileSets = new ArrayList<FileSet>(); 70 private boolean verbose; 71 72 public void execute() throws BuildException { 73 if (fileSets.size() == 0) { 74 throw new BuildException("At least one fileset must be specified"); 75 } 76 try { 77 int nFiles = 0; 78 ClassEnhancer enhancer = new ClassEnhancer(); 79 enhancer.setVerbose(verbose); 80 for (FileSet fileSet : fileSets) { 81 DirectoryScanner scanner = 82 fileSet.getDirectoryScanner(getProject()); 83 String[] fileNames = scanner.getIncludedFiles(); 84 for (String fileName : fileNames) { 85 File file = new File(scanner.getBasedir(), fileName); 86 try { 87 nFiles += enhancer.enhanceFile(file); 88 } catch (IOException e) { 89 throw new BuildException(e); 90 } 91 } 92 } 93 if (nFiles > 0) { 94 System.out.println("Enhanced: " + nFiles + " files"); 95 } 96 } catch (RuntimeException e) { 97 e.printStackTrace(); 98 throw e; 99 } 100 } 101 102 public void addConfiguredFileset(FileSet files) { 103 fileSets.add(files); 104 } 105 106 public void setVerbose(boolean verbose) { 107 this.verbose = verbose; 108 } 109} 110