1 17 package org.jahia.services.webapps_deployer; 18 19 20 import org.jahia.data.applications.ApplicationBean; 21 import org.jahia.data.webapps.*; 22 import org.jahia.exceptions.JahiaException; 23 import org.jahia.exceptions.JahiaInitializationException; 24 import org.jahia.services.sites.JahiaSite; 25 import org.jahia.services.webapps_deployer.orion.Application_Element; 26 import org.jahia.services.webapps_deployer.orion.Default_Web_Site_Xml; 27 import org.jahia.services.webapps_deployer.orion.Server_Xml; 28 import org.jahia.services.webapps_deployer.orion.Web_App_Element; 29 import org.jahia.settings.SettingsBean; 30 import org.jahia.utils.JahiaTools; 31 32 import java.io.File ; 33 import java.util.Vector ; 34 35 36 43 public class JahiaOrionWebAppsDeployerBaseService extends JahiaWebAppsDeployerService { 44 45 private static org.apache.log4j.Logger logger = 46 org.apache.log4j.Logger.getLogger (JahiaOrionWebAppsDeployerBaseService.class); 47 48 private static JahiaOrionWebAppsDeployerBaseService m_Instance = null; 49 50 51 private String m_ServerXmlFilePath; 52 53 54 private String m_DefaultWebSiteFilePath; 55 56 57 private boolean m_OrionInitErr = false; 58 59 60 63 protected JahiaOrionWebAppsDeployerBaseService () { 64 65 logger.info ("***** Starting the Jahia Orion Deployer Base Service *****"); 66 67 } 68 69 70 73 public static synchronized JahiaOrionWebAppsDeployerBaseService getInstance () { 74 75 if (m_Instance == null) { 76 m_Instance = new JahiaOrionWebAppsDeployerBaseService (); 77 } 78 return m_Instance; 79 } 80 81 82 87 public void init (SettingsBean jSettings) 88 throws JahiaInitializationException { 89 90 if (!isInitialized ()) { 91 92 super.init (jSettings); 93 94 m_ServerXmlFilePath = 95 m_ServerHomeDiskPath + "config" + File.separator + "server.xml"; 96 m_DefaultWebSiteFilePath = 97 m_ServerHomeDiskPath + "config" + File.separator + "default-web-site.xml"; 98 99 File serverXmlFile = new File (m_ServerXmlFilePath); 100 File defaultWebSiteFile = new File (m_DefaultWebSiteFilePath); 101 if (serverXmlFile == null || !serverXmlFile.isFile () 102 || !serverXmlFile.canWrite () || defaultWebSiteFile == null 103 || !defaultWebSiteFile.isFile () || !defaultWebSiteFile.canWrite ()) { 104 String errMsg = serverXmlFile.getAbsolutePath () + " or " + defaultWebSiteFile.getAbsolutePath () + 105 " files cannot be accessed or don't exist "; 106 logger.error (errMsg + "\n"); 107 m_OrionInitErr = true; 108 } 109 110 logger.debug (" Server Home is :" + m_ServerHomeDiskPath); 111 m_WebAppRootPath = jSettings.getJahiaWebAppsDiskPath (); 112 logger.debug (" webAppRoot is :" + m_WebAppRootPath); 113 m_NewWebAppPath = jSettings.getJahiaNewWebAppsDiskPath (); 114 logger.debug (" newWebAppPath is :" + m_NewWebAppPath); 115 116 mIsServiceInitialized = true; 117 118 } 119 120 } 122 123 131 public synchronized boolean deploy (JahiaSite site, String context, String filePath) 132 throws JahiaException { 133 134 if (!canDeploy ()) { 135 return false; 136 } 137 138 boolean success = false; 139 140 if (filePath.endsWith (".ear")) { 141 success = deployEarFile (site, context, filePath); 142 } 143 144 if (success) { 145 deletePackage (site, filePath); 146 return true; 147 } else { 148 return false; 149 } 150 151 } 152 153 154 159 public boolean undeploy (ApplicationBean app) throws JahiaException { 160 161 if (!canDeploy ()) { 162 return false; 163 } 164 165 Server_Xml doc = new Server_Xml (m_ServerXmlFilePath); 166 167 String context = app.getContext (); 168 doc.removeApplication (context.substring (1, context.length ())); doc.write (); 170 171 File f = new File (m_WebAppRootPath + File.separator + app.getContext ()); 173 JahiaTools.deleteFile (f); 174 175 f = new File (m_WebAppRootPath + File.separator + app.getFilename ()); 177 JahiaTools.deleteFile (f); 178 179 return true; 180 } 181 182 183 192 public boolean deploy (JahiaSite site, Vector files) { 193 194 if (!canDeploy ()) { 195 return false; 196 } 197 198 synchronized (files) { 199 200 int size = files.size (); 201 File fileItem = null; 202 203 for (int i = 0; i < size; i++) { 204 205 fileItem = (File ) files.get (i); 206 207 if (fileItem != null && fileItem.isFile ()) { 208 209 logger.debug (" JahiaOrionWebAppsDeployerBaseService found new webApp: " + 210 fileItem.getName ()); 211 212 try { 213 214 JahiaWebAppsPackage pack = null; 215 if (site.getWebAppsAutoDeployMode ()) { 216 if (fileItem.getName ().endsWith (".ear")) { 217 pack = loadWebAppInfo (fileItem.getAbsolutePath ()); 218 if (pack != null) { 219 if (!deploy (site, pack.getContextRoot (), 220 fileItem.getAbsolutePath ())) { 221 fileItem.delete (); 222 return false; 223 } 224 } 225 } 226 } else { 227 if (fileItem.getName ().endsWith (".ear")) { 228 addNewFile (site, fileItem.getAbsolutePath ()); 229 } 230 } 231 232 } catch (JahiaException e) { 233 String errMsg = "Failed deploying Application file " + fileItem.getName (); 234 logger.error (errMsg + "\n", e); 235 236 fileItem.delete (); 237 } 238 } 239 } 240 } 241 return true; 242 } 243 244 245 252 protected boolean deployWarFile (String webContext, String filePath) throws JahiaException { 253 254 return false; 255 } 256 257 258 267 protected boolean deployEarFile (JahiaSite site, String appContext, String filePath) 268 throws JahiaException { 269 270 logger.debug ("started"); 271 272 File earFile = new File (filePath); 274 File tmpFile = new File (m_WebAppRootPath + earFile.getName ()); 275 if (tmpFile != null && tmpFile.isFile ()) { 276 tmpFile.delete (); 277 } 278 279 if (!earFile.renameTo (tmpFile)) { 280 281 earFile.delete (); 282 String errMsg = "Failed moving the ear file in the deployed context root"; 283 logger.error (errMsg); 284 throw new JahiaException ("JahiaOrionWebAppsDeployerBaseService.deployEarFile", 285 "JahiaOrionWebAppsDeployerBaseService" + errMsg, 286 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY); 287 } 288 289 filePath = tmpFile.getAbsolutePath (); 290 291 JahiaEarFileHandler earh = null; 293 294 try { 295 296 earh = new JahiaEarFileHandler (filePath); 297 298 String appContextPath = createApplicationContext (appContext); 300 if (appContextPath == null) { 301 302 String errMsg = "Failed creating the application context root dir"; 303 logger.error (errMsg); 304 throw new JahiaException ("JahiaOrionWebAppsDeployerBaseService.deployEarFile", 305 "JahiaOrionWebAppsDeployerBaseService" + errMsg, 306 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY); 307 } 308 309 Vector webComponents = earh.getWebComponents (); 311 int size = webComponents.size (); 312 313 if (size <= 0) { 314 return false; 315 } 316 317 Web_Component webComp = null; 319 String webURI = null; String webAppContext = ""; 321 322 323 for (int i = 0; i < size; i++) { 324 325 webComp = (Web_Component) webComponents.get (i); 326 327 webURI = webComp.getWebURI (); 328 329 if (webURI != null && (webURI.length () > 0)) { 330 331 logger.debug ("start extracting entry " + webURI); 332 333 earh.extractEntry (webURI, m_TempFolderDiskPath); 335 336 File warFile = new File (m_TempFolderDiskPath + File.separator + webURI); 338 339 webAppContext = JahiaTools.removeFileExtension (warFile.getName (), ".war"); 341 String webAppPath = appContextPath + File.separator + webAppContext; 342 Vector webApps = handleWarFile (webAppPath, warFile.getAbsolutePath ()); 343 344 warFile.delete (); 345 346 355 356 activateWebApps (appContext, appContext, webApps); 358 359 registerWebApps (site, appContext, earFile.getName (), webApps); 361 362 } 363 } 364 365 earh.unzip (appContextPath); 367 earh.closeArchiveFile (); 368 369 Server_Xml doc = new Server_Xml (m_ServerXmlFilePath); 371 372 Application_Element appEl = new Application_Element (appContext, 373 ".." + File.separator + "applications" + File.separator + tmpFile.getName (), 374 "", "", "" ); 378 if (doc.appendApplication (appEl)) { 379 doc.write (); 380 } 381 382 383 } catch (JahiaException e) { 384 385 String errMsg = "Failed handling webApps file "; 386 logger.error (errMsg + "\n" + e.toString ()); 387 throw new JahiaException ("JahiaOrionWebAppsDeployerBaseService::deployWarFile()", 388 "JahiaOrionWebAppsDeployerBaseService" + errMsg, 389 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY); 390 } finally { 391 392 if (earh != null) { 395 earh.closeArchiveFile (); 396 } 397 } 398 399 return true; 400 } 401 402 403 412 protected Vector handleWarFile (String webAppContext, String filePath) 413 throws JahiaException { 414 415 JahiaWebAppsWarHandler wah = null; 417 418 Vector webApps = new Vector (); 419 420 try { 421 422 wah = new JahiaWebAppsWarHandler (filePath); 423 424 webApps = wah.getWebAppsPackage ().getWebApps (); 425 int size = webApps.size (); 426 427 if (size > 0) { 428 429 File f = new File (webAppContext); 431 if (!f.isDirectory () && !f.mkdirs ()) { 432 433 String errMsg = "Failed creating the war context root dir " + f.getAbsolutePath (); 434 logger.error (errMsg + "\n"); 435 throw new JahiaException ( 436 "JahiaOrionWebAppsDeployerBaseService::deployWarFile()", 437 "JahiaOrionWebAppsDeployerBaseService" + errMsg, 438 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY); 439 } 440 441 removeMetaInfFolder (f.getAbsolutePath ()); 443 444 wah.unzip (webAppContext); 446 } 447 } catch (JahiaException e) { 448 449 String errMsg = "Failed handling webApps file "; 450 logger.error (errMsg + "\n", e); 451 throw new JahiaException ("JahiaOrionWebAppsDeployerBaseService::deployWarFile()", 452 "JahiaOrionWebAppsDeployerBaseService" + errMsg, 453 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY); 454 } finally { 455 if (wah != null) { 458 wah.closeArchiveFile (); 459 } 460 } 461 return webApps; 462 } 463 464 465 471 protected void activateWebApps (String application, 472 String appContext, 473 Vector webApps) 474 throws JahiaException { 475 476 Default_Web_Site_Xml doc = new Default_Web_Site_Xml ( 477 m_DefaultWebSiteFilePath); 478 479 Web_App_Element appEl = null; 480 int size = webApps.size (); 481 int counter = 0; 482 483 for (int i = 0; i < size; i++) { 484 485 JahiaWebAppDef webAppDef = (JahiaWebAppDef) webApps.get (i); 486 appEl = new Web_App_Element ( 487 application, 488 "/" + appContext, 489 webAppDef.getContextRoot (), 490 "", "", "" ); 494 if (doc.addWebApp (appEl)) { 495 counter += 1; 496 } 497 498 } 499 500 if (counter > 0) { 501 doc.write (); 502 } 503 } 504 505 506 513 protected String createApplicationContext (String appContext) { 514 515 StringBuffer strBuf = new StringBuffer (1024); 516 strBuf.append (m_WebAppRootPath); 517 strBuf.append (appContext); 518 519 String appContextPath = strBuf.toString (); 521 522 File f = new File (appContextPath); 524 525 if (!f.isDirectory () && !f.mkdirs ()) { 526 527 return null; 528 } 529 530 return appContextPath; 531 } 532 533 539 public boolean canDeploy () { 540 541 return (!m_OrionInitErr); 542 543 } 544 545 546 } | Popular Tags |