1 20 21 package org.jahia.services.templates_deployer; 22 23 24 import java.io.File ; 25 import java.io.IOException ; 26 import java.util.Properties ; 27 import java.util.Vector ; 28 29 import org.jahia.bin.Jahia; 30 import org.jahia.data.templates.JahiaTemplateDef; 31 import org.jahia.data.templates.JahiaTemplatesPackage; 32 import org.jahia.data.templates.JahiaTemplatesPackageHandler; 33 import org.jahia.exceptions.JahiaException; 34 import org.jahia.exceptions.JahiaInitializationException; 35 import org.jahia.registries.ServicesRegistry; 36 import org.jahia.services.pages.JahiaPageDefinition; 37 import org.jahia.services.sites.JahiaSite; 38 import org.jahia.settings.SettingsBean; 39 import org.jahia.utils.JahiaTools; 40 import org.jahia.utils.zip.JahiaArchiveFileHandler; 41 42 43 49 public class JahiaTemplatesDeployerBaseService extends JahiaTemplatesDeployerService { 50 51 private static org.apache.log4j.Logger logger = 52 org.apache.log4j.Logger.getLogger(JahiaTemplatesDeployerBaseService.class); 53 54 private static JahiaTemplatesDeployerBaseService m_Instance = null; 55 56 57 private int m_VisibleStatus = 0; 59 60 private static String m_TemplateRootPath = ""; 61 62 63 private static String m_SharedTemplatesPath = ""; 64 65 66 private static String m_JspContext = ""; 67 68 69 private static String m_TemplatesContext = ""; 70 71 72 private static String m_NewTemplatePath = ""; 73 74 75 private static String m_ClassesRootPath; 76 77 78 private static String m_TEMPLATES_CLASSES_ROOT_FOLDER = "jahiatemplates"; 79 80 81 82 protected static final String m_META_INF = "Meta-Inf"; 83 84 85 86 87 90 protected JahiaTemplatesDeployerBaseService(){ 91 logger.info("***** Starting the Jahia Templates Deployer Base Service *****" ); 92 } 93 94 95 99 public static synchronized JahiaTemplatesDeployerBaseService getInstance(){ 100 101 if ( m_Instance == null ){ 102 m_Instance = new JahiaTemplatesDeployerBaseService(); 103 } 104 return m_Instance; 105 } 106 107 108 112 public void init( SettingsBean jSettings ) 113 throws JahiaInitializationException 114 { 115 116 if (!isInitialized ()) { 117 118 119 Properties props = jSettings.getPropertiesFile(); 120 m_TemplateRootPath = jSettings.getJahiaTemplatesDiskPath(); 121 m_JspContext = jSettings.getJspContext(); 122 m_TemplatesContext = jSettings.getTemplatesContext(); 123 m_ClassesRootPath = jSettings.getClassDiskPath() + File.separator + m_TEMPLATES_CLASSES_ROOT_FOLDER; 124 m_NewTemplatePath = jSettings.getJahiaNewTemplatesDiskPath(); 125 m_SharedTemplatesPath = jSettings.getJahiaSharedTemplatesDiskPath(); 126 127 logger.debug("m_TemplateRootPath=" + m_TemplateRootPath); 128 logger.debug("m_JspContext=" + m_JspContext); 129 logger.debug("m_TemplatesContext" + m_TemplatesContext); 130 logger.debug("m_ClassesRootPath=" + m_ClassesRootPath); 131 132 133 134 File f = new File (m_NewTemplatePath); 136 File parent = f.getParentFile(); 137 if ( parent != null ){ 138 File tmpFolder = new File (parent.getAbsolutePath() + File.separator + "tmp"); 139 tmpFolder.mkdirs(); 140 if ( tmpFolder == null || !tmpFolder.isDirectory() ){ 141 String errMsg = " cannot create a temporaty folder " ; 142 logger.error (errMsg + "\n" ); 143 throw new JahiaInitializationException ( errMsg ); 144 } 145 m_TempFolderDiskPath = tmpFolder.getAbsolutePath(); 146 } 147 148 File sTemplates = new File (m_SharedTemplatesPath); 150 sTemplates.mkdirs(); 151 152 mIsServiceInitialized = true; 153 } 154 155 156 } 158 159 171 public boolean deploy(JahiaSite site, String rootFolder, String filePath, boolean moveTemplate) throws JahiaException { 172 173 boolean success = false; 174 String fullPath = null; 175 176 if ( filePath.endsWith(".jar") ){ 177 178 JahiaTemplatesPackageHandler ph = null; 179 180 try { 181 182 logger.debug("Trying to deploy " + filePath + "..."); 183 184 ph = new JahiaTemplatesPackageHandler(filePath); 185 186 JahiaTemplatesPackage tempPack = ph.getPackage(); 187 188 if ( tempPack == null ){ 189 String errMsg =" cannot create a JahiaTemplatesPackage on file " + filePath ; 190 logger.debug(errMsg); 191 throw new JahiaException ("JahiaTemplatesDeployerBaseService::deploy()", "JahiaTemplatesDeployerBaseService" + errMsg , 192 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY); 193 } 194 195 StringBuffer strBuf = new StringBuffer (1024); 196 strBuf.append(m_TemplateRootPath); 197 strBuf.append(File.separator); 198 strBuf.append(site.getSiteKey()); 199 strBuf.append(File.separator); 200 if ( rootFolder.equals("") ){ 201 strBuf.append(tempPack.getRootFolder()); 202 } else { 203 strBuf.append(rootFolder); 204 } 205 206 fullPath = strBuf.toString(); 208 209 File f = new File (fullPath); 211 if ( f != null && !f.isDirectory() ){ 212 f.mkdirs(); 214 } 215 logger.debug ("Delete meta-inf folder=" + f.getAbsolutePath()); 216 removeMetaInfFolder(f.getAbsolutePath()); 218 219 ph.unzip(fullPath); 220 221 222 if ( tempPack.hasClasses () ){ 224 String classesFilePath = fullPath + File.separator + tempPack.getClassesFile(); 225 226 File tmpFile = new File (classesFilePath); 227 try{ 228 if ( tmpFile != null && tmpFile.isFile() ){ 229 JahiaArchiveFileHandler arch = new JahiaArchiveFileHandler(classesFilePath); 230 arch.unzip(m_ClassesRootPath); 231 } else { 232 success = false; 233 } 234 } catch ( IOException ioe ){ 235 String errMsg = "Failed creating JahiaArchiveFileHandler on classes file " ; 236 logger.error(errMsg, ioe); 237 throw new JahiaException ("JahiaTemplatesDeployerBaseService::deploy()", "JahiaTemplatesDeployerBaseService" + errMsg , 238 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY); 239 } 240 } 241 success = true; 242 } catch ( Throwable t ) { 243 244 String errMsg = "Failed handling templates file " ; 245 logger.debug(errMsg, t); 246 throw new JahiaException ("JahiaTemplatesDeployerBaseService::deploy()", "JahiaTemplatesDeployerBaseService" + errMsg , 247 JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY, t); 248 } finally { 249 if ( ph != null ){ 250 ph.closeArchiveFile(); 251 } 252 } 253 254 if (success && moveTemplate){ 255 256 File fileItem = new File (filePath); 258 fileItem.renameTo(new File (fullPath + File.separator + fileItem.getName())); 259 } 260 } 261 262 return success; 263 } 264 265 266 271 public boolean undeploy(JahiaPageDefinition template) throws JahiaException { 272 273 283 return false; 284 285 } 286 287 288 295 public boolean deploy(JahiaSite site, Vector files) { 296 297 synchronized (files) { 298 299 if ( site == null ) { 300 return false; 301 } 302 if (!ServicesRegistry.getInstance().getJahiaPageTemplateService().isInitialized()) { 304 return false; 305 } 306 307 if ( !Jahia.isInitiated() ){ 308 return false; 309 } 310 311 int size = files.size(); 312 File fileItem = null; 313 314 for ( int i=0 ; i<size ; i++ ){ 315 316 fileItem = (File )files.get(i); 317 318 if ( fileItem.isFile() && fileItem.getName().endsWith(".jar") ){ 319 320 324 try { 325 326 int templateLimit = Jahia.getTemplateLimit(); 327 328 if ( site.getTemplatesAutoDeployMode() ){ 329 JahiaTemplatesPackage pack = loadTemplatesInfo(fileItem.getAbsolutePath()); 330 if ( pack != null ) { 331 332 int nbTemplates = ServicesRegistry.getInstance() 334 .getJahiaPageTemplateService() 335 .getNbPageTemplates(site.getID()); 336 337 if ( (Jahia.getTemplateLimit() != -1) 338 && (nbTemplates + pack.getTemplates().size()) > 339 templateLimit ){ 340 site.setTemplatesAutoDeployMode(false); 341 342 ServicesRegistry.getInstance().getJahiaSitesService().updateSite(site); 343 344 addNewFile(site,fileItem.getAbsolutePath()); 345 } else { 346 347 if ( deploy(site,"",fileItem.getAbsolutePath(),true) ){ 348 registerTemplates(site, pack); 350 deletePackage(site,fileItem.getAbsolutePath()); 351 } else { 352 fileItem.delete(); 353 deletePackage(site,fileItem.getAbsolutePath()); 354 return false; 355 } 356 } 357 358 } else { 359 try { 360 File newFile = new File (fileItem.getAbsolutePath()+"_error"); 361 fileItem.renameTo(newFile); 363 } catch ( Throwable t ){ 364 } 366 } 367 } else { 368 addNewFile(site,fileItem.getAbsolutePath()); 369 } 370 } catch ( JahiaException e ) { 371 String errMsg = "Failed deploying file " ; 372 logger.error(errMsg, e); 373 fileItem.delete(); 374 return false; 375 } 376 377 } 378 } 379 return true; 380 } 381 } 382 383 384 391 public JahiaTemplatesPackage loadTemplatesInfo(String path) { 392 393 JahiaTemplatesPackage pack = null; 394 395 if ( !JahiaTools.checkFileNameCaseSensitive(path) ){ 397 return null; 398 } 399 400 File f = new File (path); 401 402 try { 403 long fLength = f.length(); 405 Thread.sleep(500); 406 while ( fLength != f.length() ){ 407 fLength = f.length(); 408 Thread.sleep(500); 409 } 410 } catch ( Throwable tr ){ 411 return null; 412 } 413 414 415 if ( f != null && ( f.isFile() || f.isDirectory() ) ){ 416 417 JahiaTemplatesPackageHandler ph = null; 418 try { 419 ph = new JahiaTemplatesPackageHandler(path); 420 pack = ph.getPackage(); 421 return pack; 422 } catch ( JahiaException je ){ 423 return null; 426 } finally { 427 if ( ph != null ){ 428 ph.closeArchiveFile(); 429 } 430 } 431 } 432 433 return pack; 434 } 435 436 437 438 444 public void registerTemplates(JahiaSite site, JahiaTemplatesPackage pack ) throws JahiaException { 445 446 448 449 451 int size = pack.getTemplates().size(); 453 454 StringBuffer tempFullPath = new StringBuffer (1024); 455 tempFullPath.append(m_TemplatesContext); 456 tempFullPath.append(site.getSiteKey()); 457 tempFullPath.append("/") ; 458 tempFullPath.append(pack.getRootFolder()); 459 tempFullPath.append("/") ; 460 461 for ( int i=0 ; i<size ; i++ ) { 462 463 JahiaTemplateDef tempDef = (JahiaTemplateDef)pack.getTemplates().get(i); 464 465 467 JahiaPageDefinition pageDef = ServicesRegistry.getInstance().getJahiaPageTemplateService(). 468 getPageTemplateBySourcePath(site.getID(),tempFullPath.toString() + tempDef.getFileName()); 469 470 if ( pageDef == null || ( !pageDef.getName().equals(tempDef.getDisplayName()) ) ){ 471 472 ServicesRegistry.getInstance().getJahiaPageTemplateService(). 473 createPageTemplate ( 474 475 site.getID(), 476 tempDef.getDisplayName(), 477 tempFullPath.toString() + tempDef.getFileName(), 478 tempDef.isVisible(), 479 "", site.getAclID()); 481 482 logger.debug ("Added template " + tempDef.getFileName() ); 483 } 484 } 485 } 486 487 488 494 public String getTemplateRootPath(){ 495 return m_TemplateRootPath; 496 } 497 498 499 505 public String getTemplatesContext(){ 506 return m_TemplatesContext; 507 } 508 509 510 516 public String getNewTemplatesPath(){ 517 return m_NewTemplatePath; 518 } 519 520 521 527 public String getSharedTemplatesPath(){ 528 return m_SharedTemplatesPath; 529 } 530 531 532 538 protected boolean removeMetaInfFolder(String parentPath){ 539 540 if ( parentPath == null ){ 541 return false; 542 } 543 544 File f = new File (parentPath); 545 File [] files = f.listFiles(); 546 for (int i=0 ; i<files.length ; i++){ 547 if ( files[i].getName().equalsIgnoreCase(m_META_INF) ){ 548 try { 549 return JahiaTools.deleteFile(files[i],false); 550 }catch ( Throwable t ){ 551 t.printStackTrace(); 552 return false; 553 } 554 } 555 } 556 557 return false; 558 } 559 560 } 561 562 | Popular Tags |