1 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 private String moduleID = null; 44 private Map options = null; 45 private int currentOperation = 0; 46 47 private Map targetState = null; 49 private Vector targetList = null; 50 51 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 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 moduleID, Map options) { 64 this.currentOperation = operation; 65 this.moduleID = moduleID; 66 this.options = options; 67 } 68 69 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 (state)); 76 targetList.add(target); 77 return; 78 } 79 80 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; 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 )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 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 )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 (APP_REF_CREATED)); 186 } 187 } 188 } catch (Exception 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 )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 (APP_STOPPED)); 212 } 213 } 214 } catch (Exception 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 )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 targetState.remove(targetObjs[i].getName()); 236 } 237 } 238 } catch (Exception e) { 239 return false; 240 } 241 return true; 242 } 243 } 244 | Popular Tags |