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.ArrayList; |
27 |
|
import java.util.Collections; |
28 |
|
import java.util.Iterator; |
29 |
|
import java.util.List; |
30 |
|
import org.apache.commons.io.FileUtils; |
31 |
|
import org.apache.commons.io.filefilter.SuffixFileFilter; |
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.types.PatternSet; |
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
public class WarType extends ApplicationType { |
42 |
0 |
private static String WEB_INF_CLASSES = "WEB-INF" + File.separator + "classes"; |
43 |
|
|
44 |
0 |
private static String WEB_INF_LIB = "WEB-INF" + File.separator + "lib"; |
45 |
|
|
46 |
0 |
private PatternSet patterns = new PatternSet(); |
47 |
|
|
48 |
|
private Project project; |
49 |
|
|
50 |
|
private DirectoryScanner ds; |
51 |
|
|
52 |
0 |
private List jarsToInstrument = new ArrayList(); |
53 |
|
|
54 |
0 |
private List classesToInstrument = new ArrayList(); |
55 |
|
|
56 |
0 |
private List packagesToInstrument = new ArrayList(); |
57 |
|
|
58 |
|
private String name; |
59 |
|
|
60 |
|
public WarType(Project project) { |
61 |
0 |
super(project); |
62 |
0 |
} |
63 |
|
|
64 |
|
public PatternSet.NameEntry createInclude() { |
65 |
0 |
return patterns.createInclude(); |
66 |
|
} |
67 |
|
|
68 |
|
public void setIncludes(String includes) { |
69 |
0 |
patterns.setIncludes(includes); |
70 |
0 |
} |
71 |
|
|
72 |
|
public PatternSet.NameEntry createExclude() { |
73 |
0 |
return patterns.createExclude(); |
74 |
|
} |
75 |
|
|
76 |
|
public void setExcludes(String excludes) { |
77 |
0 |
patterns.setExcludes(excludes); |
78 |
0 |
} |
79 |
|
|
80 |
|
public void setName(String name) { |
81 |
0 |
this.name = name; |
82 |
0 |
} |
83 |
|
|
84 |
|
public String getName() { |
85 |
0 |
return this.name; |
86 |
|
} |
87 |
|
|
88 |
|
public void setup() { |
89 |
0 |
verifyInputs(); |
90 |
0 |
if (getSrc() != null) { |
91 |
0 |
backupSrcToWorkDir(); |
92 |
0 |
getLogger().verbose("[WAR:"+ getSrc().getName() + "] Backed up " + getSrc().getAbsolutePath() + |
93 |
|
" to " + getBackupDir().getAbsolutePath()); |
94 |
|
} |
95 |
0 |
findClassesAndJarsToInstrument(); |
96 |
0 |
} |
97 |
|
|
98 |
|
public void cleanup() { |
99 |
0 |
if (getSrc() != null) { |
100 |
0 |
copyBackupToDest(); |
101 |
0 |
getLogger().verbose("[WAR:"+ getSrc().getName() + "] Copied backup " + |
102 |
|
getBackupDir().getAbsolutePath() + " to destination " + getDest().getAbsolutePath()); |
103 |
|
} |
104 |
0 |
} |
105 |
|
|
106 |
|
public File[] getApplicationClassPath() { |
107 |
0 |
if (ds == null) { |
108 |
0 |
setupDirectoryScanner(); |
109 |
|
} |
110 |
|
|
111 |
0 |
File base = ds.getBasedir(); |
112 |
0 |
File web_inf_classes = new File(base, WEB_INF_CLASSES); |
113 |
0 |
File web_inf_lib = new File(base, WEB_INF_LIB); |
114 |
0 |
String[] jars = web_inf_lib.list( new SuffixFileFilter(".jar") ); |
115 |
|
|
116 |
0 |
String cpString = ""; |
117 |
|
|
118 |
0 |
int numberOfJars = (jars == null) ? 0 : jars.length; |
119 |
0 |
File[] cp = new File[numberOfJars + 1]; |
120 |
0 |
for (int i = 0; i < numberOfJars; i++) { |
121 |
0 |
cp[i] = new File(web_inf_lib, jars[i]); |
122 |
0 |
cpString += cp[i].getAbsolutePath() + ", "; |
123 |
|
} |
124 |
0 |
cp[cp.length - 1] = web_inf_classes; |
125 |
0 |
cpString += web_inf_classes.getAbsolutePath(); |
126 |
0 |
getLogger().verbose("[WAR:"+ getSrc().getName() + "] Application ClassPath is[" + cpString + "]"); |
127 |
0 |
return cp; |
128 |
|
} |
129 |
|
|
130 |
|
public File[] getJarsToInstrument() { |
131 |
0 |
return (File[]) jarsToInstrument.toArray(new File[0]); |
132 |
|
} |
133 |
|
|
134 |
|
public File[] getClassesToInstrument() { |
135 |
0 |
return (File[]) classesToInstrument.toArray(new File[0]); |
136 |
|
} |
137 |
|
|
138 |
|
public String[] getPackagesToInstrument() { |
139 |
0 |
return (String[]) packagesToInstrument.toArray(new String[0]); |
140 |
|
} |
141 |
|
|
142 |
|
public void appendToManifestClassPath(String cp) { |
143 |
0 |
if ((cp == null) || (cp.trim().length() == 0)) { |
144 |
0 |
return; |
145 |
|
} |
146 |
0 |
File manifestDotMf = ensureManifestDotMfExists(); |
147 |
|
|
148 |
0 |
String encoding = ""; |
149 |
0 |
List manifestLines = Collections.EMPTY_LIST; |
150 |
|
try { |
151 |
0 |
manifestLines = FileUtils.readLines(manifestDotMf, encoding); |
152 |
0 |
} catch (IOException ex) { |
153 |
0 |
throw new RuntimeException("Failed to read MANIFEST.MF from " + |
154 |
|
manifestDotMf.getAbsolutePath(), ex); |
155 |
0 |
} |
156 |
0 |
for (Iterator it = manifestLines.iterator(); it.hasNext();) { |
157 |
0 |
String line = (String) it.next(); |
158 |
0 |
if (line.startsWith("ClassPath:")) { |
159 |
0 |
line += " " + cp; |
160 |
|
} |
161 |
|
} |
162 |
0 |
manifestLines.add("ClassPath:" + cp); |
163 |
|
try { |
164 |
0 |
FileUtils.writeLines(manifestDotMf, encoding, manifestLines); |
165 |
0 |
} catch (IOException ex) { |
166 |
0 |
throw new RuntimeException("Failed to edit MANIFEST.MF at " + |
167 |
|
manifestDotMf.getAbsolutePath(), ex); |
168 |
0 |
} |
169 |
0 |
getLogger().verbose("[WAR:"+ getSrc().getName() + "] Added " + cp + " to MANIFEST classpath"); |
170 |
0 |
} |
171 |
|
|
172 |
|
File ensureManifestDotMfExists() { |
173 |
0 |
File metaInfDir = new File(getBackupDir(), "META-INF"); |
174 |
0 |
File manifestDotMf = new File(metaInfDir, "MANIFEST.MF"); |
175 |
|
|
176 |
0 |
if (! metaInfDir.exists()) { |
177 |
|
try { |
178 |
0 |
FileUtils.forceMkdir(metaInfDir); |
179 |
0 |
} catch (IOException ex) { |
180 |
0 |
throw new RuntimeException("Failed to create " + metaInfDir.getAbsolutePath(), ex); |
181 |
0 |
} |
182 |
|
} |
183 |
|
|
184 |
0 |
if (! manclass="keyword">ifestDotMf.exists()) { |
185 |
|
try { |
186 |
0 |
FileUtils.touch(manifestDotMf); |
187 |
0 |
} catch (IOException ex) { |
188 |
0 |
throw new RuntimeException("Failed to create manifest file " + |
189 |
|
manifestDotMf.getAbsolutePath(), ex); |
190 |
0 |
} |
191 |
|
} |
192 |
0 |
return manifestDotMf; |
193 |
|
} |
194 |
|
|
195 |
|
void findClassesAndJarsToInstrument() { |
196 |
0 |
if (ds == null) { |
197 |
0 |
setupDirectoryScanner(); |
198 |
|
} |
199 |
0 |
File base = ds.getBasedir(); |
200 |
0 |
String[] includes = ds.getIncludedFiles(); |
201 |
0 |
for (int i = 0; i < includes.length; i++) { |
202 |
0 |
if (includes[i].startsWith( WEB_INF_CLASSES )) { |
203 |
0 |
File classFile = new File(base, includes[i]); |
204 |
0 |
String pkg = getPackageName(includes[i]); |
205 |
0 |
if (classFile.getName().endsWith(".class")) { |
206 |
0 |
classesToInstrument.add(classFile); |
207 |
0 |
packagesToInstrument.add(pkg); |
208 |
|
} |
209 |
|
} |
210 |
0 |
if (includes[i].startsWith( WEB_INF_LIB )) { |
211 |
0 |
jarsToInstrument.add(new File(base, includes[i])); |
212 |
|
} |
213 |
|
} |
214 |
0 |
getLogger().verbose("[WAR:"+ getSrc().getName() + "] Collected all jars and classes to instrument: " + |
215 |
|
"\n\tJars = " + jarsToInstrument + |
216 |
|
"\n\tClasses = " + classesToInstrument + |
217 |
|
"\n\tPackages = " + packagesToInstrument); |
218 |
0 |
} |
219 |
|
|
220 |
|
void setupDirectoryScanner() { |
221 |
0 |
ds = new DirectoryScanner(); |
222 |
0 |
if (getSrc() != null) { |
223 |
0 |
ds.setBasedir(getBackupDir()); |
224 |
0 |
} else if (name != null) { |
225 |
0 |
ds.setBasedir(getBackupDir()); |
226 |
|
} |
227 |
0 |
ds.setIncludes(patterns.getIncludePatterns(project)); |
228 |
0 |
ds.setExcludes(patterns.getExcludePatterns(project)); |
229 |
0 |
ds.scan(); |
230 |
0 |
} |
231 |
|
|
232 |
|
void verifyInputs() { |
233 |
0 |
if ( (getSrc() == null) && (name == class="keyword">null) ){ |
234 |
0 |
throw new BuildException("'src' or 'name' attributes must be specified"); |
235 |
|
} |
236 |
0 |
if ( (getSrc() != null) && (name != class="keyword">null) ){ |
237 |
0 |
throw new BuildException("both 'src' and 'name' attributes cannot be specified"); |
238 |
|
} |
239 |
0 |
if ( (getSrc() != null) && (getDest() == class="keyword">null) ) { |
240 |
0 |
throw new BuildException("'dest' attribute not specified"); |
241 |
|
} |
242 |
0 |
if ( (name != null) && (getDest() != class="keyword">null) ) { |
243 |
0 |
throw new BuildException("'dest' attribute cannot be specified with 'name' attribute"); |
244 |
|
} |
245 |
0 |
if ( (getDest() != null) && (getDest().exists()) ) { |
246 |
0 |
throw new BuildException(getDest().getAbsolutePath() + " exists already"); |
247 |
|
} |
248 |
0 |
getLogger().verbose("[WAR:"+ getSrc().getName() + "] Verified all inputs"); |
249 |
0 |
} |
250 |
|
|
251 |
|
String getPackageName(String path) { |
252 |
0 |
String pkg = path.substring(WEB_INF_CLASSES.length() + 1); |
253 |
0 |
int index = pkg.lastIndexOf(File.separatorChar); |
254 |
0 |
if (index != -1) { |
255 |
0 |
pkg = pkg.substring(0, index); |
256 |
0 |
if ("\\".equals(File.separator)) { |
257 |
0 |
pkg = pkg.replaceAll("\\\\", "."); |
258 |
|
} else { |
259 |
0 |
pkg = pkg.replaceAll(File.separator, "."); |
260 |
|
} |
261 |
|
} else { |
262 |
0 |
pkg = ""; |
263 |
|
} |
264 |
0 |
return pkg; |
265 |
|
} |
266 |
|
} |