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.launcher;
22  
23  import java.net.URL;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Vector;
27  
28  import org.apache.cactus.eclipse.runner.common.LibraryHelper;
29  import org.apache.cactus.eclipse.runner.ui.CactusMessages;
30  import org.apache.cactus.eclipse.runner.ui.CactusPlugin;
31  import org.apache.cactus.eclipse.runner.ui.CactusPreferences;
32  import org.eclipse.ant.core.AntCorePlugin;
33  import org.eclipse.core.runtime.CoreException;
34  import org.eclipse.core.runtime.IProgressMonitor;
35  import org.eclipse.debug.core.ILaunch;
36  import org.eclipse.debug.core.ILaunchConfiguration;
37  import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
38  import org.eclipse.debug.core.model.ILaunchConfigurationDelegate;
39  import org.eclipse.jdt.core.IClasspathEntry;
40  import org.eclipse.jdt.core.IJavaProject;
41  import org.eclipse.jdt.internal.junit.ui.JUnitPlugin;
42  import org.eclipse.jdt.junit.ITestRunListener;
43  import org.eclipse.jdt.junit.launcher.JUnitLaunchConfigurationDelegate;
44  import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;
45  import org.eclipse.jdt.launching.IRuntimeClasspathEntry;
46  import org.eclipse.jdt.launching.JavaRuntime;
47  import org.eclipse.swt.widgets.Display;
48  
49  /**
50   * Provides a launcher to start Cactus tests. This is done by extending
51   * the JUnit Plugin launch configuration and adding Cactus specific VM
52   * configuration (Cactus jars, VM parameters) and by registering this
53   * class as an 
54   * <code>"org.eclipse.debug.core.launchConfigurationTypes"</code> Eclipse
55   * extension point.
56   * 
57   * @version $Id: CactusLaunchConfiguration.java 238816 2004-02-29 16:36:46Z vmassol $
58   */
59  public class CactusLaunchConfiguration
60      extends JUnitLaunchConfigurationDelegate
61      implements ITestRunListener
62  {
63      /**
64       * Indicates whether we already went through the launch cycle.
65       * This is used because there currently is no way to unregister
66       * an ITestRunListener from the JUnit plugin.
67       */
68      private boolean launchEnded;
69  
70      /**
71       * Id under which the Cactus launch configuration has been registered. 
72       */
73      public static final String ID_CACTUS_APPLICATION =
74          "org.apache.cactus.eclipse.runner.launchconfig";
75  
76      /**
77       * Separator between VM arguments
78       */
79      protected static final String VM_ARG_SEPARATOR = " ";
80  
81  	private static final String KEY_NO_STARTUP = "nO STARTUP";
82  
83      /**
84       * @see ILaunchConfigurationDelegate#launch(ILaunchConfiguration, String)
85       */
86      public void launch(
87          final ILaunchConfiguration theConfiguration,
88          final String theMode,
89          final ILaunch theLaunch,
90          final IProgressMonitor thePM)
91          throws CoreException
92      {
93          final IJavaProject javaProject = getJavaProject(theConfiguration);
94          Vector userClasspath =
95              toMemento(
96                  JavaRuntime.computeUnresolvedRuntimeClasspath(javaProject));
97          Vector cactusClasspath = toMemento(getCactusClasspath());
98          List classpath =
99              theConfiguration.getAttribute(
100                 IJavaLaunchConfigurationConstants.ATTR_CLASSPATH,
101                 (List) null);
102         if (classpath == null)
103         {
104             classpath = userClasspath;
105             classpath.addAll(0, cactusClasspath);
106         }
107         else
108         {
109             classpath.addAll(0, userClasspath);
110             classpath.addAll(0, cactusClasspath);
111         }
112         final ILaunchConfigurationWorkingCopy cactusConfig =
113             theConfiguration.getWorkingCopy();
114         cactusConfig.setAttribute(
115             IJavaLaunchConfigurationConstants.ATTR_CLASSPATH,
116             classpath);
117         cactusConfig.setAttribute(
118             IJavaLaunchConfigurationConstants.ATTR_DEFAULT_CLASSPATH,
119             false);
120         String jUnitArgs = getVMArguments(theConfiguration);
121         String cactusVMArgs = getCactusVMArgs(javaProject);
122         String globalArgs = jUnitArgs + VM_ARG_SEPARATOR + cactusVMArgs;
123         cactusConfig.setAttribute(
124             IJavaLaunchConfigurationConstants.ATTR_VM_ARGUMENTS,
125             globalArgs);
126         this.launchEnded = false;
127         // Register the instance of CactusLaunchShortcut to the JUnitPlugin
128         // for TestRunEnd notification.
129         JUnitPlugin.getDefault().addTestRunListener(this);
130         // Run the preparation in a new thread so that the UI thread which is
131         // the current thread be not blocked by the sleep of
132         // IContainerManager.prepare().
133         try {
134         new Thread(new Runnable()
135         {
136             public void run()
137             {
138                 try
139                 {
140                     CactusPlugin.getContainerManager(true).prepare(javaProject);
141                     Display.getDefault().asyncExec(new Runnable()
142                     {
143                         public void run()
144                         {
145                             try
146                             {
147                                 CactusLaunchConfiguration.super.launch(
148                                     cactusConfig,
149                                     theMode,
150                                     theLaunch,
151                                     thePM);
152                             }
153                             catch (CoreException e)
154                             {
155                                 CactusPlugin.displayErrorMessage(
156                                     CactusMessages.getString(
157                             "CactusLaunch.message.containerManager.error"),
158                                     e.getMessage(),
159                                     null);
160                                 return;
161                             }
162                             catch(Exception ex) {
163                             	CactusPlugin.log("ATTENTION."+ex.getMessage());
164                             	
165                             	CactusPlugin.displayErrorMessage(
166                                         CactusMessages.getString(
167                                 "CactusLaunch.message.containerManager.error"),
168                                         ex.getMessage(),
169                                         null);
170                             }
171                         }
172                     });
173                 }
174                 catch (CoreException e)
175                 {
176                     CactusPlugin.displayErrorMessage(
177                         CactusMessages.getString(
178                             "CactusLaunch.message.containerManager.error"),
179                         e.getMessage(),
180                         null);
181                     return;
182                 }
183                 catch(Exception ex) {
184                 	CactusPlugin.log("ATTENTION1."+ex.getMessage());
185                     CactusPlugin.displayErrorMessage(
186                             CactusMessages.getString(
187                     "CactusLaunch.message.containerManager.error"),
188                             ex.getMessage(),
189                             null);
190                 }
191             }
192         }).start();
193         } catch(Exception ex) {
194         	
195             CactusPlugin.displayErrorMessage(
196                     CactusMessages.getString(
197             "CactusLaunch.message.containerManager.error"),
198                     ex.getMessage(),
199                     null);
200         }
201 
202 
203 
204 
205     }
206 
207     /**
208      * @param theEntries the array of IClasspathEntry to build mementos from 
209      * @return a Vector of mementos from the given array
210      */
211     private Vector toMemento(IClasspathEntry[] theEntries)
212     {
213         Vector result = new Vector();
214         for (int i = 0; i < theEntries.length; i++)
215         {
216             try
217             {
218                 result.add(
219                     JavaRuntime
220                         .newArchiveRuntimeClasspathEntry(
221                             theEntries[i].getPath())
222                         .getMemento());
223             }
224             catch (CoreException e)
225             {
226                 CactusPlugin.log(e.getMessage());
227             	// Do nothing
228             }
229         }
230         return result;
231     }
232 
233     /**
234      * @param theEntries the array of IRuntimeClasspathEntry to build
235      * mementos from 
236      * @return a Vector of mementos from the given array
237      */
238     private Vector toMemento(IRuntimeClasspathEntry[] theEntries)
239     {
240         Vector result = new Vector();
241         for (int i = 0; i < theEntries.length; i++)
242         {
243             try
244             {
245                 result.add(theEntries[i].getMemento());
246             }
247             catch (CoreException e)
248             {
249                 // Do nothing
250             }
251         }
252         return result;
253     }
254 
255     /**
256      * @return an array of classpaths needed for Cactus
257      * @throws CoreException when an error occurs while
258      * trying to build the classpath
259      */
260     protected IClasspathEntry[] getCactusClasspath() throws CoreException
261     {
262         IClasspathEntry[] cactusClasspath =
263             LibraryHelper.getClientSideEntries();
264         URL[] antURLs = AntCorePlugin.getPlugin().getPreferences().getAntURLs();
265         IClasspathEntry[] apacheClasspath = getClasspathEntryArray(antURLs);
266         cactusClasspath =
267             LibraryHelper.concatenateEntries(cactusClasspath, apacheClasspath);
268         return cactusClasspath;
269     }
270 
271     /**
272      * @param theJavaProject the Java project to get the arguments for
273      * @return an array of the specific Cactus VM arguments
274      */
275     protected String getCactusVMArgs(IJavaProject theJavaProject)
276     {
277         String cactusVMArgs = "";
278         cactusVMArgs += "-Dcactus.contextURL="
279             + CactusPreferences.getContextURL()
280             + VM_ARG_SEPARATOR;
281         return cactusVMArgs;
282     }
283 
284     /**
285      * @param theAntURLs array of URLs to convert to Jar paths
286      * @return the array of jar paths from the given URLs
287      */
288     private IClasspathEntry[] getClasspathEntryArray(URL[] theAntURLs)
289     {
290         IClasspathEntry[] result = new IClasspathEntry[theAntURLs.length];
291         for (int i = 0; i < theAntURLs.length; i++)
292         {
293             result[i] =
294                 LibraryHelper.getIClasspathEntry(theAntURLs[i].getPath());
295         }
296         return result;
297     }
298 
299     /**
300      * @see ITestRunListener#testRunStarted(int)
301      */
302     public void testRunStarted(int theTestCount)
303     {
304     }
305 
306     /**
307      * Test run has ended so we tear down the container setup.
308      * @param theElapsedTime not used here
309      */
310     public void testRunEnded(long theElapsedTime)
311     {
312         // If we already finished the launch (i.e. we already went here)
313         // we do nothing. 
314         if (this.launchEnded)
315         {
316             return;
317         }
318         try
319         {
320             CactusPlugin.getContainerManager(false).tearDown();
321         }
322         catch (CoreException e)
323         {
324             CactusPlugin.displayErrorMessage(
325                 CactusMessages.getString(
326                     "CactusLaunch.message.containerManager.error"),
327                 e.getMessage(),
328                 null);
329             return;
330         }
331         catch (Exception ex) {
332         	CactusPlugin.log("Exception tearing down:"+ex.getMessage());
333         }
334         this.launchEnded = true;
335     }
336 
337     /**
338      * If test run has stopped we have to do the same thing
339      * as if the test run had ended normally.
340      * @param theElapsedTime not used here
341      */
342     public void testRunStopped(long theElapsedTime)
343     {
344         testRunEnded(0);
345     }
346 
347     /**
348      * @see ITestRunListener#testStarted(String, String)
349      */
350     public void testStarted(String theTestId, String theTestName)
351     {
352     }
353 
354     /**
355      * @see ITestRunListener#testEnded(String, String)
356      */
357     public void testEnded(String theTestId, String theTestName)
358     {
359     }
360 
361     /**
362      * @see ITestRunListener#testFailed (int, String, String, String)
363      */
364     public void testFailed(
365         int theStatus,
366         String theTestId,
367         String theTestName,
368         String theTrace)
369     {
370     }
371 
372     /**
373      * @see ITestRunListener#testTreeEntry(String)
374      */
375     public void testTreeEntry(String theEntry)
376     {
377     }
378 
379     /**
380      * If test run has been terminated we have to do the same thing
381      * as if the test run had ended normally.
382      */
383     public void testRunTerminated()
384     {
385         testRunEnded(0);
386     }
387 
388     /**
389      * @see ITestRunListener#testReran(String, String, String, int, String)
390      */
391     public void testReran(
392         String theTestId,
393         String theTestClass,
394         String theTestName,
395         int theStatus,
396         String theTrace)
397     {
398     }
399 /*
400 	@Override
401 	protected VMRunnerConfiguration createVMRunner(ILaunchConfiguration configuration,
402 			TestSearchResult testTypes, int port, String runMode) throws CoreException {
403 		String[] classpath = LauncherUtils.constructClasspath();
404 		if (classpath == null) {
405 			abort("Startup failed", null, IStatus.OK);
406 		}
407 
408 		String[] programArgs = computeProgramArguments(configuration, testTypes, port, runMode);
409 		if (programArgs == null)
410 			return null;
411 			
412 		VMRunnerConfiguration runnerConfig =
413 			new VMRunnerConfiguration("org.eclipse.core.launcher.Main", classpath);
414 		runnerConfig.setVMArguments(computeVMArguments(configuration));
415 		runnerConfig.setProgramArguments(programArgs);
416 		
417 		return runnerConfig;
418 	}
419 	*/
420 }