View Javadoc

    1   /*
    2    * $Id: AspectWerkzCTask.java,v 1.8 2005/02/21 08:33:24 avasseur Exp $
    3    * $Date: 2005/02/21 08:33:24 $
    4    */
    5   package org.codehaus.aspectwerkz.compiler;
    6
    7   import java.io.File;
    8   import java.util.ArrayList;
    9   import java.util.List;
    10  import java.util.Iterator;
    11
    12  import org.apache.tools.ant.BuildException;
    13  import org.apache.tools.ant.Task;
    14  import org.apache.tools.ant.types.Path;
    15  import org.apache.tools.ant.types.Reference;
    16  import org.apache.tools.ant.types.FileSet;
    17  import org.codehaus.aspectwerkz.transform.inlining.AspectModelManager;
    18
    19  /***
    20   * AspectWerkzC offline Ant task.
    21   *
    22   * Use the following parameters to configure the task:
    23   * <ul>
    24   * <li>verbose: [optional] flag marking the weaver verbosity [true / false]</li>
    25   * <li>details: [optional] flag marking the weaver verbosity on matching [true / false, requires verbose=true]</li>
    26   * <li>genjp: [optional] flag marking the need to keep the generated jp classes [true / false]</li>
    27   * <li>taskverbose: [optional] flag marking the task verbose [true / false]</li>
    28   * <li>definition: [optional] path to aspect definition xml file (optional, can be found on the path as META-INF/aop.xml - even several)</li>
    29   * <li>aspectmodels: [optional] models FQN list separated by ":" (see AspectModelManager)</li>
    30   * </ul>
    31   * <p/>
    32   * Use the following parameters to configure the classpath and to point to the classes to be weaved. Those can be specified
    33   * with nested elements as well / instead:
    34   * <ul>
    35   * <li>classpath: classpath to use</li>
    36   * <li>classpathref: classpath reference to use</li>
    37   * <li>targetdir: directory where to find classes to weave</li>
    38   * <li>targetpath: classpath where to find classes to weave</li>
    39   * <li>targetpathref: classpath reference where to find classes to weave</li>
    40   * </ul>
    41   * <p/>
    42   * Nested elements are similar to the "java" task when you configure a classpath:
    43   * <ul>
    44   * <li>classpath: Path-like structure for the classpath to be used by the weaver. Similar to "java" task classpath</li>
    45   * <li>targetpath: Path-like structure for the class to be weaved</li>
    46   * </ul>
    47   * <p/>
    48   * Some rarely used options are also available:
    49   * <ul>
    50   * <li>backupdir: directory where to backup original classes during compilation, defautls to ./_aspectwerkzc</li>
    51   * <li>preprocessor: fully qualified name of the preprocessor. If not set the default is used.</li>
    52   * </ul>
    53   *
    54   * @author <a href='mailto:the_mindstorm@evolva.ro'>the_mindstorm(at)evolva(dot)ro</a>
    55   * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
    56   */
    57  public class AspectWerkzCTask extends Task {
    58
    59      private final static String AW_TRANSFORM_DETAILS = "aspectwerkz.transform.details";
    60
    61      private final static String AW_TRANSFORM_VERBOSE = "aspectwerkz.transform.verbose";
    62
    63      private static final String AW_DEFINITION_FILE = "aspectwerkz.definition.file";
    64
    65      private boolean m_verbose;
    66      private boolean m_details;
    67      private boolean m_genjp;
    68      private boolean m_taskVerbose = false;
    69      private String m_aspectModels;
    70      private File m_backupdir;
    71      private String m_preprocessor;
    72      private File m_definitionFile;
    73      private Path m_classpath;
    74      private Path m_target;
    75      //private List m_filesets = new ArrayList();
    76
    77
    78      /***
    79       * definition=..
    80       * @param defFile
    81       */
    82      public void setDefinition(File defFile) {
    83          m_definitionFile = defFile;
    84      }
    85
    86      /***
    87       * verbose=..
    88       * @param verbose
    89       */
    90      public void setVerbose(boolean verbose) {
    91          m_verbose = verbose;
    92      }
    93
    94      /***
    95       * details=..
    96       * @param details
    97       */
    98      public void setDetails(boolean details) {
    99          m_details = details;
    100     }
    101
    102     /***
    103      * genjp=..
    104      * @param genjp
    105      */
    106     public void setGenjp(boolean genjp) {
    107         m_genjp = genjp;
    108     }
    109
    110     /***
    111      * compilerverbose=..
    112      * @param verbose
    113      */
    114     public void setTaskVerbose(boolean verbose) {
    115         m_taskVerbose = verbose;
    116     }
    117
    118     /***
    119      * aspectmodels=..
    120      * @param aspectModels
    121      */
    122     public void setAspectModels(String aspectModels) {
    123         m_aspectModels = aspectModels;
    124     }
    125
    126     //-- <target .., <targetpath.. and targetdir=.. targetpathref=..
    127
    128     public Path createTarget() {
    129         if (m_target == null)
    130             m_target = new Path(getProject());
    131         return m_target.createPath();
    132     }
    133
    134     public void setTargetdir(Path srcDir) {
    135         if (m_target == null)
    136             m_target = srcDir;
    137         else
    138             m_target.append(srcDir);
    139     }
    140
    141     public void setTargetpath(Path targetpath) {
    142         if (m_target == null)
    143             m_target = targetpath;
    144         else
    145             m_target.append(targetpath);
    146     }
    147
    148     public Path createTargetpath() {
    149         if (m_target == null)
    150             m_target = new Path(getProject());
    151         return m_target.createPath();
    152     }
    153
    154     public void setTargetpathRef(Reference r) {
    155         createTargetpath().setRefid(r);
    156     }
    157
    158     /***
    159      * backupdir=..
    160      * @param backupDir
    161      */
    162     public void setBackupdir(File backupDir) {
    163         m_backupdir = backupDir;
    164     }
    165
    166     /***
    167      * preprocessor=..
    168      * @param preprocessorFqn
    169      */
    170     public void setPreprocessor(String preprocessorFqn) {
    171         m_preprocessor = preprocessorFqn;
    172     }
    173
    174     //--- classpath
    175
    176     public void setClasspath(Path classpath) {
    177         if (m_classpath == null)
    178             m_classpath = classpath;
    179         else
    180             m_classpath.append(classpath);
    181     }
    182
    183     public Path createClasspath() {
    184         if (m_classpath == null)
    185             m_classpath = new Path(getProject());
    186         return m_classpath.createPath();
    187     }
    188
    189     public void setClasspathRef(Reference r) {
    190         createClasspath().setRefid(r);
    191     }
    192
    193 //    //---- fileset for source files
    194 //    public void addFileset(FileSet fileset) {
    195 //        m_filesets.add(fileset);
    196 //    }
    197
    198     public void execute() throws BuildException {
    199         try {
    200             if (m_definitionFile!=null && !!m_definitionFile.exists() && !m_definitionFile.isFile()) {
    201                 throw new BuildException("Definition file provided does not exists");
    202             }
    203
    204             AspectWerkzC compiler = new AspectWerkzC();
    205
    206             compiler.setHaltOnError(true);
    207             compiler.setVerbose(m_taskVerbose);
    208             compiler.setGenJp(m_genjp);
    209             compiler.setVerify(false);
    210
    211             if (m_definitionFile != null) {
    212                 System.setProperty(AW_DEFINITION_FILE, m_definitionFile.getAbsolutePath());
    213             }
    214
    215             if (m_verbose) {
    216                 System.setProperty(AW_TRANSFORM_VERBOSE, m_verbose ? "true" : "false");
    217             }
    218
    219             if (m_details) {
    220                 System.setProperty(AW_TRANSFORM_DETAILS, m_details ? "true" : "false");
    221             }
    222
    223             if (m_aspectModels != null) {
    224                 System.setProperty(AspectModelManager.ASPECT_MODELS_VM_OPTION, m_aspectModels);
    225             }
    226
    227             if (m_backupdir != null && m_backupdir.isDirectory()) {
    228                 compiler.setBackupDir(m_backupdir.getAbsolutePath());
    229             }
    230
    231             if (m_taskVerbose) {
    232                 System.out.println("Classpath    : " + dump(getDirectories(m_classpath)));
    233                 System.out.println("Target       : " + dump(getDirectories(m_target)));
    234                 System.out.println("Definition   : " + m_definitionFile);
    235                 System.out.println("Backupdir    : " + m_backupdir);
    236                 System.out.println("Preprocessor : " + m_preprocessor);
    237             }
    238
    239             AspectWerkzC.compile(compiler,
    240                                  getClass().getClassLoader(),
    241                                  m_preprocessor,
    242                                  getDirectories(m_classpath),
    243                                  getDirectories(m_target)
    244             );
    245         } catch (Exception e) {
    246             e.printStackTrace();
    247             throw new BuildException(e, getLocation());
    248         }
    249     }
    250
    251     private List getDirectories(Path path) throws BuildException {
    252         List dirs = new ArrayList();
    253         if (path == null)
    254             return dirs;
    255         for (int i = 0; i < path.list().length; i++) {
    256             File dir = getProject().resolveFile(path.list()[i]);
    257             if (!dir.exists()) {
    258                 throw new BuildException(" \"" + dir.getPath() + "\" does not exist!", getLocation());
    259             }
    260             dirs.add(dir);//.getAbsolutePath());
    261         }
    262         return dirs;
    263     }
    264
    265     private String dump(List strings) {
    266         StringBuffer sb = new StringBuffer();
    267         for (Iterator iterator = strings.iterator(); iterator.hasNext();) {
    268             Object o = (Object) iterator.next();
    269             sb.append(o.toString()).append(File.pathSeparator);
    270         }
    271         return sb.toString();
    272     }
    273 }