001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.tools.pamanager.servletcontainer;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.net.MalformedURLException;
023: import java.net.Socket;
024: import java.net.UnknownHostException;
025:
026: import org.apache.commons.httpclient.HostConfiguration;
027: import org.apache.commons.httpclient.HttpClient;
028: import org.apache.commons.httpclient.HttpMethod;
029: import org.apache.commons.httpclient.NameValuePair;
030: import org.apache.commons.httpclient.UsernamePasswordCredentials;
031: import org.apache.commons.httpclient.methods.GetMethod;
032: import org.apache.commons.httpclient.methods.PutMethod;
033: import org.apache.commons.logging.Log;
034: import org.apache.commons.logging.LogFactory;
035:
036: /**
037: * <p>
038: * TomcatManager
039: * </p>
040: *
041: * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
042: * @version $Id: TomcatManager.java 517719 2007-03-13 15:05:48Z ate $
043: *
044: */
045: public class TomcatManager implements ApplicationServerManager {
046: private static final String DEFAULT_MANAGER_APP_PATH = "/manager";
047: protected static final Log log = LogFactory.getLog("deployment");
048:
049: private String hostUrl;
050: private int hostPort;
051: private String userName;
052: private String password;
053:
054: private String managerAppPath = DEFAULT_MANAGER_APP_PATH;
055: private String stopPath = managerAppPath + "/stop";
056: private String startPath = managerAppPath + "/start";
057: private String deployPath = managerAppPath + "/deploy";
058: private String undeployPath = managerAppPath + "/undeploy";
059: private HttpClient client;
060:
061: private HttpMethod start;
062:
063: private HttpMethod stop;
064:
065: private HttpMethod undeploy;
066:
067: private PutMethod deploy;
068:
069: public TomcatManager(String catalinaBase, String catalinaEngine,
070: String hostName, int hostPort, String userName,
071: String password) throws IOException {
072: super ();
073:
074: if (!catalinaBase.endsWith("/")) {
075: } else {
076: }
077: this .hostUrl = hostName;
078: this .hostPort = hostPort;
079: this .userName = userName;
080: this .password = password;
081: }
082:
083: private ApplicationServerManagerResult parseResult(
084: String responseBody) {
085: if (responseBody.startsWith("OK - ")) {
086: return new ApplicationServerManagerResult(true,
087: responseBody.substring(5), responseBody);
088: } else if (responseBody.startsWith("FAIL - ")) {
089: return new ApplicationServerManagerResult(false,
090: responseBody.substring(7), responseBody);
091: } else {
092: return new ApplicationServerManagerResult(false,
093: responseBody, responseBody);
094: }
095: }
096:
097: public void start() {
098: client = new HttpClient();
099:
100: HostConfiguration hostConfig = new HostConfiguration();
101: hostConfig.setHost(hostUrl, hostPort, "http");
102:
103: client.setHostConfiguration(hostConfig);
104: // Fix for non-buffereing large WAR files during deploy
105: client.getState().setAuthenticationPreemptive(true);
106: client.getState().setCredentials(null, hostUrl,
107: new UsernamePasswordCredentials(userName, password));
108:
109: start = new GetMethod(startPath);
110: stop = new GetMethod(stopPath);
111: undeploy = new GetMethod(undeployPath);
112: deploy = new PutMethod(deployPath);
113: }
114:
115: public ApplicationServerManagerResult start(String appPath)
116: throws IOException {
117: try {
118: start.setQueryString(buildPathQueryArgs(appPath));
119: client.executeMethod(start);
120: return parseResult(start.getResponseBodyAsString());
121: } finally {
122: start.recycle();
123: start.setPath(startPath);
124: }
125: }
126:
127: public ApplicationServerManagerResult stop(String appPath)
128: throws IOException {
129: try {
130: stop.setQueryString(buildPathQueryArgs(appPath));
131: client.executeMethod(stop);
132: return parseResult(stop.getResponseBodyAsString());
133: } finally {
134: stop.recycle();
135: stop.setPath(stopPath);
136: }
137: }
138:
139: public ApplicationServerManagerResult reload(String appPath)
140: throws IOException {
141: try {
142: // reload.setQueryString(buildPathQueryArgs(appPath));
143: // This is the only way to get changes made to web.xml to
144: // be picked up, reload DOES NOT reload the web.xml
145: stop(appPath);
146: Thread.sleep(1500);
147: return start(appPath);
148: } catch (InterruptedException e) {
149: return parseResult("FAIL - " + e.toString());
150: } finally {
151: stop.recycle();
152: stop.setPath(stopPath);
153: start.recycle();
154: start.setPath(startPath);
155: }
156: }
157:
158: public ApplicationServerManagerResult undeploy(String appPath)
159: throws IOException {
160: try {
161: undeploy.setQueryString(buildPathQueryArgs(appPath));
162: client.executeMethod(undeploy);
163: return parseResult(undeploy.getResponseBodyAsString());
164: } finally {
165: undeploy.recycle();
166: undeploy.setPath(undeployPath);
167: }
168: }
169:
170: public ApplicationServerManagerResult deploy(String appPath,
171: InputStream is, int size) throws IOException {
172: try {
173: deploy.setQueryString(buildPathQueryArgs(appPath));
174:
175: //deploy.setRequestContentLength(PutMethod.CONTENT_LENGTH_CHUNKED);
176:
177: if (size != -1) {
178: deploy.setRequestContentLength(size);
179: }
180: deploy.setRequestBody(is);
181:
182: client.executeMethod(deploy);
183: return parseResult(deploy.getResponseBodyAsString());
184: } finally {
185: deploy.recycle();
186: deploy.setPath(deployPath);
187: }
188: }
189:
190: protected NameValuePair[] buildPathQueryArgs(String appPath) {
191: if (!appPath.startsWith("/")) {
192: appPath = "/" + appPath;
193: }
194: return new NameValuePair[] { new NameValuePair("path", appPath) };
195: }
196:
197: protected NameValuePair[] buildWarQueryArgs(String warPath,
198: String appPath) throws MalformedURLException {
199: return new NameValuePair[] {
200: new NameValuePair("war", new File(warPath).toURL()
201: .toString()),
202: new NameValuePair("path", appPath) };
203: }
204:
205: protected NameValuePair[] buildConfigQueryArgs(String configPath,
206: String appPath) throws MalformedURLException {
207: return new NameValuePair[] {
208: new NameValuePair("config", new File(configPath)
209: .toURL().toString()),
210: new NameValuePair("path", appPath) };
211: }
212:
213: /**
214: * @return
215: */
216: public int getHostPort() {
217: return hostPort;
218: }
219:
220: /**
221: * @return
222: */
223: public String getHostUrl() {
224: return hostUrl;
225: }
226:
227: /**
228: * <p>
229: * isConnected
230: * </p>
231: *
232: * @see org.apache.jetspeed.tools.pamanager.servletcontainer.ApplicationServerManager#isConnected()
233: * @return
234: */
235: public boolean isConnected() {
236: Socket checkSocket = null;
237: try {
238: checkSocket = new Socket(hostUrl, hostPort);
239: return true;
240: } catch (UnknownHostException e1) {
241: log.error("Unknown server: " + e1.toString());
242:
243: return false;
244: } catch (IOException e1) {
245: log.error("IOException: " + e1.toString());
246:
247: return false;
248: } finally {
249: try {
250: // close the server check
251: if (checkSocket != null) {
252: checkSocket.close();
253: }
254: } catch (IOException e2) {
255: // do nothing
256: }
257: }
258: }
259:
260: /**
261: * <p>
262: * stop
263: * </p>
264: *
265: * @see org.picocontainer.Startable#stop()
266: *
267: */
268: public void stop() {
269: }
270:
271: public String getAppServerTarget(String appName) {
272: return appName;
273: }
274: }
|