KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > compiler > AspectWerkzCTask


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.compiler;
5
6 import java.io.File JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Iterator JavaDoc;
10
11 import org.apache.tools.ant.BuildException;
12 import org.apache.tools.ant.Task;
13 import org.apache.tools.ant.types.Path;
14 import org.apache.tools.ant.types.Reference;
15
16 import com.tc.aspectwerkz.transform.inlining.AspectModelManager;
17
18 /**
19  * AspectWerkzC offline Ant task.
20  * <p/>
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>details: [optional] flag marking the weaver verbosity on matching [true / false, requires verbose=true]</li>
25  * <li>genjp: [optional] flag marking the need to keep the generated jp classes [true / false]</li>
26  * <li>taskverbose: [optional] flag marking the task verbose [true / false]</li>
27  * <li>definition: [optional] path to aspect definition xml file (optional, can be found on the path as META-INF/aop.xml - even several)</li>
28  * <li>aspectmodels: [optional] models FQN list separated by ":" (see AspectModelManager)</li>
29  * </ul>
30  * <p/>
31  * Use the following parameters to configure the classpath and to point to the classes to be weaved. Those can be specified
32  * with nested elements as well / instead:
33  * <ul>
34  * <li>classpath: classpath to use</li>
35  * <li>classpathref: classpath reference to use</li>
36  * <li>targetdir: directory where to find classes to weave</li>
37  * <li>targetpath: classpath where to find classes to weave</li>
38  * <li>targetpathref: classpath reference where to find classes to weave</li>
39  * </ul>
40  * <p/>
41  * Nested elements are similar to the "java" task when you configure a classpath:
42  * <ul>
43  * <li>classpath: Path-like structure for the classpath to be used by the weaver. Similar to "java" task classpath</li>
44  * <li>targetpath: Path-like structure for the class to be weaved</li>
45  * </ul>
46  * <p/>
47  * Some rarely used options are also available:
48  * <ul>
49  * <li>backupdir: directory where to backup original classes during compilation, defautls to ./_aspectwerkzc</li>
50  * <li>preprocessor: fully qualified name of the preprocessor. If not set the default is used.</li>
51  * </ul>
52  *
53  * @author <a HREF='mailto:the_mindstorm@evolva.ro'>the_mindstorm(at)evolva(dot)ro</a>
54  * @author <a HREF="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
55  */

56 public class AspectWerkzCTask extends Task {
57
58   private final static String JavaDoc AW_TRANSFORM_DETAILS = "aspectwerkz.transform.details";
59
60   private final static String JavaDoc AW_TRANSFORM_VERBOSE = "aspectwerkz.transform.verbose";
61
62   private static final String JavaDoc AW_DEFINITION_FILE = "aspectwerkz.definition.file";
63
64   private boolean m_verbose;
65   private boolean m_details;
66   private boolean m_genjp;
67   private boolean m_taskVerbose = false;
68   private String JavaDoc m_aspectModels;
69   private File JavaDoc m_backupdir;
70   private String JavaDoc m_preprocessor;
71   private File JavaDoc m_definitionFile;
72   private Path m_classpath;
73   private Path m_target;
74   //private List m_filesets = new ArrayList();
75

76
77   /**
78    * definition=..
79    *
80    * @param defFile
81    */

82   public void setDefinition(File JavaDoc defFile) {
83     m_definitionFile = defFile;
84   }
85
86   /**
87    * verbose=..
88    *
89    * @param verbose
90    */

91   public void setVerbose(boolean verbose) {
92     m_verbose = verbose;
93   }
94
95   /**
96    * details=..
97    *
98    * @param details
99    */

100   public void setDetails(boolean details) {
101     m_details = details;
102   }
103
104   /**
105    * genjp=..
106    *
107    * @param genjp
108    */

109   public void setGenjp(boolean genjp) {
110     m_genjp = genjp;
111   }
112
113   /**
114    * compilerverbose=..
115    *
116    * @param verbose
117    */

118   public void setTaskVerbose(boolean verbose) {
119     m_taskVerbose = verbose;
120   }
121
122   /**
123    * aspectmodels=..
124    *
125    * @param aspectModels
126    */

127   public void setAspectModels(String JavaDoc aspectModels) {
128     m_aspectModels = aspectModels;
129   }
130
131   //-- <target .., <targetpath.. and targetdir=.. targetpathref=..
132

133   public Path createTarget() {
134     if (m_target == null)
135       m_target = new Path(getProject());
136     return m_target.createPath();
137   }
138
139   public void setTargetdir(Path srcDir) {
140     if (m_target == null)
141       m_target = srcDir;
142     else
143       m_target.append(srcDir);
144   }
145
146   public void setTargetpath(Path targetpath) {
147     if (m_target == null)
148       m_target = targetpath;
149     else
150       m_target.append(targetpath);
151   }
152
153   public Path createTargetpath() {
154     if (m_target == null)
155       m_target = new Path(getProject());
156     return m_target.createPath();
157   }
158
159   public void setTargetpathRef(Reference r) {
160     createTargetpath().setRefid(r);
161   }
162
163   /**
164    * backupdir=..
165    *
166    * @param backupDir
167    */

168   public void setBackupdir(File JavaDoc backupDir) {
169     m_backupdir = backupDir;
170   }
171
172   /**
173    * preprocessor=..
174    *
175    * @param preprocessorFqn
176    */

177   public void setPreprocessor(String JavaDoc preprocessorFqn) {
178     m_preprocessor = preprocessorFqn;
179   }
180
181   //--- classpath
182

183   public void setClasspath(Path classpath) {
184     if (m_classpath == null)
185       m_classpath = classpath;
186     else
187       m_classpath.append(classpath);
188   }
189
190   public Path createClasspath() {
191     if (m_classpath == null)
192       m_classpath = new Path(getProject());
193     return m_classpath.createPath();
194   }
195
196   public void setClasspathRef(Reference r) {
197     createClasspath().setRefid(r);
198   }
199
200 // //---- fileset for source files
201
// public void addFileset(FileSet fileset) {
202
// m_filesets.add(fileset);
203
// }
204

205   public void execute() throws BuildException {
206     try {
207       if (m_definitionFile != null && !!m_definitionFile.exists() && !m_definitionFile.isFile()) {
208         throw new BuildException("Definition file provided does not exists");
209       }
210
211       AspectWerkzC compiler = new AspectWerkzC();
212
213       compiler.setHaltOnError(true);
214       compiler.setVerbose(m_taskVerbose);
215       compiler.setGenJp(m_genjp);
216       compiler.setVerify(false);
217
218       if (m_definitionFile != null) {
219         System.setProperty(AW_DEFINITION_FILE, m_definitionFile.getAbsolutePath());
220       }
221
222       if (m_verbose) {
223         System.setProperty(AW_TRANSFORM_VERBOSE, m_verbose ? "true" : "false");
224       }
225
226       if (m_details) {
227         System.setProperty(AW_TRANSFORM_DETAILS, m_details ? "true" : "false");
228       }
229
230       if (m_aspectModels != null) {
231         System.setProperty(AspectModelManager.ASPECT_MODELS_VM_OPTION, m_aspectModels);
232       }
233
234       if (m_backupdir != null && m_backupdir.isDirectory()) {
235         compiler.setBackupDir(m_backupdir.getAbsolutePath());
236       }
237
238       if (m_taskVerbose) {
239         System.out.println("Classpath : " + dump(getDirectories(m_classpath)));
240         System.out.println("Target : " + dump(getDirectories(m_target)));
241         System.out.println("Definition : " + m_definitionFile);
242         System.out.println("Backupdir : " + m_backupdir);
243         System.out.println("Preprocessor : " + m_preprocessor);
244       }
245
246       AspectWerkzC.compile(compiler,
247               getClass().getClassLoader(),
248               m_preprocessor,
249               getDirectories(m_classpath),
250               getDirectories(m_target)
251       );
252     } catch (Exception JavaDoc e) {
253       e.printStackTrace();
254       throw new BuildException(e, getLocation());
255     }
256   }
257
258   private List JavaDoc getDirectories(Path path) throws BuildException {
259     List JavaDoc dirs = new ArrayList JavaDoc();
260     if (path == null)
261       return dirs;
262     for (int i = 0; i < path.list().length; i++) {
263       File JavaDoc dir = getProject().resolveFile(path.list()[i]);
264       if (!dir.exists()) {
265         throw new BuildException(" \"" + dir.getPath() + "\" does not exist!", getLocation());
266       }
267       dirs.add(dir);//.getAbsolutePath());
268
}
269     return dirs;
270   }
271
272   private String JavaDoc dump(List JavaDoc strings) {
273     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
274     for (Iterator JavaDoc iterator = strings.iterator(); iterator.hasNext();) {
275       Object JavaDoc o = (Object JavaDoc) iterator.next();
276       sb.append(o.toString()).append(File.pathSeparator);
277     }
278     return sb.toString();
279   }
280 }
281
Popular Tags