KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > client > UndeployAction


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 package com.sun.enterprise.deployment.client;
25
26 import com.sun.appserv.management.client.ConnectionSource;
27 import com.sun.appserv.management.client.ProxyFactory;
28 import com.sun.appserv.management.config.ConfigConfig;
29 import com.sun.appserv.management.config.DomainConfig;
30 import com.sun.appserv.management.config.HTTPServiceConfig;
31 import com.sun.appserv.management.config.VirtualServerConfig;
32 import com.sun.appserv.management.deploy.DeploymentMgr;
33 import com.sun.appserv.management.deploy.DeploymentProgress;
34 import com.sun.appserv.management.deploy.DeploymentProgressImpl;
35 import com.sun.appserv.management.deploy.DeploymentSource;
36 import com.sun.appserv.management.deploy.DeploymentSupport;
37 import com.sun.enterprise.deployapi.ProgressObjectImpl;
38 import com.sun.enterprise.deployapi.SunTarget;
39 import com.sun.enterprise.deployapi.SunTargetModuleID;
40 import com.sun.enterprise.deployment.backend.DeploymentStatus;
41 import com.sun.enterprise.deployment.util.DeploymentProperties;
42 import com.sun.enterprise.util.i18n.StringManager;
43
44 import java.util.ArrayList JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import java.util.Map JavaDoc;
47 import java.util.Set JavaDoc;
48
49 import javax.enterprise.deploy.shared.ModuleType JavaDoc;
50 import javax.enterprise.deploy.shared.StateType JavaDoc;
51 import javax.enterprise.deploy.spi.Target JavaDoc;
52 import javax.enterprise.deploy.spi.TargetModuleID JavaDoc;
53
54 public class UndeployAction extends ProgressObjectImpl {
55
56     private DeploymentMgr deplMgr = null;
57     private static StringManager localStrings = StringManager.getManager(UndeployAction.class);
58     
59     public UndeployAction(SunTarget[] targets) {
60         super(targets);
61     }
62
63     public void run() {
64         ConnectionSource dasConnection= (ConnectionSource) args[0];
65         String JavaDoc moduleID = (String JavaDoc) args[1];
66         Map JavaDoc deployOptions = (Map JavaDoc) args[2];
67         SunTarget[] targetList = (SunTarget[]) args[3];
68         SunTarget domain = (SunTarget) args[4];
69         boolean isLocalConnectionSource = ((Boolean JavaDoc) args[5]).booleanValue();
70
71         try {
72             // First check if this module is a web module and if so, it is a default web module
73
if((DeploymentClientUtils.getModuleType(dasConnection.getExistingMBeanServerConnection(), moduleID)
74                 == ModuleType.WAR) &&
75                (isDefaultWebModule(domain, dasConnection, moduleID))) {
76                 return;
77             }
78             deplMgr = ProxyFactory.getInstance(dasConnection).getDomainRoot().getDeploymentMgr();
79             // Get list of all targets on which this module is already deployed
80
Map JavaDoc deployedTargets = DeploymentClientUtils.getDeployedTargetList(dasConnection, moduleID);
81
82             // the target module ids in which the operation was successful
83
ArrayList JavaDoc resultTargetModuleIDs = new ArrayList JavaDoc();
84
85             // if there is already app ref associated with this app
86
if (deployedTargets.size() > 0) {
87                 // if it's undeploy from domain, then it's equivalent
88
// to undeploy from all targets
89
if ((TargetType.DOMAIN.equals(targetList[0].getName()))) {
90                     DeploymentFacility deploymentFacility;
91                     if(isLocalConnectionSource) {
92                         deploymentFacility = DeploymentFacilityFactory.getLocalDeploymentFacility();
93                     } else {
94                         deploymentFacility = DeploymentFacilityFactory.getDeploymentFacility();
95                     }
96                     deploymentFacility.connect(
97                         targetList[0].getConnectionInfo());
98                     Set JavaDoc nameSet = deployedTargets.keySet();
99                     String JavaDoc[] targetNames = (String JavaDoc[])nameSet.toArray(
100                         new String JavaDoc[nameSet.size()]);
101                     Target[] targetList2 =
102                         deploymentFacility.createTargets(targetNames);
103                     if (targetList2 == null) {
104                         setupForAbnormalExit(localStrings.getString("enterprise.deployment.client.createTargetsFailed"), domain);
105                         return;
106                     }
107                     targetList = new SunTarget[targetList2.length];
108                     for (int ii = 0; ii < targetList2.length; ii++) {
109                         targetList[ii] = (SunTarget)targetList2[ii];
110                     }
111                 }
112
113                 // if all targets on which the app is deployed is not given,
114
// return error
115
else if (!DeploymentClientUtils.isTargetListComplete(deployedTargets, targetList)) {
116                     setupForAbnormalExit(
117                         localStrings.getString("enterprise.deployment.client.specifyAllTargets", moduleID, "undeploy"),
118                         domain);
119                     return;
120                 }
121
122                 // First stop all apps and remove all app references
123
RollBackAction rollback = new RollBackAction(RollBackAction.DELETE_APP_REF_OPERATION,
124                                                                 moduleID, deployOptions);
125                 deployOptions.put(DeploymentProperties.DEPLOY_OPTION_FORCE_KEY, "false");
126                 for(int i=0; i<targetList.length; i++) {
127
128                     // We dont rollback for stop failure because the failure may be because of server being down
129
// We just add DeploymentStatus of this phase to the complete DeploymentStatus
130

131                     DeploymentClientUtils.setResourceOptions(
132                         deployOptions,
133                         DeploymentProperties.RES_UNDEPLOYMENT,
134                         targetList[i].getName());
135                     DeploymentStatus stat = DeploymentClientUtils.stopApplication(
136                         dasConnection.getExistingMBeanServerConnection(),
137                         moduleID, targetList[i], deployOptions);
138                     checkStatusAndAddStage(targetList[i], null,
139                                 localStrings.getString("enterprise.deployment.client.undeploy_stop", targetList[i].getName()), dasConnection, stat);
140                     
141                     stat = DeploymentClientUtils.deleteApplicationReference(
142                         dasConnection.getExistingMBeanServerConnection(),
143                         moduleID, targetList[i], deployOptions);
144                     if(!checkStatusAndAddStage(targetList[i], rollback,
145                                 localStrings.getString("enterprise.deployment.client.undeploy_remove_ref", targetList[i].getName()), dasConnection, stat)) {
146                         return;
147                     }
148                     rollback.addTarget(targetList[i], RollBackAction.APP_REF_DELETED);
149                     resultTargetModuleIDs.add(new SunTargetModuleID(moduleID, targetList[i]));
150                 }
151             }
152             
153             // if undeploy from "domain" with no existing application-ref
154
if ((TargetType.DOMAIN.equals(targetList[0].getName()))) {
155                 DeploymentClientUtils.setResourceOptions(
156                     deployOptions,
157                     DeploymentProperties.RES_UNDEPLOYMENT,
158                     targetList);
159             } else {
160                 DeploymentClientUtils.setResourceOptions(
161                     deployOptions,
162                     DeploymentProperties.RES_NO_OP,
163                     targetList);
164             }
165  
166             // Call DeploymentMgr to start undeploy
167
fireProgressEvent(StateType.RUNNING, localStrings.getString("enterprise.deployment.client.undeploying"), domain);
168             Map JavaDoc undeployStatus = deplMgr.undeploy(moduleID, deployOptions);
169             
170             com.sun.appserv.management.deploy.DeploymentStatus finalStatusFromMBean =
171                             DeploymentSupport.mapToDeploymentStatus(undeployStatus);
172             DeploymentStatus tmp = DeploymentClientUtils.getDeploymentStatusFromAdminStatus(finalStatusFromMBean);
173             if(!checkStatusAndAddStage(domain, null, localStrings.getString("enterprise.deployment.client.undeploy_from_domain"), dasConnection, tmp)) {
174                 return;
175             }
176             
177             // undeploy over - add this to the result target module ID
178
resultTargetModuleIDs.add(new SunTargetModuleID(moduleID, domain));
179                 
180             // initialize the instance variable targetModuleIDs using
181
// the successful module ids
182
this.targetModuleIDs = new TargetModuleID[resultTargetModuleIDs.size()];
183             this.targetModuleIDs =
184                 (TargetModuleID[])resultTargetModuleIDs.toArray(this.targetModuleIDs);
185             
186             setupForNormalExit(localStrings.getString("enterprise.deployment.client.undeploy_application", moduleID), domain);
187         } catch (Throwable JavaDoc ioex) {
188             finalDeploymentStatus.setStageException(ioex);
189             setupForAbnormalExit(localStrings.getString("enterprise.deployment.client.undeploy_application_failed", ioex.getMessage()), domain);
190             return;
191         }
192     }
193     
194     private boolean isDefaultWebModule(SunTarget domain, ConnectionSource dasConnection, String JavaDoc moduleID) {
195         try {
196             DomainConfig cfg = ProxyFactory.getInstance(dasConnection).getDomainRoot().getDomainConfig();
197             Map JavaDoc cfgcfg = cfg.getConfigConfigMap();
198             for(Iterator JavaDoc it1 = cfgcfg.keySet().iterator(); it1.hasNext(); ) {
199                 ConfigConfig cf1 = (ConfigConfig) cfgcfg.get(it1.next());
200                 HTTPServiceConfig httpSvc = cf1.getHTTPServiceConfig();
201                 Map JavaDoc vsMap = httpSvc.getVirtualServerConfigMap();
202                 for(Iterator JavaDoc it2 = vsMap.keySet().iterator(); it2.hasNext(); ) {
203                     VirtualServerConfig vs = (VirtualServerConfig) vsMap.get(it2.next());
204                     if(moduleID.equals(vs.getDefaultWebModule())) {
205                         setupForAbnormalExit(
206                             localStrings.getString("enterprise.deployment.client.def_web_module_refs_exist", moduleID), domain);
207                         return true;
208                     }
209                 }
210             }
211         } catch (Throwable JavaDoc ioex) {
212             finalDeploymentStatus.setStageException(ioex);
213             setupForAbnormalExit(localStrings.getString("enterprise.deployment.client.undeploy_application_failed", ioex.getMessage()), domain);
214             return true;
215         }
216         return false;
217     }
218 }
219
Popular Tags