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.runner.ui;
22
23 import java.io.File;
24 import java.io.FilenameFilter;
25 import java.net.MalformedURLException;
26 import java.net.URL;
27 import java.util.Vector;
28
29 import org.apache.cactus.eclipse.runner.containers.IContainerManager;
30 import org.apache.cactus.eclipse.runner.containers.ant.AntContainerManager;
31 import org.apache.cactus.eclipse.runner.containers.jetty.JettyContainerManager;
32 import org.eclipse.core.runtime.CoreException;
33 import org.eclipse.core.runtime.IPluginDescriptor;
34 import org.eclipse.core.runtime.IStatus;
35 import org.eclipse.core.runtime.Path;
36 import org.eclipse.core.runtime.Status;
37 import org.eclipse.jface.dialogs.MessageDialog;
38 import org.eclipse.jface.preference.IPreferenceStore;
39 import org.eclipse.jface.resource.ImageDescriptor;
40 import org.eclipse.jface.resource.ImageRegistry;
41 import org.eclipse.swt.widgets.Display;
42 import org.eclipse.swt.widgets.Shell;
43 import org.eclipse.ui.IWorkbench;
44 import org.eclipse.ui.IWorkbenchWindow;
45 import org.eclipse.ui.plugin.AbstractUIPlugin;
46
47
48
49
50
51
52 public class CactusPlugin extends AbstractUIPlugin
53 {
54
55
56
57 private static CactusPlugin plugin;
58
59
60
61
62 private static final String BUILD_FILE_PATH = "./script/build.xml";
63
64
65
66
67 private static final String CONTAINER_BUILD_FILES_PATH = "./script";
68
69
70
71
72 private static final String CONTAINER_BUILD_FILES_PREFIX = "build-tests-";
73
74
75
76
77
78 public static final String CACTUS_VARIABLE_ECLIPSE_HOME = "ECLIPSE_HOME";
79
80
81
82
83 public static final String CACTUS_LIBRARY_PATH = "lib/";
84
85
86
87
88 public static final String CACTUS_COMMON_LIBRARY_PATH = "common";
89
90
91
92
93 public static final String CACTUS_CLIENT_LIBRARY_PATH = "client";
94
95 public static final String CACTUS_RUN_IMAGE = "cactus.run.image";
96
97
98
99
100
101
102 private static IContainerManager containerManager = null;
103
104
105
106
107 public CactusPlugin(IPluginDescriptor theDescription)
108 {
109 super(theDescription);
110 plugin = this;
111 }
112
113
114
115
116
117
118
119
120 public static CactusPlugin getDefault()
121 {
122 return plugin;
123 }
124
125
126
127
128 public static Shell getActiveWorkbenchShell()
129 {
130 IWorkbenchWindow workBenchWindow = getActiveWorkbenchWindow();
131 if (workBenchWindow == null)
132 {
133 return null;
134 }
135 return workBenchWindow.getShell();
136 }
137
138
139
140
141 public static IWorkbenchWindow getActiveWorkbenchWindow()
142 {
143 if (plugin == null)
144 {
145 return null;
146 }
147 IWorkbench workBench = plugin.getWorkbench();
148 if (workBench == null)
149 {
150 return null;
151 }
152 return workBench.getActiveWorkbenchWindow();
153 }
154
155
156
157
158 public static String getPluginId()
159 {
160 return getDefault().getDescriptor().getUniqueIdentifier();
161 }
162
163
164
165
166 public static void log(String theMessage)
167 {
168 log(
169 new Status(
170 IStatus.INFO,
171 getPluginId(),
172 IStatus.OK,
173 theMessage,
174 null));
175 }
176
177
178
179
180 public static void log(Throwable theThrowable)
181 {
182 log(
183 new Status(
184 IStatus.ERROR,
185 getPluginId(),
186 IStatus.ERROR,
187 "Error",
188 theThrowable));
189 }
190
191
192
193
194 public static void log(IStatus theStatus)
195 {
196 getDefault().getLog().log(theStatus);
197 }
198
199
200
201
202 public static Display getDisplay()
203 {
204 Shell shell = getActiveWorkbenchShell();
205 if (shell != null)
206 {
207 return shell.getDisplay();
208 }
209 Display display = Display.getCurrent();
210 if (display == null)
211 {
212 display = Display.getDefault();
213 }
214 return display;
215 }
216
217
218
219
220
221 protected void initializeDefaultPreferences(IPreferenceStore theStore)
222 {
223 theStore.setDefault(
224 CactusPreferences.CONTEXT_URL_SCHEME,
225 CactusMessages.getString("CactusPreferencePage.protocol.init"));
226 theStore.setDefault(
227 CactusPreferences.CONTEXT_URL_HOST,
228 CactusMessages.getString("CactusPreferencePage.host.init"));
229 theStore.setDefault(
230 CactusPreferences.CONTEXT_URL_PORT,
231 CactusMessages.getString("CactusPreferencePage.port.init"));
232 theStore.setDefault(
233 CactusPreferences.CONTEXT_URL_PATH,
234 CactusMessages.getString("CactusPreferencePage.context.init"));
235 theStore.setDefault(
236 CactusPreferences.TEMP_DIR,
237 System.getProperty("java.io.tmpdir"));
238 theStore.setDefault(CactusPreferences.JETTY, true);
239 theStore.setDefault(
240 CactusPreferences.JETTY_XML,
241 CactusMessages.getString("ContainersPreferencePage.jettyxml.init"));
242 }
243
244
245
246
247
248
249
250
251
252 public static IContainerManager getContainerManager(
253 boolean theInitializeFlag)
254 throws CoreException
255 {
256 if (containerManager != null && !theInitializeFlag)
257 {
258 return containerManager;
259 }
260 if (CactusPreferences.getJetty())
261 {
262 containerManager = new JettyContainerManager();
263 return containerManager;
264 }
265 containerManager =
266 new AntContainerManager(
267 BUILD_FILE_PATH,
268 CactusPreferences.getContextURLPort(),
269 CactusPreferences.getTempDir(),
270 CactusPreferences.getContainerHomes(),
271 CactusPreferences.getContextURLPath());
272 return containerManager;
273 }
274
275
276
277
278
279
280
281 public static void displayErrorMessage(
282 final String theTitle,
283 final String theMessage,
284 IStatus theStatus)
285 {
286 if (theStatus == null)
287 {
288 log(
289 new Status(
290 IStatus.ERROR,
291 getPluginId(),
292 IStatus.OK,
293 theMessage,
294 null));
295 }
296 else
297 {
298 log(theStatus);
299 }
300
301 Display.getDefault().asyncExec(new Runnable()
302 {
303 public void run()
304 {
305 MessageDialog.openError(
306 getActiveWorkbenchShell(),
307 (theTitle == null) ? "" : theTitle,
308 (theMessage == null) ? "" : theMessage);
309 }
310 });
311 }
312
313
314
315
316
317
318
319 public static CoreException createCoreException(
320 String theMessageKey,
321 Throwable theException)
322 {
323 return createCoreException(theMessageKey, "", theException);
324 }
325
326
327
328
329
330
331
332
333
334 public static CoreException createCoreException(
335 String theMessageKey,
336 String theString,
337 Throwable theException)
338 {
339 String message = CactusMessages.getString(theMessageKey);
340 message += theString;
341 return new CoreException(
342 new Status(
343 IStatus.ERROR,
344 CactusPlugin.getPluginId(),
345 IStatus.OK,
346 message,
347 theException));
348 }
349
350
351
352
353
354
355
356 private static class BuildFilenameFilter implements FilenameFilter
357 {
358
359
360
361 public boolean accept(File theDir, String theFilename)
362 {
363 return theFilename.startsWith(CONTAINER_BUILD_FILES_PREFIX);
364 }
365 }
366
367
368
369 public static String[] getContainerIds()
370 {
371 Vector containers = new Vector();
372 URL containerDirURL =
373 CactusPlugin.getDefault().find(
374 new Path(CONTAINER_BUILD_FILES_PATH));
375 if (containerDirURL == null)
376 {
377
378 return new String[0];
379 }
380 Path containerDir = new Path(containerDirURL.getPath());
381 File dir = containerDir.toFile();
382 String[] containerFiles = dir.list(new BuildFilenameFilter());
383 for (int i = 0; i < containerFiles.length; i++)
384 {
385 String currentFileName = containerFiles[i];
386 if (currentFileName.startsWith(CONTAINER_BUILD_FILES_PREFIX))
387 {
388 String currentId =
389 currentFileName.substring(
390 CONTAINER_BUILD_FILES_PREFIX.length(),
391 currentFileName.lastIndexOf("."));
392 containers.add(currentId);
393 }
394 }
395 return (String[]) containers.toArray(new String[containers.size()]);
396 }
397
398
399 protected ImageRegistry createImageRegistry() {
400 final ImageRegistry registry = super.createImageRegistry();
401
402 URL url = null;
403 try {
404 url = new URL(CactusPlugin.getDefault().getDescriptor().getInstallURL(),"icons/calaunch.gif");
405 } catch (MalformedURLException e) {
406
407 e.printStackTrace();
408 }
409
410
411 registry.put(CACTUS_RUN_IMAGE, ImageDescriptor.createFromURL(url));
412 return registry;
413 }
414
415 }