KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > templates_deployer > JahiaTemplatesDeployerService


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
// JahiaTemplatesDeployerService
15
//
16
// NK 12.01.2001
17
//
18
//
19

20
21 package org.jahia.services.templates_deployer;
22
23
24 import java.io.File JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.Vector JavaDoc;
28
29 import org.jahia.data.templates.JahiaTemplatesPackage;
30 import org.jahia.exceptions.JahiaException;
31 import org.jahia.services.JahiaService;
32 import org.jahia.services.pages.JahiaPageDefinition;
33 import org.jahia.services.sites.JahiaSite;
34
35
36 /**
37  * This Service Deploy templates packed in a .jar file.
38  *
39  * @author Khue ng
40  * @version 1.0
41  */

42 public abstract class JahiaTemplatesDeployerService extends JahiaService {
43
44
45
46     /** a temporary folder **/
47     protected static String JavaDoc m_TempFolderDiskPath = "";
48
49     /** temp folder prefix **/
50     protected static String JavaDoc m_TempFolderPrefix = "todelete_" ;
51
52     /**
53      * The hashtable of templates packages.
54      * The entry key is the path to an archive file ( .jar )
55      * The value Object is a JahiaTemplatesPackage object
56      * containing a list of JahiaWebAppDef ( web components informations bean )
57      *
58      */

59     protected static Hashtable JavaDoc m_TemplatesPackage = new Hashtable JavaDoc();
60
61
62     /** The templates.xml descriptor **/
63     private static final String JavaDoc TEMPLATES_XML = "templates.xml";
64
65
66
67     //-------------------------------------------------------------------------
68
/**
69      * Deploy a template .jar file
70      *
71      * @param (int) the site
72      * @param (String) rootFolder, root folder for the template
73      * @param (String) filePath , the full path to the template.jar file
74      * @param (boolean) moveTemplate , if true, move te template package to the deployed directory when done
75      */

76     public abstract boolean deploy(JahiaSite site, String JavaDoc rootFolder, String JavaDoc filePath, boolean moveTemplate) throws JahiaException ;
77
78
79     //-------------------------------------------------------------------------
80
/**
81      * Undeploy a template
82      *
83      * @param (JahiaPageDefinition) template
84      */

85     public abstract boolean undeploy(JahiaPageDefinition template) throws JahiaException ;
86
87
88     //-------------------------------------------------------------------------
89
/**
90      * Deploy a Vector of .jar templates files
91      *
92      * @param (JahiaSite) the site
93      * @param (Vector) files, a vector of .jar templates package file
94      * @return (boolean) false on exception
95      */

96     public abstract boolean deploy(JahiaSite site, Vector JavaDoc files) ;
97
98
99     //-------------------------------------------------------------------------
100
/**
101      * Return the template root path
102      *
103      * @return String the template root path
104      */

105     public abstract String JavaDoc getTemplateRootPath() ;
106
107
108     //-------------------------------------------------------------------------
109
/**
110      * Return the template jsp context
111      *
112      * @return String the template jsp context
113      */

114     public abstract String JavaDoc getTemplatesContext();
115
116
117     //-------------------------------------------------------------------------
118
/**
119      * Return the new templates root path
120      *
121      * @return String the new templates root path
122      */

123     public abstract String JavaDoc getNewTemplatesPath() ;
124
125
126     //-------------------------------------------------------------------------
127
/**
128      * Return the shared templates folder disk path
129      *
130      * @return String the shared templates folder disk path
131      */

132     public abstract String JavaDoc getSharedTemplatesPath();
133
134
135     //-------------------------------------------------------------------------
136
/**
137      * Try to load info from a .jar template file or an unzipped directory
138      * Informations are read from the templates.xml file
139      *
140      * @param (String) full path to a file or directory
141      * @return a JahiaTemplatesPackage object or null
142      */

143     public abstract JahiaTemplatesPackage loadTemplatesInfo(String JavaDoc path) ;
144
145
146     //-------------------------------------------------------------------------
147
/**
148      * Register templates in Jahia
149      *
150      * @param JahiaSite the site
151      * @param JahiaTemplatesPackage the templates package
152      */

153     public abstract void registerTemplates( JahiaSite site, JahiaTemplatesPackage pack ) throws JahiaException ;
154
155
156
157
158
159
160
161
162     /************************************************************************
163      * Templates Packages ( .jar files ) handling
164      *
165      *
166      * The directory jahiafiles/new_templates is periodically scanned for new
167      * templates packages. A package is a .jar file
168      *
169      * If the informations contained in the templates.xml descriptor
170      * file is scanned successfully a JahiaTemplatesPackage object that holds
171      * all informations needed by Jahia to deploy them is created.
172      *
173      * If the deployment mode is automatic, they are automatically deployed and
174      * registered in Jahia. If not, they are added in the HashTable of packages
175      * waiting to be deployed manually throught the JahiaAdministration interface.
176      *
177      * The Key in the hashtable of packages used to identify each package is
178      * the filename of the package.
179      *
180      ************************************************************************/

181
182
183     //-------------------------------------------------------------------------
184
/**
185      * delete a package reference in the hashtable of detected packages
186      * and delete it physically from disk.
187      *
188      * @param (JahiaSite) the site in which the templates package belong to
189      * @param (String) full path to a file or directory
190      * @return true if successful
191      */

192     public boolean deletePackage(JahiaSite site, String JavaDoc path) {
193         
194         
195         synchronized(m_TemplatesPackage){
196             
197             File JavaDoc tmpFile = new File JavaDoc(path);
198             StringBuffer JavaDoc filename = new StringBuffer JavaDoc(site.getSiteKey());
199             filename.append("_");
200             filename.append(tmpFile.getName());
201             
202             //System.out.println(" deletePackage key is =" + filename );
203

204             if ( tmpFile != null && tmpFile.isFile() ){
205                 if (tmpFile.delete()){
206                     m_TemplatesPackage.remove(filename.toString());
207                     return true;
208                 } else {
209                     return false;
210                 }
211             } else {
212                 m_TemplatesPackage.remove(filename.toString());
213                 return true;
214             }
215         }
216     }
217
218
219     //-------------------------------------------------------------------------
220
/**
221      * add a new file or directory only if it is recognized
222      * as a valid template package.
223      * And only if it is not registered yet.
224      *
225      * @param (JahiaSite) the site for which to register the templates
226      * @param (String) full path to a file or directory
227      */

228     public void addNewFile(JahiaSite site, String JavaDoc path) {
229         
230                     
231         synchronized(m_TemplatesPackage){
232             
233             File JavaDoc tmpFile = new File JavaDoc(path);
234             StringBuffer JavaDoc filename = new StringBuffer JavaDoc(site.getSiteKey());
235             filename.append("_");
236             filename.append(tmpFile.getName());
237             
238             JahiaTemplatesPackage pack = null;
239                 
240             if ( m_TemplatesPackage.get(filename.toString())==null ) {
241                     
242                 pack = loadTemplatesInfo(path);
243                 if ( pack != null ){
244                     m_TemplatesPackage.remove(filename.toString());
245                     m_TemplatesPackage.put(filename.toString(),pack);
246                 }
247             }
248         }
249     }
250
251
252     //-------------------------------------------------------------------------
253
/***
254      * return an Enumerations of the keys of the Hashtable of package
255      *
256      * @return (Enumeration) the enumeration of all keys
257      */

258     public Enumeration JavaDoc getTemplatesPackageKeys(){
259         
260         synchronized(m_TemplatesPackage){
261             return m_TemplatesPackage.keys();
262         }
263     
264     }
265
266
267     //-------------------------------------------------------------------------
268
/***
269      * return an Enumerations of the keys of the Hashtable of package for a gived site
270      *
271      * @return (String) the site key identifier
272      * @return (Enumeration) the enumeration of all keys
273      */

274     public Enumeration JavaDoc getTemplatesPackageKeys(String JavaDoc siteKey){
275         
276         Enumeration JavaDoc enumeration = null;
277         Vector JavaDoc result = new Vector JavaDoc();
278         synchronized(m_TemplatesPackage){
279                 
280             enumeration = m_TemplatesPackage.keys();
281             String JavaDoc name = null;
282             String JavaDoc siteIdent = siteKey + "_";
283             while ( enumeration.hasMoreElements() ){
284                 name = (String JavaDoc)enumeration.nextElement();
285                 if ( name.startsWith(siteIdent) ){
286                     result.add(siteIdent);
287                 }
288             }
289             return result.elements();
290         }
291     
292     }
293
294
295     //-------------------------------------------------------------------------
296
/***
297      * return an enumeration of templates package Hashtable for a gived site
298      *
299      * @return (String) the site key identifier
300      * @return (Enumeration) the enumeration of the packages
301      */

302     public Enumeration JavaDoc getTemplatesPackages(String JavaDoc siteKey){
303
304         Enumeration JavaDoc enumeration = null;
305         Vector JavaDoc result = new Vector JavaDoc();
306         synchronized(m_TemplatesPackage){
307                 
308             enumeration = m_TemplatesPackage.keys();
309             String JavaDoc name = null;
310             String JavaDoc siteIdent = siteKey + "_";
311             while ( enumeration.hasMoreElements() ){
312                 name = (String JavaDoc)enumeration.nextElement();
313                 if ( name.startsWith(siteIdent) ){
314                     result.add(m_TemplatesPackage.get(name));
315                 }
316             }
317             return result.elements();
318         }
319     }
320
321
322     //-------------------------------------------------------------------------
323
/***
324      * return a web app package in the Hashtable m_TemplatesPackage
325      * looking at the key
326      * @param (String) the key
327      * @return (Object) a templates package or null
328      */

329     public Object JavaDoc getTemplatesPackage(String JavaDoc theKey){
330         
331         synchronized(m_TemplatesPackage){
332             return m_TemplatesPackage.get(theKey);
333         }
334     }
335
336
337     //-------------------------------------------------------------------------
338
/***
339      * scan a directory for templates package.
340      * Add them to the HashTable of TemplatesPackage.
341      *
342      * @param the full path to a directory
343      * @return an Hashtable
344      */

345     public void scanDirectory(String JavaDoc path)
346     throws JahiaException {
347         
348         synchronized(m_TemplatesPackage){
349         
350             JahiaTemplatesPackage pack = null;
351         
352             File JavaDoc dir = new File JavaDoc(path);
353             if ( dir != null && dir.isDirectory() ){
354             
355                 File JavaDoc[] files = dir.listFiles();
356                 int size = files.length;
357
358                 for ( int i=0; i<size ; i++ ){
359                 
360                     if ( files[i].canWrite() ){
361                 
362                         pack = loadTemplatesInfo(files[i].getAbsolutePath());
363                         if ( pack != null ){
364                             //System.out.println("added package in hashtable " + files[i].getAbsolutePath() );
365
// remove old map
366
m_TemplatesPackage.remove(files[i].getName());
367                             // set the new map
368
m_TemplatesPackage.put(files[i].getName(),pack);
369                         }
370                     }
371                 }
372             }
373         }
374     }
375
376
377
378
379
380
381
382
383
384
385    
386
387 } // end JahiaTemplatesDeployerService
Popular Tags