View Javadoc

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