KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > aop > ant > AnnotationC


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.aop.ant;
23
24
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.DirectoryScanner;
27 import org.apache.tools.ant.Project;
28 import org.apache.tools.ant.taskdefs.Java;
29 import org.apache.tools.ant.taskdefs.MatchingTask;
30 import org.apache.tools.ant.types.CommandlineJava;
31 import org.apache.tools.ant.types.Path;
32 import org.apache.tools.ant.types.Reference;
33
34 import java.io.File JavaDoc;
35 import java.util.ArrayList JavaDoc;
36
37 /**
38  * Ant task. Copied a bunch of shit from javac task
39  *
40  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
41  * @version $Revision: 37406 $
42  *
43  **/

44 public class AnnotationC extends MatchingTask
45 {
46    private static String JavaDoc lSep = System.getProperty("line.separator");
47
48    private Path classpath;
49    private Path output;
50    private Path compilerClasspath;
51    private Path compileSourcepath;
52    boolean xml = false;
53    boolean bytecode = false;
54
55    /**
56     * flag to control action on execution trouble
57     */

58    protected boolean failOnError = true;
59
60    public void setXml(boolean i)
61    {
62       xml = i;
63    }
64
65    public boolean getXml()
66    {
67       return xml;
68    }
69
70    public void setBytecode(boolean i)
71    {
72       bytecode = i;
73    }
74
75    public boolean getBytecode()
76    {
77       return xml;
78    }
79
80    /**
81     * Whether or not the build should halt if compilation fails.
82     * Defaults to <code>true</code>.
83     */

84    public void setFailonerror(boolean fail)
85    {
86       failOnError = fail;
87    }
88
89    /**
90     * Gets the failonerror flag.
91     */

92    public boolean getFailonerror()
93    {
94       return failOnError;
95    }
96
97    /**
98     * Set the classpath to be used for this compilation.
99     */

100    public void setClasspath(Path cp)
101    {
102       if (classpath == null)
103       {
104          classpath = cp;
105       }
106       else
107       {
108          classpath.append(cp);
109       }
110    }
111
112    /**
113     * Adds a path to the classpath.
114     */

115    public Path createClasspath()
116    {
117       if (classpath == null)
118       {
119          classpath = new Path(getProject());
120       }
121       return classpath.createPath();
122    }
123
124    /**
125     * Adds a reference to a classpath defined elsewhere
126     */

127    public void setClasspathRef(Reference r)
128    {
129       createClasspath().setRefid(r);
130    }
131
132    public Path getClasspath()
133    {
134       return classpath;
135    }
136
137    /**
138     * Adds a reference to a classpath defined elsewhere
139     */

140    public void setCompilerClasspathRef(Reference r)
141    {
142       if (compilerClasspath == null) compilerClasspath = new Path(getProject());
143       compilerClasspath.setRefid(r);
144    }
145
146    /**
147     * Set the classpath to be used to find this compiler adapter
148     */

149    public void setCompilerclasspath(Path cp)
150    {
151       if (compilerClasspath == null)
152       {
153          compilerClasspath = cp;
154       }
155       else
156       {
157          compilerClasspath.append(cp);
158       }
159    }
160
161    /**
162     * get the classpath used to find the compiler adapter
163     */

164    public Path getCompilerclasspath()
165    {
166       return compilerClasspath;
167    }
168
169    /**
170     * Support nested compiler classpath, used to locate compiler adapter
171     */

172    public Path createCompilerclasspath()
173    {
174       if (compilerClasspath == null)
175       {
176          compilerClasspath = new Path(getProject());
177       }
178       return compilerClasspath.createPath();
179    }
180
181    /**
182     * Adds a path for source compilation.
183     *
184     * @return a nested src element.
185     */

186    public Path createSrc()
187    {
188       if (compileSourcepath == null)
189       {
190          compileSourcepath = new Path(getProject());
191       }
192       return compileSourcepath.createPath();
193    }
194
195    public Path createOutput()
196    {
197       if (output == null)
198       {
199          output = new Path(getProject());
200       }
201       return output.createPath();
202    }
203
204    /**
205     * Set the classpath to be used to find this compiler adapter
206     */

207    public void setOutput(Path cp)
208    {
209       if (output == null)
210       {
211          output = cp;
212       }
213       else
214       {
215          output.append(cp);
216       }
217    }
218
219    /**
220     * get the classpath used to find the compiler adapter
221     */

222    public Path getOutput()
223    {
224       return output;
225    }
226
227    public void execute()
228            throws BuildException
229    {
230       CommandlineJava cmd = new CommandlineJava();
231
232       if (output != null)
233       {
234          cmd.createArgument().setValue("-o");
235          cmd.createArgument().setValue(output.toString());
236       }
237       if (xml)
238       {
239          cmd.createArgument().setValue("-xml");
240       }
241       if (bytecode)
242       {
243          cmd.createArgument().setValue("-bytecode");
244       }
245       logAndAddFilesToCompile(cmd);
246       try
247       {
248          // Create an instance of the compiler, redirecting output to
249
// the project log
250
classpath.append(compilerClasspath);
251          Java java = (Java) (getProject().createTask("java"));
252          if (getClasspath() != null)
253          {
254             getProject().log("using user supplied classpath: "
255                     + getClasspath(), Project.MSG_DEBUG);
256             java.setClasspath(getClasspath()
257                     .concatSystemClasspath("ignore"));
258          }
259          else
260          {
261             Path classpath = new Path(getProject());
262             classpath = classpath.concatSystemClasspath("only");
263             getProject().log("using system classpath: " + classpath,
264                     Project.MSG_DEBUG);
265             java.setClasspath(classpath);
266          }
267          java.setDir(getProject().getBaseDir());
268          java.setClassname("org.jboss.aop.annotation.compiler.AnnotationCompiler");
269          //this is really irritating; we need a way to set stuff
270
String JavaDoc args[] = cmd.getJavaCommand().getArguments();
271          for (int i = 0; i < args.length; i++)
272          {
273             java.createArg().setValue(args[i]);
274          }
275          java.setFailonerror(getFailonerror());
276          //we are forking here to be sure that if JspC calls
277
//System.exit() it doesn't halt the build
278
java.setFork(false);
279          java.setTaskName("annotationc");
280          java.execute();
281       }
282       catch (Exception JavaDoc ex)
283       {
284          if (ex instanceof BuildException)
285          {
286             throw (BuildException) ex;
287          }
288          else
289          {
290             throw new BuildException("Error running aopc compiler: ",
291                     ex, getLocation());
292          }
293       }
294    }
295
296    protected void logAndAddFilesToCompile(CommandlineJava cmd)
297    {
298       // scan source directories and dest directory to build up
299
// compile lists
300
String JavaDoc[] list = compileSourcepath.list();
301       ArrayList JavaDoc compilingFiles = new ArrayList JavaDoc();
302       for (int i = 0; i < list.length; i++)
303       {
304          File JavaDoc srcDir = getProject().resolveFile(list[i]);
305          if (!srcDir.exists())
306          {
307             throw new BuildException("srcdir \""
308                     + srcDir.getPath()
309                     + "\" does not exist!", getLocation());
310          }
311
312          DirectoryScanner ds = this.getDirectoryScanner(srcDir);
313          String JavaDoc[] files = ds.getIncludedFiles();
314          for (int j = 0; j < files.length; j++)
315          {
316             if (files[j].endsWith(".java"))
317             {
318                File JavaDoc f = new File JavaDoc(srcDir, files[j]);
319                compilingFiles.add(f.getAbsolutePath());
320             }
321          }
322       }
323
324
325       StringBuffer JavaDoc niceSourceList = new StringBuffer JavaDoc("File");
326       for (int i = 0; i < compilingFiles.size(); i++)
327       {
328          String JavaDoc file = (String JavaDoc) compilingFiles.get(i);
329          cmd.createArgument().setValue(file);
330          niceSourceList.append(" " + file + lSep);
331       }
332       //getProject().log(niceSourceList.toString());
333
}
334
335 }
336
Popular Tags