2011/08/05 - Jakarta Cactus has been retired.
For more information, please explore the Attic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.cactus.eclipse.webapp.internal;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.util.Vector;
26
27 import org.apache.cactus.eclipse.webapp.internal.ui.WebappMessages;
28 import org.apache.cactus.eclipse.webapp.internal.ui.WebappPlugin;
29 import org.apache.tools.ant.Project;
30 import org.apache.tools.ant.taskdefs.War;
31 import org.apache.tools.ant.taskdefs.Zip;
32 import org.apache.tools.ant.types.FileSet;
33 import org.apache.tools.ant.types.ZipFileSet;
34 import org.eclipse.core.resources.IFile;
35 import org.eclipse.core.resources.ResourcesPlugin;
36 import org.eclipse.core.runtime.CoreException;
37 import org.eclipse.core.runtime.IPath;
38 import org.eclipse.core.runtime.IProgressMonitor;
39 import org.eclipse.core.runtime.IStatus;
40 import org.eclipse.core.runtime.NullProgressMonitor;
41 import org.eclipse.core.runtime.Path;
42 import org.eclipse.core.runtime.Status;
43 import org.eclipse.jdt.core.IClasspathEntry;
44 import org.eclipse.jdt.core.IJavaProject;
45 import org.eclipse.jdt.core.JavaCore;
46 import org.eclipse.jdt.core.JavaModelException;
47 import org.eclipse.jdt.internal.core.JavaModel;
48
49
50
51
52
53
54 public class WarBuilder
55 {
56
57
58
59 private IJavaProject javaProject;
60
61
62
63
64 private Webapp webapp;
65
66
67
68
69 public static final String WEBINF = "WEB-INF";
70
71
72
73
74 public static final String LIB = "lib";
75
76
77
78
79 public static final String WEBXML = "web.xml";
80
81
82
83
84
85
86 public WarBuilder(final IJavaProject theJavaProject)
87 throws JavaModelException
88 {
89 this.javaProject = theJavaProject;
90 this.webapp = new Webapp(theJavaProject);
91 }
92
93
94
95
96
97
98 public static File getWebXML(final File theWebFilesDir)
99 {
100 if (theWebFilesDir == null)
101 {
102 return null;
103 }
104 else
105 {
106 String userWebXMLPath =
107 theWebFilesDir.getAbsolutePath()
108 + File.separator
109 + WEBINF
110 + File.separator
111 + WEBXML;
112 return new File(userWebXMLPath);
113 }
114 }
115
116
117
118
119
120 private static IClasspathEntry[] getAbsoluteEntries(
121 final IClasspathEntry[] theEntries)
122 {
123 if (theEntries == null)
124 {
125 return new IClasspathEntry[0];
126 }
127 Vector result = new Vector();
128 for (int i = 0; i < theEntries.length; i++)
129 {
130 IClasspathEntry currentEntry = theEntries[i];
131 if (currentEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
132 {
133 IPath path = currentEntry.getPath();
134 Object target =
135 JavaModel.getTarget(
136 ResourcesPlugin.getWorkspace().getRoot(),
137 path,
138 true);
139 if (target instanceof IFile)
140 {
141 IFile file = (IFile) target;
142 IPath absolutePath = file.getLocation();
143 result.add(
144 JavaCore.newLibraryEntry(absolutePath, null, null));
145 }
146 else
147 if (target instanceof File)
148 {
149 File file = (File) target;
150 result.add(
151 JavaCore.newLibraryEntry(
152 new Path(file.getAbsolutePath()),
153 null,
154 null));
155 }
156 }
157 }
158 return (IClasspathEntry[]) result.toArray(
159 new IClasspathEntry[result.size()]);
160 }
161
162
163
164
165
166
167
168
169 private static File getAbsoluteOutputLocation(
170 final IJavaProject theJavaProject) throws JavaModelException
171 {
172 IPath projectPath = theJavaProject.getProject().getLocation();
173 IPath outputLocation = theJavaProject.getOutputLocation();
174 IPath classFilesPath =
175 projectPath.append(outputLocation.removeFirstSegments(1));
176 return classFilesPath.toFile();
177 }
178
179
180
181
182
183
184
185
186 public final File createWar(final IProgressMonitor thePM)
187 throws CoreException
188 {
189 IProgressMonitor progressMonitor = thePM;
190 if (progressMonitor == null)
191 {
192 progressMonitor = new NullProgressMonitor();
193 }
194
195 progressMonitor.subTask(
196 WebappMessages.getString("WarBuilder.message.createwar.monitor"));
197 this.webapp.loadValues();
198 File outputWar = getOutputWar();
199 File userWebFilesDir = getUserWebFilesDir();
200 File userWebXML = getWebXML(userWebFilesDir);
201 IClasspathEntry[] jarEntries = getJarEntries();
202 File userClassFilesDir = getAbsoluteOutputLocation(this.javaProject);
203
204 outputWar.delete();
205 War warTask = new War();
206 progressMonitor.worked(1);
207 Project antProject = new Project();
208 antProject.init();
209 warTask.setProject(antProject);
210 warTask.setDestFile(outputWar);
211 ZipFileSet classes = new ZipFileSet();
212 classes.setDir(userClassFilesDir);
213 warTask.addClasses(classes);
214 classes = new ZipFileSet();
215 classes.setDir(userClassFilesDir);
216 classes.setIncludes("log4j.properties");
217 warTask.addClasses(classes);
218 if (userWebFilesDir != null && userWebFilesDir.exists())
219 {
220 FileSet webFiles = new FileSet();
221 webFiles.setDir(userWebFilesDir);
222 webFiles.setExcludes(WEBINF);
223 warTask.addFileset(webFiles);
224 }
225 if (userWebXML != null && userWebXML.exists())
226 {
227 warTask.setWebxml(userWebXML);
228 }
229 else
230 {
231
232
233
234 try
235 {
236
237 File voidFile = File.createTempFile("void", null);
238 createZipFile(outputWar, voidFile);
239 voidFile.delete();
240 }
241 catch (IOException e)
242 {
243 throw new CoreException(
244 new Status(
245 IStatus.ERROR,
246 WebappPlugin.getPluginId(),
247 IStatus.OK,
248 WebappMessages.getString(
249 "WarBuilder.message.createwar.temp"),
250 e));
251 }
252
253 warTask.setUpdate(true);
254 }
255
256 ZipFileSet[] jarFS = getZipFileSets(jarEntries);
257 for (int i = 0; i < jarFS.length; i++)
258 {
259 warTask.addLib(jarFS[i]);
260 }
261 warTask.execute();
262 progressMonitor.worked(2);
263 return outputWar;
264 }
265
266
267
268
269
270 private static ZipFileSet[] getZipFileSets(
271 final IClasspathEntry[] theJarEntries)
272 {
273 Vector result = new Vector();
274 for (int i = 0; i < theJarEntries.length; i++)
275 {
276
277 IClasspathEntry currentEntry = theJarEntries[i];
278 if (currentEntry.getEntryKind() == IClasspathEntry.CPE_LIBRARY)
279 {
280 File currentJar = currentEntry.getPath().toFile();
281 ZipFileSet zipFS = new ZipFileSet();
282 zipFS.setFile(currentJar);
283 result.add(zipFS);
284 }
285 }
286 return (ZipFileSet[]) result.toArray(new ZipFileSet[result.size()]);
287 }
288
289
290
291
292 private File getUserWebFilesDir()
293 {
294
295 String userWebFilesPath = this.webapp.getDir();
296 if (userWebFilesPath == null || userWebFilesPath.equals(""))
297 {
298 return null;
299 }
300 else
301 {
302 IPath projectPath = this.javaProject.getProject().getLocation();
303
304
305 return projectPath.append(userWebFilesPath).toFile();
306 }
307 }
308
309
310
311
312 private IClasspathEntry[] getJarEntries()
313 {
314 return getAbsoluteEntries(this.webapp.getClasspath());
315 }
316
317
318
319
320 private File getOutputWar()
321 {
322 return new File(this.webapp.getOutput());
323 }
324
325
326
327
328
329 public static final void delete(final File theFile)
330 {
331 if (theFile.isDirectory())
332 {
333 File[] dir = theFile.listFiles();
334 for (int i = 0; i < dir.length; i++)
335 {
336 delete(dir[i]);
337 }
338 theFile.delete();
339 }
340 else
341 if (theFile.exists())
342 {
343 theFile.delete();
344 }
345 }
346
347
348
349
350
351
352 private void createZipFile(final File theZipFile,
353 final File theExistingFile)
354 {
355 Project antProject = new Project();
356 antProject.init();
357 Zip zip = new Zip();
358 zip.setProject(antProject);
359 zip.setDestFile(theZipFile);
360 FileSet existingFileSet = new FileSet();
361 existingFileSet.setFile(theExistingFile);
362 zip.addFileset(existingFileSet);
363 zip.execute();
364 }
365
366 }