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.util.StringTokenizer;
25 import java.util.Vector;
26
27 import org.apache.cactus.eclipse.webapp.internal.ui.WebappPlugin;
28 import org.eclipse.core.resources.IProject;
29 import org.eclipse.core.runtime.CoreException;
30 import org.eclipse.core.runtime.IPath;
31 import org.eclipse.core.runtime.Path;
32 import org.eclipse.core.runtime.QualifiedName;
33 import org.eclipse.jdt.core.IClasspathEntry;
34 import org.eclipse.jdt.core.IJavaProject;
35 import org.eclipse.jdt.core.JavaCore;
36 import org.eclipse.jdt.core.JavaModelException;
37
38
39
40
41
42
43
44
45 public class Webapp
46 {
47
48
49
50
51 private static final String CLASSPATH_DELIMITER = ";";
52
53
54
55
56
57 private static final QualifiedName OUTPUT_QN =
58 new QualifiedName(WebappPlugin.getPluginId(), "output");
59
60
61
62
63
64 private static final QualifiedName DIR_QN =
65 new QualifiedName(WebappPlugin.getPluginId(), "dir");
66
67
68
69
70
71 private static final QualifiedName CLASSPATH_QN =
72 new QualifiedName(WebappPlugin.getPluginId(), "webappClasspath");
73
74
75
76
77 private static final String DEFAULT_OUTPUT =
78 System.getProperty("java.io.tmpdir") + "webapp.war";
79
80
81
82
83 private static final String DEFAULT_DIR =
84 "src" + File.separator + "webapp";
85
86
87
88
89 private String output;
90
91
92
93
94 private String dir;
95
96
97
98
99 private IClasspathEntry[] classpath;
100
101
102
103
104 private IJavaProject javaProject;
105
106
107
108
109 public Webapp(final IJavaProject theJavaProject)
110 {
111 this.javaProject = theJavaProject;
112 }
113
114
115
116
117
118
119 public final boolean init()
120 {
121 return loadValues();
122 }
123
124
125
126
127
128
129
130
131
132 public final boolean loadValues()
133 {
134 boolean isDefaultValues;
135
136 try
137 {
138 loadPersistentValues();
139 isDefaultValues = false;
140 }
141 catch (CoreException ce)
142 {
143 loadDefaultValues();
144 isDefaultValues = true;
145 }
146
147 if (output == null
148 || dir == null
149 || classpath == null)
150 {
151 loadDefaultValues();
152 isDefaultValues = true;
153 }
154
155 return isDefaultValues;
156 }
157
158
159
160
161
162 public final void loadPersistentValues() throws CoreException
163 {
164 IProject theProject = javaProject.getProject();
165
166 this.output = theProject.getPersistentProperty(OUTPUT_QN);
167 this.dir = theProject.getPersistentProperty(DIR_QN);
168 this.classpath = toClasspathEntryArray(
169 theProject.getPersistentProperty(CLASSPATH_QN));
170 }
171
172
173
174
175 public final void loadDefaultValues()
176 {
177 this.output = DEFAULT_OUTPUT;
178 this.dir = DEFAULT_DIR;
179
180 try
181 {
182 this.classpath = javaProject.getRawClasspath();
183 }
184 catch (JavaModelException e)
185 {
186 this.classpath = new IClasspathEntry[0];
187 }
188 }
189
190
191
192
193
194 public final void persist() throws CoreException
195 {
196 IProject project = javaProject.getProject();
197 project.setPersistentProperty(OUTPUT_QN, output);
198 project.setPersistentProperty(DIR_QN, dir);
199 project.setPersistentProperty(CLASSPATH_QN, toString(classpath));
200 }
201
202
203
204
205
206
207
208 private IClasspathEntry[] toClasspathEntryArray(
209 final String theClasspathEntriesString)
210 {
211 if (theClasspathEntriesString == null)
212 {
213 return null;
214 }
215
216 Vector result = new Vector();
217
218 StringTokenizer cpTokenizer =
219 new StringTokenizer(theClasspathEntriesString,
220 CLASSPATH_DELIMITER);
221
222 while (cpTokenizer.hasMoreElements())
223 {
224 String element = cpTokenizer.nextToken();
225 try
226 {
227 IClasspathEntry newEntry =
228 JavaCore.newLibraryEntry(new Path(element), null, null);
229 result.add(newEntry);
230 }
231 catch (Exception e)
232 {
233
234 }
235 }
236
237 return (IClasspathEntry[]) result.toArray(
238 new IClasspathEntry[result.size()]);
239 }
240
241
242
243
244
245
246 private String toString(final IClasspathEntry[] theClasspathEntries)
247 {
248 StringBuffer result = new StringBuffer();
249 for (int i = 0; i < theClasspathEntries.length; i++)
250 {
251 IClasspathEntry current = theClasspathEntries[i];
252 result.append(current.getPath());
253 result.append(CLASSPATH_DELIMITER);
254 }
255 return result.toString();
256 }
257
258
259
260
261
262 public final void setClasspath(final IClasspathEntry[] theClasspath)
263 {
264 this.classpath = theClasspath;
265 }
266
267
268
269
270
271 public final void setDir(final String theDir)
272 {
273 this.dir = theDir;
274 }
275
276
277
278
279
280 public final void setOutput(final String theOutput)
281 {
282 this.output = theOutput;
283 }
284
285
286
287
288 public final IClasspathEntry[] getClasspath()
289 {
290 return this.classpath;
291 }
292
293
294
295
296
297 public final String getDir()
298 {
299 return this.dir;
300 }
301
302
303
304
305 public final File getAbsoluteDir()
306 {
307 File result = null;
308
309 if (this.dir != null)
310 {
311 IPath projectPath = javaProject.getProject().getLocation();
312 result = projectPath.append(this.dir).toFile();
313 }
314 return result;
315 }
316
317
318
319
320 public final String getOutput()
321 {
322 return this.output;
323 }
324 }