KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > backend > ModuleDeployer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * ModuleDeployer.java
26  *
27  * Created on January 3, 2002, 12:45 AM
28  */

29
30 package com.sun.enterprise.deployment.backend;
31
32 import java.io.*;
33 import java.util.Set JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.logging.*;
36 import java.util.Properties JavaDoc;
37 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
38
39 import com.sun.ejb.codegen.IASEJBCTimes;
40 import com.sun.enterprise.instance.ModuleEnvironment;
41 import com.sun.enterprise.instance.WebModulesManager;
42 import com.sun.enterprise.instance.InstanceEnvironment;
43 import com.sun.enterprise.instance.BaseManager;
44 import com.sun.enterprise.config.ConfigException;
45 import com.sun.enterprise.config.serverbeans.ServerTags;
46 import com.sun.enterprise.util.io.FileUtils;
47 import com.sun.enterprise.util.io.FileSource;
48 import com.sun.enterprise.util.zip.ZipItem;
49 import com.sun.enterprise.util.i18n.StringManager;
50 import com.sun.enterprise.server.Constants;
51 import com.sun.enterprise.loader.EJBClassPathUtils;
52 import com.sun.enterprise.deployment.Application;
53 import com.sun.enterprise.deployment.BundleDescriptor;
54 import com.sun.enterprise.deployment.WebServiceEndpoint;
55 import com.sun.enterprise.deployment.WebServicesDescriptor;
56 import com.sun.enterprise.deployment.WebService;
57 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
58 import com.sun.enterprise.deployment.deploy.shared.FileArchiveFactory;
59 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive;
60 import com.sun.enterprise.deployment.archivist.ApplicationArchivist;
61 import com.sun.enterprise.deployment.archivist.Archivist;
62 import com.sun.enterprise.deployment.phasing.DeploymentServiceUtils;
63
64 // verifier imports
65
import com.sun.enterprise.tools.verifier.AppVerifier;
66
67 /** ModuleDeployer is an abstract class with common methods required for deploying Web Modules
68  * and EJB Modules.
69  *
70  * @author bnevins
71  * @version
72  */

73
74 public abstract class ModuleDeployer extends Deployer
75 {
76     abstract protected BaseManager createConfigManager(InstanceEnvironment ienv, ModuleEnvironment menv) throws IASDeploymentException, ConfigException;
77     abstract protected void preDeploy() throws IASDeploymentException;
78     abstract protected void deploy() throws IASDeploymentException, ConfigException;
79     abstract protected void preRedeploy() throws IASDeploymentException, ConfigException;
80     
81     ///////////////////////////////////////////////////////////////////////////
82

83     protected ModuleDeployer(DeploymentRequest r) throws IASDeploymentException
84     {
85         super(r);
86     }
87     
88     ///////////////////////////////////////////////////////////////////////////
89

90     protected boolean needsStubs()
91     {
92         // override this for any module that needs stubs created
93
return false;
94     }
95     
96     ///////////////////////////////////////////////////////////////////////////
97

98     protected boolean needsJSPs()
99     {
100         // override this for any module that works with generated JSPs
101
return false;
102     }
103     
104     ///////////////////////////////////////////////////////////////////////////
105

106     protected final File getModuleDir()
107     {
108         return moduleDir;
109     }
110     
111     ///////////////////////////////////////////////////////////////////////////
112

113     public void doRequest() throws IASDeploymentException
114     {
115         doRequestPrepare();
116         doRequestFinish();
117     }
118     
119     ///////////////////////////////////////////////////////////////////////////
120

121     public void doRequestPrepare() throws IASDeploymentException
122     {
123         try
124         {
125             begin();
126         }
127         catch(Exception JavaDoc e)
128         {
129             if(shouldRollback)
130                 rollback();
131             
132             String JavaDoc msg = localStrings.getString(
133             "enterprise.deployment.backend.dorequest_exception");
134             logger.log(Level.WARNING, msg, e);
135             throw new IASDeploymentException(msg, e);
136         }
137     }
138     
139     ///////////////////////////////////////////////////////////////////////////
140

141     public void doRequestFinish() throws IASDeploymentException
142     {
143         try
144         {
145             if(request.isDeploy())
146             {
147                                 // retrieve the J2EECPhase deployment status
148
DeploymentStatus J2EECPhaseStatus =
149                                     request.getCurrentDeploymentStatus();
150
151                 beginFinish();
152                 preDeploy();
153
154                                 // create a DeploymentStatus for runEJBC stage
155
// it is a substage of J2EECPhase status
156
DeploymentStatus runEJBCStatus =
157                                     new DeploymentStatus(J2EECPhaseStatus);
158                                 request.setCurrentDeploymentStatus(
159                                     runEJBCStatus);
160                 deploy();
161                                 register();
162
163                                 // create a DeploymentStatus for postDeploy
164
// stage, it is a substage of J2EECPhase status
165
DeploymentStatus postDeployStatus =
166                                     new DeploymentStatus(J2EECPhaseStatus);
167                                 request.setCurrentDeploymentStatus(
168                                     postDeployStatus);
169                 postDeploy();
170
171                 generatePolicy();
172             }
173             else if(request.isReDeploy())
174             {
175                                 // retrieve the J2EECPhase deployment status
176
DeploymentStatus J2EECPhaseStatus =
177                                     request.getCurrentDeploymentStatus();
178
179                 beginFinish();
180                 preRedeploy();
181
182                                 // create a DeploymentStatus for runEJBC stage
183
// it is a substage of J2EECPhase status
184
DeploymentStatus runEJBCStatus =
185                                     new DeploymentStatus(J2EECPhaseStatus);
186                                 request.setCurrentDeploymentStatus(
187                                     runEJBCStatus);
188                 redeploy();
189                                 register();
190
191                                 // create a DeploymentStatus for postDeploy
192
// stage, it is a substage of J2EECPhase status
193
DeploymentStatus postDeployStatus =
194                                     new DeploymentStatus(J2EECPhaseStatus);
195                                 request.setCurrentDeploymentStatus(
196                                     postDeployStatus);
197                 postRedeploy();
198
199                 generatePolicy();
200             }
201             else if(request.isUnDeploy())
202             {
203                 beginFinish();
204                 preundeploy();
205                 undeploy();
206                 removePolicy();
207             }
208             else
209             {
210                 String JavaDoc msg = localStrings.getString(
211                 "enterprise.deployment.backend.unknown_deployment_command" );
212                 throw new IASDeploymentException( msg );
213             }
214         }
215         catch(Exception JavaDoc e)
216         {
217             if(shouldRollback)
218                 rollback();
219             
220             String JavaDoc msg = localStrings.getString(
221             "enterprise.deployment.backend.dorequest_exception");
222             logger.log(Level.FINE, msg, e);
223                         if (e instanceof IASDeploymentException) {
224                             throw (IASDeploymentException) e;
225                         } else {
226                             throw new IASDeploymentException(msg, e);
227                         }
228         }
229         finally
230         {
231             finish();
232         }
233     }
234     
235     // this method is overridden from Deployer. It is final so you know FOR SURE
236
// that it isn't overridden by any subclasses.
237
protected final void begin() throws IASDeploymentException
238     {
239         super.begin();
240         
241         // get environment object refs
242

243         InstanceEnvironment instanceEnv = getInstanceEnv();
244         moduleEnv = request.getModuleEnv();
245         moduleName = request.getName();
246         
247         // check them
248

249         if(moduleEnv == null)
250         {
251             String JavaDoc msg = localStrings.getString("enterprise.deployment.backend.null_moduleenvironment");
252             throw new IASDeploymentException(msg);
253         }
254         
255         try
256         {
257             moduleEnv.verify();
258             modulesMgr = createConfigManager(instanceEnv, moduleEnv);
259             setDeployCommand();
260             
261             if(request.isReDeploy())
262             {
263                                 originalModuleDir = new File(DeploymentServiceUtils.getLocation(moduleName, request.getType()));
264                 unregister();
265                                 removePolicy();
266             }
267         }
268         catch(Exception JavaDoc e)
269         {
270             throw new IASDeploymentException(e);
271         }
272         
273         shouldRollback = true;
274         
275         // for redeploy -- when we get to doRequestFinish() -- the module will NOT be registered
276
// any longer.
277
}
278     
279     ///////////////////////////////////////////////////////////////////////////
280

281     // bnevins -- added August 2003 to allow for the MBean to unload the module between
282
// begin() and now.
283

284     private void beginFinish() throws IASDeploymentException
285     {
286         setDirs();
287     }
288     
289     ///////////////////////////////////////////////////////////////////////////
290

291     private void setDirs() throws IASDeploymentException
292     {
293         assert modulesMgr != null;
294         assert moduleName != null;
295         assert moduleEnv != null;
296         
297         if(request.isDeploy())
298             setDirsDeploy();
299         else if(request.isReDeploy())
300             setDirsReDeploy();
301         else if(request.isUnDeploy())
302             setDirsUnDeploy();
303         else
304         {
305             String JavaDoc msg = localStrings.getString(
306             "enterprise.deployment.backend.deployment_type_error" );
307             throw new IASDeploymentException( msg );
308         }
309         
310         request.setDeployedDirectory(moduleDir);
311         request.setJSPDirectory(jspDir);
312         request.setStubsDirectory(stubsDir);
313         request.setGeneratedXMLDirectory(xmlDir);
314     }
315     
316         /**
317      * @return the module classpath
318      */

319     protected List JavaDoc getModuleClasspath(Archivist archivist,
320                 AbstractArchive archive) throws IASDeploymentException {
321         try {
322         String JavaDoc location = request.getDeployedDirectory().getAbsolutePath();
323         return EJBClassPathUtils.getModuleClasspath(request.getName(), location, this.getManager());
324         } catch(Exception JavaDoc e) {
325         throw new IASDeploymentException(e);
326         }
327     }
328         
329     ///////////////////////////////////////////////////////////////////////////
330

331     private void setDirsDeploy() throws IASDeploymentException
332     {
333         if(isRegistered())
334         {
335             String JavaDoc msg = localStrings.getString(
336             "enterprise.deployment.backend.deploy_error_module_exists" );
337             throw new IASDeploymentException( msg );
338         }
339                 xmlDir = new File(moduleEnv.getModuleGeneratedXMLPath());
340                 jwsDir = new File(moduleEnv.getJavaWebStartPath());
341         
342         if(needsStubs())
343         {
344             stubsDir = new File(moduleEnv.getModuleStubPath());
345         }
346         else
347             stubsDir = null;
348         
349         if(needsJSPs())
350         {
351             assert (modulesMgr instanceof WebModulesManager);
352             jspDir = new File(moduleEnv.getModuleJSPPath());
353         }
354         if(isArchive())
355         {
356             File parent = new File(getInstanceEnv().getModuleRepositoryPath());
357                         moduleDir = new File(parent, moduleName);
358                         moduleDir.mkdirs();
359         }
360         else if(isDirectory())
361         {
362             FileSource fileSource = request.getFileSource();
363             
364             if(fileSource == null || !fileSource.exists())
365             {
366                 String JavaDoc msg = localStrings.getString(
367                 "enterprise.deployment.backend.file_source_does_not_exist",
368                 fileSource );
369                 throw new IASDeploymentException( msg );
370             }
371             
372             moduleDir = fileSource.getFile();
373             
374             if(!FileUtils.safeIsDirectory(moduleDir))
375             {
376                 String JavaDoc msg = localStrings.getString(
377                 "enterprise.deployment.backend.deployment_directory_does_not_exist",
378                 moduleDir.getPath() );
379                 throw new IASDeploymentException( msg );
380             }
381         }
382         else
383         {
384             String JavaDoc msg = localStrings.getString(
385             "enterprise.deployment.backend.deployment_not_dir_or_archive" );
386             throw new IASDeploymentException( msg );
387         }
388     }
389         
390     /**
391      * @return a fully initialized and validated deployment descriptors for this
392      * deployment request.
393      */

394     protected Application loadDescriptors() throws IASDeploymentException {
395             Application app = super.loadDescriptors();
396             (new com.sun.enterprise.webservice.WsUtil()).genWSInfo(app, request);
397             return app;
398         }
399                 
400     ///////////////////////////////////////////////////////////////////////////
401

402     private void setDirsUnDeploy() throws IASDeploymentException
403     {
404         // Use the already registered location
405
try
406         {
407             if(!isRegistered())
408             {
409                 String JavaDoc msg = localStrings.getString(
410                 "enterprise.deployment.backend.undeploy_error_module_not_registered" );
411                 throw new IASDeploymentException( msg );
412             }
413             
414             moduleDir = new File(DeploymentServiceUtils.getLocation(moduleName, request.getType()));
415                         xmlDir = new File(modulesMgr.getGeneratedXMLLocation(moduleName));
416                         jwsDir = new File(moduleEnv.getJavaWebStartPath());
417             stubsDir = null;
418             jspDir = null;
419             
420             if(needsStubs())
421                 stubsDir = new File(modulesMgr.getStubLocation(moduleName));
422             if(needsJSPs())
423             {
424                 assert (modulesMgr instanceof WebModulesManager);
425                 WebModulesManager mgr = (WebModulesManager)modulesMgr;
426                 jspDir = new File(mgr.getJSPLocation(moduleName));
427             }
428         }
429         catch(Exception JavaDoc e)
430         {
431             String JavaDoc msg = localStrings.getString(
432             "enterprise.deployment.backend.error_getting_module_directory",
433             e );
434             throw new IASDeploymentException( msg );
435         }
436     }
437     
438     ///////////////////////////////////////////////////////////////////////////
439

440     private void setDirsReDeploy() throws IASDeploymentException
441     {
442         if(!wasReg)
443         {
444             String JavaDoc msg = localStrings.getString(
445             "enterprise.deployment.backend.redeploy_error_module_not_registered" );
446             throw new IASDeploymentException( msg );
447         }
448         
449                 xmlDir = new File(modulesMgr.getGeneratedXMLLocation(moduleName));
450                 jwsDir = new File(moduleEnv.getJavaWebStartPath());
451
452         // let's get the easy stuff out of the way first -- stubs & jsps
453
stubsDir = null;
454         jspDir = null;
455         
456         if(needsStubs()) {
457             stubsDir = new File(modulesMgr.getStubLocation(moduleName));
458                 }
459         if(needsJSPs())
460         {
461             assert (modulesMgr instanceof WebModulesManager);
462             WebModulesManager mgr = (WebModulesManager)modulesMgr;
463             jspDir = new File(mgr.getJSPLocation(moduleName));
464         }
465         
466         if(isArchive())
467         {
468             // be sure we have the original deployed directory
469
if(!FileUtils.safeIsDirectory(originalModuleDir))
470             {
471                 String JavaDoc msg = localStrings.getString(
472                 "enterprise.deployment.backend.modulesmanager_error_getting_module_location",
473                 moduleName );
474                 throw new IASDeploymentException(msg);
475             }
476             
477             moduleDir = originalModuleDir;
478             oldModuleDir = new File(moduleDir.getAbsolutePath() + "_old");
479             
480             
481             
482             // wipe it out if it happens to already exist
483
FileUtils.whack(oldModuleDir);
484             
485             
486             // note -- java does NOT change the path of moduleDir, it just renames the file on disk!
487
// i.e. "moduleDir" will still be pointing at the same file after the call as before the call.
488
moduleDirWasRenamed = FileUtils.renameFile(moduleDir,oldModuleDir);
489             
490             
491             if(!moduleDirWasRenamed)
492                 
493                 
494             {
495                 String JavaDoc msg = localStrings.getString("enterprise.deployment.backend.directory_rename_error",
496                 moduleDir.getAbsolutePath(), oldModuleDir.getAbsolutePath());
497                 throw new IASDeploymentException(msg);
498             }
499         }
500         else if(isDirectory())
501         {
502             FileSource fileSource = request.getFileSource();
503             
504             if(!fileSource.exists())
505             {
506                 String JavaDoc msg = localStrings.getString(
507                 "enterprise.deployment.backend.file_source_does_not_exist",
508                 fileSource );
509                 throw new IASDeploymentException( msg );
510             }
511             
512             assert fileSource.isDirectory();
513             moduleDir = fileSource.getFile();
514             
515             oldModuleDir = new File(moduleEnv.getModuleBackupPath());
516             
517             if (!FileUtils.safeIsDirectory(oldModuleDir))
518             {
519                 oldModuleDir = null;
520             }
521         }
522         else
523         {
524             String JavaDoc msg = localStrings.getString(
525             "enterprise.deployment.backend.redeployment_not_dir_or_archive" );
526             throw new IASDeploymentException( msg );
527         }
528         
529         moduleDir.mkdirs();
530     }
531
532     /**
533     * Attempt to delete the deployed directory-tree.
534     * <p> This method call is <b>guaranteed</b> to never throw any kind of Exception
535     */

536     public void cleanup_internal()
537     {
538                 try
539                 {
540                         if(request.isUnDeploy())
541                         {
542                                 if(isMaybeCMPDropTables)
543                                         dropTables();
544
545                                 liquidate();
546                         }
547
548                         // note: oldModulDir will be pointing at the just-copied backup dir
549
// if this is a directory-redeploy!!
550
else if(request.isReDeploy() && isArchive() && oldModuleDir != null)
551                         {
552                                 FileUtils.whack(oldModuleDir);
553                         }
554
555                         // nothing to do for Deploy
556
}
557                 catch(Exception JavaDoc e)
558                 {
559                         logger.warning("Exception caught and ignored in cleanup_internal()");
560                 }
561     }
562
563     
564     ///////////////////////////////////////////////////////////////////////////
565

566     protected void liquidate() throws IASDeploymentException, ConfigException
567     {
568         assert !request.isReDeploy(); // only can be called for DEPLOY and UNDEPLOY
569
assert moduleDir != null;
570         
571         boolean shouldDeleteModuleFiles = false;
572         
573         if(request.isDeploy() && isArchive())
574         {
575             shouldDeleteModuleFiles = true;
576         }
577         else if(request.isUnDeploy() && !(DeploymentServiceUtils.isDirectoryDeployed(request.getName(), request.getType()) || request.isReload()) )
578         {
579             shouldDeleteModuleFiles = true;
580         }
581         
582         if(shouldDeleteModuleFiles)
583         {
584             if(FileUtils.safeIsDirectory(moduleDir))
585             {
586                 FileUtils.whack(moduleDir);
587                 logger.fine("Deleted module Directory: " + moduleDir.getPath());
588             }
589             else
590             {
591                 logger.warning("Can't delete module Directory -- it isn't a directory: "
592                 + FileUtils.safeGetCanonicalPath(moduleDir));
593             }
594         }
595         else
596         {
597             logger.fine("Did NOT delete module Directory (Directory-Deployment): " + moduleDir.getPath());
598         }
599         
600         DeleteOrKeepFailedStubs(stubsDir);
601         
602         if(FileUtils.safeIsDirectory(jspDir))
603         {
604             FileUtils.whack(jspDir);
605         }
606
607         if(FileUtils.safeIsDirectory(xmlDir))
608         {
609             FileUtils.whack(xmlDir);
610         }
611                 if(FileUtils.safeIsDirectory(jwsDir))
612                 {
613                         FileUtils.whack(jwsDir);
614                 }
615     }
616     
617     ///////////////////////////////////////////////////////////////////////////
618

619     
620     protected void preundeploy() throws IASDeploymentException, ConfigException
621     {
622                 // setup the cmp stuff for undeployment...
623

624                 if(getRequest().isMaybeCMPDropTables())
625                 {
626                         isMaybeCMPDropTables = true;
627                 }
628     }
629     
630     ///////////////////////////////////////////////////////////////////////////
631

632     private void undeploy() throws IASDeploymentException, ConfigException
633     {
634         if(!isRegistered())
635         {
636             String JavaDoc msg = localStrings.getString(
637             "enterprise.deployment.backend.undeploy_error_module_not_registered" );
638             throw new IASDeploymentException( msg );
639         }
640         
641         try
642         {
643             unregister();
644         }
645         catch(ConfigException e)
646         {
647             String JavaDoc msg = localStrings.getString(
648             "enterprise.deployment.backend.config_exception_on_remove",
649             moduleName, e );
650             throw new IASDeploymentException( msg, e);
651         }
652     }
653     
654     ///////////////////////////////////////////////////////////////////////////
655

656     private void rollbackUndeploy() throws ConfigException
657     {
658         // nothing to do -- the only thing that can get us here is an error
659
// trying to unregister()...
660
}
661     
662     ///////////////////////////////////////////////////////////////////////////
663

664     protected void postDeploy() throws IASDeploymentException, ConfigException
665     {
666                 // save the object type in optional attributes
667
Properties JavaDoc optionalAttributes = request.getOptionalAttributes();
668                 if (optionalAttributes == null) {
669                     optionalAttributes = new Properties JavaDoc();
670                 }
671                 String JavaDoc resourceType = getResourceType(moduleDir);
672                 if(resourceType != null) {
673                     optionalAttributes.setProperty(ServerTags.OBJECT_TYPE,
674                         resourceType);
675                 }
676     
677                 handlePostDeployEvent();
678           }
679
680     /**
681     * Called from postDeploy and postRedeploy to process extra events.
682     * Currently only cmp is registered for these events.
683     */

684     protected void handlePostDeployEvent() throws IASDeploymentException
685     {
686                 DeploymentEventInfo info = getEventInfo();
687                 DeploymentEvent ev = new DeploymentEvent(
688                 DeploymentEventType.POST_DEPLOY, info);
689                 DeploymentEventManager.notifyDeploymentEvent(ev);
690
691      }
692     
693    /**
694     * Create DeploymentEvent info instance.
695     * @return DeploymentEventInfo
696     */

697     protected DeploymentEventInfo getEventInfo() throws IASDeploymentException
698     {
699         return new DeploymentEventInfo(moduleDir, stubsDir, oldStubsDir,
700             request.getDescriptor(), getRequest());
701     }
702
703     ///////////////////////////////////////////////////////////////////////////
704

705     protected final void rollbackDeploy() throws IASDeploymentException
706     {
707         // pretty simple -- remove the module if it actually got registered (unlikely)
708
// and hose-down the deployed files
709
try
710         {
711             if(wasRegisteredThisSession)
712             {
713                 // i.e. the error came AFTER registering.
714
// this will be false if, say, we are trying to deploy an already deployed module
715
unregister();
716             }
717             
718             liquidate();
719         }
720         catch(ConfigException e)
721         {
722             // can't do anything -- we are already in an error situation!
723
}
724     }
725     
726     ///////////////////////////////////////////////////////////////////////////
727

728     protected final void redeploy() throws IASDeploymentException, ConfigException
729     {
730         deploy(); // same thing!
731
}
732     
733     ///////////////////////////////////////////////////////////////////////////
734

735     private void rollback()
736     {
737         // IMPORTANT: WE ARE BEING CALLED FROM INSIDE A CATCH BLOCK --
738
// THUS YOU BETTER NOT LET ANY EXCEPTIONS OUT OF HERE!!!
739

740         try
741         {
742             if(request.isDeploy())
743                 rollbackDeploy();
744             else if(request.isReDeploy())
745             {
746                 // bnevins 9/8/2003
747
// no rollbacks for dir-redeploy!
748
if(request.isArchive())
749                     rollbackRedeploy();
750                 else
751                     request.setReRegisterOnFailure(false);
752             }
753             else if(request.isUnDeploy())
754                 rollbackUndeploy();
755         }
756         catch(Throwable JavaDoc t)
757         {
758             // can't do anything!
759
}
760     }
761     
762     ///////////////////////////////////////////////////////////////////////////
763

764     protected void register() throws IASDeploymentException, ConfigException
765     {
766         modulesMgr.registerDescriptor(moduleName, request.getDescriptor());
767         wasRegisteredThisSession = true;
768     }
769         
770     ///////////////////////////////////////////////////////////////////////////
771

772     protected void liquidateModuleDirAndStubsDirIfTheyHappenToExist() throws IASDeploymentException
773     {
774         if(request.isArchive() && FileUtils.safeIsDirectory(moduleDir))
775         {
776             FileUtils.whack(moduleDir);
777             
778             if(FileUtils.safeIsDirectory(moduleDir))
779             {
780                 String JavaDoc msg = localStrings.getString(
781                 "enterprise.deployment.backend.deploy_error_dir_is_locked",
782                 "Module", moduleDir.getAbsolutePath());
783                 throw new IASDeploymentException( msg );
784             }
785         }
786         
787         
788         if(FileUtils.safeIsDirectory(stubsDir))
789         {
790             FileUtils.whack(stubsDir);
791             
792             if(FileUtils.safeIsDirectory(stubsDir))
793             {
794                 String JavaDoc msg = localStrings.getString(
795                 "enterprise.deployment.backend.deploy_error_dir_is_locked",
796                 "Stubs", stubsDir.getAbsolutePath());
797                 throw new IASDeploymentException( msg );
798             }
799         }
800         
801         if(FileUtils.safeIsDirectory(jspDir))
802         {
803             FileUtils.whack(jspDir);
804             
805             if(FileUtils.safeIsDirectory(jspDir))
806             {
807                 String JavaDoc msg = localStrings.getString(
808                 "enterprise.deployment.backend.deploy_error_dir_is_locked",
809                 "JSP", jspDir.getAbsolutePath());
810                 throw new IASDeploymentException( msg );
811             }
812         }
813
814                 if(FileUtils.safeIsDirectory(xmlDir))
815                 {
816                         FileUtils.whack(xmlDir);
817                                 
818                         if(FileUtils.safeIsDirectory(xmlDir))
819                         {
820                                 String JavaDoc msg = localStrings.getString(
821                                 "enterprise.deployment.backend.deploy_error_dir_is_locked",
822                                 "XML", xmlDir.getAbsolutePath());
823                                 throw new IASDeploymentException( msg );
824                         }
825                 }
826                 if(FileUtils.safeIsDirectory(jwsDir))
827                 {
828                         FileUtils.whack(jwsDir);
829
830                         if(FileUtils.safeIsDirectory(jwsDir))
831                         {
832                                 String JavaDoc msg = localStrings.getString(
833                                 "enterprise.deployment.backend.deploy_error_dir_is_locked",
834                                 "JWS", jwsDir.getAbsolutePath());
835                                 throw new IASDeploymentException( msg );
836                         }
837                 }
838     }
839     
840     ///////////////////////////////////////////////////////////////////////////
841

842     protected void setOldDirs() throws IASDeploymentException
843     {
844         assert moduleDir != null;
845         assert moduleEnv != null;
846         assert request.isReDeploy(); // this must only be called for Redeploy!!
847

848         // note: oldModuleDir is already set (or null) from setDirsRedeploy()
849

850         if(needsStubs() && stubsDir != null && stubsDir.exists() && stubsDir.isDirectory())
851         {
852             oldStubsDir = new File(stubsDir.getPath() + "_old");
853             
854             if(oldStubsDir.exists())
855             {
856                 // what if there is a regular file with the name we need? Let's check...
857

858                 FileUtils.whack(oldStubsDir);
859             }
860             
861             if(!FileUtils.renameFile(stubsDir,oldStubsDir))
862             {
863                 String JavaDoc msg = localStrings.getString(
864                 "enterprise.deployment.backend.directory_rename_error",
865                 stubsDir.getPath(), oldStubsDir.getPath() );
866                 throw new IASDeploymentException( msg );
867             }
868         }
869         
870         if(needsJSPs() && FileUtils.safeIsDirectory(jspDir))
871         {
872             oldJSPDir = new File(jspDir.getPath() + "_old");
873             
874             if(oldJSPDir.exists())
875             {
876                 // what if there is a regular file with the name we need? Let's check...
877

878                 FileUtils.whack(oldJSPDir);
879                         }
880             
881             if(!FileUtils.renameFile(jspDir,oldJSPDir))
882             {
883                 String JavaDoc msg = localStrings.getString(
884                 "enterprise.deployment.backend.directory_rename_error",
885                 jspDir.getPath(), oldJSPDir.getPath() );
886                 throw new IASDeploymentException( msg );
887             }
888         }
889
890                 if(FileUtils.safeIsDirectory(xmlDir))
891                 {
892                         oldXMLDir = new File(xmlDir.getPath() + "_old");
893
894                         if(oldXMLDir.exists())
895                         {
896                                 // what if there is a regular file with the name we need? Let's check...
897

898                                 FileUtils.whack(oldXMLDir);
899                         }
900
901                         if(!FileUtils.renameFile(xmlDir,oldXMLDir))
902                         {
903                                 String JavaDoc msg = localStrings.getString(
904                                 "enterprise.deployment.backend.directory_rename_error",
905                                 xmlDir.getPath(), oldXMLDir.getPath() );
906                                 throw new IASDeploymentException( msg );
907                         }
908                 }
909                 if (FileUtils.safeIsDirectory(jwsDir)) {
910                     // warning if the directory exist and has some contents
911
if (jwsDir.list().length > 0) {
912                         DeploymentStatus jwsStatus = new DeploymentStatus(
913                             request.getCurrentDeploymentStatus());
914                         jwsStatus.setStageStatus(DeploymentStatus.WARNING);
915                         jwsStatus.setStageStatusMessage(localStrings.getString(
916                             "enterprise.deployment.backend.jws_redeploy",
917                             jwsDir.getPath()));
918                     }
919
920                     oldJWSDir = new File(jwsDir.getPath() + "_old");
921                                                                                    
922                     if(oldJWSDir.exists())
923                     {
924                         // what if there is a regular file with the name we need?
925
// Let's check...
926
FileUtils.whack(oldJWSDir);
927                     }
928                                                                                    
929                     if(!FileUtils.renameFile(jwsDir, oldJWSDir))
930                     {
931                         String JavaDoc msg = localStrings.getString(
932                             "enterprise.deployment.backend.directory_rename_error",
933                             jwsDir.getPath(), oldJWSDir.getPath() );
934                             throw new IASDeploymentException( msg );
935                     }
936
937                 }
938     }
939     
940     ///////////////////////////////////////////////////////////////////////////
941

942     protected boolean isRegistered() throws IASDeploymentException
943     {
944         return DeploymentServiceUtils.isRegistered(moduleName, request.getType());
945     }
946     
947     ///////////////////////////////////////////////////////////////////////////
948
protected boolean isSystem() throws IASDeploymentException
949     {
950         try
951         {
952                         boolean isSystem;
953             isSystem = DeploymentServiceUtils.isSystem(moduleName, request.getType());
954             boolean guardSystemApp = !(Boolean.valueOf(System.getProperty(Constants.ALLOW_SYSAPP_DEPLOYMENT, "false")).booleanValue());
955             if(isSystem && guardSystemApp)
956                 return true;
957             else
958                 return false;
959         }catch(Exception JavaDoc e)
960         {
961             throw new IASDeploymentException(e);
962         }
963     }
964     
965     ///////////////////////////////////////////////////////////////////////////
966

967     protected void setShared(boolean what) throws ConfigException
968     {
969     /* WBN 510-02
970      * This feature wasn't implemented by core server.
971      * I'm commenting-out the call but leaving everything else
972      * in place -- since it will probably be added in a later
973      * version
974      */

975         //modulesMgr.setShared(moduleName, what);
976
}
977     
978     ///////////////////////////////////////////////////////////////////////////
979

980     
981     protected void unregister() throws ConfigException
982     {
983         modulesMgr.unregisterDescriptor(moduleName);
984     }
985
986         ///////////////////////////////////////////////////////////////////////////
987

988         protected void postRedeploy() throws IASDeploymentException, ConfigException
989         {
990                 handlePostDeployEvent();
991
992                 if(FileUtils.safeIsDirectory(oldStubsDir))
993                 {
994                         FileUtils.whack(oldStubsDir);
995                 }
996
997                 if(FileUtils.safeIsDirectory(oldJSPDir))
998                 {
999                         FileUtils.whack(oldJSPDir);
1000                }
1001
1002                if(FileUtils.safeIsDirectory(oldXMLDir))
1003                {
1004                        FileUtils.whack(oldXMLDir);
1005                }
1006
1007                if(FileUtils.safeIsDirectory(oldJWSDir))
1008                {
1009                        FileUtils.whack(oldJWSDir);
1010                }
1011        }
1012
1013    
1014        ///////////////////////////////////////////////////////////////////////////
1015

1016        protected void rollbackRedeploy() throws IASDeploymentException
1017        {
1018                if(isArchive() && wasModuleDirRenamed())
1019                {
1020                        if(FileUtils.safeIsDirectory(moduleDir))
1021                                FileUtils.whack(moduleDir);
1022                        
1023                        if(FileUtils.safeIsDirectory(oldModuleDir))
1024                                oldModuleDir.renameTo(moduleDir);
1025                }
1026                
1027                if(FileUtils.safeIsDirectory(oldStubsDir))
1028                {
1029                        DeleteOrKeepFailedStubs(stubsDir);
1030                        oldStubsDir.renameTo(stubsDir);
1031                }
1032
1033                if(FileUtils.safeIsDirectory(jspDir))
1034                        FileUtils.whack(jspDir);
1035                
1036                if(FileUtils.safeIsDirectory(oldJSPDir))
1037                        oldJSPDir.renameTo(jspDir);
1038                
1039                if(FileUtils.safeIsDirectory(xmlDir))
1040                        FileUtils.whack(xmlDir);
1041                
1042                if(FileUtils.safeIsDirectory(oldXMLDir))
1043                       oldJSPDir.renameTo(xmlDir);
1044
1045                if(FileUtils.safeIsDirectory(oldJWSDir))
1046                       oldJWSDir.renameTo(jwsDir);
1047
1048                try
1049                {
1050                    register();
1051                 
1052                }
1053                catch(Exception JavaDoc e)
1054                {
1055                        throw new IASDeploymentException(e);
1056                }
1057        }
1058
1059
1060    
1061    ///////////////////////////////////////////////////////////////////////////
1062

1063    protected final boolean wasModuleDirRenamed()
1064    {
1065        // this is of interest for redeploy rollback. It needs to know whether
1066
// or not to rename xxx_old back to xxx. If it never happened the rollback
1067
// would not work.
1068
// bnevins 8/03
1069

1070        return moduleDirWasRenamed;
1071    }
1072    
1073    /**
1074     * Runs the verifier on this module if verification is ON in
1075     * the deployment request.
1076     *
1077     * @throws IASDeploymentException if an error found in this module
1078     * after verification
1079     */

1080    protected void runVerifier() throws IASDeploymentException
1081    {
1082        if (request.isVerifying()) {
1083            try {
1084                String JavaDoc archive = request.getDeployedDirectory().getCanonicalPath();
1085                File jspOutDir = (request.getPrecompileJSP())? jspDir:null;
1086                new AppVerifier().verify(request.getDescriptor(),
1087                        (new FileArchiveFactory()).openArchive(archive),
1088                        request.getCompleteClasspath(),
1089                        jspOutDir);
1090            } catch (Exception JavaDoc e) {
1091                String JavaDoc msg = localStrings.getString(
1092                        "enterprise.deployment.backend.verifier_error");
1093                throw new IASDeploymentException(msg);
1094            }
1095        }
1096    }
1097    
1098    ///////////////////////////////////////////////////////////////////////////
1099

1100    private void setDeployCommand() throws IASDeploymentException
1101    {
1102        boolean isReg = isRegistered();
1103        boolean isSystem = false;
1104        wasReg = isReg;
1105        
1106        if(isReg)
1107        {
1108            isSystem = isSystem();
1109        }
1110        
1111        if(request.isUnDeploy())
1112        {
1113            if(!isReg)
1114            {
1115                String JavaDoc msg = localStrings.getString(
1116                "enterprise.deployment.backend.undeploy_error_module_not_registered" );
1117                throw new IASDeploymentException( msg );
1118            }
1119            else if(isSystem)
1120            {
1121                String JavaDoc msg = localStrings.getString(
1122                "enterprise.deployment.backend.undeploy_error_module_is_a_system_resource" );
1123                throw new IASDeploymentException( msg );
1124            }
1125        }
1126        
1127        else if(request.isDeploy())
1128        {
1129            if(isReg)
1130            {
1131                if(isSystem)
1132                {
1133                    String JavaDoc msg = localStrings.getString(
1134                    "enterprise.deployment.backend.redeploy_error_module_is_a_system_resource" );
1135                    throw new IASDeploymentException( msg );
1136                }
1137                
1138                //WBN 2/27/02 - By definition -- a REDEPLOY is now a forced DEPLOY if
1139
// the Module already exists
1140
if(request.isForced())
1141                {
1142                    request.setCommand(DeploymentCommand.REDEPLOY);
1143                }
1144                else
1145                {
1146                    String JavaDoc msg = localStrings.getString(
1147                    "enterprise.deployment.backend.deploy_error_module_exists" );
1148                    throw new IASDeploymentException( msg );
1149                }
1150            }
1151            else
1152            {
1153                // isReg is false. This means that it isn't registered as the current type
1154
// of module. But we might be clashing with a registered module of a different flavor.
1155
// E.g. this may be an ejb deployment and there is a web module already deployed
1156
// with the same name.
1157
// this will throw an IASDeploymentException if it is registered to another type...
1158
checkRegisteredAnywhereElse(moduleName);
1159            }
1160        }
1161    }
1162    
1163    /**
1164     * runs the ejbc compiler for the deployable module
1165     */

1166    protected ZipItem[] runEJBC() throws IASDeploymentException
1167    {
1168        ZipItem[] clientStubs = null;
1169        
1170        try
1171        {
1172            
1173            // ejbc timing info
1174
IASEJBCTimes timing = new IASEJBCTimes();
1175            
1176            EJBCompiler compiler = new EJBCompiler
1177            (
1178            moduleName,
1179            moduleDir,
1180            oldModuleDir,
1181            stubsDir,
1182            oldStubsDir,
1183            getManager(),
1184            request,
1185            timing
1186            );
1187            
1188            // runs ejbc
1189
clientStubs = compiler.compile();
1190            
1191            // add the ejbc timing info to deployment time
1192
addEJBCTime(timing);
1193            
1194        } catch (Exception JavaDoc e)
1195        {
1196            logger.log( Level.WARNING,
1197            "enterprise.deployment_ejbc_error", e );
1198            String JavaDoc msg = localStrings.getString(
1199            "enterprise.deployment.backend.ejbc_error" );
1200            throw new IASDeploymentException(msg, e);
1201        }
1202        
1203        // returns the client stubs or an empty array if no stubs
1204
return clientStubs;
1205    }
1206    
1207    /**
1208     * @return the BaseManager implementation for this deployer
1209     */

1210    protected BaseManager getManager()
1211    {
1212        return modulesMgr;
1213    }
1214    
1215   ///////////////////////////////////////////////////////////////////////////
1216

1217    /**
1218    * Call into CMP to drop tables. This is called just before files are deleted as part
1219    * of the cleanup() call. We do it very late in the process since dropping tables
1220    * can't be rolled-back. Thus all of the other steps that can have errors that require a
1221    * rollback have already been done successfully - or we wouldn't be here.
1222    * bnevins April 2003
1223    * <p> This method call is <b>guaranteed</b> to never throw any kind of Exception
1224    */

1225    protected void dropTables()
1226    {
1227                assert isMaybeCMPDropTables; // programmer error if this is false!
1228

1229                try
1230                {
1231                    Application moduleDD;
1232                    if (FileUtils.safeIsDirectory(xmlDir)) {
1233                        moduleDD =
1234                            DeploymentUtils.getModuleDescriptor(xmlDir.getAbsolutePath());
1235                            FileArchive archive = new FileArchive();
1236                            archive.open(moduleDir.getAbsolutePath());
1237                            try {
1238                                ApplicationArchivist.
1239                                    readPersistenceDeploymentDescriptorsRecursively(
1240                                        archive, moduleDD);
1241                            } finally {
1242                                archive.close();
1243                            }
1244                    } else {
1245                        moduleDD =
1246                            DeploymentUtils.getModuleDescriptor(moduleDir.getAbsolutePath());
1247                    }
1248                    moduleDD.setRegistrationName(moduleName);
1249                                                                                
1250                    DeploymentEventInfo info = new DeploymentEventInfo(moduleDir, stubsDir, oldStubsDir, moduleDD, request);
1251                                                                                
1252                        DeploymentEvent ev = new DeploymentEvent(DeploymentEventType.PRE_UNDEPLOY, info);
1253                        DeploymentEventManager.notifyDeploymentEvent(ev);
1254                }
1255                catch(Throwable JavaDoc t)
1256                {
1257                        // yes we are swallowing all possible errors from outside this package!
1258
logger.log( Level.WARNING,
1259
1260                        "enterprise.deployment_pre_undeploy_event_error", t);
1261                }
1262    }
1263
1264    public void removePolicy() throws IASDeploymentException {
1265        //no op
1266
}
1267    
1268    ///////////////////////////////////////////////////////////////////////////
1269

1270    protected BaseManager modulesMgr = null;
1271    protected String JavaDoc moduleName = null;
1272    protected File moduleDir = null;
1273    protected File stubsDir = null;
1274    protected File jspDir = null;
1275    protected File xmlDir = null;
1276    protected File jwsDir = null;
1277    protected File originalModuleDir = null;
1278    protected String JavaDoc originalContextRoot = null;
1279    protected File oldModuleDir = null;
1280    protected File oldStubsDir = null;
1281    protected File oldJSPDir = null;
1282    protected File oldXMLDir = null;
1283    protected File oldJWSDir = null;
1284    protected ModuleEnvironment moduleEnv = null;
1285    protected String JavaDoc oldRegisteredLocation = null;
1286        protected boolean isMaybeCMPDropTables = false;
1287    private boolean wasReg = false;
1288        private boolean wasRegisteredThisSession = false;
1289    private boolean shouldRollback = false;
1290    private boolean moduleDirWasRenamed = false;
1291    private static StringManager localStrings = StringManager.getManager( ModuleDeployer.class );
1292}
1293
Popular Tags