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.ant;
22  
23  import java.net.URL;
24  import java.util.Vector;
25  
26  import org.apache.cactus.eclipse.runner.containers.ContainerInfo;
27  import org.apache.cactus.eclipse.runner.containers.Credential;
28  import org.apache.cactus.eclipse.runner.containers.IContainerProvider;
29  import org.apache.cactus.eclipse.runner.ui.CactusMessages;
30  import org.eclipse.core.runtime.CoreException;
31  import org.eclipse.core.runtime.IProgressMonitor;
32  import org.eclipse.core.runtime.SubProgressMonitor;
33  import org.eclipse.debug.core.ILaunch;
34  import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
35  import org.eclipse.debug.core.ILaunchManager;
36  
37  /**
38   * Implementation of IContainerProvider that uses ant scripts to set up
39   * and launch containers.
40   * 
41   * @version $Id: AntContainerProvider.java 238816 2004-02-29 16:36:46Z vmassol $
42   */
43  public class AntContainerProvider implements IContainerProvider
44  {
45      /**
46       * the manager of this provider
47       */
48      private AntContainerManager manager;
49  
50      /**
51       * the Ant arguments specific to this provider
52       */
53      private Vector antArguments = new Vector();
54  
55      /**
56       * The Eclipse runner associated to the Ant container provider.
57       */
58      private EclipseRunTests eclipseRunner;
59  
60      /**
61       * The home directory of this provider.
62       */
63      private String home;
64  
65      /**
66       * The Ant target mask for this provider, for example "tomcat4x".
67       */
68      private String targetMask;
69  
70      /**
71       * Reference to the launch created to start the container.
72       */    
73      private ILaunch startLaunch;
74      
75      /**
76       * @param theManager the manager of this provider
77       * @param theTargetMask target mask for this provider
78       * @param theHome home directory for this provider's container
79       */
80      public AntContainerProvider(AntContainerManager theManager,
81          String theTargetMask, String theHome)
82      {
83          this.targetMask = theTargetMask;
84          this.home = theHome;
85          this.manager = theManager;
86      }
87  
88      /**
89       * @see IContainerProvider#deploy(String, URL, Credential)
90       */
91      public void deploy(String theContextPath, URL theDeployableObject,
92          Credential theCredentials, IProgressMonitor thePM)
93          throws CoreException
94      {
95          thePM.subTask(CactusMessages.getString("CactusLaunch.message.deploy"));
96          String warPath = theDeployableObject.getPath();
97          antArguments.add("-Dcactus.war=" + warPath);
98          antArguments.add("-Dcactus.context=" + theContextPath);
99      }
100 
101     /**
102      * @see IContainerProvider#start(ContainerInfo)
103      */
104     public void start(ContainerInfo theContainerInfo, IProgressMonitor thePM)
105         throws CoreException
106     {
107         thePM.subTask(CactusMessages.getString("CactusLaunch.message.start"));
108         String target = getTarget("cactus.run.");
109         antArguments.add(
110             "-Dcactus.test.task=" + EclipseRunTests.class.getName());
111         antArguments.add("-Dcactus.home." + this.targetMask + "=" + home);
112         String[] arguments =
113             (String[]) antArguments.toArray(new String[antArguments.size()]);
114         ILaunchConfigurationWorkingCopy antCopy =
115             this.manager.createAntLaunchConfiguration(arguments, target);
116         this.startLaunch = antCopy.launch(
117             ILaunchManager.RUN_MODE,
118             new SubProgressMonitor(thePM, 8));
119     }
120 
121     /**
122      * @see IContainerProvider#stop(ContainerInfo)
123      */
124     public void stop(ContainerInfo theContainerInfo, IProgressMonitor thePM)
125         throws CoreException
126     {
127         thePM.subTask(CactusMessages.getString("CactusLaunch.message.stop"));
128         if (eclipseRunner != null)
129         {
130             eclipseRunner.finish();
131             try
132             {
133                 thePM.worked(30);
134                 while (this.startLaunch == null
135                     || !this.startLaunch.isTerminated())
136                 {
137                     Thread.sleep(300);
138                     thePM.worked(7);
139                 }
140             }
141             catch (InterruptedException e)
142             {
143                 // Do nothing
144             }
145         }
146     }
147 
148     /**
149      * @see IContainerProvider#undeploy (String, Credential)
150      */
151     public void undeploy(String theContextPath, Credential theCredentials,
152         IProgressMonitor thePM) throws CoreException
153     {
154         thePM.subTask(
155             CactusMessages.getString("CactusLaunch.message.undeploy"));
156         String antTarget = getTarget("cactus.clean.");
157         String[] arguments =
158             (String[]) antArguments.toArray(new String[antArguments.size()]);
159         ILaunchConfigurationWorkingCopy antCopy =
160             this.manager.createAntLaunchConfiguration(arguments, antTarget);
161         antCopy.launch(
162             ILaunchManager.RUN_MODE,
163             new SubProgressMonitor(thePM, 50));
164     }
165     
166     /**
167      * @param theEclipseRunner The EclipseRunTests instance to associate with
168      *        this container provider, which will be notified of test run end
169      */
170     public void setEclipseRunner(EclipseRunTests theEclipseRunner)
171     {
172         this.eclipseRunner = theEclipseRunner;
173     }
174 
175     /**
176      * Returns the concatenated String of the prefix and the provider target.
177      * 
178      * @param thePrefix The prefix to append to the provider target
179      * @return The concatenated string
180      */
181     private String getTarget(String thePrefix)
182     {
183         return thePrefix + this.targetMask;
184     }
185 }