KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > webapps_deployer > JahiaWebAppsDeployerService


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

20
21 package org.jahia.services.webapps_deployer;
22
23
24 import org.jahia.data.applications.ApplicationBean;
25 import org.jahia.data.webapps.*;
26 import org.jahia.exceptions.JahiaException;
27 import org.jahia.exceptions.JahiaInitializationException;
28 import org.jahia.registries.ServicesRegistry;
29 import org.jahia.services.JahiaService;
30 import org.jahia.services.acl.ACLNotFoundException;
31 import org.jahia.services.acl.JahiaBaseACL;
32 import org.jahia.services.sites.JahiaSite;
33 import org.jahia.settings.SettingsBean;
34 import org.jahia.utils.JahiaTools;
35 import org.xml.sax.EntityResolver JavaDoc;
36
37 import java.io.File JavaDoc;
38 import java.util.Enumeration JavaDoc;
39 import java.util.Hashtable JavaDoc;
40 import java.util.Vector JavaDoc;
41
42
43 /**
44  * Web Application Deployment service API
45  *
46  * @author Khue ng
47  * @version 1.0
48  */

49 public abstract class JahiaWebAppsDeployerService extends JahiaService {
50
51     private static org.apache.log4j.Logger logger =
52             org.apache.log4j.Logger.getLogger (JahiaWebAppsDeployerService.class);
53
54     private static final String JavaDoc CLASS_NAME = JahiaWebAppsDeployerService.class.getName ();
55
56     /** The Server Type * */
57     protected String JavaDoc m_ServerType = "";
58
59     /** The Server Home Disk Path * */
60     protected String JavaDoc m_ServerHomeDiskPath = "";
61
62     /** The full path to the WebApp Root Folder * */
63     protected static String JavaDoc m_WebAppRootPath = "";
64
65     /** The full path to the New WebApp Folder * */
66     protected static String JavaDoc m_NewWebAppPath = "";
67
68     /** The full path to jahia folder * */
69     protected static String JavaDoc m_JahiaHomeDiskPath = "";
70
71     /** The Shared Components Path * */
72     private static String JavaDoc m_SharedComponentsPath = "";
73
74
75     /** The right * */
76     protected int m_AppRight = 1; // FIXME , where to get it ?
77

78     /** Is the web Apps visible by default to all user * */
79     protected int m_VisibleStatus = 1; // FIXME , where to get it ?
80

81     /** a temporary folder * */
82     protected static String JavaDoc m_TempFolderDiskPath = "";
83
84     /** temp folder prefix * */
85     protected static String JavaDoc m_TempFolderPrefix = "todelete_";
86
87     /** the DTD Entity Resolver we use with local dtd files * */
88     protected static EntityResolver JavaDoc m_Resolver;
89
90
91     /** WEB-INF folder * */
92     protected static final String JavaDoc m_WEB_INF = "WEB-INF";
93
94     /** Meta-Inf folder * */
95     protected static final String JavaDoc m_META_INF = "Meta-Inf";
96
97
98     /**
99      * The hashtable of web apps packages.
100      * The entry key is the path to an archive file ( war, ear )
101      * or an unziped directory
102      * The value Object is a vector of JahiaWebAppsPackage
103      * containing a list of JahiaWebAppDef ( web components informations bean )
104      */

105     protected static Hashtable JavaDoc m_WebAppsPackage = new Hashtable JavaDoc ();
106
107
108     /** The web.xml file in case of .war file * */
109     private static final String JavaDoc WEB_XML_FILE = "WEB-INF/web.xml";
110
111
112     /**
113      * Services initializations
114      *
115      * @param JahiaPrivateSettings jSettings
116      */

117     public void init (SettingsBean jSettings)
118             throws JahiaInitializationException {
119
120         if (!isInitialized ()) {
121
122             m_ServerType = jSettings.getServer ();
123             m_ServerHomeDiskPath = jSettings.getServerHomeDiskPath ();
124             m_WebAppRootPath = jSettings.getJahiaWebAppsDiskPath ();
125             m_NewWebAppPath = jSettings.getJahiaNewWebAppsDiskPath ();
126             m_JahiaHomeDiskPath = jSettings.getJahiaHomeDiskPath ();
127             m_SharedComponentsPath = jSettings.getJahiaSharedComponentsDiskPath ();
128             m_Resolver = (EntityResolver JavaDoc) jSettings.getDtdEntityResolver ();
129
130             logger.debug (" jahiaHomeDiskPath= " + m_JahiaHomeDiskPath);
131
132             // create the temporary folder
133
File JavaDoc f = new File JavaDoc (m_NewWebAppPath);
134             File JavaDoc parent = f.getParentFile ();
135             if (parent != null) {
136                 File JavaDoc tmpFolder = new File JavaDoc (parent.getAbsolutePath () + File.separator + "tmp");
137                 tmpFolder.mkdirs ();
138                 if (tmpFolder == null || !tmpFolder.isDirectory ()) {
139                     String JavaDoc errMsg = " cannot create a temporaty folder ";
140                     logger.error (errMsg);
141                     throw new JahiaInitializationException (errMsg);
142                 }
143                 m_TempFolderDiskPath = tmpFolder.getAbsolutePath ();
144             }
145
146             // create the shared components folder
147
File JavaDoc sComponents = new File JavaDoc (m_SharedComponentsPath);
148             sComponents.mkdirs ();
149
150             mIsServiceInitialized = true;
151         }
152
153     } // end init
154

155
156
157     /************************************************************************
158      * Abstract Methods , need different implementations depending of the
159      * type of server ( Tomcat, orion , ... )
160      *
161      *
162      *
163      ***********************************************************************/

164
165
166     /**
167      * Hot Deploy a .ear or .war file.
168      * Hot Deploy a web component on the specifiy server, active them to
169      * be immediately accessible in Jahia
170      * If it's a .ear file, the context is the application context
171      * If it's a .war file, the context is the web application context
172      *
173      * @param (JahiaSite) the site
174      * @param (String) context , the context
175      * @param (String) filePath , the full path to the ear file
176      *
177      * @return (boolean) true if successfull
178      */

179     public abstract boolean deploy (JahiaSite site, String JavaDoc context, String JavaDoc filePath)
180             throws JahiaException;
181
182
183     /**
184      * Undeploy a web application. Delete the web component from disk.
185      *
186      * @param (ApplicationBean) the application bean object
187      *
188      * @return (boolean) true if successfull
189      */

190     public abstract boolean undeploy (ApplicationBean app) throws JahiaException;
191
192
193     /**
194      * A Simple way to pass a vector of .ear or .war files
195      * to be deployed or to be added in the hashtable of packages
196      *
197      * @param (JahiaSite) the site
198      * @param (Vector) files, a vector of .ear application files
199      */

200     public abstract boolean deploy (JahiaSite site, Vector JavaDoc files);
201
202     //-------------------------------------------------------------------------
203
/**
204      * Return true if deployment functionallities are available or not
205      * If they are not available ( due to some init initialization or missing resources),
206      * deploy services always return false.
207      */

208     public abstract boolean canDeploy ();
209
210
211     /**
212      * ***********************************************************************
213      * Default Implementations
214      * ***********************************************************************
215      */

216
217     public static String JavaDoc getNewWebAppsPath () {
218
219         return m_NewWebAppPath;
220
221     }
222
223
224     public static String JavaDoc getWebAppsRootPath () {
225
226         return m_WebAppRootPath;
227
228     }
229
230     public static String JavaDoc getJahiaHomeDiskPath () {
231
232         return m_JahiaHomeDiskPath;
233
234     }
235
236     public static String JavaDoc getSharedComponentsPath () {
237
238         return m_SharedComponentsPath;
239
240     }
241
242
243     /**
244      * Register Web Apps Definition in Jahia
245      *
246      * @param (JahiaSite) the site
247      * @param (String) appContext , the context root for the web apps
248      * @param (String) filename , the package file ( .war or .ear needed for undeploying under orion
249      * @param (Vector) webApps , the vector of JahiaWebAppDef objects
250      */

251     public void registerWebApps (JahiaSite site, String JavaDoc appContext, String JavaDoc filename,
252                                  Vector JavaDoc webApps) throws JahiaException {
253
254         int size = webApps.size ();
255
256         //System.out.println("registerWebApps started");
257

258         for (int i = 0; i < size; i++) {
259
260             JahiaWebAppDef webAppDef = (JahiaWebAppDef) webApps.get (i);
261
262             // check if an application with same context and servletsrc already exists or not
263
ApplicationBean theWebApp = ServicesRegistry.getInstance ().
264                     getJahiaApplicationsManagerService ().
265                     getApplication ("/" + appContext);
266
267             if (theWebApp == null) {
268
269                 //System.out.println("registerWebApps app context " + appContext + " not used");
270
int parentAclID = site.getAclID ();
271
272                 // Create a new ACL.
273
JahiaBaseACL acl = new JahiaBaseACL ();
274                 if (acl != null) {
275                     try {
276                         acl.create (parentAclID);
277                     } catch (ACLNotFoundException ex) {
278                         throw new JahiaException ("Could not create the page def.",
279                                 "The parent ACL ID [" + parentAclID + "] could not be found," +
280                                 " while trying to create a new page def.",
281                                 JahiaException.TEMPLATE_ERROR, JahiaException.ERROR_SEVERITY);
282                     }
283                 } else {
284                     throw new JahiaException ("Could not create page def.",
285                             "Could not instanciate a new ACL object while trying to create a new page def.",
286                             JahiaException.TEMPLATE_ERROR, JahiaException.CRITICAL_SEVERITY);
287                 }
288
289                 // save definition in db
290
theWebApp = new ApplicationBean (
291                         0, // id
292
site.getID (),
293                         webAppDef.getName (),
294                         "/" + appContext,
295                         m_VisibleStatus,
296                         false, // not shared
297
acl.getID (),
298                         filename,
299                         webAppDef.getdesc ()
300                 );
301
302                 ServicesRegistry.getInstance ().getJahiaApplicationsManagerService ()
303                         .addDefinition (theWebApp);
304
305                 //System.out.println("registerWebApps()" + webAppDef.getName() );
306

307             }
308         }
309     }
310
311     /**
312      * Try to scan web component deployment descriptor files
313      * ( META-INF\applications.xml or WEB-INF\web.xml file ).
314      * A JahiaWebAppsPackage object is returned that holds
315      * informations extracted from those files
316      *
317      * @param (String) full path to a file or directory
318      *
319      * @return a JahiaWebAppsPackage object or null
320      */

321     public JahiaWebAppsPackage loadWebAppInfo (String JavaDoc path) {
322
323         synchronized (m_WebAppsPackage) {
324
325             JahiaWebAppsPackage pack = null;
326
327             // check for case sensitive
328
if (!JahiaTools.checkFileNameCaseSensitive (path)) {
329                 return null;
330             }
331
332             File JavaDoc f = new File JavaDoc (path);
333
334             try {
335                 // wait while the file is still modified
336
long fLength = f.length ();
337                 Thread.sleep (500);
338                 while (fLength != f.length ()) {
339                     fLength = f.length ();
340                     Thread.sleep (500);
341                 }
342             } catch (Throwable JavaDoc tr) {
343                 return null;
344             }
345
346             if (f != null && (f.isFile () || f.isDirectory ())) {
347                 if (f.isDirectory ()) {
348                     try {
349                         pack = loadWebAppInfoFromDirectory (path);
350                     } catch (JahiaException je) {
351                         return null;
352                     }
353                 } else {
354                     try {
355                         pack = loadWebAppInfoFromFile (path);
356                     } catch (JahiaException je) {
357                         return null;
358                     }
359                 }
360             }
361             return pack;
362         }
363     }
364
365
366     /**
367      * read the informations contained in a web.xml file
368      *
369      * @param path , the full path to a directory
370      *
371      * @return a JahiaWebAppsPackage or null
372      */

373     protected JahiaWebAppsPackage loadWebAppInfoFromDirectory (String JavaDoc path)
374             throws JahiaException {
375
376         Web_App_Xml doc = null;
377         Vector JavaDoc vec = new Vector JavaDoc ();
378         JahiaWebAppsPackage pack = null;
379
380         // extract data from the web.xml file
381
doc = new Web_App_Xml (path + File.separator + WEB_XML_FILE);
382
383         doc.extractDocumentData ();
384
385         // Actually the Context Root for the web application is the current directory
386
File JavaDoc tmpFile = new File JavaDoc (path);
387
388
389         String JavaDoc contextRoot = tmpFile.getName ();
390         int endIndex = contextRoot.lastIndexOf(".");
391         if(endIndex>0)
392         contextRoot = contextRoot.substring(0,endIndex);
393         // build the list of the Web Apps Definition
394
pack = new JahiaWebAppsPackage (contextRoot);
395
396         Vector JavaDoc servlets = doc.getServlets ();
397         int size = servlets.size ();
398
399         Servlet_Element servlet = null;
400
401         JahiaWebAppDef webAppDef = new JahiaWebAppDef (doc.getDisplayName (),
402                 contextRoot
403         );
404         webAppDef.addRoles (doc.getRoles ());
405
406         for (int i = 0; i < size; i++) {
407
408             servlet = (Servlet_Element) servlets.get (i);
409             webAppDef.addServlet (servlet);
410         }
411
412         pack.addWebAppDef (webAppDef);
413
414         if (pack.getWebApps ().size () > 0) {
415
416             pack.setFileName (tmpFile.getName ());
417             pack.setFilePath (path);
418             if(tmpFile.isDirectory()) {
419                 pack.setType(JahiaWebAppsPackage.DIR);
420             }
421         } else {
422             return null;
423         }
424
425         return pack;
426     }
427
428
429     /**
430      * Load Web App info from a file
431      *
432      * @param path , the full path to a file
433      *
434      * @return a JahiaWebAppsPackage or null
435      */

436     protected JahiaWebAppsPackage loadWebAppInfoFromFile (String JavaDoc path) throws JahiaException {
437
438         File JavaDoc fileItem = new File JavaDoc (path);
439
440         JahiaWebAppsPackage pack = null;
441
442         if (fileItem != null && fileItem.getName ().endsWith (".war")) {
443
444             JahiaWebAppsWarPackage warPackage = loadWebAppInfoFromWar (
445                     fileItem.getAbsolutePath ());
446
447             if (warPackage != null && warPackage.getWebApps ().size () > 0) {
448
449                 pack = new JahiaWebAppsPackage (warPackage.getContextRoot ());
450                 pack.addWebAppDef (warPackage.getWebApps ());
451                 pack.setFileName (fileItem.getName ());
452                 pack.setFilePath (path);
453             }
454
455         } else if (fileItem != null && fileItem.getName ().endsWith (".ear")) {
456
457             JahiaWebAppsEarPackage earPackage = loadWebAppInfoFromEar (
458                     fileItem.getAbsolutePath ());
459
460             if (earPackage != null && earPackage.getWebApps ().size () > 0) {
461
462                 pack = new JahiaWebAppsPackage (earPackage.getContextRoot ());
463                 pack.addWebAppDef (earPackage.getWebApps ());
464                 pack.setFileName (fileItem.getName ());
465                 pack.setFilePath (path);
466             }
467         }
468         return pack;
469     }
470
471
472     /**
473      * read the informations contained in a war file
474      *
475      * @param path , the full path to the file
476      *
477      * @return a JahiaWebAppsWarPackage or null
478      */

479     protected JahiaWebAppsWarPackage loadWebAppInfoFromWar (String JavaDoc path) throws JahiaException {
480
481         File JavaDoc fileItem = new File JavaDoc (path);
482
483         if (fileItem != null && fileItem.getName ().endsWith (".war")) {
484
485             // Create a war file Handler
486
JahiaWebAppsWarHandler wah = null;
487
488             try {
489                 wah = new JahiaWebAppsWarHandler (path);
490                 JahiaWebAppsWarPackage warPackage = wah.getWebAppsPackage ();
491                 return warPackage;
492             } finally {
493
494                 if (wah != null) {
495                     wah.closeArchiveFile ();
496                 }
497             }
498
499         }
500         return null;
501     }
502
503
504     /**
505      * read the informations contained in a ear file
506      *
507      * @param path , the full path to the file
508      *
509      * @return a JahiaWebAppsEarPackage or null
510      */

511     protected JahiaWebAppsEarPackage loadWebAppInfoFromEar (String JavaDoc path) throws JahiaException {
512
513         File JavaDoc fileItem = new File JavaDoc (path);
514
515         if (fileItem != null && fileItem.getName ().endsWith (".ear")) {
516
517             // Create a Ear Handler
518
JahiaEarFileHandler earh = null;
519
520             try {
521
522                 earh = new JahiaEarFileHandler (path);
523
524                 JahiaWebAppsEarPackage earPackage = new JahiaWebAppsEarPackage (
525                         JahiaTools.removeFileExtension (fileItem.getName (), ".ear"));
526
527                 // Get WebComponents ( list of war files )
528
Vector JavaDoc webComponents = earh.getWebComponents ();
529
530                 int size = webComponents.size ();
531
532                 Web_Component webComp = null;
533                 String JavaDoc webURI = null; // name of the war file
534
JahiaWebAppsWarPackage warPackage = null;
535
536                 for (int i = 0; i < size; i++) {
537
538                     webComp = (Web_Component) webComponents.get (i);
539                     webURI = webComp.getWebURI ();
540
541                     if (webURI != null && (webURI.length () > 0)) {
542
543                         //logger.debug("start extracting entry " + webURI);
544

545                         // extract the war file
546
earh.extractEntry (webURI, m_TempFolderDiskPath);
547
548                         // get the extracted war file
549
File JavaDoc warFile = new File JavaDoc (
550                                 m_TempFolderDiskPath + File.separator + webURI);
551
552                         // load info from war
553
warPackage = loadWebAppInfoFromWar (warFile.getAbsolutePath ());
554                         if (warPackage != null && (warPackage.getWebApps ().size () > 0)) {
555                             earPackage.addWebAppDefs ((Vector JavaDoc) warPackage.getWebApps ());
556                         }
557
558                         warFile.delete ();
559                     }
560                 }
561
562                 return earPackage;
563
564             } catch (JahiaException e) {
565
566                 String JavaDoc errMsg = "Failed handling webApps file ";
567                 logger.error (errMsg, e);
568                 if (earh != null) {
569                     earh.closeArchiveFile ();
570                 }
571
572                 throw new JahiaException (
573                         "JahiaWebAppsDeployerBaseService::loadWebAppInfoFromEar()",
574                         "JahiaWebAppsDeployerBaseService" + errMsg,
575                         JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY);
576             } finally {
577
578                 // Important to close the JarFile object !!!!
579
// cannot delete it otherwise
580
if (earh != null) {
581                     earh.closeArchiveFile ();
582                 }
583             }
584
585         }
586         return null;
587     }
588
589     /**
590      * return a reference to the DTD entity resolver
591      */

592     public EntityResolver JavaDoc getDtdEntityResolver () {
593         return (EntityResolver JavaDoc) m_Resolver;
594     }
595
596
597
598
599     /************************************************************************
600      * Web Application Packages ( .war , .ear or unzipped directory )
601      * handling.
602      *
603      * The directory jahiafiles/new_webapps is periodically scanned for new component
604      * packages. A package can be a war or ear file.
605      *
606      * If the informations contained in their deployment descriptor
607      * files ( META-INF/applications.xml, WEB-INF/web.xml ) is scanned successfully
608      * a JahiaWebAppsPackage object that holds all informations needed by Jahia
609      * to deploy them is created.
610      *
611      * If the deployment mode is automatic, they are automatically deployed and
612      * registered in Jahia. If not, they are added in the HashTable of packages
613      * waiting to be deployed manually throught the JahiaAdministration interface.
614      *
615      * The Key in the hashtable of packages used to identify each package is
616      * the filename of the package.
617      *
618      ************************************************************************/

619
620
621
622     /**
623      * delete a package reference in the hashtable of detected packages
624      * and delete it physically from disk.
625      *
626      * @param (JahiaSite) the site in which the webapp package belong to
627      * @param (String) full path to a file or directory
628      *
629      * @return true if successful
630      */

631     public boolean deletePackage (JahiaSite site, String JavaDoc path) {
632
633
634         synchronized (m_WebAppsPackage) {
635
636             File JavaDoc tmpFile = new File JavaDoc (path);
637             StringBuffer JavaDoc filename = new StringBuffer JavaDoc (site.getSiteKey ());
638             filename.append ("_");
639             filename.append (tmpFile.getName ());
640
641             //System.out.println(" deletePackage key is =" + filename );
642

643             if (tmpFile != null && tmpFile.isFile ()) {
644                 if (tmpFile.delete ()) {
645                     m_WebAppsPackage.remove (filename.toString ());
646                     return true;
647                 } else {
648                     return false;
649                 }
650             } else if (tmpFile != null && tmpFile.isDirectory ()) {
651
652                 StringBuffer JavaDoc buff = new StringBuffer JavaDoc (m_TempFolderDiskPath);
653                 buff.append (File.separator);
654                 buff.append (m_TempFolderPrefix);
655                 buff.append (filename.toString ());
656                 buff.append (JahiaTools.getUniqueDirName ());
657                 File JavaDoc tmpFolder = new File JavaDoc (buff.toString ());
658                 //System.out.println(" try to move to " + tmpFolder.getAbsolutePath() );
659
if (tmpFile.renameTo (tmpFolder)) {
660                     m_WebAppsPackage.remove (filename.toString ());
661                     // delete folder
662
JahiaTools.deleteFile (tmpFolder);
663                     return true;
664                 }
665
666             } else {
667                 m_WebAppsPackage.remove (filename.toString ());
668                 return true;
669             }
670             return false;
671         }
672     }
673
674
675     /**
676      * add a new file or directory only if it is recognized
677      * as a valid component package.
678      * And only if it is not registered yet.
679      *
680      * @param (JahiaSite) the site for which to register the webapp
681      * @param (String) full path to a file or directory
682      */

683     public void addNewFile (JahiaSite site, String JavaDoc path) {
684
685
686         synchronized (m_WebAppsPackage) {
687
688             File JavaDoc tmpFile = new File JavaDoc (path);
689             StringBuffer JavaDoc filename = new StringBuffer JavaDoc (site.getSiteKey ());
690             filename.append ("_");
691             filename.append (tmpFile.getName ());
692             if (tmpFile != null) {
693
694                 JahiaWebAppsPackage pack = null;
695
696                 if (m_WebAppsPackage.get (filename.toString ()) == null) {
697
698                     pack = loadWebAppInfo (path);
699                     if (pack != null) {
700                         m_WebAppsPackage.remove (filename.toString ());
701                         m_WebAppsPackage.put (filename.toString (), pack);
702                     }
703                 }
704             }
705         }
706     }
707
708
709     /**
710      * return an Enumerations of the keys of the Hashtable of package
711      *
712      * @return (Enumeration) the enumeration of all keys
713      */

714     public Enumeration JavaDoc getWebAppsPackageKeys () {
715
716         synchronized (m_WebAppsPackage) {
717             return m_WebAppsPackage.keys ();
718         }
719
720     }
721
722     /**
723      * return an Enumerations of the keys of the Hashtable of package for a gived site
724      *
725      * @return (Enumeration) the enumeration of all keys
726      */

727     public Enumeration JavaDoc getWebAppsPackageKeys (String JavaDoc siteKey) {
728
729         Enumeration JavaDoc enumeration = null;
730         Vector JavaDoc result = new Vector JavaDoc ();
731         synchronized (m_WebAppsPackage) {
732
733             enumeration = m_WebAppsPackage.keys ();
734             String JavaDoc name = null;
735             String JavaDoc siteIdent = siteKey + "_";
736             while (enumeration.hasMoreElements ()) {
737                 name = (String JavaDoc) enumeration.nextElement ();
738                 if (name.startsWith (siteIdent)) {
739                     result.add (siteIdent);
740                 }
741             }
742             return result.elements ();
743         }
744     }
745
746
747     /**
748      * return an enumeration of the web apps package Hashtable
749      *
750      * @return (Enumeration) the enumeration of the packages
751      */

752     public Enumeration JavaDoc getWebAppsPackages () {
753
754         return m_WebAppsPackage.elements ();
755     }
756
757
758     /**
759      * return an enumeration of the web apps package Hashtable
760      *
761      * @return (Enumeration) the enumeration of the packages
762      */

763     public Enumeration JavaDoc getWebAppsPackages (String JavaDoc siteKey) {
764
765         Enumeration JavaDoc enumeration = null;
766         Vector JavaDoc result = new Vector JavaDoc ();
767         synchronized (m_WebAppsPackage) {
768
769             enumeration = m_WebAppsPackage.keys ();
770             String JavaDoc name = null;
771             String JavaDoc siteIdent = siteKey + "_";
772             while (enumeration.hasMoreElements ()) {
773                 name = (String JavaDoc) enumeration.nextElement ();
774                 if (name.startsWith (siteIdent)) {
775                     result.add (m_WebAppsPackage.get (name));
776                 }
777             }
778             return result.elements ();
779         }
780     }
781
782
783     /**
784      * return a web app package in the Hashtable m_WebAppsPackage
785      * looking at the key
786      *
787      * @param (String) the key
788      *
789      * @return (Object) a webapps package or null
790      */

791     public Object JavaDoc getWebAppsPackage (String JavaDoc theKey) {
792
793         synchronized (m_WebAppsPackage) {
794             return m_WebAppsPackage.get (theKey);
795         }
796     }
797
798
799     /**
800      * scan a directory for web apps package.
801      * Add them to the HashTable of WebAppsPackage.
802      *
803      * @param the full path to a directory
804      *
805      * @return an Hashtable
806      */

807     public void scanDirectory (String JavaDoc path)
808             throws JahiaException {
809
810         synchronized (m_WebAppsPackage) {
811
812             JahiaWebAppsPackage pack = null;
813
814             File JavaDoc dir = new File JavaDoc (path);
815             if (dir != null && dir.isDirectory ()) {
816
817                 File JavaDoc[] files = dir.listFiles ();
818                 int size = files.length;
819
820                 for (int i = 0; i < size; i++) {
821
822                     if (files[i].canWrite ()) {
823
824                         pack = loadWebAppInfo (files[i].getAbsolutePath ());
825                         if (pack != null) {
826                             //System.out.println("added package in hashtable " + files[i].getAbsolutePath() );
827
// remove old map
828
m_WebAppsPackage.remove (files[i].getName ());
829                             // set the new map
830
m_WebAppsPackage.put (files[i].getName (), pack);
831                         }
832                     }
833                 }
834             }
835         }
836     }
837
838
839
840
841     //-------------------------------------------------------------------------
842
/**
843      * Search and remove the Meta-Inf folder
844      *
845      * @param String parentPath, the path of the parent Folder
846      *
847      * @return (boolean) true if successfull
848      */

849     protected boolean removeMetaInfFolder (String JavaDoc parentPath) {
850
851         if (parentPath == null) {
852             return false;
853         }
854
855         File JavaDoc f = new File JavaDoc (parentPath);
856         File JavaDoc[] files = f.listFiles ();
857         for (int i = 0; i < files.length; i++) {
858             if (files[i].getName ().equalsIgnoreCase (m_META_INF)) {
859                 try {
860                     return JahiaTools.deleteFile (files[i], false);
861                 } catch (Throwable JavaDoc t) {
862                     t.printStackTrace ();
863                     return false;
864                 }
865             }
866         }
867
868         return false;
869     }
870
871
872 } // end JahiaWebAppsDeployerService
Popular Tags