KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.BufferedWriter JavaDoc;
26 import java.io.File JavaDoc;
27 import java.io.FileWriter JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import org.apache.tools.ant.BuildException;
33 import org.apache.tools.ant.DirectoryScanner;
34 import org.apache.tools.ant.Project;
35 import org.apache.tools.ant.taskdefs.Java;
36 import org.apache.tools.ant.taskdefs.MatchingTask;
37 import org.apache.tools.ant.types.CommandlineJava;
38 import org.apache.tools.ant.types.Environment;
39 import org.apache.tools.ant.types.Path;
40 import org.apache.tools.ant.types.Reference;
41
42 /**
43  * Ant task. Copied a bunch of shit from javac task
44  *
45  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
46  * @version $Revision: 46168 $
47  */

48 public class AopC extends MatchingTask
49 {
50    final static String JavaDoc SYS_OPTIMIZED = "jboss.aop.optimized";
51    final static String JavaDoc SYS_INSTRUMENTOR = "jboss.aop.instrumentor";
52    
53    private static String JavaDoc lSep = System.getProperty("line.separator");
54    private int maxSrc = 1000;
55
56    private String JavaDoc instrumentor;
57    private String JavaDoc jvm;
58    private Path classpath;
59    private Path aoppath;
60    private Path aopclasspath;
61    private Path compilerClasspath;
62    private Path compileSourcepath;
63    private boolean verbose = false;
64    private boolean suppress = true;
65    private boolean report = false;
66    private boolean optimized = true;
67    private String JavaDoc maxmemory = null;
68    ArrayList JavaDoc sysproperties = new ArrayList JavaDoc();
69
70    /**
71     * flag to control action on execution trouble
72     */

73    protected boolean failOnError = true;
74
75    /**
76     * Set the verbose level of the compiler
77     */

78    public void setVerbose(boolean i)
79    {
80       verbose = i;
81    }
82
83    public boolean getVerbose()
84    {
85       return verbose;
86    }
87
88    public String JavaDoc getInstrumentor()
89    {
90       return instrumentor;
91    }
92
93    public void setInstrumentor(String JavaDoc instrumentor)
94    {
95       this.instrumentor = instrumentor;
96    }
97
98    /**
99     * Set the verbose level of the compiler
100     */

101    public void setSuppress(boolean i)
102    {
103       suppress = i;
104    }
105
106    public boolean getSupress()
107    {
108       return suppress;
109    }
110
111    public void setReport(boolean i)
112    {
113       report = i;
114    }
115
116    public boolean getReport()
117    {
118       return report;
119    }
120
121    public String JavaDoc getJvm()
122    {
123       return jvm;
124    }
125
126    public void setJvm(String JavaDoc jvm)
127    {
128       this.jvm = jvm;
129    }
130
131    public void setOptimized(boolean optimized)
132    {
133       this.optimized = optimized;
134    }
135
136    public boolean getOptimized()
137    {
138       return optimized;
139    }
140
141    /**
142     * Set the maxmemory of the Java task forked to apply the AOP
143     */

144    public void setMaxmemory(String JavaDoc maxmemory)
145    {
146       this.maxmemory = maxmemory;
147    }
148
149    public String JavaDoc getMaxmemory()
150    {
151       return maxmemory;
152    }
153
154
155    /**
156     * Whether or not the build should halt if compilation fails.
157     * Defaults to <code>true</code>.
158     */

159    public void setFailonerror(boolean fail)
160    {
161       failOnError = fail;
162    }
163
164    /**
165     * Gets the failonerror flag.
166     */

167    public boolean getFailonerror()
168    {
169       return failOnError;
170    }
171
172    /**
173     * Set the classpath to be used for this compilation.
174     */

175    public void setClasspath(Path cp)
176    {
177       if (classpath == null)
178       {
179          classpath = cp;
180       }
181       else
182       {
183          classpath.append(cp);
184       }
185    }
186
187    public void setMaxSrc(int maxSrc)
188    {
189       this.maxSrc = maxSrc;
190    }
191
192    public int getMaxSrc()
193    {
194       return maxSrc;
195    }
196
197    /**
198     * Adds a path to the classpath.
199     */

200    public Path createClasspath()
201    {
202       if (classpath == null)
203       {
204          classpath = new Path(getProject());
205       }
206       return classpath.createPath();
207    }
208
209    /**
210     * Adds a reference to a classpath defined elsewhere
211     */

212    public void setClasspathRef(Reference r)
213    {
214       createClasspath().setRefid(r);
215    }
216
217    public Path getClasspath()
218    {
219       return classpath;
220    }
221
222    /**
223     * Adds a reference to a classpath defined elsewhere
224     */

225    public void setCompilerClasspathRef(Reference r)
226    {
227       if (compilerClasspath == null) compilerClasspath = new Path(getProject());
228       compilerClasspath.setRefid(r);
229    }
230
231    /**
232     * Set the classpath to be used to find this compiler adapter
233     */

234    public void setCompilerclasspath(Path cp)
235    {
236       if (compilerClasspath == null)
237       {
238          compilerClasspath = cp;
239       }
240       else
241       {
242          compilerClasspath.append(cp);
243       }
244    }
245
246    /**
247     * get the classpath used to find the compiler adapter
248     */

249    public Path getCompilerclasspath()
250    {
251       return compilerClasspath;
252    }
253
254    /**
255     * Support nested compiler classpath, used to locate compiler adapter
256     */

257    public Path createCompilerclasspath()
258    {
259       if (compilerClasspath == null)
260       {
261          compilerClasspath = new Path(getProject());
262       }
263       return compilerClasspath.createPath();
264    }
265
266    /**
267     * Adds a path for source compilation.
268     *
269     * @return a nested src element.
270     */

271    public Path createSrc()
272    {
273       if (compileSourcepath == null)
274       {
275          compileSourcepath = new Path(getProject());
276       }
277       return compileSourcepath.createPath();
278    }
279
280    public Path createAoppath()
281    {
282       if (aoppath == null)
283       {
284          aoppath = new Path(getProject());
285       }
286       //return aoppath.createPath();
287
Path path = aoppath.createPath();
288       return path;
289    }
290
291    public Path createAopclasspath()
292    {
293       if (aopclasspath == null)
294       {
295          aopclasspath = new Path(getProject());
296       }
297       return aopclasspath.createPath();
298    }
299    
300    public void addSysproperty(Environment.Variable property)
301    {
302       sysproperties.add(property);
303    }
304
305    public void execute()
306            throws BuildException
307    {
308       CommandlineJava cmd = new CommandlineJava();
309
310       if (verbose)
311          cmd.createArgument().setValue("-verbose");
312       if (suppress)
313          cmd.createArgument().setValue("-suppress");
314       if (!optimized)
315          cmd.createArgument().setValue("-noopt");
316       if (report)
317          cmd.createArgument().setValue("-report");
318       if (aoppath != null && aoppath.size() > 0)
319       {
320          cmd.createArgument().setValue("-aoppath");
321          cmd.createArgument().setValue(aoppath.toString());
322       }
323       if (aopclasspath != null && aopclasspath.size() > 0)
324       {
325          cmd.createArgument().setValue("-aopclasspath");
326          cmd.createArgument().setValue(aopclasspath.toString());
327       }
328       logAndAddFilesToCompile(cmd);
329       
330       try
331       {
332          // Create an instance of the compiler, redirecting output to
333
// the project log
334
classpath.append(compilerClasspath);
335          Java java = (Java) (getProject().createTask("java"));
336          if (jvm != null && jvm.length() > 0)
337          {
338             java.setJvm(jvm);
339          }
340          if (getClasspath() != null)
341          {
342             getProject().log("using user supplied classpath: "
343                     + getClasspath(), Project.MSG_DEBUG);
344             java.setClasspath(getClasspath()
345                     .concatSystemClasspath("ignore"));
346          }
347          else
348          {
349             Path classpath = new Path(getProject());
350             classpath = classpath.concatSystemClasspath("only");
351             getProject().log("using system classpath: " + classpath,
352                     Project.MSG_DEBUG);
353             java.setClasspath(classpath);
354          }
355          java.setDir(getProject().getBaseDir());
356          java.setClassname("org.jboss.aop.standalone.Compiler");
357          //this is really irritating; we need a way to set stuff
358
String JavaDoc args[] = cmd.getJavaCommand().getArguments();
359          for (int i = 0; i < args.length; i++)
360          {
361             java.createArg().setValue(args[i]);
362          }
363          java.setFailonerror(getFailonerror());
364          //we are forking here to be sure that if JspC calls
365
//System.exit() it doesn't halt the build
366
java.setFork(true);
367          java.setTaskName("aopc");
368
369          Environment.Variable optimize = new Environment.Variable();
370          optimize.setKey(SYS_OPTIMIZED);
371          optimize.setValue(String.valueOf(optimized));
372          java.addSysproperty(optimize);
373
374          for (Iterator JavaDoc it = sysproperties.iterator() ; it.hasNext() ; )
375          {
376             Environment.Variable var = (Environment.Variable)it.next();
377             java.addSysproperty(var);
378          }
379          
380          if (maxmemory != null)
381          {
382             java.setMaxmemory(maxmemory);
383          }
384          java.execute();
385       }
386       catch (Exception JavaDoc ex)
387       {
388          if (ex instanceof BuildException)
389          {
390             throw (BuildException) ex;
391          }
392          else
393          {
394             throw new BuildException("Error running aopc compiler: ",
395                     ex, getLocation());
396          }
397       }
398    }
399
400    protected void logAndAddFilesToCompile(CommandlineJava cmd)
401    {
402       // scan source directories and dest directory to build up
403
// compile lists
404
String JavaDoc[] list = compileSourcepath.list();
405       ArrayList JavaDoc compilingFiles = new ArrayList JavaDoc();
406       for (int i = 0; i < list.length; i++)
407       {
408          File JavaDoc srcDir = getProject().resolveFile(list[i]);
409          if (!srcDir.exists())
410          {
411             throw new BuildException("srcdir \""
412                     + srcDir.getPath()
413                     + "\" does not exist!", getLocation());
414          }
415
416          DirectoryScanner ds = this.getDirectoryScanner(srcDir);
417          String JavaDoc[] files = ds.getIncludedFiles();
418          for (int j = 0; j < files.length; j++)
419          {
420             if (files[j].endsWith(".class"))
421             {
422                File JavaDoc f = new File JavaDoc(srcDir, files[j]);
423                compilingFiles.add(f.getAbsolutePath());
424             }
425          }
426       }
427
428       int length = 0;
429       for (int i = 0; i < compilingFiles.size(); i++)
430       {
431          //Add 1 to allow for the spaces between args
432
length += ((String JavaDoc) compilingFiles.get(i)).length() + 1;
433
434          if (length > maxSrc)
435          {
436             break;
437          }
438       }
439
440       if (length < maxSrc)
441       {
442          StringBuffer JavaDoc niceSourceList = new StringBuffer JavaDoc("Files\n");
443          for (int i = 0; i < compilingFiles.size(); i++)
444          {
445             String JavaDoc file = (String JavaDoc) compilingFiles.get(i);
446             cmd.createArgument().setValue(file);
447             niceSourceList.append(" " + file + lSep);
448          }
449
450          //getProject().log(niceSourceList.toString());
451
}
452       else
453       {
454          BufferedWriter JavaDoc writer = null;
455          try
456          {
457             File JavaDoc sourceFiles = File.createTempFile("src", ".tmp");
458             if (verbose)
459             {
460                System.out.println("[info] Total length of filenames to be compiled is greater than "
461                        + maxSrc + ", listing files in --SOURCEPATH: " + sourceFiles.getAbsolutePath());
462             }
463
464             sourceFiles.deleteOnExit();
465             writer = new BufferedWriter JavaDoc(new FileWriter JavaDoc(sourceFiles));
466
467             for (int i = 0; i < compilingFiles.size(); i++)
468             {
469                writer.write((String JavaDoc) compilingFiles.get(i));
470                writer.newLine();
471             }
472             writer.flush();
473             cmd.createArgument().setValue("--SOURCEPATH");
474             cmd.createArgument().setValue(sourceFiles.getAbsolutePath());
475          }
476          catch (IOException JavaDoc e)
477          {
478             if (writer != null)
479             {
480                try
481                {
482                   writer.close();
483                }
484                catch (IOException JavaDoc e1)
485                {
486                }
487             }
488             throw new RuntimeException JavaDoc(e);
489          }
490       }
491    }
492
493 }
Popular Tags