View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.tools.pamanager.servletcontainer;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.net.MalformedURLException;
23  import java.net.Socket;
24  import java.net.UnknownHostException;
25  
26  import org.apache.commons.httpclient.HostConfiguration;
27  import org.apache.commons.httpclient.HttpClient;
28  import org.apache.commons.httpclient.HttpMethod;
29  import org.apache.commons.httpclient.NameValuePair;
30  import org.apache.commons.httpclient.UsernamePasswordCredentials;
31  import org.apache.commons.httpclient.methods.GetMethod;
32  import org.apache.commons.httpclient.methods.PutMethod;
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  
36  /***
37   * <p>
38   * TomcatManager
39   * </p>
40   * 
41   * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
42   * @version $Id: TomcatManager.java 517719 2007-03-13 15:05:48Z ate $
43   *
44   */
45  public class TomcatManager implements ApplicationServerManager
46  {
47      private static final String DEFAULT_MANAGER_APP_PATH = "/manager";
48      protected static final Log log = LogFactory.getLog("deployment");
49  
50      private String hostUrl;
51      private int hostPort;
52      private String userName;
53      private String password;
54      
55      
56      private String managerAppPath = DEFAULT_MANAGER_APP_PATH;
57      private String stopPath = managerAppPath + "/stop";
58      private String startPath = managerAppPath + "/start";
59      private String deployPath = managerAppPath + "/deploy";
60      private String undeployPath = managerAppPath + "/undeploy";
61      private HttpClient client;
62  
63      private HttpMethod start;
64  
65      private HttpMethod stop;
66  
67      private HttpMethod undeploy;
68  
69      private PutMethod deploy;
70  
71      public TomcatManager(String catalinaBase, String catalinaEngine, String hostName, int hostPort, String userName, String password) throws IOException
72      {
73          super();
74          
75          if ( !catalinaBase.endsWith("/") )
76          {
77          }
78          else
79          {
80          }    
81          this.hostUrl = hostName;
82          this.hostPort = hostPort;
83          this.userName = userName;
84          this.password = password;
85      }
86      
87      private ApplicationServerManagerResult parseResult(String responseBody)
88      {
89          if ( responseBody.startsWith("OK - "))
90          {
91              return new ApplicationServerManagerResult(true, responseBody.substring(5), responseBody);
92          }
93          else if ( responseBody.startsWith("FAIL - "))
94          {
95              return new ApplicationServerManagerResult(false, responseBody.substring(7), responseBody);
96          }
97          else
98          {
99              return new ApplicationServerManagerResult(false, responseBody, responseBody);
100         }
101     }
102 
103     public void start() 
104     {     
105         client = new HttpClient();
106 
107         HostConfiguration hostConfig = new HostConfiguration();
108         hostConfig.setHost(hostUrl, hostPort, "http");
109 
110         client.setHostConfiguration(hostConfig);
111         // Fix for non-buffereing large WAR files during deploy
112         client.getState().setAuthenticationPreemptive(true);
113         client.getState().setCredentials(null, hostUrl, new UsernamePasswordCredentials(userName, password));
114 
115         start = new GetMethod(startPath);
116         stop = new GetMethod(stopPath);
117         undeploy = new GetMethod(undeployPath);
118         deploy = new PutMethod(deployPath);
119     }
120 
121     public ApplicationServerManagerResult start(String appPath) throws IOException
122     {
123         try
124         {
125             start.setQueryString(buildPathQueryArgs(appPath));
126             client.executeMethod(start);
127             return parseResult(start.getResponseBodyAsString());
128         }
129         finally
130         {
131             start.recycle();
132             start.setPath(startPath);
133         }
134     }
135 
136     public ApplicationServerManagerResult stop(String appPath) throws IOException
137     {
138         try
139         {
140             stop.setQueryString(buildPathQueryArgs(appPath));
141             client.executeMethod(stop);
142             return parseResult(stop.getResponseBodyAsString());
143         }
144         finally
145         {
146             stop.recycle();
147             stop.setPath(stopPath);
148         }
149     }
150 
151     public ApplicationServerManagerResult reload(String appPath) throws IOException
152     {
153         try
154         {
155            // reload.setQueryString(buildPathQueryArgs(appPath));
156             // This is the only way to get changes made to web.xml to
157             // be picked up, reload DOES NOT reload the web.xml
158             stop(appPath);
159             Thread.sleep(1500);
160             return start(appPath);
161         }
162         catch (InterruptedException e)
163         {
164             return parseResult("FAIL - "+e.toString());
165         }
166         finally
167         {
168             stop.recycle();
169             stop.setPath(stopPath);
170             start.recycle();
171             start.setPath(startPath);
172         }
173     }
174 
175     public ApplicationServerManagerResult undeploy(String appPath) throws IOException
176     {
177         try
178         {
179             undeploy.setQueryString(buildPathQueryArgs(appPath));
180             client.executeMethod(undeploy);
181             return parseResult(undeploy.getResponseBodyAsString());
182         }
183         finally
184         {
185             undeploy.recycle();
186             undeploy.setPath(undeployPath);
187         }
188     }
189 
190     public ApplicationServerManagerResult deploy(String appPath, InputStream is, int size) throws IOException
191     {
192         try
193         {
194             deploy.setQueryString(buildPathQueryArgs(appPath));
195 
196             //deploy.setRequestContentLength(PutMethod.CONTENT_LENGTH_CHUNKED);
197 
198             if (size != -1)
199             {
200                 deploy.setRequestContentLength(size);
201             }
202             deploy.setRequestBody(is);
203 
204             client.executeMethod(deploy);
205             return parseResult(deploy.getResponseBodyAsString());
206         }
207         finally
208         {
209             deploy.recycle();
210             deploy.setPath(deployPath);
211         }
212     }
213 
214     protected NameValuePair[] buildPathQueryArgs(String appPath)
215     {
216         if (!appPath.startsWith("/"))
217         {
218             appPath = "/" + appPath;
219         }
220         return new NameValuePair[] { new NameValuePair("path", appPath)};
221     }
222 
223     protected NameValuePair[] buildWarQueryArgs(String warPath, String appPath) throws MalformedURLException
224     {
225         return new NameValuePair[] {
226                 new NameValuePair("war", new File(warPath).toURL().toString()),
227                 new NameValuePair("path", appPath)};
228     }
229 
230     protected NameValuePair[] buildConfigQueryArgs(String configPath, String appPath) throws MalformedURLException
231     {
232         return new NameValuePair[] {
233                 new NameValuePair("config", new File(configPath).toURL().toString()),
234                 new NameValuePair("path", appPath)};
235     }
236 
237     /***
238      * @return
239      */
240     public int getHostPort()
241     {
242         return hostPort;
243     }
244 
245     /***
246      * @return
247      */
248     public String getHostUrl()
249     {
250         return hostUrl;
251     }
252 
253     /***
254      * <p>
255      * isConnected
256      * </p>
257      *
258      * @see org.apache.jetspeed.tools.pamanager.servletcontainer.ApplicationServerManager#isConnected()
259      * @return
260      */
261     public boolean isConnected()
262     {
263         Socket checkSocket = null;
264         try
265         {
266             checkSocket = new Socket(hostUrl, hostPort);
267             return true;
268         }
269         catch (UnknownHostException e1)
270         {
271             log.error("Unknown server: " + e1.toString());
272 
273             return false;
274         }
275         catch (IOException e1)
276         {
277             log.error("IOException: " + e1.toString());
278 
279             return false;
280         }
281         finally
282         {
283             try
284             {
285                 // close the server check
286                 if (checkSocket != null)
287                 {
288                     checkSocket.close();
289                 }
290             }
291             catch (IOException e2)
292             {
293                 // do nothing
294             }
295         }
296     }
297     /***
298      * <p>
299      * stop
300      * </p>
301      * 
302      * @see org.picocontainer.Startable#stop()
303      *  
304      */
305     public void stop()
306     {
307     }
308     
309     public String getAppServerTarget(String appName)
310     {
311         return appName;
312     }
313 }