KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.*;
27
28 import com.sun.enterprise.deployapi.SunTarget;
29
30 import com.sun.enterprise.deployment.backend.DeploymentStatus;
31
32 import com.sun.enterprise.deployment.util.DeploymentProperties;
33
34 import com.sun.appserv.management.client.ConnectionSource;
35 import com.sun.appserv.management.client.ProxyFactory;
36 import com.sun.appserv.management.deploy.DeploymentMgr;
37
38 import com.sun.appserv.management.deploy.DeploymentSupport;
39
40 public class RollBackAction {
41
42     // Stores the moduleID and options required for undeploy
43
private String JavaDoc moduleID = null;
44     private Map options = null;
45     private int currentOperation = 0;
46     
47     // Keeps track of the targets on which operations are already over and state of each target
48
private Map targetState = null;
49     private Vector targetList = null;
50
51     // Used to indicate state of the app on a target
52
public static final int APP_REF_CREATED = 1;
53     public static final int APP_STARTED = 2;
54     public static final int APP_REF_DELETED = 3;
55     public static final int APP_STOPPED = 4;
56     
57     // Used to indicate what operation is currently being done
58
public static final int DEPLOY_OPERATION = 1;
59     public static final int UNDEPLOY_OPERATION = 2;
60     public static final int CREATE_APP_REF_OPERATION = 3;
61     public static final int DELETE_APP_REF_OPERATION = 4;
62     
63     public RollBackAction(int operation, String JavaDoc moduleID, Map options) {
64         this.currentOperation = operation;
65         this.moduleID = moduleID;
66         this.options = options;
67     }
68
69     // Called after successful completion of each start/create-app-ref on a target
70
public void addTarget(SunTarget target, int state) {
71         if(targetList == null) {
72             targetList = new Vector();
73             targetState= new HashMap();
74         }
75         targetState.put(target.getName(), new Integer JavaDoc(state));
76         targetList.add(target);
77         return;
78     }
79
80     // Called when the client wants to rollback current operation
81
public boolean rollback(ConnectionSource dasConnection, DeploymentStatus rollbackStatus) {
82         boolean retVal = false;
83         
84         switch(currentOperation) {
85             case DEPLOY_OPERATION :
86                 if(!stopModules(dasConnection, rollbackStatus)) {
87                     return false;
88                 }
89                 if(!deleteAppRefs(dasConnection, rollbackStatus)) {
90                     return false;
91                 }
92                 retVal = undeployModule(dasConnection, rollbackStatus);
93                 break;
94             
95             case CREATE_APP_REF_OPERATION :
96                 if(!stopModules(dasConnection, rollbackStatus)) {
97                     return false;
98                 }
99                 retVal = deleteAppRefs(dasConnection, rollbackStatus);
100                 break;
101                 
102             case UNDEPLOY_OPERATION :
103                 retVal = true; // failures during undeploy from domain will never be rolled back
104
break;
105                 
106             case DELETE_APP_REF_OPERATION :
107                 if(!createAppRefs(dasConnection, rollbackStatus)) {
108                     return false;
109                 }
110                 retVal = startModules(dasConnection, rollbackStatus);
111                 break;
112                 
113             default :
114                 break;
115         }
116         return retVal;
117     }
118     
119     private boolean undeployModule(ConnectionSource dasConnection, DeploymentStatus rollbackStatus) {
120         DeploymentMgr deplMgr = ProxyFactory.getInstance(dasConnection).getDomainRoot().getDeploymentMgr();
121         Map undeployStatus = deplMgr.undeploy(moduleID, options);
122         com.sun.appserv.management.deploy.DeploymentStatus finalStatusFromMBean =
123                             DeploymentSupport.mapToDeploymentStatus(undeployStatus);
124         DeploymentStatus tmp = DeploymentClientUtils.getDeploymentStatusFromAdminStatus(finalStatusFromMBean);
125         rollbackStatus.addSubStage(tmp);
126         if (tmp!=null && tmp.getStatus() < DeploymentStatus.WARNING) {
127             return false;
128         }
129         return true;
130     }
131     
132     private boolean deleteAppRefs(ConnectionSource dasConnection, DeploymentStatus rollbackStatus) {
133         if(targetList == null) {
134             return true;
135         }
136         if("true".equals(options.get(DeploymentProperties.DEPLOY_OPTION_FORCE_KEY))) {
137             options.put(DeploymentProperties.DEPLOY_OPTION_CASCADE_KEY, "false");
138         } else {
139             options.put(DeploymentProperties.DEPLOY_OPTION_CASCADE_KEY, "true");
140         }
141         try {
142             SunTarget[] targetObjs = (SunTarget[]) targetList.toArray(new SunTarget[targetList.size()]);
143             for(int i=0; i<targetObjs.length; i++) {
144                 int state = ((Integer JavaDoc)targetState.get(targetObjs[i].getName())).intValue();
145                 if(state == APP_REF_CREATED) {
146                     DeploymentStatus status =
147                         DeploymentClientUtils.deleteApplicationReference(
148                             dasConnection.getExistingMBeanServerConnection(),
149                             moduleID, targetObjs[i], options);
150                     rollbackStatus.addSubStage(status);
151                     if (status!=null && status.getStatus() < DeploymentStatus.WARNING) {
152                         return false;
153                     }
154                     targetState.remove(targetObjs[i].getName());
155                 }
156             }
157         } catch (Exception JavaDoc e) {
158             return false;
159         }
160         return true;
161     }
162     
163     private boolean stopModules(ConnectionSource dasConnection, DeploymentStatus rollbackStatus) {
164         if(targetList == null) {
165             return true;
166         }
167         
168         Map tmpOptions = new HashMap();
169         tmpOptions.putAll(options);
170         tmpOptions.put(DeploymentProperties.DEPLOY_OPTION_CASCADE_KEY, "true");
171         tmpOptions.put(DeploymentProperties.DEPLOY_OPTION_FORCE_KEY, "true");
172         try {
173             SunTarget[] targetObjs = (SunTarget[]) targetList.toArray(new SunTarget[targetList.size()]);
174             for(int i=0; i<targetObjs.length; i++) {
175                 int state = ((Integer JavaDoc)targetState.get(targetObjs[i].getName())).intValue();
176                 if(state == APP_STARTED) {
177                     DeploymentStatus status =
178                         DeploymentClientUtils.stopApplication(
179                             dasConnection.getExistingMBeanServerConnection(),
180                             moduleID, targetObjs[i], tmpOptions);
181                     rollbackStatus.addSubStage(status);
182                     if (status!=null && status.getStatus() < DeploymentStatus.WARNING) {
183                         return false;
184                     }
185                     targetState.put(targetObjs[i].getName(), new Integer JavaDoc(APP_REF_CREATED));
186                 }
187             }
188         } catch (Exception JavaDoc e) {
189             return false;
190         }
191         return true;
192     }
193     
194     private boolean createAppRefs(ConnectionSource dasConnection, DeploymentStatus rollbackStatus) {
195         if(targetList == null) {
196             return true;
197         }
198         try {
199             SunTarget[] targetObjs = (SunTarget[]) targetList.toArray(new SunTarget[targetList.size()]);
200             for(int i=0; i<targetObjs.length; i++) {
201                 int state = ((Integer JavaDoc)targetState.get(targetObjs[i].getName())).intValue();
202                 if(state == APP_REF_DELETED) {
203                     DeploymentStatus status =
204                         DeploymentClientUtils.createApplicationReference(
205                             dasConnection.getExistingMBeanServerConnection(),
206                             moduleID, targetObjs[i], options);
207                     rollbackStatus.addSubStage(status);
208                     if (status!=null && status.getStatus() < DeploymentStatus.WARNING) {
209                         return false;
210                     }
211                     targetState.put(targetObjs[i].getName(), new Integer JavaDoc(APP_STOPPED));
212                 }
213             }
214         } catch (Exception JavaDoc e) {
215             return false;
216         }
217         return true;
218     }
219     
220     private boolean startModules(ConnectionSource dasConnection, DeploymentStatus rollbackStatus) {
221         if(targetList == null) {
222             return true;
223         }
224         try {
225             SunTarget[] targetObjs = (SunTarget[]) targetList.toArray(new SunTarget[targetList.size()]);
226             for(int i=0; i<targetObjs.length; i++) {
227                 int state = ((Integer JavaDoc)targetState.get(targetObjs[i].getName())).intValue();
228                 if(state == APP_STOPPED) {
229                     DeploymentStatus status =
230                         DeploymentClientUtils.startApplication(
231                             dasConnection.getExistingMBeanServerConnection(),
232                             moduleID, targetObjs[i], options);
233                     rollbackStatus.addSubStage(status);
234                     // We don't check the start return because we can't do anything if the instance is down
235
targetState.remove(targetObjs[i].getName());
236                 }
237             }
238         } catch (Exception JavaDoc e) {
239             return false;
240         }
241         return true;
242     }
243 }
244
Popular Tags