KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > webapps_deployer > tomcat > TomcatWebAppsDeployer


1 //
2
// ____.
3
// __/\ ______| |__/\. _______
4
// __ .____| | \ | +----+ \
5
// _______| /--| | | - \ _ | : - \_________
6
// \\______: :---| : : | : | \________>
7
// |__\---\_____________:______: :____|____:_____\
8
// /_____|
9
//
10
// . . . i n j a h i a w e t r u s t . . .
11
//
12
//
13
// NK 06.02.2001
14
//
15
//
16

17
18 package org.jahia.services.webapps_deployer.tomcat;
19
20
21 import org.jahia.data.constants.JahiaConstants;
22 import org.jahia.utils.JahiaTools;
23
24 import java.io.BufferedReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.InputStreamReader JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.net.URLConnection JavaDoc;
31 import java.util.Vector JavaDoc;
32
33
34 /**
35  * Handle web app deployment and activation under tomcat
36  * Create a Url object handler used to call the Tomcat Management
37  * Application servlet Url ( org.apache.catalina.servlets.ManagerServlet ).
38  * We call this servlet to deploy webapps under tomcat
39  *
40  * @author Khue ng
41  * @version 1.0
42  */

43
44
45 public class TomcatWebAppsDeployer {
46
47     private static org.apache.log4j.Logger logger =
48             org.apache.log4j.Logger.getLogger (TomcatWebAppsDeployer.class);
49
50     /** The Tomcat Version * */
51     private static String JavaDoc m_TomcatVersion = JahiaConstants.SERVER_TOMCAT;
52
53     /** The Tomcat user name * */
54     private static String JavaDoc m_TomcatUserName = "Jahia";
55
56     /** The Tomcat user password * */
57     private static String JavaDoc m_TomcatUserPassword = "Jahia";
58
59     /** the Server Host Http Path * */
60     private String JavaDoc m_JahiaWebAppsDeployerBaseURL = "";
61
62     /** the list url * */
63     private String JavaDoc m_ListUrlBase = "";
64
65     /** the deploy url * */
66     private String JavaDoc m_DeployUrlBase = "";
67
68     /** the undeploy url * */
69     private String JavaDoc m_UnDeployUrlBase = "";
70
71     /** the reload url * */
72     private String JavaDoc m_ReloadUrlBase = "";
73
74     /** the start url * */
75     private String JavaDoc m_StartUrlBase = "";
76
77     /** the stop url * */
78     private String JavaDoc m_StopUrlBase = "";
79
80     /** The Url Connection * */
81     private URLConnection JavaDoc m_Conn = null;
82
83     private URL JavaDoc m_ContextURL = null;
84
85     private static final String JavaDoc CLASS_NAME = "TomcatWebAppsDeployer";
86
87     /**
88      * Constructor
89      *
90      * @param (String) the host http
91      */

92     public TomcatWebAppsDeployer (String JavaDoc tomcatVersion,
93                                   String JavaDoc m_WebAppsDeployerBaseURL,
94                                   String JavaDoc username,
95                                   String JavaDoc password
96                                   ) {
97
98
99         m_TomcatVersion = tomcatVersion;
100         m_JahiaWebAppsDeployerBaseURL = m_WebAppsDeployerBaseURL;
101         m_TomcatUserName = username;
102         m_TomcatUserPassword = password;
103
104         try {
105             m_ContextURL = new URL JavaDoc ("http://localhost:8080");
106         } catch (MalformedURLException JavaDoc mue) {
107             logger.error ("Error in URL http://localhost:8080", mue);
108         }
109
110         /*
111         logger.debug("TomcatWebAppsDeployer, using username=" +
112                              m_TomcatUserName + " and password=" +
113                              m_TomcatUserPassword);
114         */

115
116         //default
117
m_ListUrlBase = m_JahiaWebAppsDeployerBaseURL + "/manager/list?";
118         m_DeployUrlBase = m_JahiaWebAppsDeployerBaseURL + "/manager/deploy?";
119         m_UnDeployUrlBase = m_JahiaWebAppsDeployerBaseURL + "/manager/undeploy?";
120         m_ReloadUrlBase = m_JahiaWebAppsDeployerBaseURL + "/manager/reload?";
121         m_StartUrlBase = m_JahiaWebAppsDeployerBaseURL + "/manager/start?";
122         m_StopUrlBase = m_JahiaWebAppsDeployerBaseURL + "/manager/stop?";
123
124         if (!m_TomcatVersion.endsWith (JahiaConstants.SERVER_TOMCAT4_BETA1)) { // the server is tomcatb1...
125
m_DeployUrlBase = m_JahiaWebAppsDeployerBaseURL + "/manager/install?";
126             m_UnDeployUrlBase = m_JahiaWebAppsDeployerBaseURL + "/manager/remove?";
127         }
128
129         m_DeployUrlBase = m_JahiaWebAppsDeployerBaseURL + "/manager/install?";
130         m_UnDeployUrlBase = m_JahiaWebAppsDeployerBaseURL + "/manager/remove?";
131
132     }
133
134
135     //-------------------------------------------------------------------------
136
/**
137      * Call the list url , below is what we receive in text/plain
138      * * OK - Listed applications for virtual host localhost
139      * * /Bookcards:running:0
140      * * /TodoList:stopped:0
141      * * /examples2:running:0
142      * * /webdav:running:0
143      * * /jahia:running:1
144      * * /examples:running:0
145      * * /manager:running:0
146      * * /:running:0
147      *
148      * @return (Vector) return a vector of context retrieved from the response
149      * null if fail.
150      */

151     public Vector JavaDoc getAppList () {
152
153         logger.debug ("Retrieving application list from server through URL="
154                 + m_ListUrlBase);
155
156         String JavaDoc outPut = null;
157         try {
158             outPut = readInputStream (new URL JavaDoc (m_ContextURL, m_ListUrlBase),
159                     m_TomcatUserName,
160                     m_TomcatUserPassword);
161         } catch (java.net.MalformedURLException JavaDoc mfu) {
162             logger.error ("Error in URL processing", mfu);
163             return null;
164         }
165
166         Vector JavaDoc vec = new Vector JavaDoc ();
167
168         if (outPut != null && outPut.startsWith ("OK")) {
169
170             String JavaDoc[] tockens = JahiaTools.getTokens (outPut, "/");
171             for (int i = 1; i < (tockens.length - 1); i++) {
172                 vec.add (tockens[i]);
173             }
174         } else {
175             logger.error (
176                     "Error while retrieving application list with URL=" + m_ListUrlBase + " output=" + outPut);
177             vec = null;
178         }
179         return vec;
180     }
181
182
183
184     //-------------------------------------------------------------------------
185
/**
186      * Call the deploy url
187      *
188      * @param (String) path, the context path to associate to the web apps
189      * @param (String) war, the path to a war file to deploy or the path to
190      * the unpacked directory to asscotiate with a context
191      * ex: http://127.0.0.1:8080/manager/deploy?path=/AbsenceRequest&
192      * war=jar:file:/d:/tomcat/webapps/jahia/war_webapps/AbsenceRequest.war!/
193      *
194      * @return (boolean) return true if deployed correctly
195      */

196     public boolean deploy (String JavaDoc path,
197                            String JavaDoc war) {
198
199
200         StringBuffer JavaDoc buff = new StringBuffer JavaDoc (1024);
201         buff.append (m_DeployUrlBase);
202         buff.append ("path=");
203         buff.append (path);
204         buff.append ("&");
205         buff.append ("war=");
206         buff.append (war);
207
208         String JavaDoc outPut = null;
209         URL JavaDoc deployURL = null;
210
211         try {
212             deployURL = new URL JavaDoc (m_ContextURL, buff.toString ());
213
214             logger.debug ("deploying via URL=" +
215                     deployURL.toString () + "...");
216
217             outPut = readInputStream (deployURL,
218                     m_TomcatUserName,
219                     m_TomcatUserPassword);
220
221         } catch (java.net.MalformedURLException JavaDoc mfu) {
222             logger.error ("Error while deploying", mfu);
223             return false;
224         }
225
226
227         if (outPut != null) {
228             if (outPut.toString ().startsWith ("OK")) {
229                 logger.debug ("deployment via URL " +
230                         deployURL.toString () + " successfull");
231                 return true;
232             } else {
233                 logger.error ("deployment via URL " +
234                         deployURL.toString () +
235                         " not successful. Returned output=" +
236                         outPut);
237             }
238         }
239         return false;
240     }
241
242
243     //-------------------------------------------------------------------------
244
/**
245      * Call the undeploy url
246      *
247      * @param (String) path, the context path to associate to the web apps
248      * ex: http://127.0.0.1:8080/manager/undeploy?path=/AbsenceRequest
249      *
250      * @return (boolean) return true if deployed correctly
251      */

252     public boolean undeploy (String JavaDoc path) {
253
254
255         StringBuffer JavaDoc buff = new StringBuffer JavaDoc (1024);
256         buff.append (m_UnDeployUrlBase);
257         buff.append ("path=" + path);
258
259         logger.debug ("Undeploying using URL="
260                 + buff.toString ());
261
262         String JavaDoc outPut = null;
263         try {
264             outPut = readInputStream (new URL JavaDoc (m_ContextURL, buff.toString ()),
265                     m_TomcatUserName,
266                     m_TomcatUserPassword);
267         } catch (java.net.MalformedURLException JavaDoc mfu) {
268             logger.error ("Error while undeploying", mfu);
269             return false;
270         }
271
272
273         if (outPut != null && outPut.toString ().startsWith ("OK")) {
274             logger.debug ("Undeploy successful");
275             return true;
276         } else {
277             logger.error ("Undeployment via URL " +
278                     buff.toString () +
279                     " not successful. Returned output=" +
280                     outPut);
281         }
282         return false;
283     }
284
285
286     //-------------------------------------------------------------------------
287
/**
288      * Call the stop url
289      *
290      * @param (String) path, the context path to associate to the web apps
291      * ex: http://127.0.0.1:8080/manager/stop?path=/AbsenceRequest
292      *
293      * @return (boolean) return true if stopped correctly
294      */

295     public boolean stop (String JavaDoc path) {
296
297
298         StringBuffer JavaDoc buff = new StringBuffer JavaDoc (1024);
299         buff.append (m_StopUrlBase);
300         buff.append ("path=" + path);
301
302         logger.debug ("Stopping application using URL="
303                 + buff.toString ());
304
305         String JavaDoc outPut = null;
306         try {
307
308             outPut = readInputStream (new URL JavaDoc (m_ContextURL, buff.toString ()),
309                     m_TomcatUserName,
310                     m_TomcatUserPassword);
311         } catch (java.net.MalformedURLException JavaDoc mfu) {
312             logger.error ("Error in stop URL", mfu);
313             return false;
314         }
315
316         if (outPut != null && outPut.toString ().startsWith ("OK")) {
317             logger.debug ("Stop successful");
318             return true;
319         } else {
320             logger.error (
321                     "Stop via URL=" + buff.toString () + " unsuccessful. Output=" + outPut);
322         }
323         return false;
324     }
325
326
327     //-------------------------------------------------------------------------
328
/**
329      * Call the start url
330      *
331      * @param (String) path, the context path to associate to the web apps
332      * ex: http://127.0.0.1:8080/manager/start?path=/AbsenceRequest
333      *
334      * @return (boolean) return true if started correctly
335      */

336     public boolean start (String JavaDoc path) {
337
338
339         StringBuffer JavaDoc buff = new StringBuffer JavaDoc (1024);
340         buff.append (m_StartUrlBase);
341         buff.append ("path=" + path);
342
343         logger.debug ("Starting application using URL=" + buff.toString ());
344
345         String JavaDoc outPut = null;
346         try {
347             outPut = readInputStream (new URL JavaDoc (m_ContextURL, buff.toString ()),
348                     m_TomcatUserName,
349                     m_TomcatUserPassword);
350         } catch (java.net.MalformedURLException JavaDoc mfu) {
351             logger.error ("Error while starting application using URL" + buff.toString (), mfu);
352             return false;
353         }
354
355         if (outPut != null && outPut.toString ().startsWith ("OK")) {
356             logger.debug ("Application started successfully");
357             return true;
358         } else {
359             logger.error (
360                     "Error starting application with URL=" + buff.toString () + ". Output=" + outPut);
361         }
362         return false;
363     }
364
365
366     //-------------------------------------------------------------------------
367
/**
368      * Call the reload url
369      *
370      * @param (String) path, the context path to associate to the web apps
371      * ex: http://127.0.0.1:8080/manager/reload?path=/AbsenceRequest
372      *
373      * @return (boolean) return true if reloaded correctly
374      */

375     public boolean reload (String JavaDoc path) {
376
377         StringBuffer JavaDoc buff = new StringBuffer JavaDoc (1024);
378         buff.append (m_ReloadUrlBase);
379         buff.append ("path=" + path);
380
381         logger.debug ("Reloading application using URL=" + buff.toString ());
382
383         String JavaDoc outPut = null;
384         try {
385             outPut = readInputStream (new URL JavaDoc (m_ContextURL, buff.toString ()),
386                     m_TomcatUserName,
387                     m_TomcatUserPassword);
388         } catch (java.net.MalformedURLException JavaDoc mfu) {
389             logger.error ("Error while reloading application using URL=" + buff.toString (),
390                     mfu);
391             return false;
392         }
393
394         if (outPut != null && outPut.toString ().startsWith ("OK")) {
395             logger.debug ("Application successfully reloaded.");
396             return true;
397         } else {
398             logger.error (
399                     "Error while reloading application using URL=" + buff.toString () + ". Output=" + outPut);
400         }
401
402         return false;
403     }
404
405
406     //-------------------------------------------------------------------------
407
/**
408      * Read input stream from url
409      *
410      * @param (String) url, the url
411      * @param (String) username, the username
412      * @param (String) password, the password
413      *
414      * @return (String) the response in text/plain
415      */

416     public String JavaDoc readInputStream (URL JavaDoc url,
417                                    String JavaDoc username,
418                                    String JavaDoc password
419                                    ) {
420
421         StringBuffer JavaDoc outPut = new StringBuffer JavaDoc (1024);
422         String JavaDoc line = null;
423
424         try {
425             BufferedReader JavaDoc in = new BufferedReader JavaDoc (
426                     new InputStreamReader JavaDoc (openURLForInput (url, username, password)));
427             while ((line = in.readLine ()) != null) {
428                 outPut.append (line);
429             }
430         } catch (IOException JavaDoc e) {
431             logger.debug ("Error while retrieving content of URL" + url.toString (), e);
432             return null;
433         }
434         return outPut.toString ();
435     }
436
437
438     //-------------------------------------------------------------------------
439
/**
440      * Open an url connection
441      *
442      * @param (String) url, the url
443      * @param (String) username, the username
444      * @param (String) password, the password
445      *
446      * @return (InputStream)
447      */

448     public InputStream JavaDoc openURLForInput (URL JavaDoc url,
449                                         String JavaDoc username,
450                                         String JavaDoc password
451                                         ) throws IOException JavaDoc {
452         m_Conn = url.openConnection ();
453         m_Conn.setDoInput (true);
454         m_Conn.setRequestProperty ("Authorization",
455                 userNamePasswordBase64 (username, password)
456         );
457         m_Conn.connect ();
458         return m_Conn.getInputStream ();
459     }
460
461
462     //-------------------------------------------------------------------------
463
/**
464      * generate a base 64 encode user name and password string
465      *
466      * @param (String) username, the username
467      * @param (String) password, the password
468      *
469      * @return (String) the base 64 encode user name and password
470      */

471     public String JavaDoc userNamePasswordBase64 (String JavaDoc username, String JavaDoc password) {
472         return "Basic " + base64Encode (username + ":" + password);
473     }
474
475
476     private final static char base64Array [] = {
477         'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
478         'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
479         'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
480         'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
481         'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
482         'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
483         'w', 'x', 'y', 'z', '0', '1', '2', '3',
484         '4', '5', '6', '7', '8', '9', '+', '/'
485     };
486
487
488     //-------------------------------------------------------------------------
489
private static String JavaDoc base64Encode (String JavaDoc string) {
490         String JavaDoc encodedString = "";
491         byte bytes [] = string.getBytes ();
492         int i = 0;
493         int pad = 0;
494         while (i < bytes.length) {
495             byte b1 = bytes[i++];
496             byte b2;
497             byte b3;
498             if (i >= bytes.length) {
499                 b2 = 0;
500                 b3 = 0;
501                 pad = 2;
502             } else {
503                 b2 = bytes[i++];
504                 if (i >= bytes.length) {
505                     b3 = 0;
506                     pad = 1;
507                 } else
508                     b3 = bytes[i++];
509             }
510             byte c1 = (byte) (b1 >> 2);
511             byte c2 = (byte) (((b1 & 0x3) << 4) | (b2 >> 4));
512             byte c3 = (byte) (((b2 & 0xf) << 2) | (b3 >> 6));
513             byte c4 = (byte) (b3 & 0x3f);
514             encodedString += base64Array[c1];
515             encodedString += base64Array[c2];
516             switch (pad) {
517                 case 0:
518                     encodedString += base64Array[c3];
519                     encodedString += base64Array[c4];
520                     break;
521                 case 1:
522                     encodedString += base64Array[c3];
523                     encodedString += "=";
524                     break;
525                 case 2:
526                     encodedString += "==";
527                     break;
528             }
529         }
530         return encodedString;
531     }
532
533
534 }
535
Popular Tags