KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > registries > SharedTemplatePackagesRegistry


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 //
14
// SharedTemplatePackagesRegistry
15
// NK 26.09.2001
16
//
17

18 package org.jahia.registries;
19
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Hashtable JavaDoc;
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 /**
33  * Registry of shared template packages ( package located in the shared template folder )
34  *
35  * @author Khue ng
36  * @version 1.0
37  */

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 JavaDoc CLASS_NAME = "SharedTemplatePackagesRegistry";
45     private static SharedTemplatePackagesRegistry mInstance = null;
46
47     /**
48      * @associates JahiaTemplatesPackage
49      */

50     private Hashtable JavaDoc theRegistry;
51     private Hashtable JavaDoc tmplPackageNames;
52
53     /**
54      * @associates String
55      */

56     private Hashtable JavaDoc tmplPackageThumbnails;
57
58     /**
59      * @associates String
60      */

61     private Hashtable JavaDoc tmplPackageDescrs;
62     private int count = 0;
63     private static boolean isLoaded = false;
64     private String JavaDoc sharedTemplateFolder = null;
65     private String JavaDoc templatePreviewFolder = null;
66
67     public static final String JavaDoc TEMPLATES_PREVIEW_FOLDER = "templates_preview";
68
69     //--------------------------------------------------------------------------
70
/**
71      * constructor
72      */

73     private SharedTemplatePackagesRegistry()
74     {
75         logger.debug("Starting Shared Template Packages Registry" );
76         init();
77     } // end constructor
78

79     //--------------------------------------------------------------------------
80
/***
81      * getInstance
82      *
83      */

84     public static synchronized SharedTemplatePackagesRegistry getInstance()
85     {
86         if (mInstance == null) {
87             mInstance = new SharedTemplatePackagesRegistry();
88         }
89         return mInstance;
90     } // end getInstance
91

92     //--------------------------------------------------------------------------
93
/***
94      * init
95      * called from init() method in Jahia servlet
96      *
97      */

98     private void init()
99     {
100         theRegistry = new Hashtable JavaDoc();
101         tmplPackageNames = new Hashtable JavaDoc();
102         tmplPackageThumbnails = new Hashtable JavaDoc();
103         tmplPackageDescrs = new Hashtable JavaDoc();
104         isLoaded = true;
105     } // end init
106

107     //--------------------------------------------------------------------------
108
/***
109      * init and load template package from the shared template folder
110      *
111      * @param String sharedTemplateFolder
112      * @param String templatePreviewFolder
113      *
114      */

115     public void init(String JavaDoc sharedTemplateFolder, String JavaDoc 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 JavaDoc previewFolder = new File JavaDoc(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 JavaDoc f = new File JavaDoc( sharedTemplateFolder );
130             String JavaDoc filename = null;
131             if ( f.isDirectory() ){
132                 File JavaDoc[] 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     } // end init
146

147     //--------------------------------------------------------------------------
148
/***
149      * get Templates Package by its index
150      * @author NK
151      *
152      * @param int index
153      */

154     public JahiaTemplatesPackage getTemplatePackage( int index )
155     throws JahiaException
156     {
157         return (JahiaTemplatesPackage)theRegistry.get(new Integer JavaDoc(index));
158     }
159
160     //--------------------------------------------------------------------------
161
/***
162      * get Templates Package by its file name
163      * @author NK
164      *
165      * @param String filename, the package's file name
166      */

167     public JahiaTemplatesPackage getTemplatePackage( String JavaDoc filename )
168     throws JahiaException
169     {
170         if ( filename == null ){
171             return null;
172         }
173         Integer JavaDoc I = (Integer JavaDoc)tmplPackageNames.get(filename);
174         if ( I == null ){
175             return null;
176         }
177         return getTemplatePackage(I.intValue());
178     }
179
180     //--------------------------------------------------------------------------
181
/***
182      * Add a template package, if there is already a package with same file name
183      * replace it.
184      * @author NK
185      *
186      * @param JahiaTemplatesPackage tmplPack, a templates package
187      */

188     public void addTemplatePackage( JahiaTemplatesPackage tmplPack )
189     throws JahiaException
190     {
191         if ( tmplPack != null && tmplPack.getFileName() != null ){
192             Integer JavaDoc I = (Integer JavaDoc)tmplPackageNames.get(tmplPack.getFileName());
193             if ( I == null ){
194                 count++;
195                 tmplPack.setID(count);
196                 theRegistry.put(new Integer JavaDoc(count),tmplPack);
197             } else {
198                 // replace
199
theRegistry.put(I,tmplPack);
200             }
201
202             // extract the thumbnail img
203
extractThumbnailImg(tmplPack);
204             extractDescrFile(tmplPack);
205
206             logger.debug("Package name=" + tmplPack.getName());
207         }
208     }
209
210     //--------------------------------------------------------------------------
211
/***
212      * Add a template package
213      * @author NK
214      *
215      * @param String filePath, the full path to a template jar file
216      */

217     public void addTemplatePackage( String JavaDoc filePath )
218     throws JahiaException
219     {
220         JahiaTemplatesPackageHandler ph = new JahiaTemplatesPackageHandler(filePath);
221         addTemplatePackage(ph.getPackage());
222         ph.closeArchiveFile();
223         ph = null;
224     }
225
226     //--------------------------------------------------------------------------
227
/***
228      * remove a templates package by its file name
229      *
230      * @author NK
231      *
232      * @param String filename
233      */

234     public void removeTemplatePackage( String JavaDoc filename )
235     throws JahiaException
236     {
237         Integer JavaDoc 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     //--------------------------------------------------------------------------
248
/**
249      * Return the list of template packages
250      *
251      * @author NK
252      *
253      */

254     public Enumeration JavaDoc getAllTemplatePackages(){
255         return theRegistry.elements();
256     }
257
258     //--------------------------------------------------------------------------
259
/**
260      * Return the thumbnail image file for a gived template package
261      *
262      * @author NK
263      * @param String filename, the filename of the template package
264      */

265     public String JavaDoc getTemplatePackageThumbnail(String JavaDoc filename){
266         return (String JavaDoc)tmplPackageThumbnails.get(filename);
267     }
268
269     //--------------------------------------------------------------------------
270
/**
271      * Return the index attached to a template package filename
272      *
273      * @author NK
274      *
275      * @param (String) the package filename
276      * @return (Integer) the index
277      */

278     private Integer JavaDoc getIndex(String JavaDoc filename){
279         if ( filename == null ){
280             return null;
281         }return (Integer JavaDoc)tmplPackageNames.get(filename);
282     }
283
284     //--------------------------------------------------------------------------
285
/**
286      * Return the number of package in the registry
287      *
288      * @author NK
289      *
290      * @return (Integer) the number of package in the registry
291      */

292     public int getNbPackage(){
293         return theRegistry.size();
294     }
295
296
297     //--------------------------------------------------------------------------
298
/**
299      * Extract the thumbnail image
300      *
301      * @author NK
302      *
303      * @param (JahiaTemplatesPackage) the template package
304      */

305     private void extractThumbnailImg(JahiaTemplatesPackage tmplPack){
306
307         if ( tmplPack == null ){
308             return;
309         }
310
311         String JavaDoc thumbnail = tmplPack.getThumbnail();
312         String JavaDoc thumbnailExt = "";
313         if ( ( thumbnail!= null) && (thumbnail.indexOf(".") !=-1) ){
314             thumbnailExt = thumbnail.substring(thumbnail.indexOf("."),thumbnail.length());
315
316             // extract the thumbnail img
317
File JavaDoc f = new File JavaDoc(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 JavaDoc filename =
325                                 new StringBuffer JavaDoc(this.templatePreviewFolder);
326
327                         filename.append(File.separator);
328                         filename.append(JahiaTools.removeFileExtension(tmplPack.getFileName(),".jar"));
329                         filename.append(thumbnailExt);
330                         File JavaDoc tmpFile = zip.extractFile(tmplPack.getThumbnail());
331                         tmpFile.renameTo(new File JavaDoc(filename.toString()));
332                         File JavaDoc thumbFile = new File JavaDoc(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 JavaDoc ioe ){
342                     logger.error("Exception "+ ioe.getMessage(), ioe );
343                 } finally {
344                     zip.closeArchiveFile();
345                 }
346             }
347         }
348     }
349
350
351     //--------------------------------------------------------------------------
352
/**
353      * Extract the description file
354      *
355      * @author NK
356      *
357      * @param (JahiaTemplatesPackage) the template package
358      */

359     private void extractDescrFile(JahiaTemplatesPackage tmplPack){
360
361         if ( tmplPack == null ){
362             return;
363         }
364
365         // extract the thumbnail img
366
File JavaDoc f = new File JavaDoc(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 JavaDoc filename =
375                             new StringBuffer JavaDoc(this.templatePreviewFolder);
376
377                     filename.append(File.separator);
378                     filename.append(JahiaTools.removeFileExtension(tmplPack.getFileName(),".jar"));
379                     filename.append(".txt");
380                     File JavaDoc tmpFile = zip.extractFile(JahiaTemplatesPackage.DESCR_FILE);
381                     tmpFile.renameTo(new File JavaDoc(filename.toString()));
382                     File JavaDoc descrFile = new File JavaDoc(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 JavaDoc ioe ){
392                 logger.error("Exception "+ ioe.getMessage(), ioe );
393             } finally {
394                 zip.closeArchiveFile();
395             }
396         }
397     }
398
399
400     //--------------------------------------------------------------------------
401
/**
402      * Return true is this Class is already initialized
403      *
404      * @author NK
405      *
406      * @return (boolean) isLoaded
407      */

408     public static boolean isLoaded(){
409         return isLoaded;
410     }
411
412
413 }
Popular Tags