1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
package net.sf.infrared.tools.weaving; |
24 |
|
|
25 |
|
import java.io.BufferedWriter; |
26 |
|
import java.io.File; |
27 |
|
import java.io.FileWriter; |
28 |
|
import java.io.IOException; |
29 |
|
import java.net.URL; |
30 |
|
import java.util.ArrayList; |
31 |
|
import java.util.Iterator; |
32 |
|
import java.util.List; |
33 |
|
|
34 |
|
import net.sf.infrared.tools.util.JarHelper; |
35 |
|
|
36 |
|
import org.apache.commons.io.FileUtils; |
37 |
|
import org.aspectj.tools.ajc.Main; |
38 |
|
|
39 |
0 |
public class Aj5System extends AbstractAspectSystem { |
40 |
0 |
private boolean loggedOnce = false; |
41 |
|
|
42 |
|
public void instrumentJars(File[] jars) { |
43 |
0 |
if (! loggedOnce) { |
44 |
0 |
getLogger().verbose("[Aj5] Setup: " + |
45 |
|
"\n\t AspectPath = " + getAspectPathAsString() + |
46 |
|
"\n\t ClassPath = " + getClassPathAsString()); |
47 |
0 |
loggedOnce = true; |
48 |
|
} |
49 |
0 |
checkIfXlintPropertiesExist(); |
50 |
0 |
for (int i = 0; i < jars.length; i++) { |
51 |
0 |
instrumentJar(jars[i]); |
52 |
|
} |
53 |
0 |
} |
54 |
|
|
55 |
|
public void instrumentJar(File jar) { |
56 |
0 |
File temp = null; |
57 |
|
try { |
58 |
0 |
temp = createTempCopy(jar); |
59 |
0 |
} catch (IOException ex) { |
60 |
0 |
throw new RuntimeException("Failed to copy jar file " + jar.getAbsolutePath() |
61 |
|
+ " to temp file for instrumentation", ex); |
62 |
0 |
} |
63 |
|
|
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
0 |
String[] args = new String[10]; |
72 |
0 |
args[0] = "-injars"; |
73 |
0 |
args[1] = temp.getAbsolutePath(); |
74 |
0 |
args[2] = "-outjar"; |
75 |
0 |
args[3] = jar.getAbsolutePath(); |
76 |
0 |
args[4] = "-aspectpath"; |
77 |
0 |
args[5] = getAspectPathAsString(); |
78 |
0 |
args[6] = "-cp"; |
79 |
0 |
args[7] = getClassPathAsString(); |
80 |
0 |
args[8] = "-Xlintfile"; |
81 |
0 |
args[9] = System.getProperty("java.io.tmpdir") + File.separator + |
82 |
|
"META-INF" + File.separator + "Xlint.properties"; |
83 |
|
|
84 |
|
|
85 |
0 |
List fails = new ArrayList(); |
86 |
0 |
List errors = new ArrayList(); |
87 |
0 |
List warnings = new ArrayList(); |
88 |
0 |
List infos = new ArrayList(); |
89 |
0 |
Main.bareMain(args, false, fails, errors, warnings, infos); |
90 |
|
|
91 |
0 |
if (errors.size() != 0) { |
92 |
0 |
String msg = "\n"; |
93 |
0 |
for (Iterator it = errors.iterator(); it.hasNext();) { |
94 |
0 |
String elem = it.next().toString(); |
95 |
0 |
msg += elem + "\n"; |
96 |
|
} |
97 |
0 |
throw new RuntimeException("Error instrumenting jar file " + |
98 |
|
jar.getAbsolutePath() + " " + msg); |
99 |
|
} |
100 |
0 |
getLogger().verbose("[Aj5] Instrumented jar " + jar.getAbsolutePath()); |
101 |
0 |
} |
102 |
|
|
103 |
|
public void instrumentClasses(File[] classes, String[] packages) { |
104 |
0 |
if (! loggedOnce) { |
105 |
0 |
getLogger().verbose("[Aj5] Setup: " + |
106 |
|
"\n\t AspectPath = " + getAspectPathAsString() + |
107 |
|
"\n\t ClassPath = " + getClassPathAsString()); |
108 |
0 |
loggedOnce = true; |
109 |
|
} |
110 |
0 |
for (int i = 0; i < classes.length; i++) { |
111 |
0 |
instrumentClass(classes[i], packages[i]); |
112 |
|
} |
113 |
0 |
} |
114 |
|
|
115 |
|
public void instrumentClass(File classFile, String pkg) { |
116 |
|
File tempClassDir; |
117 |
|
try { |
118 |
0 |
tempClassDir = copyClassToTempDir(classFile, pkg); |
119 |
0 |
} catch (IOException ex) { |
120 |
0 |
throw new RuntimeException("Failed to create working copy of class " + |
121 |
|
classFile.getAbsoluteFile() + " in package " + pkg); |
122 |
0 |
} |
123 |
|
|
124 |
0 |
File outputDir = null; |
125 |
|
try { |
126 |
0 |
outputDir = createTempDir(); |
127 |
0 |
} catch (IOException ex) { |
128 |
0 |
throw new RuntimeException("Failed to create directory to hold the aspect " + |
129 |
|
"class temporarily", ex); |
130 |
0 |
} |
131 |
|
|
132 |
0 |
String[] args = new String[10]; |
133 |
0 |
args[0] = "-inpath"; |
134 |
0 |
args[1] = tempClassDir.getAbsolutePath(); |
135 |
0 |
args[2] = "-d"; |
136 |
0 |
args[3] = outputDir.getAbsolutePath(); |
137 |
0 |
args[4] = "-aspectpath"; |
138 |
0 |
args[5] = getAspectPathAsString(); |
139 |
0 |
args[6] = "-cp"; |
140 |
0 |
args[7] = getClassPathAsString(); |
141 |
0 |
args[8] = "-verbose"; |
142 |
0 |
args[9] = "-Xlint:warning"; |
143 |
|
|
144 |
0 |
List fails = new ArrayList(); |
145 |
0 |
List errors = new ArrayList(); |
146 |
0 |
List warnings = new ArrayList(); |
147 |
0 |
List infos = new ArrayList(); |
148 |
0 |
Main.bareMain(args, false, fails, errors, warnings, infos); |
149 |
|
|
150 |
0 |
if (errors.size() != 0) { |
151 |
0 |
String msg = "\n"; |
152 |
0 |
for (Iterator it = errors.iterator(); it.hasNext();) { |
153 |
0 |
String elem = it.next().toString(); |
154 |
0 |
msg += elem + "\n"; |
155 |
|
} |
156 |
0 |
throw new RuntimeException("Error instrumenting class file " + |
157 |
|
classFile.getAbsolutePath() + " " + msg); |
158 |
|
} |
159 |
|
|
160 |
0 |
File aspectedClassFile = |
161 |
|
new File(outputDir, convertPackageToPath(pkg) + File.separator + classFile.getName()); |
162 |
|
try { |
163 |
0 |
FileUtils.copyFile(aspectedClassFile, classFile); |
164 |
0 |
} catch (IOException ex) { |
165 |
0 |
throw new RuntimeException("Failed to copy aspected class" + |
166 |
|
aspectedClassFile.getAbsolutePath() + " over the original class " + |
167 |
|
classFile.getAbsolutePath()); |
168 |
0 |
} |
169 |
0 |
getLogger().verbose("[Aj5] Instrumented class " + classFile.getAbsolutePath()); |
170 |
0 |
} |
171 |
|
|
172 |
|
public void addAspectPath(File[] aspects) { |
173 |
0 |
for (int i = 0; i < aspects.length; i++) { |
174 |
0 |
if (aspects[i].isFile()) { |
175 |
0 |
continue; |
176 |
|
} |
177 |
|
try { |
178 |
0 |
File dir = aspects[i]; |
179 |
0 |
aspects[i] = createTempArchive(dir); |
180 |
0 |
getLogger().verbose("[Aj5] Archived " + dir.getAbsolutePath() + " to " + |
181 |
|
aspects[i] + " temporarily to pass into AspectJ as aspectpath"); |
182 |
0 |
} catch (IOException ex) { |
183 |
0 |
throw new RuntimeException("Failed to create temporary archive of directory " + |
184 |
|
aspects[i].getAbsolutePath() + " in aspect path.", ex); |
185 |
0 |
} |
186 |
|
} |
187 |
0 |
super.addAspectPath(aspects); |
188 |
0 |
} |
189 |
|
|
190 |
|
String getClassPathAsString() { |
191 |
0 |
String cp = super.getClassPathAsString(); |
192 |
0 |
URL u = getClass().getClassLoader().getResource("aspectjrt.jar"); |
193 |
0 |
cp += new File(u.getFile()).getAbsolutePath(); |
194 |
0 |
return cp; |
195 |
|
} |
196 |
|
|
197 |
|
|
198 |
|
private void checkIfXlintPropertiesExist() { |
199 |
0 |
String DEFAULT_XLINT_PATH = System.getProperty("java.io.tmpdir") + File.separator + "META-INF"; |
200 |
|
try { |
201 |
0 |
if( !new File(DEFAULT_XLINT_PATH).exists()) |
202 |
0 |
new File(System.getProperty("java.io.tmpdir"), "META-INF").mkdir(); |
203 |
|
|
204 |
|
|
205 |
|
|
206 |
0 |
if( !new File(DEFAULT_XLINT_PATH, "Xlint.properties").exists()) { |
207 |
0 |
File tempFile = (new File(DEFAULT_XLINT_PATH, "Xlint.properties")); |
208 |
0 |
BufferedWriter out = new BufferedWriter(class="keyword">new FileWriter(tempFile)); |
209 |
0 |
out.write("cantFindType = ignore"); |
210 |
0 |
out.close(); |
211 |
|
} |
212 |
0 |
} catch (IOException e) { |
213 |
0 |
e.printStackTrace(); |
214 |
0 |
getLogger().verbose("Unable to create the META-INF/Xlint.properties file"); |
215 |
0 |
} |
216 |
0 |
} |
217 |
|
|
218 |
|
|
219 |
|
File createTempArchive(File dir) throws IOException { |
220 |
0 |
File temp = createTempFile(dir.getName() + "-archive", ".jar"); |
221 |
0 |
new JarHelper().jarDir(dir, temp); |
222 |
0 |
temp.deleteOnExit(); |
223 |
0 |
return temp; |
224 |
|
} |
225 |
|
|
226 |
|
File createTempCopy(File orig) throws IOException { |
227 |
0 |
File temp = createTempFile(orig.getName(), ".jar"); |
228 |
0 |
FileUtils.copyFile(orig, temp); |
229 |
0 |
temp.deleteOnExit(); |
230 |
0 |
return temp; |
231 |
|
} |
232 |
|
|
233 |
|
File copyClassToTempDir(File clazz, String pkg) throws IOException { |
234 |
0 |
String p = convertPackageToPath(pkg); |
235 |
|
|
236 |
|
|
237 |
0 |
int i = (p.length() == 0) ? clazz.getAbsolutePath().lastIndexOf( |
238 |
|
File.separator) + 1 : clazz.getAbsolutePath().lastIndexOf(p); |
239 |
|
|
240 |
0 |
if (i == -1) { |
241 |
0 |
throw new RuntimeException("Failed to figure out the parent directory of class " + |
242 |
|
clazz.getAbsolutePath() + " in package " + pkg); |
243 |
|
} |
244 |
0 |
File tempDir = createTempDir(); |
245 |
0 |
File copy = new File(tempDir, clazz.getAbsolutePath().substring(i)); |
246 |
0 |
FileUtils.forceMkdir(copy.getParentFile()); |
247 |
0 |
FileUtils.copyFile(clazz, copy); |
248 |
0 |
copy.deleteOnExit(); |
249 |
0 |
return tempDir; |
250 |
|
} |
251 |
|
|
252 |
|
File createTempDir() throws IOException { |
253 |
0 |
File tempDir = null; |
254 |
|
|
255 |
0 |
for (int i = 0; true; i++) { |
256 |
0 |
tempDir = new File(System.getProperty("java.io.tmpdir"), "infrared-temp-" + i); |
257 |
0 |
if (! tempDir.exists()) { |
258 |
0 |
break; |
259 |
|
} |
260 |
|
} |
261 |
0 |
FileUtils.forceMkdir(tempDir); |
262 |
0 |
FileUtils.forceDeleteOnExit(tempDir); |
263 |
0 |
return tempDir; |
264 |
|
} |
265 |
|
|
266 |
|
File createTempFile(String name, String extn) throws IOException { |
267 |
0 |
File tempFile = null; |
268 |
|
|
269 |
0 |
for (int i = 0; true; i++) { |
270 |
0 |
tempFile = new File(System.getProperty("java.io.tmpdir"), name + "-" + i + extn); |
271 |
0 |
if (! tempFile.exists()) { |
272 |
0 |
break; |
273 |
|
} |
274 |
|
} |
275 |
|
|
276 |
0 |
FileUtils.forceDeleteOnExit(tempFile); |
277 |
0 |
return tempFile; |
278 |
|
} |
279 |
|
|
280 |
|
String convertPackageToPath(String pkg) { |
281 |
0 |
String p = pkg; |
282 |
0 |
if ("\\".equals(File.separator)) { |
283 |
0 |
p = pkg.replaceAll("\\.", "\\\\"); |
284 |
|
} else { |
285 |
0 |
p = pkg.replaceAll("\\.", File.separator); |
286 |
|
} |
287 |
0 |
return p; |
288 |
|
} |
289 |
|
|
290 |
|
} |