001: /*
002: * $Id: AspectWerkzCTask.java,v 1.8 2005/02/21 08:33:24 avasseur Exp $
003: * $Date: 2005/02/21 08:33:24 $
004: */
005: package org.codehaus.aspectwerkz.compiler;
006:
007: import java.io.File;
008: import java.util.ArrayList;
009: import java.util.List;
010: import java.util.Iterator;
011:
012: import org.apache.tools.ant.BuildException;
013: import org.apache.tools.ant.Task;
014: import org.apache.tools.ant.types.Path;
015: import org.apache.tools.ant.types.Reference;
016: import org.apache.tools.ant.types.FileSet;
017: import org.codehaus.aspectwerkz.transform.inlining.AspectModelManager;
018:
019: /**
020: * AspectWerkzC offline Ant task.
021: *
022: * Use the following parameters to configure the task:
023: * <ul>
024: * <li>verbose: [optional] flag marking the weaver verbosity [true / false]</li>
025: * <li>details: [optional] flag marking the weaver verbosity on matching [true / false, requires verbose=true]</li>
026: * <li>genjp: [optional] flag marking the need to keep the generated jp classes [true / false]</li>
027: * <li>taskverbose: [optional] flag marking the task verbose [true / false]</li>
028: * <li>definition: [optional] path to aspect definition xml file (optional, can be found on the path as META-INF/aop.xml - even several)</li>
029: * <li>aspectmodels: [optional] models FQN list separated by ":" (see AspectModelManager)</li>
030: * </ul>
031: * <p/>
032: * Use the following parameters to configure the classpath and to point to the classes to be weaved. Those can be specified
033: * with nested elements as well / instead:
034: * <ul>
035: * <li>classpath: classpath to use</li>
036: * <li>classpathref: classpath reference to use</li>
037: * <li>targetdir: directory where to find classes to weave</li>
038: * <li>targetpath: classpath where to find classes to weave</li>
039: * <li>targetpathref: classpath reference where to find classes to weave</li>
040: * </ul>
041: * <p/>
042: * Nested elements are similar to the "java" task when you configure a classpath:
043: * <ul>
044: * <li>classpath: Path-like structure for the classpath to be used by the weaver. Similar to "java" task classpath</li>
045: * <li>targetpath: Path-like structure for the class to be weaved</li>
046: * </ul>
047: * <p/>
048: * Some rarely used options are also available:
049: * <ul>
050: * <li>backupdir: directory where to backup original classes during compilation, defautls to ./_aspectwerkzc</li>
051: * <li>preprocessor: fully qualified name of the preprocessor. If not set the default is used.</li>
052: * </ul>
053: *
054: * @author <a href='mailto:the_mindstorm@evolva.ro'>the_mindstorm(at)evolva(dot)ro</a>
055: * @author <a href="mailto:alex AT gnilux DOT com">Alexandre Vasseur</a>
056: */
057: public class AspectWerkzCTask extends Task {
058:
059: private final static String AW_TRANSFORM_DETAILS = "aspectwerkz.transform.details";
060:
061: private final static String AW_TRANSFORM_VERBOSE = "aspectwerkz.transform.verbose";
062:
063: private static final String AW_DEFINITION_FILE = "aspectwerkz.definition.file";
064:
065: private boolean m_verbose;
066: private boolean m_details;
067: private boolean m_genjp;
068: private boolean m_taskVerbose = false;
069: private String m_aspectModels;
070: private File m_backupdir;
071: private String m_preprocessor;
072: private File m_definitionFile;
073: private Path m_classpath;
074: private Path m_target;
075:
076: //private List m_filesets = new ArrayList();
077:
078: /**
079: * definition=..
080: * @param defFile
081: */
082: public void setDefinition(File defFile) {
083: m_definitionFile = defFile;
084: }
085:
086: /**
087: * verbose=..
088: * @param verbose
089: */
090: public void setVerbose(boolean verbose) {
091: m_verbose = verbose;
092: }
093:
094: /**
095: * details=..
096: * @param details
097: */
098: public void setDetails(boolean details) {
099: 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()
201: && !m_definitionFile.isFile()) {
202: throw new BuildException(
203: "Definition file provided does not exists");
204: }
205:
206: AspectWerkzC compiler = new AspectWerkzC();
207:
208: compiler.setHaltOnError(true);
209: compiler.setVerbose(m_taskVerbose);
210: compiler.setGenJp(m_genjp);
211: compiler.setVerify(false);
212:
213: if (m_definitionFile != null) {
214: System.setProperty(AW_DEFINITION_FILE, m_definitionFile
215: .getAbsolutePath());
216: }
217:
218: if (m_verbose) {
219: System.setProperty(AW_TRANSFORM_VERBOSE,
220: m_verbose ? "true" : "false");
221: }
222:
223: if (m_details) {
224: System.setProperty(AW_TRANSFORM_DETAILS,
225: m_details ? "true" : "false");
226: }
227:
228: if (m_aspectModels != null) {
229: System.setProperty(
230: AspectModelManager.ASPECT_MODELS_VM_OPTION,
231: 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 : "
240: + dump(getDirectories(m_classpath)));
241: System.out.println("Target : "
242: + dump(getDirectories(m_target)));
243: System.out
244: .println("Definition : " + m_definitionFile);
245: System.out.println("Backupdir : " + m_backupdir);
246: System.out.println("Preprocessor : " + m_preprocessor);
247: }
248:
249: AspectWerkzC.compile(compiler, getClass().getClassLoader(),
250: m_preprocessor, getDirectories(m_classpath),
251: getDirectories(m_target));
252: } catch (Exception e) {
253: e.printStackTrace();
254: throw new BuildException(e, getLocation());
255: }
256: }
257:
258: private List getDirectories(Path path) throws BuildException {
259: List dirs = new ArrayList();
260: if (path == null)
261: return dirs;
262: for (int i = 0; i < path.list().length; i++) {
263: File dir = getProject().resolveFile(path.list()[i]);
264: if (!dir.exists()) {
265: throw new BuildException(" \"" + dir.getPath()
266: + "\" does not exist!", getLocation());
267: }
268: dirs.add(dir);//.getAbsolutePath());
269: }
270: return dirs;
271: }
272:
273: private String dump(List strings) {
274: StringBuffer sb = new StringBuffer();
275: for (Iterator iterator = strings.iterator(); iterator.hasNext();) {
276: Object o = (Object) iterator.next();
277: sb.append(o.toString()).append(File.pathSeparator);
278: }
279: return sb.toString();
280: }
281: }
|