1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
package net.sf.infrared.tools.ant; |
23 |
|
|
24 |
|
import java.io.File; |
25 |
|
import java.io.IOException; |
26 |
|
import java.util.Iterator; |
27 |
|
import java.util.List; |
28 |
|
import java.util.ArrayList; |
29 |
|
import net.sf.infrared.tools.server.JbossIntegrator; |
30 |
|
import net.sf.infrared.tools.server.ServerIntegrator; |
31 |
|
import net.sf.infrared.tools.server.ServletIntegrator; |
32 |
|
import net.sf.infrared.tools.server.WeblogicIntegrator; |
33 |
|
import net.sf.infrared.tools.weaving.Aj2System; |
34 |
|
import net.sf.infrared.tools.weaving.Aj5System; |
35 |
|
import net.sf.infrared.tools.weaving.AspectSystem; |
36 |
|
import net.sf.infrared.tools.weaving.Aw2System; |
37 |
|
import org.apache.commons.io.FileUtils; |
38 |
|
import org.apache.tools.ant.BuildException; |
39 |
|
import org.apache.tools.ant.Project; |
40 |
|
import org.apache.tools.ant.taskdefs.MatchingTask; |
41 |
|
import org.apache.tools.ant.types.Path; |
42 |
|
import org.apache.tools.ant.types.Reference; |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
0 |
public class InfraRedTask extends MatchingTask { |
49 |
|
public static final String WEBLOGIC = "weblogic"; |
50 |
|
|
51 |
|
public static final String TOMCAT = "tomcat"; |
52 |
|
|
53 |
|
public static final String JBOSS = "jboss"; |
54 |
|
|
55 |
|
public static final String WORK_DIR = ".infrared-work"; |
56 |
|
|
57 |
|
private String serverName; |
58 |
|
|
59 |
|
private ServerIntegrator server; |
60 |
|
|
61 |
|
private String systemName; |
62 |
|
|
63 |
|
private AspectSystem system; |
64 |
|
|
65 |
0 |
private List ears = new ArrayList(); |
66 |
|
|
67 |
0 |
private List wars = new ArrayList(); |
68 |
|
|
69 |
|
private Path extracp; |
70 |
|
|
71 |
|
private Path aspects; |
72 |
|
|
73 |
|
private File workDir; |
74 |
|
|
75 |
0 |
private boolean offline = true; |
76 |
|
|
77 |
0 |
private String applicationName = "unknown-app"; |
78 |
|
|
79 |
0 |
private String portNumber = "0"; |
80 |
|
|
81 |
|
private File agentCfg; |
82 |
|
|
83 |
|
private Logger log ; |
84 |
|
|
85 |
|
public void setAgentConfiguration(File f) { |
86 |
0 |
agentCfg = f; |
87 |
0 |
} |
88 |
|
|
89 |
|
public void setApplicationName(String name) { |
90 |
0 |
this.applicationName = name; |
91 |
0 |
} |
92 |
|
|
93 |
|
public void setApplicationPort(String port) { |
94 |
0 |
this.portNumber = port; |
95 |
0 |
} |
96 |
|
|
97 |
|
public void setOffline(boolean off) { |
98 |
0 |
this.offline = off; |
99 |
|
|
100 |
0 |
getProject().log(this, "'offline' set to " + this.offline, Project.MSG_VERBOSE); |
101 |
0 |
} |
102 |
|
|
103 |
|
public void setServer(String s) { |
104 |
0 |
this.serverName = s; |
105 |
0 |
s = s.trim(); |
106 |
0 |
if (WEBLOGIC.equalsIgnoreCase(s)) { |
107 |
0 |
this.server = new WeblogicIntegrator(); |
108 |
0 |
} else if (TOMCAT.equalsIgnoreCase(s)) { |
109 |
0 |
this.server = new ServletIntegrator(); |
110 |
0 |
} else if (JBOSS.equalsIgnoreCase(s)) { |
111 |
0 |
this.server = new JbossIntegrator(); |
112 |
|
} else { |
113 |
0 |
throw new BuildException("Unknow server '" + serverName + "'; legal values of " + |
114 |
|
"this attribute are " + WEBLOGIC + ", " + TOMCAT + " and " + JBOSS); |
115 |
|
} |
116 |
|
|
117 |
0 |
getProject().log(this, "'server' set to " + this.serverName, Project.MSG_VERBOSE); |
118 |
0 |
} |
119 |
|
|
120 |
|
public void setSystem(String system) { |
121 |
0 |
this.systemName = system; |
122 |
|
|
123 |
0 |
system = system.trim(); |
124 |
0 |
if ( ("aspectwerkz".equals(system)) || ("aw".equals(system)) ) { |
125 |
0 |
this.system = new Aw2System(); |
126 |
0 |
} else if (("aspectj".equals(system)) || ("aj".equals(system))){ |
127 |
0 |
this.system = new Aj5System(); |
128 |
|
} |
129 |
|
else { |
130 |
0 |
throw new BuildException("Unknown aspect system '" + system + "'' legal values of " + |
131 |
|
"this attribute are aspectwerkz and aspectj"); |
132 |
|
} |
133 |
|
|
134 |
0 |
getProject().log(this, "'system' set to " + this.systemName, Project.MSG_VERBOSE); |
135 |
0 |
} |
136 |
|
|
137 |
|
public Path createExtraClasspath() { |
138 |
0 |
if (extracp == null) { |
139 |
0 |
extracp = new Path(getProject()); |
140 |
|
} |
141 |
0 |
return extracp.createPath(); |
142 |
|
} |
143 |
|
|
144 |
|
public void setExtraClasspathRef(Reference r) { |
145 |
0 |
createExtraClasspath().setRefid(r); |
146 |
0 |
} |
147 |
|
|
148 |
|
public Path createAspectPath() { |
149 |
0 |
if (aspects == null) { |
150 |
0 |
aspects = new Path(getProject()); |
151 |
|
} |
152 |
0 |
return aspects.createPath(); |
153 |
|
} |
154 |
|
|
155 |
|
public void setAspectPathRef(Reference r) { |
156 |
0 |
createAspectPath().setRefid(r); |
157 |
0 |
} |
158 |
|
|
159 |
|
public void addConfiguredEar(EarType application) { |
160 |
0 |
application.setParentTask(this); |
161 |
0 |
this.ears.add(application); |
162 |
0 |
} |
163 |
|
|
164 |
|
public void addConfiguredWar(WarType application) { |
165 |
0 |
application.setParentTask(this); |
166 |
0 |
this.wars.add(application); |
167 |
0 |
} |
168 |
|
|
169 |
|
public void execute() throws BuildException { |
170 |
0 |
log = new Logger(getProject(), this); |
171 |
0 |
this.server.setLogger(log); |
172 |
0 |
this.system.setLogger(log); |
173 |
|
|
174 |
0 |
validateInputs(); |
175 |
0 |
workDir = createCleanWorkDir(); |
176 |
0 |
if (server instanceof JbossIntegrator) { |
177 |
0 |
((JbossIntegrator) server).setApplicationName(applicationName); |
178 |
0 |
((JbossIntegrator) server).setApplicationPort(portNumber); |
179 |
0 |
((JbossIntegrator) server).setCfg(agentCfg); |
180 |
|
} |
181 |
|
try { |
182 |
0 |
setupApps(); |
183 |
0 |
if (offline) { |
184 |
0 |
instrumentApps(); |
185 |
|
} |
186 |
0 |
addServerHooksToApps(); |
187 |
0 |
copyAspectsToApps(); |
188 |
0 |
cleanup(); |
189 |
0 |
} catch (Throwable th) { |
190 |
0 |
if (th instanceof BuildException) { |
191 |
0 |
throw (BuildException) th; |
192 |
|
} else { |
193 |
0 |
throw new BuildException("Failed to integrate InfraRED", th); |
194 |
|
} |
195 |
|
} finally { |
196 |
0 |
if (! Boolean.getBoolean("infrared.dont.clean")) { |
197 |
0 |
deleteWorkDir(); |
198 |
|
} |
199 |
|
} |
200 |
0 |
} |
201 |
|
|
202 |
|
void validateInputs() { |
203 |
0 |
if ( (ears.size() + wars.size()) < 1 ) { |
204 |
0 |
getProject().log(this, "No application specified for instrumentation; " + |
205 |
|
"use the <war> or <ear> type to specify atleast one application", Project.MSG_VERBOSE); |
206 |
|
} |
207 |
|
|
208 |
0 |
if (serverName == null) { |
209 |
0 |
throw new BuildException("'server' attribute is not specified"); |
210 |
|
} |
211 |
|
|
212 |
0 |
if (systemName == null) { |
213 |
|
|
214 |
0 |
throw new BuildException("'system' attribute is not specified"); |
215 |
|
} |
216 |
0 |
if ( (offline == false) && (system instanceof Aj2System) ) { |
217 |
0 |
throw new BuildException("AspectJ2 system does not yet support online weaving; " + |
218 |
|
"set 'offline' to true if you want to use AspectJ2 system"); |
219 |
|
} |
220 |
0 |
if (! agentCfg.exists()) { |
221 |
0 |
throw new BuildException("Failed to locate agent properties file at " + |
222 |
|
agentCfg.getAbsolutePath()); |
223 |
|
} |
224 |
0 |
} |
225 |
|
|
226 |
|
void setupApps() { |
227 |
0 |
setupWars(workDir); |
228 |
0 |
setupEars(workDir); |
229 |
0 |
} |
230 |
|
|
231 |
|
void setupWars(File workDir) { |
232 |
0 |
for (Iterator i = wars.iterator(); i.hasNext(); ) { |
233 |
0 |
WarType war = (WarType) i.next(); |
234 |
0 |
war.setLogger(log); |
235 |
0 |
war.setWorkDir(workDir); |
236 |
0 |
war.setup(); |
237 |
|
} |
238 |
0 |
} |
239 |
|
|
240 |
|
void setupEars(File workDir) { |
241 |
0 |
for (Iterator i = ears.iterator(); i.hasNext(); ) { |
242 |
0 |
EarType ear = (EarType) i.next(); |
243 |
0 |
ear.setLogger(log); |
244 |
0 |
ear.setWorkDir(workDir); |
245 |
0 |
ear.setup(); |
246 |
|
} |
247 |
0 |
} |
248 |
|
|
249 |
|
void instrumentApps() { |
250 |
0 |
if (aspects != null) { |
251 |
0 |
system.addAspectPath(aspects.list()); |
252 |
|
} |
253 |
0 |
instrumentWars(); |
254 |
0 |
instrumentEars(); |
255 |
0 |
} |
256 |
|
|
257 |
|
void instrumentWars() { |
258 |
0 |
for (Iterator i = wars.iterator(); i.hasNext(); ) { |
259 |
0 |
system.resetClassPath(); |
260 |
0 |
WarType war = (WarType) i.next(); |
261 |
0 |
system.addClassPath(war.getApplicationClassPath()); |
262 |
0 |
if (extracp != null) { |
263 |
0 |
system.addClassPath(extracp.list()); |
264 |
|
} |
265 |
0 |
File[] jars = war.getJarsToInstrument(); |
266 |
0 |
File[] classFiles = war.getClassesToInstrument(); |
267 |
0 |
String[] pkgNames = war.getPackagesToInstrument(); |
268 |
|
|
269 |
0 |
getProject().log(this, "Instrumenting WAR " + war.getSrc(), Project.MSG_VERBOSE); |
270 |
0 |
getProject().log(this, "Jars:-", Project.MSG_VERBOSE); |
271 |
0 |
for (int j = 0; j < jars.length; j++) { |
272 |
0 |
getProject().log(this, "\t" + jars[j].getAbsolutePath(), Project.MSG_VERBOSE); |
273 |
|
} |
274 |
0 |
getProject().log(this, "Classes:-", Project.MSG_VERBOSE); |
275 |
0 |
for (int j = 0; j < classFiles.length; j++) { |
276 |
0 |
getProject().log("\t" + classFiles[j].getAbsolutePath() + |
277 |
|
" in package " + pkgNames[j], Project.MSG_VERBOSE); |
278 |
|
} |
279 |
0 |
getProject().log(this, "" + war.getSrc(), Project.MSG_VERBOSE); |
280 |
0 |
system.instrumentJars(jars); |
281 |
0 |
system.instrumentClasses(classFiles, pkgNames); |
282 |
|
} |
283 |
0 |
} |
284 |
|
|
285 |
|
void instrumentEars() { |
286 |
0 |
for (Iterator i = ears.iterator(); i.hasNext(); ) { |
287 |
0 |
system.resetClassPath(); |
288 |
0 |
EarType ear = (EarType) i.next(); |
289 |
0 |
system.addClassPath(ear.getApplicationClassPath()); |
290 |
0 |
if (extracp != null) { |
291 |
0 |
system.addClassPath(extracp.list()); |
292 |
|
} |
293 |
0 |
File[] jars = ear.getJarsToInstrument(); |
294 |
|
|
295 |
0 |
File[] classFiles = ear.getClassesToInstrument(); |
296 |
0 |
String[] pkgNames = ear.getPackagesToInstrument(); |
297 |
|
|
298 |
0 |
getProject().log(this, "Instrumenting EAR " + ear.getSrc(), Project.MSG_VERBOSE); |
299 |
0 |
getProject().log(this, "Jars:-", Project.MSG_VERBOSE); |
300 |
0 |
for (int j = 0; j < jars.length; j++) { |
301 |
0 |
getProject().log(this, "\t" + jars[j].getAbsolutePath(), Project.MSG_VERBOSE); |
302 |
|
} |
303 |
0 |
getProject().log(this, "Classes:-", Project.MSG_VERBOSE); |
304 |
0 |
for (int j = 0; j < classFiles.length; j++) { |
305 |
0 |
getProject().log(this, "\t" + classFiles[j].getAbsolutePath() + |
306 |
|
" in package " + pkgNames[j], Project.MSG_VERBOSE); |
307 |
|
} |
308 |
0 |
getProject().log(this, "", Project.MSG_VERBOSE); |
309 |
|
|
310 |
0 |
system.instrumentJars(jars); |
311 |
0 |
system.instrumentClasses(classFiles, pkgNames); |
312 |
|
} |
313 |
0 |
} |
314 |
|
|
315 |
|
void copyAspectsToApps() { |
316 |
0 |
copyAspectsToWars(); |
317 |
0 |
copyAspectsToEars(); |
318 |
0 |
} |
319 |
|
|
320 |
|
void copyAspectsToWars() { |
321 |
0 |
File[] cfg = new File[] {agentCfg}; |
322 |
0 |
for (Iterator i = wars.iterator(); i.hasNext();) { |
323 |
0 |
WarType war = (WarType) i.next(); |
324 |
|
try { |
325 |
0 |
server.copyIntoWar(covertPathsToFile(aspects.list()), war.getBackupDir()); |
326 |
0 |
server.copyIntoWar(cfg, war.getBackupDir()); |
327 |
0 |
} catch (Exception ex) { |
328 |
0 |
throw new BuildException("Failed to copy aspects into war " + |
329 |
|
war.getSrc().getAbsolutePath(), ex); |
330 |
0 |
} |
331 |
|
} |
332 |
0 |
} |
333 |
|
|
334 |
|
void copyAspectsToEars() { |
335 |
0 |
File[] cfg = new File[] {agentCfg}; |
336 |
0 |
for (Iterator i = ears.iterator(); i.hasNext();) { |
337 |
0 |
EarType ear = (EarType) i.next(); |
338 |
|
try { |
339 |
0 |
String cp = server.copyIntoEar(covertPathsToFile(aspects.list()), ear.getBackupDir()); |
340 |
0 |
ear.appendToManifestClassPath(cp); |
341 |
|
|
342 |
0 |
if(!(server instanceof JbossIntegrator)){ |
343 |
0 |
server.copyIntoEar(cfg, ear.getBackupDir()); |
344 |
|
} |
345 |
0 |
} catch (Exception ex) { |
346 |
0 |
throw new BuildException("Failed to copy aspects into ear " + |
347 |
|
ear.getSrc().getAbsolutePath(), ex); |
348 |
0 |
} |
349 |
|
} |
350 |
0 |
} |
351 |
|
|
352 |
|
void cleanup() { |
353 |
0 |
cleanupWars(); |
354 |
0 |
cleanupEars(); |
355 |
0 |
} |
356 |
|
|
357 |
|
File createCleanWorkDir() { |
358 |
0 |
workDir = new File(System.getProperty("user.home") + File.separator |
359 |
|
+ WORK_DIR + System.currentTimeMillis()); |
360 |
|
try { |
361 |
0 |
FileUtils.forceMkdir(workDir); |
362 |
0 |
} catch (IOException ioex) { |
363 |
0 |
throw new BuildException("Failed to create working directory " |
364 |
|
+ workDir.getAbsolutePath(), ioex); |
365 |
0 |
} |
366 |
|
|
367 |
|
try { |
368 |
0 |
FileUtils.cleanDirectory(workDir); |
369 |
0 |
} catch (IOException ioex) { |
370 |
0 |
throw new BuildException("Failed to clean working directory " |
371 |
|
+ workDir.getAbsolutePath(), ioex); |
372 |
0 |
} |
373 |
|
|
374 |
0 |
return workDir; |
375 |
|
} |
376 |
|
|
377 |
|
void deleteWorkDir() { |
378 |
|
try { |
379 |
0 |
FileUtils.forceDelete(workDir); |
380 |
0 |
} catch (IOException ex) { |
381 |
0 |
getProject().log(this, "Failed to cleanup working directory " + |
382 |
|
workDir.getAbsolutePath(), Project.MSG_WARN); |
383 |
0 |
} |
384 |
0 |
} |
385 |
|
|
386 |
|
void cleanupWars() { |
387 |
0 |
for (Iterator it = wars.iterator(); it.hasNext();) { |
388 |
0 |
WarType war = (WarType) it.next(); |
389 |
0 |
war.cleanup(); |
390 |
|
} |
391 |
0 |
} |
392 |
|
|
393 |
|
void cleanupEars() { |
394 |
0 |
for (Iterator it = ears.iterator(); it.hasNext();) { |
395 |
0 |
EarType ear = (EarType) it.next(); |
396 |
0 |
ear.cleanup(); |
397 |
|
} |
398 |
0 |
} |
399 |
|
|
400 |
|
void addServerHooksToApps() { |
401 |
0 |
addServerHooksToWars(); |
402 |
0 |
addServerHooksToEars(); |
403 |
0 |
} |
404 |
|
|
405 |
|
void addServerHooksToWars() { |
406 |
0 |
for (Iterator i = wars.iterator(); i.hasNext();) { |
407 |
0 |
WarType war = (WarType) i.next(); |
408 |
|
try { |
409 |
0 |
server.integrateWar(war.getBackupDir()); |
410 |
0 |
} catch (Exception ex) { |
411 |
0 |
throw new BuildException("Failed to integrate war " + |
412 |
|
war.getSrc().getAbsolutePath() + " for server " + serverName + |
413 |
|
". Cause is: " + ex.getMessage(), ex); |
414 |
0 |
} |
415 |
|
} |
416 |
0 |
} |
417 |
|
|
418 |
|
void addServerHooksToEars() { |
419 |
0 |
for (Iterator i = ears.iterator(); i.hasNext();) { |
420 |
0 |
EarType ear = (EarType) i.next(); |
421 |
|
try { |
422 |
0 |
server.integrateEar(ear.getBackupDir()); |
423 |
0 |
} catch (Exception ex) { |
424 |
0 |
throw new BuildException("Failed to integrate ear " + |
425 |
|
ear.getSrc().getAbsolutePath() + " for server " + serverName, ex); |
426 |
0 |
} |
427 |
|
} |
428 |
0 |
} |
429 |
|
|
430 |
|
File[] covertPathsToFile(String[] paths) { |
431 |
0 |
File[] files = new File[paths.length]; |
432 |
0 |
for (int i = 0; i < paths.length; i++) { |
433 |
0 |
files[i] = new File(paths[i]); |
434 |
|
} |
435 |
0 |
return files; |
436 |
|
} |
437 |
|
} |