View Javadoc

1   /***************************************************************************************
2    * Copyright (c) Jonas Bonér, Alexandre Vasseur. All rights reserved.                 *
3    * http://aspectwerkz.codehaus.org                                                    *
4    * ---------------------------------------------------------------------------------- *
5    * The software in this package is published under the terms of the LGPL license      *
6    * a copy of which has been included with this distribution in the license.txt file.  *
7    **************************************************************************************/
8   package org.codehaus.aspectwerkz.task;
9   
10  import org.apache.tools.ant.BuildException;
11  import org.apache.tools.ant.Task;
12  
13  import java.io.BufferedReader;
14  import java.io.File;
15  import java.io.InputStreamReader;
16  
17  /***
18   * <code>OfflineTransformationTask</code> is an Ant Task that transforms the a class directory structure recursivly
19   * using the AspectWerkz -offline mode.
20   * 
21   * @author <a href="mailto:jboner@codehaus.org">Jonas Bonér </a>
22   */
23  public class OfflineTransformationTask extends Task {
24      /***
25       * The home of the aspectwerkz distribution.
26       */
27      private String m_aspectWerkzHome;
28  
29      /***
30       * The path to the classes to transform.
31       */
32      private String m_classesToTransform;
33  
34      /***
35       * The path to the XML definition file.
36       */
37      private String m_definitionFile;
38  
39      /***
40       * The class path.
41       */
42      private String m_classPath;
43  
44      /***
45       * Sets the aspectwerkz home dir.
46       * 
47       * @param aspectWerkzHome the aspectwerkz home dir
48       */
49      public void setAspectWerkzHome(final String aspectWerkzHome) {
50          m_aspectWerkzHome = aspectWerkzHome;
51      }
52  
53      /***
54       * Sets the path to the classes to transform.
55       * 
56       * @param classesToTransform the path to the classes
57       */
58      public void setClassesToTransform(final String classesToTransform) {
59          m_classesToTransform = classesToTransform;
60      }
61  
62      /***
63       * Sets the path to the XML definition file.
64       * 
65       * @param definitionFile the path to the XML definition file
66       */
67      public void setDefinitionFile(final String definitionFile) {
68          m_definitionFile = definitionFile;
69      }
70  
71      /***
72       * The path to the meta-data dir.
73       * 
74       * @param classPath the path to the meta-data dir
75       */
76      public void setClassPath(final String classPath) {
77          m_classPath = classPath;
78      }
79  
80      /***
81       * Executes the task.
82       * 
83       * @throws org.apache.tools.ant.BuildException
84       */
85      public void execute() throws BuildException {
86          if (m_aspectWerkzHome == null) {
87              throw new BuildException("AspectWerkz home dir must be specified");
88          }
89          if (m_classesToTransform == null) {
90              throw new BuildException("classes to transform must be specified");
91          }
92          if (m_definitionFile == null) {
93              throw new BuildException("definition file must be specified");
94          }
95          System.out
96                  .println("CAUTION: This Ant task might be a bit shaky, does not show errors in compilation process properly (use at own risk or patch it :-))");
97          System.out
98                  .println("NOTE: Make shure that you don't transform your classes more than once (without recompiling first)");
99          StringBuffer command = new StringBuffer();
100         command.append(m_aspectWerkzHome);
101         command.append(File.separator);
102         command.append("bin");
103         command.append(File.separator);
104         command.append("aspectwerkz");
105         if (System.getProperty("os.name").startsWith("Win") || System.getProperty("os.name").startsWith("win")) {
106             command.append(".bat");
107         }
108         command.append(" -offline ");
109         command.append(m_definitionFile);
110         command.append(' ');
111         if (m_classPath != null) {
112             command.append(m_classPath);
113         }
114         command.append(' ');
115         command.append(m_classesToTransform);
116         try {
117             Process p = Runtime.getRuntime().exec(
118                 command.toString(),
119                 new String[] {
120                     "ASPECTWERKZ_HOME=" + m_aspectWerkzHome,
121                     "JAVA_HOME=" + System.getProperty("java.home"),
122                     "CLASSPATH=" + System.getProperty("java.class.path")
123                 });
124             System.out.flush();
125             BufferedReader stdOut = new BufferedReader(new InputStreamReader(p.getInputStream()));
126             BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
127             String out;
128             String err = null;
129             while (((out = stdOut.readLine()) != null) || ((err = stdErr.readLine()) != null)) {
130                 if (out != null) {
131                     System.out.println(out);
132                     System.out.flush();
133                 }
134                 if (err != null) {
135                     System.err.println("Error: " + err);
136                 }
137             }
138             p.waitFor();
139             if (p.exitValue() != 0) {
140                 throw new BuildException("Failed to transform classes, exit code: " + p.exitValue());
141             }
142         } catch (Throwable e) {
143             throw new BuildException("could not transform the classes due to: " + e);
144         }
145     }
146 }