2011/08/05 - Jakarta Cactus has been retired.

For more information, please explore the Attic.

View Javadoc

1   /* 
2    * ========================================================================
3    * 
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   * 
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
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   * The plug-in runtime class for the Cactus plug-in.
49   * 
50   * @version $Id: CactusPlugin.java 238816 2004-02-29 16:36:46Z vmassol $
51   */
52  public class CactusPlugin extends AbstractUIPlugin
53  {
54      /**
55       * The single instance of this plug-in runtime class.
56       */
57      private static CactusPlugin plugin;
58  
59      /**
60       * Plug-in relative path to the Ant build file.
61       */
62      private static final String BUILD_FILE_PATH = "./script/build.xml";
63  
64      /**
65       * Plug-in relative path to the Ant container build files.
66       */
67      private static final String CONTAINER_BUILD_FILES_PATH = "./script";
68  
69      /**
70       * Prefix of container build files.
71       */
72      private static final String CONTAINER_BUILD_FILES_PREFIX = "build-tests-";
73      
74      /* Some constants */
75      /**
76       * The location where Eclipse was installed;
77       */
78      public static final String CACTUS_VARIABLE_ECLIPSE_HOME = "ECLIPSE_HOME";
79      
80      /**
81       * Path to the plugin's library directory
82       */
83      public static final String CACTUS_LIBRARY_PATH = "lib/";
84  
85      /**
86       * Name of the common libraries folder
87       */
88      public static final String CACTUS_COMMON_LIBRARY_PATH = "common";
89  
90      /**
91       * Name of the client libraries folder
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      * Manager for the container provider.
101      */
102     private static IContainerManager containerManager = null;
103 
104     /**
105      * @see org.eclipse.core.runtime.Plugin#Plugin(IPluginDescriptor)
106      */
107     public CactusPlugin(IPluginDescriptor theDescription)
108     {
109         super(theDescription);
110         plugin = this;
111     }
112     /*
113     public CactusPlugin() {
114     	super();
115     } */
116 
117     /**
118      * @return the single instance of this plug-in runtime class
119      */
120     public static CactusPlugin getDefault()
121     {
122         return plugin;
123     }
124 
125     /**
126      * @return the active workbench shell
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      * @return the active workbench window
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      * @return the plugin identifier
157      */
158     public static String getPluginId()
159     {
160         return getDefault().getDescriptor().getUniqueIdentifier();
161     }
162 
163     /**
164      * @param theMessage the message to log
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      * @param theThrowable throwable to log
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      * @param theStatus status to log
193      */
194     public static void log(IStatus theStatus)
195     {
196         getDefault().getLog().log(theStatus);
197     }
198 
199     /**
200      * @return the current display
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      * Initializes all preferences to their default values.
218      * 
219      * @param theStore the preference store
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      * Returns a container manager.
246      * @param theInitializeFlag true if the container manager should be
247      * initialized
248      * @return a container provider to use for Cactus tests
249      *  or null if Jetty is selected as the container.
250      * @throws CoreException if the container manager can't be contructed
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      * Displays an error dialog.
277      * @param theTitle title of the dialog
278      * @param theMessage message to display in the dialog
279      * @param theStatus status of the error
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      * Helper method for other classes. Returns a CoreException with a message
314      * corresponding to the given message key.
315      * @param theMessageKey the key of the message to be thrown
316      * @param theException a low-level exception, or null if not applicable 
317      * @return the constructed CoreException
318      */
319     public static CoreException createCoreException(
320         String theMessageKey,
321         Throwable theException)
322     {
323         return createCoreException(theMessageKey, "", theException);
324     }
325 
326     /**
327      * Helper method for other classes. Returns a CoreException with a message
328      * corresponding to the given message key and the additional String.
329      * @param theMessageKey the key of the message to be thrown
330      * @param theString String to be concatenated with the message
331      * @param theException a low-level exception, or null if not applicable 
332      * @return the constructed CoreException
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      * Filter for container script files.
352      * i.e. accepts files like 'build-tests-mycontainer3.1.xml'
353      * 
354      * @version $Id: CactusPlugin.java 238816 2004-02-29 16:36:46Z vmassol $
355      */
356     private static class BuildFilenameFilter implements FilenameFilter
357     {
358         /**
359          * @see java.io.FilenameFilter#accept(java.io.File, java.lang.String)
360          */
361         public boolean accept(File theDir, String theFilename)
362         {
363             return theFilename.startsWith(CONTAINER_BUILD_FILES_PREFIX);
364         }
365     }
366     /**
367      * @see IContainerManager#getContainerIds()
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             // No container available
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 			// TODO Auto-generated catch block
407 			e.printStackTrace();
408 		}
409 	    //ImageDescriptor imageDescriptor = ImageDescriptor.createFromURL(CactusPlugin
410 	    //    .getDefault().getBundle().getResource("icons/calaunch.gif"));
411 	    registry.put(CACTUS_RUN_IMAGE, ImageDescriptor.createFromURL(url));
412 	    return registry;
413 	}
414 
415 }