1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
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 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
public class TomcatManager implements ApplicationServerManager |
46 |
|
{ |
47 |
|
private static final String DEFAULT_MANAGER_APP_PATH = "/manager"; |
48 |
0 |
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 |
0 |
private String managerAppPath = DEFAULT_MANAGER_APP_PATH; |
57 |
0 |
private String stopPath = managerAppPath + "/stop"; |
58 |
0 |
private String startPath = managerAppPath + "/start"; |
59 |
0 |
private String deployPath = managerAppPath + "/deploy"; |
60 |
0 |
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 |
0 |
super(); |
74 |
|
|
75 |
0 |
if ( !catalinaBase.endsWith("/") ) |
76 |
|
{ |
77 |
|
} |
78 |
|
else |
79 |
|
{ |
80 |
|
} |
81 |
0 |
this.hostUrl = hostName; |
82 |
0 |
this.hostPort = hostPort; |
83 |
0 |
this.userName = userName; |
84 |
0 |
this.password = password; |
85 |
0 |
} |
86 |
|
|
87 |
|
private ApplicationServerManagerResult parseResult(String responseBody) |
88 |
|
{ |
89 |
0 |
if ( responseBody.startsWith("OK - ")) |
90 |
|
{ |
91 |
0 |
return new ApplicationServerManagerResult(true, responseBody.substring(5), responseBody); |
92 |
|
} |
93 |
0 |
else if ( responseBody.startsWith("FAIL - ")) |
94 |
|
{ |
95 |
0 |
return new ApplicationServerManagerResult(false, responseBody.substring(7), responseBody); |
96 |
|
} |
97 |
|
else |
98 |
|
{ |
99 |
0 |
return new ApplicationServerManagerResult(false, responseBody, responseBody); |
100 |
|
} |
101 |
|
} |
102 |
|
|
103 |
|
public void start() |
104 |
|
{ |
105 |
0 |
client = new HttpClient(); |
106 |
|
|
107 |
0 |
HostConfiguration hostConfig = new HostConfiguration(); |
108 |
0 |
hostConfig.setHost(hostUrl, hostPort, "http"); |
109 |
|
|
110 |
0 |
client.setHostConfiguration(hostConfig); |
111 |
|
|
112 |
0 |
client.getState().setAuthenticationPreemptive(true); |
113 |
0 |
client.getState().setCredentials(null, hostUrl, new UsernamePasswordCredentials(userName, password)); |
114 |
|
|
115 |
0 |
start = new GetMethod(startPath); |
116 |
0 |
stop = new GetMethod(stopPath); |
117 |
0 |
undeploy = new GetMethod(undeployPath); |
118 |
0 |
deploy = new PutMethod(deployPath); |
119 |
0 |
} |
120 |
|
|
121 |
|
public ApplicationServerManagerResult start(String appPath) throws IOException |
122 |
|
{ |
123 |
|
try |
124 |
|
{ |
125 |
0 |
start.setQueryString(buildPathQueryArgs(appPath)); |
126 |
0 |
client.executeMethod(start); |
127 |
0 |
return parseResult(start.getResponseBodyAsString()); |
128 |
|
} |
129 |
|
finally |
130 |
|
{ |
131 |
0 |
start.recycle(); |
132 |
0 |
start.setPath(startPath); |
133 |
|
} |
134 |
|
} |
135 |
|
|
136 |
|
public ApplicationServerManagerResult stop(String appPath) throws IOException |
137 |
|
{ |
138 |
|
try |
139 |
|
{ |
140 |
0 |
stop.setQueryString(buildPathQueryArgs(appPath)); |
141 |
0 |
client.executeMethod(stop); |
142 |
0 |
return parseResult(stop.getResponseBodyAsString()); |
143 |
|
} |
144 |
|
finally |
145 |
|
{ |
146 |
0 |
stop.recycle(); |
147 |
0 |
stop.setPath(stopPath); |
148 |
|
} |
149 |
|
} |
150 |
|
|
151 |
|
public ApplicationServerManagerResult reload(String appPath) throws IOException |
152 |
|
{ |
153 |
|
try |
154 |
|
{ |
155 |
|
|
156 |
|
|
157 |
|
|
158 |
0 |
stop(appPath); |
159 |
0 |
Thread.sleep(1500); |
160 |
0 |
return start(appPath); |
161 |
|
} |
162 |
0 |
catch (InterruptedException e) |
163 |
|
{ |
164 |
0 |
return parseResult("FAIL - "+e.toString()); |
165 |
|
} |
166 |
|
finally |
167 |
|
{ |
168 |
0 |
stop.recycle(); |
169 |
0 |
stop.setPath(stopPath); |
170 |
0 |
start.recycle(); |
171 |
0 |
start.setPath(startPath); |
172 |
|
} |
173 |
|
} |
174 |
|
|
175 |
|
public ApplicationServerManagerResult undeploy(String appPath) throws IOException |
176 |
|
{ |
177 |
|
try |
178 |
|
{ |
179 |
0 |
undeploy.setQueryString(buildPathQueryArgs(appPath)); |
180 |
0 |
client.executeMethod(undeploy); |
181 |
0 |
return parseResult(undeploy.getResponseBodyAsString()); |
182 |
|
} |
183 |
|
finally |
184 |
|
{ |
185 |
0 |
undeploy.recycle(); |
186 |
0 |
undeploy.setPath(undeployPath); |
187 |
|
} |
188 |
|
} |
189 |
|
|
190 |
|
public ApplicationServerManagerResult deploy(String appPath, InputStream is, int size) throws IOException |
191 |
|
{ |
192 |
|
try |
193 |
|
{ |
194 |
0 |
deploy.setQueryString(buildPathQueryArgs(appPath)); |
195 |
|
|
196 |
|
|
197 |
|
|
198 |
0 |
if (size != -1) |
199 |
|
{ |
200 |
0 |
deploy.setRequestContentLength(size); |
201 |
|
} |
202 |
0 |
deploy.setRequestBody(is); |
203 |
|
|
204 |
0 |
client.executeMethod(deploy); |
205 |
0 |
return parseResult(deploy.getResponseBodyAsString()); |
206 |
|
} |
207 |
|
finally |
208 |
|
{ |
209 |
0 |
deploy.recycle(); |
210 |
0 |
deploy.setPath(deployPath); |
211 |
|
} |
212 |
|
} |
213 |
|
|
214 |
|
protected NameValuePair[] buildPathQueryArgs(String appPath) |
215 |
|
{ |
216 |
0 |
if (!appPath.startsWith("/")) |
217 |
|
{ |
218 |
0 |
appPath = "/" + appPath; |
219 |
|
} |
220 |
0 |
return new NameValuePair[] { class="keyword">new NameValuePair("path", appPath)}; |
221 |
|
} |
222 |
|
|
223 |
|
protected NameValuePair[] buildWarQueryArgs(String warPath, String appPath) throws MalformedURLException |
224 |
|
{ |
225 |
0 |
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 |
0 |
return new NameValuePair[] { |
233 |
|
new NameValuePair("config", new File(configPath).toURL().toString()), |
234 |
|
new NameValuePair("path", appPath)}; |
235 |
|
} |
236 |
|
|
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
public int getHostPort() |
241 |
|
{ |
242 |
0 |
return hostPort; |
243 |
|
} |
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
public String getHostUrl() |
249 |
|
{ |
250 |
0 |
return hostUrl; |
251 |
|
} |
252 |
|
|
253 |
|
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
public boolean isConnected() |
262 |
|
{ |
263 |
0 |
Socket checkSocket = null; |
264 |
|
try |
265 |
|
{ |
266 |
0 |
checkSocket = new Socket(hostUrl, hostPort); |
267 |
0 |
return true; |
268 |
|
} |
269 |
0 |
catch (UnknownHostException e1) |
270 |
|
{ |
271 |
0 |
log.error("Unknown server: " + e1.toString()); |
272 |
|
|
273 |
0 |
return false; |
274 |
|
} |
275 |
0 |
catch (IOException e1) |
276 |
|
{ |
277 |
0 |
log.error("IOException: " + e1.toString()); |
278 |
|
|
279 |
0 |
return false; |
280 |
|
} |
281 |
|
finally |
282 |
|
{ |
283 |
0 |
try |
284 |
|
{ |
285 |
|
|
286 |
0 |
if (checkSocket != null) |
287 |
|
{ |
288 |
0 |
checkSocket.close(); |
289 |
|
} |
290 |
|
} |
291 |
0 |
catch (IOException e2) |
292 |
|
{ |
293 |
|
|
294 |
0 |
} |
295 |
|
} |
296 |
|
} |
297 |
|
|
298 |
|
|
299 |
|
|
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
|
305 |
|
public void stop() |
306 |
|
{ |
307 |
0 |
} |
308 |
|
|
309 |
|
public String getAppServerTarget(String appName) |
310 |
|
{ |
311 |
0 |
return appName; |
312 |
|
} |
313 |
|
} |