KickJava   Java API By Example, From Geeks To Geeks.

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


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  * AppDeployer.java
26  *
27  * Created on December 11, 2001, 5:37 PM
28  */

29
30 package com.sun.enterprise.deployment.backend;
31
32 import java.io.File JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.rmi.RemoteException JavaDoc;
35 import java.security.AccessController JavaDoc;
36 import java.security.PrivilegedAction JavaDoc;
37 import java.util.logging.*;
38 import java.util.*;
39 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
40 import com.sun.enterprise.config.ConfigException;
41 import com.sun.enterprise.config.serverbeans.ServerTags;
42 import com.sun.enterprise.instance.ApplicationEnvironment;
43 import com.sun.enterprise.instance.AppsManager;
44 import com.sun.enterprise.instance.BaseManager;
45 import com.sun.enterprise.instance.InstanceEnvironment;
46 import com.sun.ejb.codegen.IASEJBCTimes;
47 import com.sun.enterprise.util.io.FileSource;
48 import com.sun.enterprise.util.io.FileUtils;
49 import com.sun.enterprise.util.zip.ZipItem;
50 import com.sun.enterprise.util.zip.ZipFileException;
51 import com.sun.enterprise.util.i18n.StringManager;
52 import com.sun.enterprise.util.diagnostics.Reminder;
53
54 import com.sun.enterprise.deployment.Application;
55 import com.sun.enterprise.deployment.BundleDescriptor;
56 import com.sun.enterprise.deployment.EjbBundleDescriptor;
57 import com.sun.enterprise.deployment.EjbDescriptor;
58 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
59 import com.sun.enterprise.deployment.deploy.shared.FileArchiveFactory;
60 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive;
61 import com.sun.enterprise.deployment.Descriptor;
62 import com.sun.enterprise.deployment.util.ModuleDescriptor;
63 import com.sun.enterprise.deployment.WebBundleDescriptor;
64 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
65 import com.sun.enterprise.deployment.archivist.ApplicationArchivist;
66
67 // imports for dd generator
68
import com.sun.enterprise.deployment.interfaces.*;
69 import com.sun.enterprise.tools.verifier.AppVerifier;
70
71 import com.sun.enterprise.security.application.EJBSecurityManager;
72 import com.sun.enterprise.security.factory.EJBSecurityManagerFactory;
73 import com.sun.web.security.WebSecurityManagerFactory;
74
75 /**
76  * AppDeployer is responsible for Deploying Applications. Both archives and
77  * user-supplied "pre-exploded" directories are supported.
78  *
79  * WBN February 2, 2002 -- this code is now officially a mess!
80  * @author bnevins
81  * @version
82  */

83 public class AppDeployer extends AppDeployerBase
84 {
85     AppDeployer(DeploymentRequest r) throws IASDeploymentException
86     {
87         super(r);
88     }
89
90     ///////////////////////////////////////////////////////////////////////////
91

92     public void doRequest() throws IASDeploymentException
93     {
94         doRequestPrepare();
95         doRequestFinish();
96     }
97
98
99     ///////////////////////////////////////////////////////////////////////////
100

101     public void doRequestPrepare() throws IASDeploymentException
102     {
103         try
104         {
105             begin();
106         }
107         catch(Exception JavaDoc e)
108         {
109             rollback();
110             String JavaDoc msg = localStrings.getString(
111                     "enterprise.deployment.backend.dorequest_exception" );
112             logger.log(Level.WARNING, msg, e);
113                         if (e instanceof IASDeploymentException) {
114                             throw (IASDeploymentException) e;
115                         } else {
116                             throw new IASDeploymentException( msg, e);
117                         }
118         }
119     }
120
121
122     ///////////////////////////////////////////////////////////////////////////
123

124     public void doRequestFinish() throws IASDeploymentException
125     {
126         try
127         {
128             predeploy();
129             deploy();
130             generatePolicy();
131         }
132         catch(Exception JavaDoc e)
133         {
134             String JavaDoc msg = localStrings.getString(
135                     "enterprise.deployment.backend.dorequest_exception" );
136             rollback();
137             logger.log(Level.FINE, msg, e);
138                         if (e instanceof IASDeploymentException) {
139                             throw (IASDeploymentException) e;
140                         } else {
141                             throw new IASDeploymentException( msg, e);
142                         }
143         }
144         finally
145         {
146             finish();
147         }
148     }
149
150     ///////////////////////////////////////////////////////////////////////////
151

152     public void cleanup_internal()
153     {
154         // nothing to do.
155
}
156
157     ///////////////////////////////////////////////////////////////////////////
158

159     protected void predeploy() throws IASDeploymentException
160     {
161         super.predeploy();
162             // if the directories already exist on disk -- wipe them out
163
// We do this because otherwise they'd never get this App deployed
164
// without wiping the dirs manually
165

166         if(request.isDeploy())
167             liquidateAppDirAndStubsDirIfTheyHappenToExist();
168     }
169     
170     ///////////////////////////////////////////////////////////////////////////
171

172     protected void deploy() throws IASDeploymentException
173     {
174         try
175         {
176             // retrieve the J2EECPhase deployment status
177
DeploymentStatus J2EECPhaseStatus =
178             request.getCurrentDeploymentStatus();
179
180             Application app;
181             if(isArchive()) {
182                 app = explodeArchive();
183             } else { //directory deploy
184
//create the generated/xml directory for directory deploy
185
getXMLDir().mkdirs();
186                 getJWSDir().mkdirs();
187
188                 // construct the standard application.xml if omitted
189
ApplicationArchivist appArchivist = new ApplicationArchivist();
190                 String JavaDoc dir = request.getDeployedDirectory().getAbsolutePath();
191                 FileArchive appArchive = new FileArchive();
192                 appArchive.open(dir);
193                 if (!appArchivist.hasStandardDeploymentDescriptor(appArchive)) {
194                     Application appDesc =
195                         Application.createApplication(appArchive,true,true);
196                     request.setDescriptor(appDesc);
197                 }
198
199                 // load deployment descriptors
200
app = loadDescriptors();
201             }
202             request.setDescriptor(app);
203                         
204                         // Set the generated XML directory in application desc
205
request.getDescriptor().setGeneratedXMLDirectory(getXMLDir().getAbsolutePath());
206
207                         // create a DeploymentStatus for runEJBC stage
208
// it is a substage of J2EECPhase status
209
DeploymentStatus runEJBCStatus =
210                             new DeploymentStatus(J2EECPhaseStatus);
211                         request.setCurrentDeploymentStatus(runEJBCStatus);
212             runJSPC();
213             runVerifier();
214             ZipItem[] clientStubs = runEJBC();
215             
216                         checkAppclientsMainClasses();
217                         
218             createClientJar(clientStubs);
219
220                         // create a DeploymentStatus for postDeploy stage
221
// it is a substage of J2EECPhase status
222
DeploymentStatus postDeployStatus =
223                             new DeploymentStatus(J2EECPhaseStatus);
224                         request.setCurrentDeploymentStatus(postDeployStatus);
225             postDeploy();
226
227             addToSummary(getSuccessMessage() + getAppName() + "\n*********************\n");
228         }
229         catch(IASDeploymentException e)
230         {
231             throw e;
232         }
233         catch(Exception JavaDoc e)
234         {
235             throw new IASDeploymentException(e);
236         }
237     }
238     
239     ///////////////////////////////////////////////////////////////////////////
240

241     protected void rollback()
242     {
243                 // do not need to roll back config since the config
244
// has not been added yet. It's added after both
245
// doRequestPrepare and doRequestFinish methods.
246

247         // blow away new dirs, rename old ondestinationes back to new ones...
248
try
249         {
250             // careful -- this method is called from inside of a catch block.
251
// If we throw an exception from here, it'll be BIG trouble!
252
DeleteOrKeepFailedStubs(getStubsDir());
253
254             if(FileUtils.safeIsDirectory(getJSPDir()))
255                 FileUtils.whack(getJSPDir());
256
257                         if (FileUtils.safeIsDirectory(getXMLDir())) {
258                             FileUtils.whack(getXMLDir());
259                         }
260
261                         if (FileUtils.safeIsDirectory(getJWSDir())) {
262                             FileUtils.whack(getJWSDir());
263                         }
264
265             if(isArchive())
266             {
267                 if(FileUtils.safeIsDirectory(getAppDir()))
268                     FileUtils.whack(getAppDir());
269             }
270
271         }
272         catch(Throwable JavaDoc t)
273         {
274             // Can't do anything about it!!
275
logger.log( Level.WARNING,
276                     "enterprise.deployment_rollback_error", t );
277         }
278     }
279
280     ///////////////////////////////////////////////////////////////////////////
281

282     private void liquidateAppDirAndStubsDirIfTheyHappenToExist() throws IASDeploymentException
283     {
284             if (isArchive()) {
285                 cleanAndCheck(getAppDir());
286             }
287             cleanAndCheck(getStubsDir());
288             cleanAndCheck(getJSPDir());
289             cleanAndCheck(getXMLDir());
290             cleanAndCheck(getJWSDir());
291     }
292     
293         /**
294          *Cleans out a directory, logging a warning and aborting the current
295          *operation (such as deployment, undeployment, etc.) if the directory
296          *remains.
297          *@param dir the directory File to be cleaned out
298          *@throws IASDeploymentException if the directory is not cleaned out successfully
299          */

300         private void cleanAndCheck(File JavaDoc dir) throws IASDeploymentException {
301             if(FileUtils.safeIsDirectory(dir)) {
302                 FileUtils.whack(dir);
303                 /*
304                  *If the directory remains after the preceding step, then
305                  *the clean-up did not succeed fully. Report this and
306                  *abort the operation so the user knows that files that
307                  *should have been deleted are still there and could interfere
308                  *with a subsequent redeployment, for example.
309                  */

310                 if(FileUtils.safeIsDirectory(dir))
311                     {
312                         String JavaDoc msg = localStrings.getString("enterprise.deployment.backend.cannot_delete_dir", dir);
313                         throw new IASDeploymentException(msg);
314                     }
315             }
316         }
317     ///////////////////////////////////////////////////////////////////////////
318

319     private Application explodeArchive() throws Exception JavaDoc
320     {
321             
322             // first explode the ear file
323
Application appDesc = J2EEModuleExploder.explodeEar(request.getFileSource().getFile(), getAppDir());
324             appDesc.setRegistrationName(request.getName());
325             
326             request.setDescriptor(appDesc);
327             getXMLDir().mkdirs();
328             getJWSDir().mkdirs();
329             getStubsDir().mkdirs();
330                         
331             // now I can call the normal descriptor loading...
332
loadDescriptors();
333
334             return appDesc;
335     }
336
337     ///////////////////////////////////////////////////////////////////////////
338

339     private ZipItem[] runEJBC() throws IASDeploymentException
340     {
341         ZipItem[] clientStubs = null;
342
343         try {
344             // ejbc timing info
345
IASEJBCTimes timing = new IASEJBCTimes();
346             EJBCompiler compiler = new EJBCompiler
347             (
348                 getAppName(),
349                 getAppDir(),
350                 getOldAppDir(),
351                 getStubsDir(),
352                 getOldStubsDir(),
353                 getManager(),
354                 request,
355                 timing
356             );
357             
358             // runs ejbc
359
clientStubs = compiler.compile();
360
361             /*
362              *Clean up the temporary jar expansion directory.
363              *
364              *Commenting this out to avoid degrading performance.
365              */

366 // cleanTempUnjarDirectories(adjustedClassPathList);
367

368             // add the ejbc timing info to deployment time
369
addEJBCTime(timing);
370
371         }
372         catch(IASDeploymentException e) {
373             throw e;
374         }
375         catch (Exception JavaDoc e) {
376             logger.log( Level.WARNING,
377             "enterprise.deployment_ejbc_error", e );
378             String JavaDoc msg = localStrings.getString(
379             "enterprise.deployment.backend.ejbc_error" );
380             throw new IASDeploymentException(msg, e);
381         }
382
383         // returns the client stubs or an empty array if no stubs
384
return clientStubs;
385     }
386
387     ///////////////////////////////////////////////////////////////////////////
388

389     private void runJSPC() throws IASDeploymentException
390     {
391         if(!request.getPrecompileJSP())
392         return;
393         
394         Application app = request.getDescriptor();
395         Set webBundleDescriptors = app.getWebBundleDescriptors();
396             if (webBundleDescriptors == null) {
397                 return;
398             }
399
400             Iterator itr = webBundleDescriptors.iterator();
401         while (itr.hasNext())
402         {
403         WebBundleDescriptor wbd = (WebBundleDescriptor) itr.next();
404         String JavaDoc warDirName = FileUtils.makeFriendlyFilename(
405                                 wbd.getModuleDescriptor().getArchiveUri());
406         File JavaDoc outDir = new File JavaDoc(getJSPDir(), warDirName);
407         File JavaDoc inDir = new File JavaDoc(getAppDir(), warDirName);
408         //String msg = "***** Call JSPCompiler(" + inDir.getPath()+ ", " + outDir.getPath() + ")";
409
//logger.log(Level.SEVERE, msg);
410
long time = System.currentTimeMillis();
411         JSPCompiler.compile(inDir, outDir, wbd,
412                                     request.getCompleteClasspath());
413         addJSPCTime(System.currentTimeMillis() - time);
414         }
415     }
416
417     ///////////////////////////////////////////////////////////////////////////
418

419     protected void postDeploy() throws ConfigException, IASDeploymentException
420     {
421         Application applicationDD = request.getDescriptor();
422                        getManager().registerDescriptor(applicationDD.getRegistrationName(),
423                                                        applicationDD);
424         DeploymentEventInfo info = new DeploymentEventInfo(
425             getAppDir(), getStubsDir(), getOldStubsDir(),
426             applicationDD, getRequest());
427         DeploymentEvent ev = new DeploymentEvent(
428             DeploymentEventType.POST_DEPLOY, info);
429         DeploymentEventManager.notifyDeploymentEvent(ev);
430
431         // need to populate web security PolicyConfig
432
WebSecurityManagerFactory wsmfactory =
433             WebSecurityManagerFactory.getInstance();
434         for (Iterator iter = applicationDD.getWebBundleDescriptors().iterator(); iter.hasNext();)
435         {
436             wsmfactory.newWebSecurityManager((WebBundleDescriptor)iter.next());
437         }
438         // need to populate ejb security PolicyConfig
439
EJBSecurityManagerFactory ejbmfactory =
440                     (EJBSecurityManagerFactory)EJBSecurityManagerFactory.getInstance();
441                 for (Object JavaDoc ejbBundleDescObj : applicationDD.getEjbBundleDescriptors()) {
442                     for (Object JavaDoc ejbDescObj : ((EjbBundleDescriptor)ejbBundleDescObj).getEjbs()) {
443                         ejbmfactory.createSecurityManager((EjbDescriptor)ejbDescObj);
444                     }
445         }
446
447         if(!isDirectory()) {
448                     postDeployArchive();
449                 }
450
451                 getManager().registerDescriptor(request.getName(), request.getDescriptor());
452
453                 // save the object type in optional attributes
454
Properties optionalAttributes = request.getOptionalAttributes();
455                 if (optionalAttributes == null) {
456                     optionalAttributes = new Properties();
457                 }
458                 String JavaDoc resourceType = getResourceType(getAppDir());
459                 if(resourceType != null) {
460                     optionalAttributes.setProperty(ServerTags.OBJECT_TYPE,
461                         resourceType);
462                 }
463     }
464
465     ///////////////////////////////////////////////////////////////////////////
466

467     private void postDeployArchive() throws ConfigException, IASDeploymentException
468     {
469     }
470
471     ///////////////////////////////////////////////////////////////////////////
472

473         protected File JavaDoc setAppDir() throws IASDeploymentException {
474             // brand-new deployment, so no versioning required.
475
ApplicationEnvironment aenv = getAppEnv();
476             File JavaDoc appDirectory;
477             
478             if(isArchive()) {
479                 File JavaDoc parent = new File JavaDoc(getInstanceEnv().getApplicationRepositoryPath());
480                 appDirectory = new File JavaDoc(parent, getAppName());
481                 appDirectory.mkdirs();
482             }
483             else if(isDirectory()) {
484                 FileSource fileSource = request.getFileSource();
485                 
486                 if(!fileSource.exists()) {
487                     String JavaDoc msg = localStrings.getString(
488                     "enterprise.deployment.backend.file_source_does_not_exist",
489                     fileSource.toString() );
490                     throw new IASDeploymentException( msg );
491                 }
492                 
493                 assert fileSource.isDirectory();
494                 appDirectory = fileSource.getFile();
495             }
496             else {
497                 String JavaDoc msg = localStrings.getString(
498                 "enterprise.deployment.backend.deployment_not_dir_or_archive");
499                 throw new IASDeploymentException( msg );
500             }
501             
502             return appDirectory;
503         }
504     
505     ///////////////////////////////////////////////////////////////////////////
506

507     protected File JavaDoc getOldStubsDir()
508     {
509         return null;
510     }
511     
512     ///////////////////////////////////////////////////////////////////////////
513

514     protected File JavaDoc getOldAppDir()
515     {
516         return null;
517     }
518     
519     ///////////////////////////////////////////////////////////////////////////
520

521     protected String JavaDoc whatAreYou()
522     {
523         return "Deployment";
524     }
525     
526     ///////////////////////////////////////////////////////////////////////////
527

528     protected final String JavaDoc getSuccessMessage()
529     {
530         return stars + "**** " + whatAreYou() + " successful for " + getAppName() + " ****" + stars;
531     }
532     
533     ///////////////////////////////////////////////////////////////////////////
534

535     protected final String JavaDoc getFailureMessage()
536     {
537         return stars + "**** " + whatAreYou() + " failed for " + getAppName() + " ****" + stars;
538     }
539
540     ///////////////////////////////////////////////////////////////////////////
541
/**
542      * Runs the verifier on this module if verification is ON in
543      * the deployment request.
544      *
545      * @throws IASDeploymentException if an error found in this module
546      * after verification
547      */

548     private void runVerifier() throws IASDeploymentException
549     {
550         if (request.isVerifying()) {
551             try {
552                 String JavaDoc archive = request.getDeployedDirectory().getCanonicalPath();
553                 File JavaDoc jspDir = (request.getPrecompileJSP())? getJSPDir():null;
554                 new AppVerifier().verify(request.getDescriptor(),
555                         (new FileArchiveFactory()).openArchive(archive),
556                         request.getCompleteClasspath(),
557                         jspDir);
558             } catch (Exception JavaDoc e) {
559                 String JavaDoc msg = localStrings.getString(
560                         "enterprise.deployment.backend.verifier_error");
561                 throw new IASDeploymentException(msg);
562             }
563         }
564     }
565     private final static String JavaDoc stars = "\n*********************\n";
566         private static StringManager localStrings =
567             StringManager.getManager( AppDeployer.class );
568         
569 }
570
Popular Tags