1 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 ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.InputStreamReader ; 28 import java.net.MalformedURLException ; 29 import java.net.URL ; 30 import java.net.URLConnection ; 31 import java.util.Vector ; 32 33 34 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 51 private static String m_TomcatVersion = JahiaConstants.SERVER_TOMCAT; 52 53 54 private static String m_TomcatUserName = "Jahia"; 55 56 57 private static String m_TomcatUserPassword = "Jahia"; 58 59 60 private String m_JahiaWebAppsDeployerBaseURL = ""; 61 62 63 private String m_ListUrlBase = ""; 64 65 66 private String m_DeployUrlBase = ""; 67 68 69 private String m_UnDeployUrlBase = ""; 70 71 72 private String m_ReloadUrlBase = ""; 73 74 75 private String m_StartUrlBase = ""; 76 77 78 private String m_StopUrlBase = ""; 79 80 81 private URLConnection m_Conn = null; 82 83 private URL m_ContextURL = null; 84 85 private static final String CLASS_NAME = "TomcatWebAppsDeployer"; 86 87 92 public TomcatWebAppsDeployer (String tomcatVersion, 93 String m_WebAppsDeployerBaseURL, 94 String username, 95 String 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 ("http://localhost:8080"); 106 } catch (MalformedURLException mue) { 107 logger.error ("Error in URL http://localhost:8080", mue); 108 } 109 110 115 116 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)) { 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 151 public Vector getAppList () { 152 153 logger.debug ("Retrieving application list from server through URL=" 154 + m_ListUrlBase); 155 156 String outPut = null; 157 try { 158 outPut = readInputStream (new URL (m_ContextURL, m_ListUrlBase), 159 m_TomcatUserName, 160 m_TomcatUserPassword); 161 } catch (java.net.MalformedURLException mfu) { 162 logger.error ("Error in URL processing", mfu); 163 return null; 164 } 165 166 Vector vec = new Vector (); 167 168 if (outPut != null && outPut.startsWith ("OK")) { 169 170 String [] 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 196 public boolean deploy (String path, 197 String war) { 198 199 200 StringBuffer buff = new StringBuffer (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 outPut = null; 209 URL deployURL = null; 210 211 try { 212 deployURL = new URL (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 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 252 public boolean undeploy (String path) { 253 254 255 StringBuffer buff = new StringBuffer (1024); 256 buff.append (m_UnDeployUrlBase); 257 buff.append ("path=" + path); 258 259 logger.debug ("Undeploying using URL=" 260 + buff.toString ()); 261 262 String outPut = null; 263 try { 264 outPut = readInputStream (new URL (m_ContextURL, buff.toString ()), 265 m_TomcatUserName, 266 m_TomcatUserPassword); 267 } catch (java.net.MalformedURLException 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 295 public boolean stop (String path) { 296 297 298 StringBuffer buff = new StringBuffer (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 outPut = null; 306 try { 307 308 outPut = readInputStream (new URL (m_ContextURL, buff.toString ()), 309 m_TomcatUserName, 310 m_TomcatUserPassword); 311 } catch (java.net.MalformedURLException 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 336 public boolean start (String path) { 337 338 339 StringBuffer buff = new StringBuffer (1024); 340 buff.append (m_StartUrlBase); 341 buff.append ("path=" + path); 342 343 logger.debug ("Starting application using URL=" + buff.toString ()); 344 345 String outPut = null; 346 try { 347 outPut = readInputStream (new URL (m_ContextURL, buff.toString ()), 348 m_TomcatUserName, 349 m_TomcatUserPassword); 350 } catch (java.net.MalformedURLException 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 375 public boolean reload (String path) { 376 377 StringBuffer buff = new StringBuffer (1024); 378 buff.append (m_ReloadUrlBase); 379 buff.append ("path=" + path); 380 381 logger.debug ("Reloading application using URL=" + buff.toString ()); 382 383 String outPut = null; 384 try { 385 outPut = readInputStream (new URL (m_ContextURL, buff.toString ()), 386 m_TomcatUserName, 387 m_TomcatUserPassword); 388 } catch (java.net.MalformedURLException 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 416 public String readInputStream (URL url, 417 String username, 418 String password 419 ) { 420 421 StringBuffer outPut = new StringBuffer (1024); 422 String line = null; 423 424 try { 425 BufferedReader in = new BufferedReader ( 426 new InputStreamReader (openURLForInput (url, username, password))); 427 while ((line = in.readLine ()) != null) { 428 outPut.append (line); 429 } 430 } catch (IOException 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 448 public InputStream openURLForInput (URL url, 449 String username, 450 String password 451 ) throws IOException { 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 471 public String userNamePasswordBase64 (String username, String 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 private static String base64Encode (String string) { 490 String 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 |