1 13 18 package org.jahia.registries; 19 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.util.Enumeration ; 24 import java.util.Hashtable ; 25 26 import org.jahia.data.templates.JahiaTemplatesPackage; 27 import org.jahia.data.templates.JahiaTemplatesPackageHandler; 28 import org.jahia.exceptions.JahiaException; 29 import org.jahia.utils.JahiaTools; 30 import org.jahia.utils.zip.JahiaArchiveFileHandler; 31 32 38 39 public class SharedTemplatePackagesRegistry { 40 41 private static org.apache.log4j.Logger logger = 42 org.apache.log4j.Logger.getLogger(SharedTemplatePackagesRegistry.class); 43 44 private static final String CLASS_NAME = "SharedTemplatePackagesRegistry"; 45 private static SharedTemplatePackagesRegistry mInstance = null; 46 47 50 private Hashtable theRegistry; 51 private Hashtable tmplPackageNames; 52 53 56 private Hashtable tmplPackageThumbnails; 57 58 61 private Hashtable tmplPackageDescrs; 62 private int count = 0; 63 private static boolean isLoaded = false; 64 private String sharedTemplateFolder = null; 65 private String templatePreviewFolder = null; 66 67 public static final String TEMPLATES_PREVIEW_FOLDER = "templates_preview"; 68 69 73 private SharedTemplatePackagesRegistry() 74 { 75 logger.debug("Starting Shared Template Packages Registry" ); 76 init(); 77 } 79 84 public static synchronized SharedTemplatePackagesRegistry getInstance() 85 { 86 if (mInstance == null) { 87 mInstance = new SharedTemplatePackagesRegistry(); 88 } 89 return mInstance; 90 } 92 98 private void init() 99 { 100 theRegistry = new Hashtable (); 101 tmplPackageNames = new Hashtable (); 102 tmplPackageThumbnails = new Hashtable (); 103 tmplPackageDescrs = new Hashtable (); 104 isLoaded = true; 105 } 107 115 public void init(String sharedTemplateFolder, String templatePreviewFolder) 116 { 117 init(); 118 if ( sharedTemplateFolder != null ){ 119 this.sharedTemplateFolder = sharedTemplateFolder; 120 this.templatePreviewFolder = templatePreviewFolder 121 + File.separator 122 + TEMPLATES_PREVIEW_FOLDER; 123 124 File previewFolder = new File (this.templatePreviewFolder); 125 previewFolder.mkdirs(); 126 logger.debug("Template preview folder is " + previewFolder.getAbsolutePath()); 127 logger.debug("Shared template folder is " + sharedTemplateFolder); 128 129 File f = new File ( sharedTemplateFolder ); 130 String filename = null; 131 if ( f.isDirectory() ){ 132 File [] files = f.listFiles(); 133 for ( int i=0 ; i<files.length ; i++){ 134 filename = files[i].getName(); 135 if ( filename.endsWith(".jar") ){ 136 try { 137 addTemplatePackage( files[i].getAbsolutePath() ); 138 } catch ( JahiaException je ){ 139 logger.debug("Exception " + je.getMessage()); 140 } 141 } 142 } 143 } 144 } 145 } 147 154 public JahiaTemplatesPackage getTemplatePackage( int index ) 155 throws JahiaException 156 { 157 return (JahiaTemplatesPackage)theRegistry.get(new Integer (index)); 158 } 159 160 167 public JahiaTemplatesPackage getTemplatePackage( String filename ) 168 throws JahiaException 169 { 170 if ( filename == null ){ 171 return null; 172 } 173 Integer I = (Integer )tmplPackageNames.get(filename); 174 if ( I == null ){ 175 return null; 176 } 177 return getTemplatePackage(I.intValue()); 178 } 179 180 188 public void addTemplatePackage( JahiaTemplatesPackage tmplPack ) 189 throws JahiaException 190 { 191 if ( tmplPack != null && tmplPack.getFileName() != null ){ 192 Integer I = (Integer )tmplPackageNames.get(tmplPack.getFileName()); 193 if ( I == null ){ 194 count++; 195 tmplPack.setID(count); 196 theRegistry.put(new Integer (count),tmplPack); 197 } else { 198 theRegistry.put(I,tmplPack); 200 } 201 202 extractThumbnailImg(tmplPack); 204 extractDescrFile(tmplPack); 205 206 logger.debug("Package name=" + tmplPack.getName()); 207 } 208 } 209 210 217 public void addTemplatePackage( String filePath ) 218 throws JahiaException 219 { 220 JahiaTemplatesPackageHandler ph = new JahiaTemplatesPackageHandler(filePath); 221 addTemplatePackage(ph.getPackage()); 222 ph.closeArchiveFile(); 223 ph = null; 224 } 225 226 234 public void removeTemplatePackage( String filename ) 235 throws JahiaException 236 { 237 Integer I = getIndex(filename); 238 if ( I == null ){ 239 return; 240 } 241 theRegistry.remove(I); 242 tmplPackageNames.remove(filename); 243 tmplPackageThumbnails.remove(filename); 244 tmplPackageDescrs.remove(filename); 245 } 246 247 254 public Enumeration getAllTemplatePackages(){ 255 return theRegistry.elements(); 256 } 257 258 265 public String getTemplatePackageThumbnail(String filename){ 266 return (String )tmplPackageThumbnails.get(filename); 267 } 268 269 278 private Integer getIndex(String filename){ 279 if ( filename == null ){ 280 return null; 281 }return (Integer )tmplPackageNames.get(filename); 282 } 283 284 292 public int getNbPackage(){ 293 return theRegistry.size(); 294 } 295 296 297 305 private void extractThumbnailImg(JahiaTemplatesPackage tmplPack){ 306 307 if ( tmplPack == null ){ 308 return; 309 } 310 311 String thumbnail = tmplPack.getThumbnail(); 312 String thumbnailExt = ""; 313 if ( ( thumbnail!= null) && (thumbnail.indexOf(".") !=-1) ){ 314 thumbnailExt = thumbnail.substring(thumbnail.indexOf("."),thumbnail.length()); 315 316 File f = new File (tmplPack.getFilePath()); 318 319 if ( f.isFile() ){ 320 JahiaArchiveFileHandler zip = null; 321 try { 322 zip = new JahiaArchiveFileHandler(f.getAbsolutePath()); 323 if ( zip != null && zip.entryExists(tmplPack.getThumbnail()) ){ 324 StringBuffer filename = 325 new StringBuffer (this.templatePreviewFolder); 326 327 filename.append(File.separator); 328 filename.append(JahiaTools.removeFileExtension(tmplPack.getFileName(),".jar")); 329 filename.append(thumbnailExt); 330 File tmpFile = zip.extractFile(tmplPack.getThumbnail()); 331 tmpFile.renameTo(new File (filename.toString())); 332 File thumbFile = new File (filename.toString()); 333 if ( thumbFile.isFile() ){ 334 tmplPackageThumbnails.put(tmplPack.getFileName(),thumbFile.getName()); 335 } 336 tmpFile = null; 337 thumbFile = null; 338 } 339 } catch ( JahiaException je ){ 340 logger.error("Exception "+ je.getMessage(), je ); 341 } catch ( IOException ioe ){ 342 logger.error("Exception "+ ioe.getMessage(), ioe ); 343 } finally { 344 zip.closeArchiveFile(); 345 } 346 } 347 } 348 } 349 350 351 359 private void extractDescrFile(JahiaTemplatesPackage tmplPack){ 360 361 if ( tmplPack == null ){ 362 return; 363 } 364 365 File f = new File (tmplPack.getFilePath()); 367 368 if ( f.isFile() ){ 369 JahiaArchiveFileHandler zip = null; 370 try { 371 zip = new JahiaArchiveFileHandler(f.getAbsolutePath()); 372 if ( zip != null && zip.entryExists(JahiaTemplatesPackage.DESCR_FILE) ){ 373 374 StringBuffer filename = 375 new StringBuffer (this.templatePreviewFolder); 376 377 filename.append(File.separator); 378 filename.append(JahiaTools.removeFileExtension(tmplPack.getFileName(),".jar")); 379 filename.append(".txt"); 380 File tmpFile = zip.extractFile(JahiaTemplatesPackage.DESCR_FILE); 381 tmpFile.renameTo(new File (filename.toString())); 382 File descrFile = new File (filename.toString()); 383 if ( descrFile.isFile() ){ 384 tmplPackageDescrs.put(tmplPack.getFileName(),descrFile.getName()); 385 } 386 tmpFile = null; 387 descrFile = null; 388 } 389 } catch ( JahiaException je ){ 390 logger.error("Exception "+ je.getMessage(), je ); 391 } catch ( IOException ioe ){ 392 logger.error("Exception "+ ioe.getMessage(), ioe ); 393 } finally { 394 zip.closeArchiveFile(); 395 } 396 } 397 } 398 399 400 408 public static boolean isLoaded(){ 409 return isLoaded; 410 } 411 412 413 } | Popular Tags |