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.containers.jetty;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.net.URL;
26  
27  import org.apache.cactus.eclipse.runner.containers.IContainerManager;
28  import org.apache.cactus.eclipse.runner.ui.CactusPlugin;
29  import org.apache.cactus.eclipse.runner.ui.CactusPreferences;
30  import org.apache.cactus.eclipse.webapp.internal.WarBuilder;
31  import org.apache.cactus.eclipse.webapp.internal.Webapp;
32  import org.apache.cactus.eclipse.webapp.internal.ui.WebappPlugin;
33  import org.apache.tools.ant.Project;
34  import org.apache.tools.ant.taskdefs.Copy;
35  import org.apache.tools.ant.types.FileSet;
36  import org.eclipse.core.runtime.CoreException;
37  import org.eclipse.core.runtime.Path;
38  import org.eclipse.core.runtime.Platform;
39  import org.eclipse.jdt.core.IJavaProject;
40  
41  /**
42   * Implementation of IContainerManager for the Jetty container.
43   * 
44   * @version $Id: JettyContainerManager.java 238816 2004-02-29 16:36:46Z vmassol $
45   */
46  public class JettyContainerManager implements IContainerManager
47  {
48  
49      /**
50       * The name of the path to the Jetty webapp
51       */
52      private static final String JETTY_WEBAPP_PATH = "jetty.webapp";
53  
54      /**
55       * The name of the jspRedirector.jsp file
56       */
57      private static final String JSPREDIRECTOR_PATH =
58  		 new String("lib/confs/jspRedirector.jsp");
59  
60      /**
61       * Directory containg the web application for Jetty
62       */
63      private File jettyWebappDir =
64          new File(
65              CactusPreferences.getTempDir()
66                  + File.separator
67                  + JETTY_WEBAPP_PATH);
68      /**
69       * @see IContainerManager#prepare(org.eclipse.jdt.core.IJavaProject)
70       */
71      public void prepare(IJavaProject theJavaProject)
72      {
73          try
74          {
75              cactifyWebapp(theJavaProject);
76          }
77          catch (CoreException e)
78          {
79          	CactusPlugin.log("Failed"+e.getMessage());
80          }
81      }
82  
83      /**
84       * @see IContainerManager#tearDown()
85       */
86      public void tearDown()
87      {
88          //WarBuilder.delete(jettyWebappDir);
89      }
90  
91      /**
92       * @param theJavaProject the project to cactify
93       * @throws CoreException if the cactification could not occur
94       */
95      private void cactifyWebapp(IJavaProject theJavaProject)
96          throws CoreException
97      {
98          WarBuilder.delete(jettyWebappDir);
99          jettyWebappDir.mkdir();
100         copyCactusWebappResources(jettyWebappDir);
101         Webapp webapp = WebappPlugin.getWebapp(theJavaProject);
102         webapp.init();
103         File webappDir = webapp.getAbsoluteDir();
104         if (webappDir != null && webappDir.exists())
105         {
106             copyContents(webappDir, jettyWebappDir);
107         }
108     }
109 
110     /**
111      * Copies Cactus webapp resources (jspredirector.jsp) to the given
112      * directory
113      * @param theDir the directory to copy resources to
114      * @throws CoreException if we cannot copy the resources
115      */
116     private void copyCactusWebappResources(File theDir) throws CoreException
117     {
118         Project antProject = new Project();
119         antProject.init();
120         Copy copy = new Copy();
121         copy.setProject(antProject);
122         copy.setTodir(theDir);
123         CactusPlugin thePlugin = CactusPlugin.getDefault();
124         URL jspRedirectorURL = null;
125 		try {
126 			jspRedirectorURL = Platform.asLocalURL(thePlugin.getBundle().getEntry(JSPREDIRECTOR_PATH));
127 		} catch (IOException e1) {
128 			//do nothing the exception is called later.
129 		}
130         if (jspRedirectorURL == null)
131         {
132             throw CactusPlugin.createCoreException(
133                 "CactusLaunch.message.prepare.error.plugin.file",
134                 " : " + JSPREDIRECTOR_PATH,
135                 null);
136         }
137         try{
138         	jspRedirectorURL = Platform.asLocalURL(jspRedirectorURL);
139         } catch(IOException e) {
140             throw CactusPlugin.createCoreException(
141                     "CactusLaunch.message.prepare.error.plugin.file",
142                     " : " + e.getMessage(),
143                     null);
144         }
145         //CactusPlugin.log(jspRedirectorURL.getPath());
146         File jspRedirector = new File(jspRedirectorURL.getPath());
147         FileSet fileSet = new FileSet();
148         fileSet.setFile(jspRedirector);
149         copy.addFileset(fileSet);
150         copy.execute();
151     }
152 
153     /**
154      * copies files from a directory to another directory
155      * @param theSourceDir directory to copy files from
156      * @param theTargetDir directory to copy files to
157      */
158     private void copyContents(File theSourceDir, File theTargetDir)
159     {
160         Project antProject = new Project();
161         antProject.init();
162         Copy copy = new Copy();
163         copy.setProject(antProject);
164         copy.setTodir(theTargetDir);
165         FileSet fileSet = new FileSet();
166         fileSet.setDir(theSourceDir);
167         copy.addFileset(fileSet);
168         copy.execute();
169     }
170 
171     /**
172      * @return the path name element of the web application directory 
173      */
174     public static String getJettyWebappPath()
175     {
176         return JETTY_WEBAPP_PATH;
177     }
178 }