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.io.File;
24  import java.io.IOException;
25  import java.net.MalformedURLException;
26  import java.net.URL;
27  import java.util.Vector;
28  
29  import org.apache.cactus.eclipse.runner.common.JarFilenameFilter;
30  import org.apache.cactus.eclipse.runner.common.LibraryHelper;
31  import org.apache.cactus.eclipse.runner.containers.jetty.JettyContainerManager;
32  import org.apache.cactus.eclipse.runner.ui.CactusPlugin;
33  import org.apache.cactus.eclipse.runner.ui.CactusPreferences;
34  import org.eclipse.core.runtime.CoreException;
35  import org.eclipse.core.runtime.ILibrary;
36  import org.eclipse.core.runtime.IPath;
37  import org.eclipse.core.runtime.IPluginDescriptor;
38  import org.eclipse.core.runtime.Path;
39  import org.eclipse.core.runtime.Platform;
40  import org.eclipse.core.runtime.Plugin;
41  import org.eclipse.jdt.core.IClasspathEntry;
42  import org.eclipse.jdt.core.IJavaProject;
43  import org.eclipse.jdt.core.JavaCore;
44  
45  /**
46   * Provides a launcher to start Cactus tests. This is done by extending
47   * the JUnit Plugin launch configuration and adding Cactus specific VM
48   * configuration (Cactus jars, VM parameters) and by registering this
49   * class as an 
50   * <code>"org.eclipse.debug.core.launchConfigurationTypes"</code> Eclipse
51   * extension point.
52   * 
53   * @version $Id: JettyCactusLaunchConfiguration.java 238816 2004-02-29 16:36:46Z vmassol $
54   */
55  public class JettyCactusLaunchConfiguration
56      extends CactusLaunchConfiguration
57  {
58      /**
59       * Id under which the Cactus launch configuration has been registered. 
60       */
61      public static final String ID_CACTUS_APPLICATION_JETTY =
62          "org.apache.cactus.eclipse.runner.launchconfig.jetty";
63  
64      /**
65       * Path to the Jetty library directory in the Cactus plugin
66       * structure
67       */
68      private static final String JETTY_LIBRARY_PATH = "./lib/";
69  
70      /**
71       * @return an array of classpaths needed for Cactus
72       * @throws CoreException when an error occurs while
73       * trying to build the classpath
74       */
75      protected IClasspathEntry[] getCactusClasspath() throws CoreException
76      {
77          IClasspathEntry[] cactusClasspath = super.getCactusClasspath();
78          try {
79          	cactusClasspath =
80                  LibraryHelper.concatenateEntries(
81                      cactusClasspath,
82                      getJettyClasspath());
83          } catch (MalformedURLException me) {
84          	throw CactusPlugin.createCoreException(
85                      "CactusLaunch.message.prepare.error.plugin.file",
86                      " : " + me.toString(),
87                      null);
88          }
89          return cactusClasspath;
90      }
91  
92      /**
93       * @return an array of Jar paths needed for Cactus
94       * @throws CoreException if a Jar cannot be found
95       * @throws MalformedURLException 
96       */
97      private IClasspathEntry[] getJettyClasspath() throws CoreException, MalformedURLException
98      {
99          CactusPlugin thePlugin = CactusPlugin.getDefault();
100        
101         IPath libURL = LibraryHelper.getLibPath();
102         
103         if (libURL == null)
104         {
105             throw CactusPlugin.createCoreException(
106                 "CactusLaunch.message.prepare.error.plugin.file",
107                 " : " + libURL.toString(),
108                 null);
109         }
110         File libDir = new File(libURL.toString());
111         IClasspathEntry[] jettyJarPaths = getJarPaths(libDir);
112         IClasspathEntry[] apacheJarPaths = new IClasspathEntry[0];
113         Plugin tomcatPlugin = Platform.getPlugin("org.eclipse.tomcat");
114         if (tomcatPlugin != null)
115         {
116             IPluginDescriptor descriptor = tomcatPlugin.getDescriptor();
117             apacheJarPaths = getLibrariesPaths(descriptor, "org.apache.jasper");
118         }
119         return LibraryHelper.concatenateEntries(jettyJarPaths, apacheJarPaths);
120     }
121 
122     /**
123      * @param theJavaProject the Java project to get the arguments for
124      * @return an array of the specific Cactus VM arguments
125      */
126     protected String getCactusVMArgs(IJavaProject theJavaProject)
127     {
128         String cactusVMArgs = super.getCactusVMArgs(theJavaProject);
129         String jettyResourcePath =
130             CactusPreferences.getTempDir()
131                 + File.separator
132                 + JettyContainerManager.getJettyWebappPath();
133         cactusVMArgs += "-Dcactus.jetty.resourceDir="
134             + jettyResourcePath
135             + VM_ARG_SEPARATOR;
136         IPath projectLocation = theJavaProject.getProject().getLocation();
137         File jettyXML =
138             projectLocation.append(CactusPreferences.getJettyXML()).toFile();
139         if (jettyXML.exists())
140         {
141             cactusVMArgs += "-Dcactus.jetty.config="
142                 + jettyXML.getAbsolutePath()
143                 + VM_ARG_SEPARATOR;
144         }
145         cactusVMArgs += "-Dcactus.initializer="
146             + "org.apache.cactus.extension.jetty.JettyTestSetup";
147         return cactusVMArgs;
148     }
149 
150     /**
151      * @param theDirectory the directory to list jars from
152      * @return the array of jar paths in the given directory
153      */
154     private IClasspathEntry[] getJarPaths(File theDirectory)
155     {
156         File[] jars = theDirectory.listFiles(new JarFilenameFilter());
157         IClasspathEntry[] jarPaths = new IClasspathEntry[jars.length];
158         for (int i = 0; i < jarPaths.length; i++)
159         {
160             jarPaths[i] =
161                 LibraryHelper.getIClasspathEntry(jars[i].getAbsolutePath());
162         }
163         return jarPaths;
164     }
165 
166     /**
167      * @param theDescriptor the plug-in descriptor to get libraries from
168      * @param thePackagePrefix package prefix used to filter libraries 
169      * @return an array of jar paths exposed by the plug-in
170      * @throws MalformedURLException 
171      */
172     private IClasspathEntry[] getLibrariesPaths(
173         IPluginDescriptor theDescriptor, String thePackagePrefix) throws MalformedURLException
174     {
175         Vector result = new Vector();
176         URL root = new URL(("file://"+JavaCore.getClasspathVariable("ECLIPSE_HOME").toFile().toURL().getPath()+theDescriptor.getInstallURL().getPath()).replaceAll("plugin","plugins"));
177         ILibrary[] libraries = theDescriptor.getRuntimeLibraries();
178         
179         for (int i = 0; i < libraries.length; i++)
180         {
181         	
182             ILibrary currentLib = libraries[i];
183             //CactusPlugin.log("Current library "+currentLib.getPath().toString());
184             if (thePackagePrefix == null
185                 || isContained(thePackagePrefix, currentLib))
186             {
187                 try
188                 {
189                     URL url = new URL(root, currentLib.getPath().toString());
190                     result.add(
191                         LibraryHelper.getIClasspathEntry(
192                             Platform.asLocalURL(url).getPath()));
193                 }
194                 catch (IOException e)
195                 {
196                     // if the URL is not valid we don't add it
197                     CactusPlugin.log(e);
198                     continue;
199                 }
200             }
201         }
202         return (IClasspathEntry[]) result.toArray(
203             new IClasspathEntry[result.size()]);
204     }
205 
206     /**
207      * @param thePackagePrefix prefix which presence is to be tested
208      * @param theCurrentLib the library in which the prefix will be searched 
209      * @return true if the library declares the given package prefix
210      */
211     private boolean isContained(String thePackagePrefix, ILibrary theCurrentLib)
212     {
213     	String[] prefixes = theCurrentLib.getPackagePrefixes();
214         if(prefixes==null || prefixes.length==0) {
215         	//CactusPlugin.log("lib: "+theCurrentLib.getPath().toString()+" is not");
216         	return false;
217         }
218         for (int i = 0; i < prefixes.length; i++)
219         {
220             if (prefixes[i].equals(thePackagePrefix))
221             {
222                 return true;
223             }
224         }
225         return false;
226     }
227 }