KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > phasing > J2EECPhase


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  * J2EECPhase.java
26  *
27  * Created on May 20, 2003, 3:13 PM
28  * @author sandhyae
29  * <BR> <I>$Source: /cvs/glassfish/appserv-core/src/java/com/sun/enterprise/deployment/phasing/J2EECPhase.java,v $
30  *
31  */

32
33 package com.sun.enterprise.deployment.phasing;
34
35 import com.sun.appserv.management.deploy.DeploymentProgress;
36 import com.sun.appserv.management.deploy.DeploymentProgressImpl;
37 import com.sun.enterprise.admin.event.BaseDeployEvent;
38 import com.sun.enterprise.appverification.factory.AppVerification;
39 import com.sun.enterprise.deployment.Application;
40 import com.sun.enterprise.deployment.backend.DeployableObjectType;
41 import com.sun.enterprise.deployment.backend.Deployer;
42 import com.sun.enterprise.deployment.backend.DeployerFactory;
43 import com.sun.enterprise.deployment.backend.DeploymentEvent;
44 import com.sun.enterprise.deployment.backend.DeploymentEventInfo;
45 import com.sun.enterprise.deployment.backend.DeploymentEventType;
46 import com.sun.enterprise.deployment.backend.DeploymentLogger;
47 import com.sun.enterprise.deployment.backend.DeploymentRequest;
48 import com.sun.enterprise.deployment.backend.DeploymentStatus;
49 import com.sun.enterprise.deployment.backend.IASDeploymentException;
50 import com.sun.enterprise.deployment.BundleDescriptor;
51 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive;
52 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
53 import com.sun.enterprise.deployment.util.DeploymentProperties;
54 import com.sun.enterprise.deployment.util.ModuleDescriptor;
55 import com.sun.enterprise.deployment.WebBundleDescriptor;
56 import com.sun.enterprise.deployment.WebService;
57 import com.sun.enterprise.management.deploy.DeploymentCallback;
58 import com.sun.enterprise.util.i18n.StringManager;
59 import com.sun.enterprise.util.io.FileUtils;
60 import com.sun.enterprise.util.RelativePathResolver;
61
62 import java.io.File JavaDoc;
63 import java.io.IOException JavaDoc;
64 import java.util.Enumeration JavaDoc;
65 import java.util.Iterator JavaDoc;
66 import java.util.logging.Level JavaDoc;
67 import java.util.logging.Logger JavaDoc;
68
69 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
70
71 /**
72  * This class represents the J2EEC phase of the deployment process.
73  * An application or module is prepared using the deployer and then registered with the
74  * config and a corresponding mbean registered with the mbean server
75  * @author Sandhya E
76  */

77 public class J2EECPhase extends DeploymentPhase {
78     
79     /** Deployment Logger object for this class */
80     public static final Logger sLogger = DeploymentLogger.get();
81     
82     /** string manager */
83     private static StringManager localStrings =
84         StringManager.getManager( J2EECPhase.class );
85    
86
87     /**
88      * Creates a new instance of J2EECPhase
89      * @param deploymentCtx DeploymentContext object
90      */

91     public J2EECPhase(DeploymentContext deploymentCtx) {
92         this.deploymentCtx = deploymentCtx;
93         this.name = J2EEC;
94     }
95     
96     /**
97      * Phase specific execution logic will go in this method. Any phase implementing
98      * this class will provide its implementation for this method.
99      * preDeploy notifications are sent from the deployment backend(deployer) to
100      * the cmp module.
101      * @param phaseCtx the DeploymentPhaseContext object
102      */

103     public void runPhase(DeploymentPhaseContext phaseCtx)
104     {
105         boolean wasUnRegistered = false;
106         String JavaDoc type = null;
107         boolean isApp = false;
108         boolean isRedeploy = false;
109         int actionCode = -1;
110         String JavaDoc targetName = null;
111         DeploymentTarget target = null;
112         DeploymentRequest req = phaseCtx.getDeploymentRequest();
113         DeploymentStatus status = phaseCtx.getDeploymentStatus();
114
115         Deployer deployer = null;
116         try {
117             DeploymentCallback callback = req.getDeploymentCallback();
118             if (callback != null) {
119                 int percent = 0;
120                 DeploymentProgress progress = new DeploymentProgressImpl(
121                     (byte)percent, "deployment started", null);
122                 callback.deploymentProgress(progress);
123             }
124         
125             // set the current deployment status in deployment request
126
// to J2EECPhase deployment status
127
req.setCurrentDeploymentStatus(status);
128
129             deployer = DeployerFactory.getDeployer(req);
130             
131             //prePhaseNotify(getPrePhaseEvent(deployer.getPreEventInfo()));
132
if(req.isApplication())
133                 isApp = true;
134             
135             deployer.doRequestPrepare();
136             
137             // the isReDeploy() call *must* come after doRequestPrepare() for modules.
138
if(req.isReDeploy()) {
139                 isRedeploy = true;
140
141                 //Re-record instrospect/instrument/verifier data if
142
//any of the application is re-deployed
143
if (AppVerification.doInstrument()) {
144                     AppVerification.getInstrumentLogger().handleChangeInDeployment();
145                 }
146             }
147             
148             if(isRedeploy) {
149                 target = (DeploymentTarget)req.getTarget();
150                 
151                 // In the case of redeploy to domain,
152
// no stop event will be sent.
153
if(target != null && ! target.getName().equals("domain")) {
154                     targetName = target.getName();
155                 
156                     if(isApp) {
157                         type = null;
158                         actionCode = BaseDeployEvent.APPLICATION_UNDEPLOYED;
159                     }
160                     else {
161                         type = DeploymentServiceUtils.getModuleTypeString(req.getType());
162                         actionCode = BaseDeployEvent.MODULE_UNDEPLOYED;
163                     }
164                     DeploymentServiceUtils.multicastEvent(actionCode, req.getName(), type, req.getCascade(), req.isForced(), targetName);
165                     wasUnRegistered = true;
166                 }
167             }
168             
169             deployer.doRequestFinish();
170             
171             // check if an abort operation has been issued
172
// throw exception if true
173
DeploymentServiceUtils.checkAbort(req.getName());
174
175             // do all the config update at the end for easy rollback
176
if(isRedeploy) {
177                 DeploymentServiceUtils.updateConfig(req);
178             } else {
179                 DeploymentServiceUtils.addToConfig(req);
180             }
181  
182             // set context roots on config bean
183
ApplicationConfigHelper.resetAppContextRoots(
184                 DeploymentServiceUtils.getConfigContext(), req.getName(),
185                 true);
186
187             wasUnRegistered = false; // addToConfig re-registered it...
188

189             deployer.cleanup();
190             
191             // everything went fine
192
status.setStageStatus(DeploymentStatus.SUCCESS);
193             
194             // some useful information for clients...
195
sLogger.log(Level.INFO, "deployed with " + DeploymentProperties.MODULE_ID + " = " + req.getName());
196             populateStatusProperties(status, req);
197             if (callback != null) {
198                 int percent = 100;
199                 DeploymentProgress progress2 = new DeploymentProgressImpl(
200                     (byte)percent, "deployment finished", null);
201                 callback.deploymentProgress(progress2);
202             }
203          
204         } catch(Throwable JavaDoc t) {
205             String JavaDoc msg =
206             localStrings.getString("enterprise.deployment.phasing.j2eec.error" );
207
208             // For any failure during the J2EEC phase (during fresh deploy or redeploy), cleanup the domain.xml
209
// so that the config is left in a clean state without any hanging j2ee-app/module elements without
210
// any app-refs;
211
// the target==null check ensures that this code is done for 8.1 only thereby preserving same
212
// behavior for 8.0 apps
213
try {
214                 if (target == null) {
215                     if (deployer != null) {
216                         deployer.removePolicy();
217                     }
218                     DeploymentServiceUtils.removeFromConfig(req.getName(),
219                         req.getType());
220                 }
221             } catch (Exception JavaDoc eee){}
222
223             if(isRedeploy && wasUnRegistered && t instanceof IASDeploymentException && req.getReRegisterOnFailure()) {
224                 // DBE rollback re-registered. We need to notify to get it reloaded now.
225
if(isApp)
226                     actionCode = BaseDeployEvent.APPLICATION_DEPLOYED;
227                 else
228                     actionCode = BaseDeployEvent.MODULE_DEPLOYED;
229                 
230                 try {
231                     DeploymentServiceUtils.multicastEvent(actionCode, req.getName(), type, req.getCascade(), req.isForced(), targetName);
232                 }
233                 catch(Throwable JavaDoc t2) {
234                     msg += t2;
235                 }
236             }
237             // log
238
sLogger.log(Level.SEVERE, msg, t);
239             
240             // we now update our status
241
status.setStageStatus(DeploymentStatus.FAILURE);
242             if (t instanceof java.io.Serializable JavaDoc) {
243                 status.setStageException(t);
244             } else {
245                 sLogger.severe(localStrings.getString("enterprise.deployment.phasing.exception_notserializable", t.getClass()));
246                 sLogger.severe(localStrings.getString("enterprise.deployment.phasing.exception_notforwarded", t.getMessage()));
247             }
248             status.setStageStatusMessage(t.getMessage());
249         }
250     }
251     
252     /**
253      * Event that will be broadcasted at the start of the phase
254      * @param info deployment event info
255      * @return DeploymentEvent
256      **/

257     protected DeploymentEvent getPrePhaseEvent(DeploymentEventInfo info) {
258         //FIXME change the event type and include a eventSource
259
return new DeploymentEvent(DeploymentEventType.PRE_DEPLOY, info );
260     }
261     
262     /**
263      * Event that will be broadcasted at the end of the phase
264      * @return DeploymentEvent
265      **/

266     protected DeploymentEvent getPostPhaseEvent(DeploymentEventInfo info) {
267         //FIXME change the event type and include a eventSource
268
return new DeploymentEvent(DeploymentEventType.POST_DEPLOY, info);
269     }
270
271     /**
272      * This method populate the properties that will be returned from server to
273      * the client, including moduleID and list of wsdl files.
274      */

275     private void populateStatusProperties(
276                     DeploymentStatus status, DeploymentRequest request)
277         throws IOException JavaDoc, IASDeploymentException {
278         DeploymentStatus mainStatus = status.getMainStatus();
279         populateModuleIDs(mainStatus, request);
280         if (request.getDescriptor() != null) {
281             populateWsdlFilesForPublish(mainStatus, request);
282         }
283     }
284
285     /**
286      * Populate the moduleID, subModuleID and ModuleType information.
287      * The returned data is constructed as follows:
288      * For example: a.ear contains b.jar, c.jar and d.war
289      * The entries in the DeploymentStatus additional properties are:
290      * (in key = value format)
291      * moduleid = a;
292      * a_moduletype = application;
293      * a_submoduleCount = 3;
294      * a_moduleid_0 = b;
295      * b_moduletype = ejb;
296      * a_moduleid_1 = c;
297      * c_moduletype = appclient;
298      * a_moduleid_2 = d;
299      * d_moduletype = web;
300      * d_contextroot= contextroot;
301      *
302      * Note that the actual construct of the submoduleID is a bit more
303      * complicated than what is in the example above. But the idea is
304      * the same.
305      */

306     private void populateModuleIDs(
307                     DeploymentStatus status, DeploymentRequest request) {
308
309         String JavaDoc sep = DeploymentStatus.KEY_SEPARATOR;
310
311         //top module
312
String JavaDoc key = DeploymentStatus.MODULE_ID;
313         String JavaDoc moduleID = request.getName();
314         status.addProperty(key, moduleID); //moduleID
315

316         key = com.sun.appserv.management.deploy.DeploymentStatus.MODULE_ID_KEY;
317         status.addProperty(key, moduleID); //moduleID
318

319         key = moduleID + sep + DeploymentStatus.MODULE_TYPE;
320         ModuleType JavaDoc moduleType = request.getType().getModuleType();
321         status.addProperty(key, String.valueOf(moduleType.getValue())); //moduleType
322

323         //sub modules
324
Application app = request.getDescriptor();
325         if (app!=null) {
326             if (!app.isVirtual()) {
327                 int counter = 0;
328                 for (Iterator JavaDoc it = app.getModules(); it.hasNext();) {
329                     ModuleDescriptor md = (ModuleDescriptor) it.next();
330
331                     key = moduleID + sep +
332                           DeploymentStatus.MODULE_ID + sep +
333                           String.valueOf(counter);
334                     String JavaDoc subModuleID = moduleID + "#" + md.getArchiveUri();
335                     status.addProperty(key, subModuleID); //subModuleID
336

337                     key = subModuleID + sep + DeploymentStatus.MODULE_TYPE;
338                     //subModuleType
339
status.addProperty(key, String.valueOf(md.getModuleType().getValue()));
340
341                     if (ModuleType.WAR.equals(md.getModuleType())) {
342                         WebBundleDescriptor wbd =
343                             (WebBundleDescriptor) md.getDescriptor();
344                         key = subModuleID + sep + DeploymentStatus.CONTEXT_ROOT;
345                         status.addProperty(key, getContextRoot(wbd)); //contextRoot
346
}
347                     counter++;
348                 }
349
350                 key = moduleID + sep + DeploymentStatus.SUBMODULE_COUNT;
351                 status.addProperty(key, String.valueOf(counter)); //nums
352
} else { //standalone module
353
BundleDescriptor bd = app.getStandaloneBundleDescriptor();
354                 if (ModuleType.WAR.equals(bd.getModuleType())) {
355                     WebBundleDescriptor wbd = (WebBundleDescriptor) bd;
356                     key = moduleID + sep + DeploymentStatus.CONTEXT_ROOT;
357                     status.addProperty(key, getContextRoot(wbd)); //contextRoot
358
}
359             }
360         }
361     }
362
363     /**
364      * Populate the wsdl files entries to download (if any).
365      */

366     private void populateWsdlFilesForPublish(
367                     DeploymentStatus status, DeploymentRequest request)
368         throws IOException JavaDoc, IASDeploymentException {
369
370         ModuleType JavaDoc moduleType = request.getType().getModuleType();
371         //only EAR, WAR and EJB archives could contain wsdl files for publish
372
if (!(ModuleType.EAR.equals(moduleType) ||
373               ModuleType.WAR.equals(moduleType) ||
374               ModuleType.EJB.equals(moduleType))) {
375             return;
376         }
377
378         String JavaDoc sep = DeploymentStatus.KEY_SEPARATOR;
379         Application app = request.getDescriptor();
380         String JavaDoc moduleID = request.getName();
381         AbstractArchive moduleArchive = null;
382         String JavaDoc key = null;
383         String JavaDoc keyPrefix = null;
384
385         FileArchive archive = new FileArchive();
386         archive.open(RelativePathResolver.resolvePath(
387                         getGeneratedAppLocation(request)));
388
389         for (Iterator JavaDoc it = app.getWebServiceDescriptors().iterator();
390                 it.hasNext();) {
391             WebService webService = (WebService) it.next();
392             // No work needed if webservice is configured for URL publishing
393
if (!webService.hasFilePublishing()) {
394                 continue;
395             }
396
397             // For file publishing, URL is a file URL for a directory
398
String JavaDoc clientPublishURL =
399                     webService.getClientPublishUrl().toExternalForm();
400             if (app.isVirtual()) { //standalone module
401
keyPrefix = moduleID;
402                 moduleArchive = archive;
403             } else {
404                 ModuleDescriptor md =
405                         webService.getBundleDescriptor().getModuleDescriptor();
406                 keyPrefix = moduleID + "#" + md.getArchiveUri();
407                 moduleArchive = archive.getEmbeddedArchive(md.getArchiveUri());
408             }
409
410             key = keyPrefix + sep + DeploymentStatus.WSDL_PUBLISH_URL;
411             status.addProperty(key, clientPublishURL);
412
413             // Collect the names of all entries in or below the
414
// dedicated wsdl directory.
415
BundleDescriptor bundle = webService.getBundleDescriptor();
416             Enumeration JavaDoc entries = moduleArchive.entries(bundle.getWsdlDir());
417
418             // Strictly speaking, we only need to write the files needed by
419
// imports of this web service's wsdl file. However, it's not
420
// worth actual parsing the whole chain just to figure this
421
// out. In the worst case, some unnecessary files under
422
// META-INF/wsdl or WEB-INF/wsdl will be written to the publish
423
// directory.
424
int counter = 0;
425             while(entries.hasMoreElements()) {
426                 String JavaDoc name = (String JavaDoc) entries.nextElement();
427                 key = keyPrefix + sep + DeploymentStatus.WSDL_FILE_ENTRIES +
428                             sep + String.valueOf(counter);
429                 status.addProperty(key, stripWsdlDir(name,bundle));
430
431                 //full path to the wsdl file location on the server
432
String JavaDoc wsdlFileLocation =
433                         moduleArchive.getArchiveUri() + File.separator +
434                         name.replace('/', File.separatorChar);
435                 key = key + sep + DeploymentStatus.WSDL_LOCATION;
436                 status.addProperty(key, wsdlFileLocation);
437                 counter++;
438             }
439             key = keyPrefix + sep + DeploymentStatus.WSDL_FILE_ENTRIES
440                     + sep + DeploymentStatus.COUNT;
441             status.addProperty(key, String.valueOf(counter));
442         }
443     }
444
445     private String JavaDoc getContextRoot(WebBundleDescriptor wbd) {
446         String JavaDoc contextRoot = wbd.getContextRoot();
447         if (!contextRoot.startsWith("/")) {
448             contextRoot = "/" + contextRoot;
449         }
450         return contextRoot;
451     }
452
453     private String JavaDoc getGeneratedAppLocation (DeploymentRequest request)
454         throws IASDeploymentException {
455         String JavaDoc xmlDir = request.getDescriptor().getGeneratedXMLDirectory();
456         // for upgrade scenario, we fall back to the original location
457
if (xmlDir == null || !FileUtils.safeIsDirectory(xmlDir)) {
458             xmlDir = DeploymentServiceUtils.getLocation(
459                         request.getName(), request.getType());
460         }
461         return xmlDir;
462     }
463
464     /**
465      * Return the entry name without "WEB-INF/wsdl" or "META-INF/wsdl".
466      */

467     private String JavaDoc stripWsdlDir(String JavaDoc entry, BundleDescriptor bundle) {
468         String JavaDoc wsdlDir = bundle.getWsdlDir();
469         return entry.substring(wsdlDir.length()+1);
470     }
471 }
472
Popular Tags