KickJava   Java API By Example, From Geeks To Geeks.

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


1 // ____.
2
// __/\ ______| |__/\. _______
3
// __ .____| | \ | +----+ \
4
// _______| /--| | | - \ _ | : - \_________
5
// \\______: :---| : : | : | \________>
6
// |__\---\_____________:______: :____|____:_____\
7
// /_____|
8
//
9
// . . . i n j a h i a w e t r u s t . . .
10
//
11
//
12
// JahiaOrionWebAppsDeployerBaseService
13
//
14
// NK 12.01.2001
15
//
16

17 package org.jahia.services.webapps_deployer;
18
19
20 import org.jahia.data.applications.ApplicationBean;
21 import org.jahia.data.webapps.*;
22 import org.jahia.exceptions.JahiaException;
23 import org.jahia.exceptions.JahiaInitializationException;
24 import org.jahia.services.sites.JahiaSite;
25 import org.jahia.services.webapps_deployer.orion.Application_Element;
26 import org.jahia.services.webapps_deployer.orion.Default_Web_Site_Xml;
27 import org.jahia.services.webapps_deployer.orion.Server_Xml;
28 import org.jahia.services.webapps_deployer.orion.Web_App_Element;
29 import org.jahia.settings.SettingsBean;
30 import org.jahia.utils.JahiaTools;
31
32 import java.io.File JavaDoc;
33 import java.util.Vector JavaDoc;
34
35
36 /**
37  * This Service Register new Application Definition,
38  * deploy .ear webApps under Orion .
39  *
40  * @author Khue ng
41  * @version 1.0
42  */

43 public class JahiaOrionWebAppsDeployerBaseService extends JahiaWebAppsDeployerService {
44
45     private static org.apache.log4j.Logger logger =
46             org.apache.log4j.Logger.getLogger (JahiaOrionWebAppsDeployerBaseService.class);
47
48     private static JahiaOrionWebAppsDeployerBaseService m_Instance = null;
49
50     /** The Orion server.xml file * */
51     private String JavaDoc m_ServerXmlFilePath;
52
53     /** The Orion default-web-site.xml file * */
54     private String JavaDoc m_DefaultWebSiteFilePath;
55
56     /** Orion initialization error * */
57     private boolean m_OrionInitErr = false;
58
59
60     /**
61      * Constructor
62      */

63     protected JahiaOrionWebAppsDeployerBaseService () {
64
65         logger.info ("***** Starting the Jahia Orion Deployer Base Service *****");
66
67     }
68
69
70     /**
71      * Use this method to get an instance of this class
72      */

73     public static synchronized JahiaOrionWebAppsDeployerBaseService getInstance () {
74
75         if (m_Instance == null) {
76             m_Instance = new JahiaOrionWebAppsDeployerBaseService ();
77         }
78         return m_Instance;
79     }
80
81
82     /**
83      * Initialize with Orion configuration files
84      *
85      * @param JahiaPrivateSettings jSettings
86      */

87     public void init (SettingsBean jSettings)
88             throws JahiaInitializationException {
89
90         if (!isInitialized ()) {
91
92             super.init (jSettings);
93
94             m_ServerXmlFilePath =
95                     m_ServerHomeDiskPath + "config" + File.separator + "server.xml";
96             m_DefaultWebSiteFilePath =
97                     m_ServerHomeDiskPath + "config" + File.separator + "default-web-site.xml";
98
99             File JavaDoc serverXmlFile = new File JavaDoc (m_ServerXmlFilePath);
100             File JavaDoc defaultWebSiteFile = new File JavaDoc (m_DefaultWebSiteFilePath);
101             if (serverXmlFile == null || !serverXmlFile.isFile ()
102                     || !serverXmlFile.canWrite () || defaultWebSiteFile == null
103                     || !defaultWebSiteFile.isFile () || !defaultWebSiteFile.canWrite ()) {
104                 String JavaDoc errMsg = serverXmlFile.getAbsolutePath () + " or " + defaultWebSiteFile.getAbsolutePath () +
105                         " files cannot be accessed or don't exist ";
106                 logger.error (errMsg + "\n");
107                 m_OrionInitErr = true;
108             }
109
110             logger.debug (" Server Home is :" + m_ServerHomeDiskPath);
111             m_WebAppRootPath = jSettings.getJahiaWebAppsDiskPath ();
112             logger.debug (" webAppRoot is :" + m_WebAppRootPath);
113             m_NewWebAppPath = jSettings.getJahiaNewWebAppsDiskPath ();
114             logger.debug (" newWebAppPath is :" + m_NewWebAppPath);
115
116             mIsServiceInitialized = true;
117
118         }
119
120     } // end init
121

122
123     /**
124      * With ear file , the application context is the ear file wihtout the extension
125      * so the context gived here is ignored
126      *
127      * @param (JahiaSite) the site
128      * @param (String) context , the context not used and can be of any value
129      * @param (String) filePath , the full path to the ear file
130      */

131     public synchronized boolean deploy (JahiaSite site, String JavaDoc context, String JavaDoc filePath)
132             throws JahiaException {
133
134         if (!canDeploy ()) {
135             return false;
136         }
137
138         boolean success = false;
139
140         if (filePath.endsWith (".ear")) {
141             success = deployEarFile (site, context, filePath);
142         }
143
144         if (success) {
145             deletePackage (site, filePath);
146             return true;
147         } else {
148             return false;
149         }
150
151     }
152
153
154     /**
155      * Undeploy a web application.
156      *
157      * @param (ApplicationBean) the application bean object
158      */

159     public boolean undeploy (ApplicationBean app) throws JahiaException {
160
161         if (!canDeploy ()) {
162             return false;
163         }
164
165         Server_Xml doc = new Server_Xml (m_ServerXmlFilePath);
166
167         String JavaDoc context = app.getContext ();
168         doc.removeApplication (context.substring (1, context.length ())); // remove the "/" first char
169
doc.write ();
170
171         // delete appliation folder from disk
172
File JavaDoc f = new File JavaDoc (m_WebAppRootPath + File.separator + app.getContext ());
173         JahiaTools.deleteFile (f);
174
175         // delete application package (.ear)
176
f = new File JavaDoc (m_WebAppRootPath + File.separator + app.getFilename ());
177         JahiaTools.deleteFile (f);
178
179         return true;
180     }
181
182
183     /**
184      * A simple way to pass a vector of J2EE .ear application files
185      * to be deployed
186      *
187      * @param (JahiaSite) the site
188      * @param (Vector) files, a vector of .ear application files
189      *
190      * @return (boolean) true if done correctly
191      */

192     public boolean deploy (JahiaSite site, Vector JavaDoc files) {
193
194         if (!canDeploy ()) {
195             return false;
196         }
197
198         synchronized (files) {
199
200             int size = files.size ();
201             File JavaDoc fileItem = null;
202
203             for (int i = 0; i < size; i++) {
204
205                 fileItem = (File JavaDoc) files.get (i);
206
207                 if (fileItem != null && fileItem.isFile ()) {
208
209                     logger.debug (" JahiaOrionWebAppsDeployerBaseService found new webApp: " +
210                             fileItem.getName ());
211
212                     try {
213
214                         JahiaWebAppsPackage pack = null;
215                         if (site.getWebAppsAutoDeployMode ()) {
216                             if (fileItem.getName ().endsWith (".ear")) {
217                                 pack = loadWebAppInfo (fileItem.getAbsolutePath ());
218                                 if (pack != null) {
219                                     if (!deploy (site, pack.getContextRoot (),
220                                             fileItem.getAbsolutePath ())) {
221                                         fileItem.delete ();
222                                         return false;
223                                     }
224                                 }
225                             }
226                         } else {
227                             if (fileItem.getName ().endsWith (".ear")) {
228                                 addNewFile (site, fileItem.getAbsolutePath ());
229                             }
230                         }
231
232                     } catch (JahiaException e) {
233                         String JavaDoc errMsg = "Failed deploying Application file " + fileItem.getName ();
234                         logger.error (errMsg + "\n", e);
235
236                         fileItem.delete ();
237                     }
238                 }
239             }
240         }
241         return true;
242     }
243
244
245     /**
246      * Under Orion actually we don't deploy war file
247      * So return false
248      *
249      * @param (String) webContext , the webn context
250      * @param (String) filePath , the full path to the war file
251      */

252     protected boolean deployWarFile (String JavaDoc webContext, String JavaDoc filePath) throws JahiaException {
253
254         return false;
255     }
256
257
258     /**
259      * Handle Ear application File Deployment
260      *
261      * @param (JahiaSite) the site
262      * @param (String) appContext, the application context
263      * @param (String) filePath , the full path to the ear file
264      *
265      * @return (boolean) true if success full
266      */

267     protected boolean deployEarFile (JahiaSite site, String JavaDoc appContext, String JavaDoc filePath)
268             throws JahiaException {
269
270         logger.debug ("started");
271
272         // move the ear file in the application deploy directory
273
File JavaDoc earFile = new File JavaDoc (filePath);
274         File JavaDoc tmpFile = new File JavaDoc (m_WebAppRootPath + earFile.getName ());
275         if (tmpFile != null && tmpFile.isFile ()) {
276             tmpFile.delete ();
277         }
278
279         if (!earFile.renameTo (tmpFile)) {
280
281             earFile.delete ();
282             String JavaDoc errMsg = "Failed moving the ear file in the deployed context root";
283             logger.error (errMsg);
284             throw new JahiaException ("JahiaOrionWebAppsDeployerBaseService.deployEarFile",
285                     "JahiaOrionWebAppsDeployerBaseService" + errMsg,
286                     JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY);
287         }
288
289         filePath = tmpFile.getAbsolutePath ();
290
291         // Create a Ear Handler
292
JahiaEarFileHandler earh = null;
293
294         try {
295
296             earh = new JahiaEarFileHandler (filePath);
297
298             // full path to the application context root folder
299
String JavaDoc appContextPath = createApplicationContext (appContext);
300             if (appContextPath == null) {
301
302                 String JavaDoc errMsg = "Failed creating the application context root dir";
303                 logger.error (errMsg);
304                 throw new JahiaException ("JahiaOrionWebAppsDeployerBaseService.deployEarFile",
305                         "JahiaOrionWebAppsDeployerBaseService" + errMsg,
306                         JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY);
307             }
308
309             // Get WebComponents ( list of war files )
310
Vector JavaDoc webComponents = earh.getWebComponents ();
311             int size = webComponents.size ();
312
313             if (size <= 0) {
314                 return false;
315             }
316
317             // Deploy web components ( war files )
318
Web_Component webComp = null;
319             String JavaDoc webURI = null; // name of the war file
320
String JavaDoc webAppContext = "";
321
322
323             for (int i = 0; i < size; i++) {
324
325                 webComp = (Web_Component) webComponents.get (i);
326
327                 webURI = webComp.getWebURI ();
328
329                 if (webURI != null && (webURI.length () > 0)) {
330
331                     logger.debug ("start extracting entry " + webURI);
332
333                     // extract the war file
334
earh.extractEntry (webURI, m_TempFolderDiskPath);
335
336                     // get the extracted war file
337
File JavaDoc warFile = new File JavaDoc (m_TempFolderDiskPath + File.separator + webURI);
338
339                     // deploy the war file in the webApp contextRoot
340
webAppContext = JahiaTools.removeFileExtension (warFile.getName (), ".war");
341                     String JavaDoc webAppPath = appContextPath + File.separator + webAppContext;
342                     Vector JavaDoc webApps = handleWarFile (webAppPath, warFile.getAbsolutePath ());
343
344                     warFile.delete ();
345
346                     /*
347                     //move the war file in the Application Context Root
348                     File destFile = new File(appContextPath + File.separator + warFile.getName());
349                     logger.debug(" dest file " + destFile.getAbsolutePath() );
350                     if ( destFile != null && destFile.isFile() ){
351                         destFile.delete();
352                     }
353                     warFile.renameTo(destFile);
354                     */

355
356                     // Activate the webapps in Orion
357
activateWebApps (appContext, appContext, webApps);
358
359                     // register web applications in Jahia
360
registerWebApps (site, appContext, earFile.getName (), webApps);
361
362                 }
363             }
364
365             //extract the ear file in the Application Context Folder
366
earh.unzip (appContextPath);
367             earh.closeArchiveFile ();
368
369             // notify Orion to deploy the application
370
Server_Xml doc = new Server_Xml (m_ServerXmlFilePath);
371
372             Application_Element appEl = new Application_Element (appContext,
373                     ".." + File.separator + "applications" + File.separator + tmpFile.getName (),
374                     "", // deploy dir, ignored by default
375
"", // parent, ignored default
376
"" // autostart, ignored by default
377
);
378             if (doc.appendApplication (appEl)) {
379                 doc.write ();
380             }
381
382
383         } catch (JahiaException e) {
384
385             String JavaDoc errMsg = "Failed handling webApps file ";
386             logger.error (errMsg + "\n" + e.toString ());
387             throw new JahiaException ("JahiaOrionWebAppsDeployerBaseService::deployWarFile()",
388                     "JahiaOrionWebAppsDeployerBaseService" + errMsg,
389                     JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY);
390         } finally {
391
392             // Important to close the JarFile object !!!!
393
// cannot delete it otherwise
394
if (earh != null) {
395                 earh.closeArchiveFile ();
396             }
397         }
398
399         return true;
400     }
401
402
403     /**
404      * extract Web Component file in the web app context and
405      * return the list of Web App Definition objects
406      *
407      * @param (String) webAppContext , the full path to the Web App Context Root
408      * @param (String) filePath , the full path to the war file
409      *
410      * @return (Vector) the list of Web App Definitions
411      */

412     protected Vector JavaDoc handleWarFile (String JavaDoc webAppContext, String JavaDoc filePath)
413             throws JahiaException {
414
415         // Create a WebApp War Handler
416
JahiaWebAppsWarHandler wah = null;
417
418         Vector JavaDoc webApps = new Vector JavaDoc ();
419
420         try {
421
422             wah = new JahiaWebAppsWarHandler (filePath);
423
424             webApps = wah.getWebAppsPackage ().getWebApps ();
425             int size = webApps.size ();
426
427             if (size > 0) {
428
429                 // create the dir
430
File JavaDoc f = new File JavaDoc (webAppContext);
431                 if (!f.isDirectory () && !f.mkdirs ()) {
432
433                     String JavaDoc errMsg = "Failed creating the war context root dir " + f.getAbsolutePath ();
434                     logger.error (errMsg + "\n");
435                     throw new JahiaException (
436                             "JahiaOrionWebAppsDeployerBaseService::deployWarFile()",
437                             "JahiaOrionWebAppsDeployerBaseService" + errMsg,
438                             JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY);
439                 }
440
441                 // Remove the Meta-Inf folder
442
removeMetaInfFolder (f.getAbsolutePath ());
443
444                 // extract the war file in the Web App Context Root
445
wah.unzip (webAppContext);
446             }
447         } catch (JahiaException e) {
448
449             String JavaDoc errMsg = "Failed handling webApps file ";
450             logger.error (errMsg + "\n", e);
451             throw new JahiaException ("JahiaOrionWebAppsDeployerBaseService::deployWarFile()",
452                     "JahiaOrionWebAppsDeployerBaseService" + errMsg,
453                     JahiaException.SERVICE_ERROR, JahiaException.ERROR_SEVERITY);
454         } finally {
455             // Important to close the JarFile object !!!!
456
// cannot delete it otherwise
457
if (wah != null) {
458                 wah.closeArchiveFile ();
459             }
460         }
461         return webApps;
462     }
463
464
465     /**
466      * Active Web Application in Orion
467      *
468      * @param (String) application , The name of the (enterprise-)application the web-apps exist in
469      * @param (String) appContext , The Root Context for the webApps
470      */

471     protected void activateWebApps (String JavaDoc application,
472                                     String JavaDoc appContext,
473                                     Vector JavaDoc webApps)
474             throws JahiaException {
475
476         Default_Web_Site_Xml doc = new Default_Web_Site_Xml (
477                 m_DefaultWebSiteFilePath);
478
479         Web_App_Element appEl = null;
480         int size = webApps.size ();
481         int counter = 0;
482
483         for (int i = 0; i < size; i++) {
484
485             JahiaWebAppDef webAppDef = (JahiaWebAppDef) webApps.get (i);
486             appEl = new Web_App_Element (
487                     application,
488                     "/" + appContext,
489                     webAppDef.getContextRoot (),
490                     "", // load-on-startup
491
"", // shared
492
"" // max-inactivity-time
493
);
494             if (doc.addWebApp (appEl)) {
495                 counter += 1;
496             }
497
498         }
499
500         if (counter > 0) {
501             doc.write ();
502         }
503     }
504
505
506     /**
507      * Create the Application context
508      *
509      * @param (String) appContext , the application context
510      *
511      * @return (String) appContextPath, the full application context path
512      */

513     protected String JavaDoc createApplicationContext (String JavaDoc appContext) {
514
515         StringBuffer JavaDoc strBuf = new StringBuffer JavaDoc (1024);
516         strBuf.append (m_WebAppRootPath);
517         strBuf.append (appContext);
518
519         // full path to the application context root folder
520
String JavaDoc appContextPath = strBuf.toString ();
521
522         //Create the Application Context Root
523
File JavaDoc f = new File JavaDoc (appContextPath);
524
525         if (!f.isDirectory () && !f.mkdirs ()) {
526
527             return null;
528         }
529
530         return appContextPath;
531     }
532
533     //-------------------------------------------------------------------------
534
/**
535      * Return true if configuration files are available
536      *
537      * @return (boolean) true if successfull
538      */

539     public boolean canDeploy () {
540
541         return (!m_OrionInitErr);
542
543     }
544
545
546 } // end JahiaOrionWebAppsDeployerService
547
Popular Tags