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