1 23 24 package com.sun.enterprise.deployapi; 25 26 import com.sun.appserv.management.client.ConnectionSource; 27 import com.sun.enterprise.admin.util.HostAndPort; 28 import com.sun.enterprise.deployapi.SunDeploymentManager; 29 import com.sun.enterprise.deployapi.SunTarget; 30 import com.sun.enterprise.deployapi.SunTargetModuleID; 31 import com.sun.enterprise.deployment.client.DeploymentClientUtils; 32 import com.sun.enterprise.deployment.client.JESProgressObject; 33 import com.sun.enterprise.deployment.client.RollBackAction; 34 import com.sun.enterprise.util.i18n.StringManager; 35 import com.sun.enterprise.util.io.FileUtils; 36 import com.sun.enterprise.util.Print; 37 38 import java.io.ByteArrayOutputStream ; 39 import java.io.File ; 40 import java.io.PrintStream ; 41 import java.io.PrintWriter ; 42 import java.net.URL ; 43 import java.rmi.RemoteException ; 44 import java.util.Iterator ; 45 import java.util.Vector ; 46 47 import javax.enterprise.deploy.shared.CommandType ; 48 import javax.enterprise.deploy.shared.ModuleType ; 49 import javax.enterprise.deploy.shared.StateType ; 50 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException ; 51 import javax.enterprise.deploy.spi.status.ClientConfiguration ; 52 import javax.enterprise.deploy.spi.status.DeploymentStatus ; 53 import javax.enterprise.deploy.spi.status.ProgressEvent ; 54 import javax.enterprise.deploy.spi.status.ProgressListener ; 55 import javax.enterprise.deploy.spi.TargetModuleID ; 56 import javax.management.MBeanServerConnection ; 57 import javax.management.ObjectName ; 58 59 63 public abstract class ProgressObjectImpl implements Runnable , JESProgressObject { 64 65 protected CommandType commandType; 66 protected Object [] args; 67 private Vector listeners = new Vector (); protected SunTarget target; 69 protected SunTarget[] targetsList; 70 protected String moduleID; 71 protected ModuleType moduleType; 72 protected DeploymentStatusImpl deploymentStatus =null; 73 protected TargetModuleID[] targetModuleIDs = null; 74 protected Vector deliveredEvents = new Vector (); 75 protected com.sun.enterprise.deployment.backend.DeploymentStatus finalDeploymentStatus = null; 76 protected boolean deployActionCompleted; 77 protected String warningMessages; 78 private static StringManager localStrings = StringManager.getManager(ProgressObjectImpl.class); 79 80 private final static String MODULE_ID = 81 com.sun.enterprise.deployment.backend.DeploymentStatus.MODULE_ID; 82 private final static String MODULE_TYPE = 83 com.sun.enterprise.deployment.backend.DeploymentStatus.MODULE_TYPE; 84 private final static String KEY_SEPARATOR = 85 com.sun.enterprise.deployment.backend.DeploymentStatus.KEY_SEPARATOR; 86 private final static String SUBMODULE_COUNT = 87 com.sun.enterprise.deployment.backend.DeploymentStatus.SUBMODULE_COUNT; 88 private final static String CONTEXT_ROOT = 89 com.sun.enterprise.deployment.backend.DeploymentStatus.CONTEXT_ROOT; 90 private final static String WARNING_PREFIX = "WARNING: "; 91 92 93 public ProgressObjectImpl(SunTarget target) { 94 this.target = target; 95 deploymentStatus = new DeploymentStatusImpl(this); 96 deploymentStatus.setState(StateType.RELEASED); 97 finalDeploymentStatus = new com.sun.enterprise.deployment.backend.DeploymentStatus(); 98 deployActionCompleted = false; 99 } 100 101 public ProgressObjectImpl(SunTarget[] targets) { 102 this.targetsList = targets; 103 deploymentStatus = new DeploymentStatusImpl(this); 104 deploymentStatus.setState(StateType.RELEASED); 105 finalDeploymentStatus = new com.sun.enterprise.deployment.backend.DeploymentStatus(); 106 deployActionCompleted = false; 107 } 108 109 115 public void addProgressListener(ProgressListener pol) { 116 synchronized (listeners) { 117 listeners.add(pol); 118 if (deliveredEvents.size() > 0) { 119 Print.dprintln("Delivering undelivered messages..."); 120 for (Iterator i = deliveredEvents.iterator(); i.hasNext();) { 121 pol.handleProgressEvent((ProgressEvent )i.next()); 122 } 123 } 124 } 125 } 126 127 137 public void cancel() throws OperationUnsupportedException { 138 throw new OperationUnsupportedException ("cancel not supported"); 139 } 140 141 147 public ClientConfiguration getClientConfiguration(TargetModuleID id) { 148 return null; 149 } 150 151 156 public DeploymentStatus getDeploymentStatus() { 157 DeploymentStatusImpl result = new DeploymentStatusImpl(this); 158 result.setState(deploymentStatus.getState()); 159 result.setMessage(deploymentStatus.getMessage()); 160 161 return result; 162 } 163 164 167 public com.sun.enterprise.deployment.backend.DeploymentStatus getCompletedStatus() { 168 if(deployActionCompleted) { 169 return finalDeploymentStatus; 170 } 171 return null; 172 } 173 174 180 public TargetModuleID[] getResultTargetModuleIDs() { 181 182 185 if (targetModuleIDs==null) { 186 if(target != null) { 187 initializeTargetModuleIDs(moduleID); 188 } else if(targetsList != null) { 189 initializeTargetModuleIDForAllServers(null, null); 190 } 191 } 192 return targetModuleIDs; 194 } 195 196 200 protected void initializeTargetModuleIDs(String moduleID) { 201 SunTargetModuleID parentTargetModuleID = new SunTargetModuleID(moduleID, target); 202 parentTargetModuleID.setModuleType(getModuleType()); 203 204 targetModuleIDs = new SunTargetModuleID[1]; 205 targetModuleIDs[0] = parentTargetModuleID; 206 } 207 208 212 protected void initializeTargetModuleIDForAllServers( 213 com.sun.enterprise.deployment.backend.DeploymentStatus status, 214 MBeanServerConnection mbsc) { 215 216 if(targetsList == null) { 217 return; 218 } 219 220 targetModuleIDs = new SunTargetModuleID[targetsList.length]; 221 String moduleID = status == null 222 ? this.moduleID : status.getProperty(MODULE_ID); 223 String key = moduleID + KEY_SEPARATOR + MODULE_TYPE; 224 ModuleType type = status == null 225 ? getModuleType() 226 : ModuleType.getModuleType((new Integer (status.getProperty(key))).intValue()); 227 228 for(int i=0; i<targetsList.length; i++) { 229 SunTargetModuleID parentTargetModuleID = new SunTargetModuleID(moduleID, targetsList[i]); 230 parentTargetModuleID.setModuleType(type); 231 targetModuleIDs[i] = parentTargetModuleID; 232 233 if (status != null) { 234 HostAndPort webHost=null; 236 try { 237 Object [] params = new Object []{ moduleID, Boolean.FALSE }; 238 String [] signature = new String []{ "java.lang.String", "boolean"}; 239 ObjectName applicationsMBean = new ObjectName (APPS_CONFIGMBEAN_OBJNAME); 240 webHost = (HostAndPort) mbsc.invoke(applicationsMBean, "getHostAndPort", params, signature); 241 } catch(Exception e) { 242 Print.dprintStackTrace(e.getLocalizedMessage(), e); 243 } 244 245 key = moduleID + KEY_SEPARATOR + SUBMODULE_COUNT; 246 if (status.getProperty(key) == null) { if (ModuleType.WAR.equals(type)) { 248 key = moduleID + KEY_SEPARATOR + CONTEXT_ROOT; 249 String contextRoot = status.getProperty(key); 250 initTargetModuleIDWebURL(parentTargetModuleID, webHost, contextRoot); 251 } 252 } else { 253 int counter = (Integer.valueOf(status.getProperty(key))).intValue(); 254 for (int j = 0; j < counter; j++) { 256 key = moduleID + KEY_SEPARATOR + MODULE_ID + KEY_SEPARATOR + String.valueOf(j); 258 String subModuleID = status.getProperty(key); 259 SunTargetModuleID subModule = new SunTargetModuleID(subModuleID, targetsList[i]); 260 261 key = subModuleID + KEY_SEPARATOR + MODULE_TYPE; 263 type = ModuleType.getModuleType((new Integer (status.getProperty(key))).intValue()); 264 subModule.setModuleType(type); 265 if (ModuleType.WAR.equals(type) && webHost!=null) { 266 key = subModuleID + KEY_SEPARATOR + CONTEXT_ROOT; 267 String contextRoot = status.getProperty(key); 268 initTargetModuleIDWebURL(subModule, webHost, contextRoot); 269 } 270 parentTargetModuleID.addChildTargetModuleID(subModule); 271 } 272 } 273 } 274 } 275 } 276 277 281 private void initTargetModuleIDWebURL( 282 SunTargetModuleID tm, HostAndPort webHost, String contextRoot) { 283 284 if (webHost==null) 285 return; 286 287 try { 288 String host; 291 SunDeploymentManager sdm = new SunDeploymentManager(tm.getConnectionInfo()); 292 if(sdm.isPE()) { 293 host = tm.getConnectionInfo().getHostName(); 294 } else { 295 host = webHost.getHost(); 296 } 297 298 URL webURL = new URL ("http", host, webHost.getPort(), contextRoot); 299 tm.setWebURL(webURL.toExternalForm()); 300 } catch(Exception e) { 301 Print.dprintStackTrace(e.getLocalizedMessage(),e); 302 } 303 } 304 305 311 public boolean isCancelSupported() { 312 return false; 313 } 314 315 321 public boolean isStopSupported() { 322 return false; 323 } 324 325 330 public void removeProgressListener(ProgressListener pol) { 331 synchronized (listeners) { 332 listeners.remove(pol); 333 } 334 } 335 336 346 public void stop() throws OperationUnsupportedException { 347 throw new OperationUnsupportedException ("stop not supported"); 348 } 349 350 351 public void setCommand(CommandType commandType, Object [] args) { 352 this.commandType = commandType; 353 this.args = args; 354 } 355 356 357 360 public abstract void run(); 361 362 363 366 protected void fireProgressEvent(ProgressEvent progressEvent) { 367 375 Vector currentListeners = null; 376 synchronized (listeners) { 377 currentListeners = (Vector ) listeners.clone(); 378 388 389 deliveredEvents.add(progressEvent); 390 } 391 392 for (Iterator listenersItr = currentListeners.iterator(); listenersItr.hasNext();) { 393 ((ProgressListener )listenersItr.next()).handleProgressEvent(progressEvent); 394 } 395 currentListeners = null; 396 } 397 398 399 402 protected void fireProgressEvent(StateType state, String message) { 403 fireProgressEvent(state, message, target); 404 } 405 406 409 protected void fireProgressEvent(StateType state, String message, SunTarget aTarget) { 410 411 StateType stateToBroadcast = (state != null) ? state : deploymentStatus.getState(); 412 413 414 DeploymentStatusImpl depStatus = new DeploymentStatusImpl(this); 415 depStatus.setState(stateToBroadcast); 416 depStatus.setMessage(message); 417 418 421 if (state != null) { 422 deploymentStatus.setMessage(message); 423 deploymentStatus.setState(state); } 425 426 427 SunTargetModuleID tmi = new SunTargetModuleID(moduleID, aTarget); 428 tmi.setModuleType(getModuleType()); 429 fireProgressEvent(new ProgressEvent (this, tmi, depStatus)); 430 } 431 432 CommandType getCommandType() { 433 return commandType; 434 } 435 436 440 public void setModuleType(ModuleType moduleType) { 441 this.moduleType = moduleType; 442 } 443 444 447 public ModuleType getModuleType() { 448 return moduleType; 449 } 450 451 457 protected String getThrowableString(Throwable t) { 458 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 459 PrintStream ps = new PrintStream (bos); 460 t.printStackTrace(ps); 461 ps.close(); return bos.toString(); 463 } 464 465 protected static final String APPS_CONFIGMBEAN_OBJNAME = 466 "com.sun.appserv:type=applications,category=config"; 467 468 protected String getDeploymentStatusMessage(com.sun.enterprise.deployment.backend.DeploymentStatus status) { 469 return getDeploymentStatusMessage(status, false); 470 } 471 474 protected String getDeploymentStatusMessage(com.sun.enterprise.deployment.backend.DeploymentStatus status, boolean isStartPhase) { 475 if(status == null) { 476 return null; 477 } 478 if (status!=null && status.getStatus() >= com.sun.enterprise.deployment.backend.DeploymentStatus.SUCCESS) { 480 return null; 481 } 482 ByteArrayOutputStream bos = new ByteArrayOutputStream (); 483 PrintWriter pw = new PrintWriter (bos); 484 com.sun.enterprise.deployment.backend.DeploymentStatus.parseDeploymentStatus(status, pw); 485 byte[] statusBytes = bos.toByteArray(); 486 String statusString = new String (statusBytes); 487 if(status.getStatus() == com.sun.enterprise.deployment.backend.DeploymentStatus.WARNING) { 489 if(warningMessages==null) { 490 warningMessages = WARNING_PREFIX + statusString; 491 } else { 492 warningMessages += statusString; 493 } 494 495 if (isStartPhase) { 497 warningMessages = localStrings.getString("enterprise.deployment.client.start_failed_msg") + warningMessages; 498 } 499 500 return null; 501 } 502 return statusString; 504 } 505 506 protected void setupForNormalExit(String message, SunTarget aTarget) { 507 String i18nmsg; 508 if(warningMessages == null) { 510 i18nmsg = localStrings.getString("enterprise.deployment.client.action_completed", message); 511 finalDeploymentStatus.setStageStatus(com.sun.enterprise.deployment.backend.DeploymentStatus.SUCCESS); 512 } else { 513 i18nmsg = localStrings.getString("enterprise.deployment.client.action_completed_with_warning", warningMessages); 514 finalDeploymentStatus.setStageStatus(com.sun.enterprise.deployment.backend.DeploymentStatus.WARNING); 515 } 516 finalDeploymentStatus.setStageStatusMessage(i18nmsg); 517 deployActionCompleted = true; 518 fireProgressEvent(StateType.COMPLETED, i18nmsg, aTarget); 519 return; 520 } 521 522 protected void setupForAbnormalExit(String errorMsg, SunTarget aTarget) { 523 String i18nmsg = localStrings.getString("enterprise.deployment.client.action_failed", errorMsg); 524 finalDeploymentStatus.setStageStatus(com.sun.enterprise.deployment.backend.DeploymentStatus.FAILURE); 525 finalDeploymentStatus.setStageStatusMessage(i18nmsg); 526 deployActionCompleted = true; 527 fireProgressEvent(StateType.FAILED, i18nmsg, aTarget); 528 return; 529 } 530 531 protected boolean checkStatusAndAddStage(SunTarget aTarget, RollBackAction rollback, String action, ConnectionSource dasConnection, com.sun.enterprise.deployment.backend.DeploymentStatus currentStatus) { 532 return checkStatusAndAddStage(aTarget, rollback, action, dasConnection, 533 currentStatus, false); 534 } 535 536 541 protected boolean checkStatusAndAddStage(SunTarget aTarget, RollBackAction rollback, String action, ConnectionSource dasConnection, com.sun.enterprise.deployment.backend.DeploymentStatus currentStatus, boolean isStartPhase) { 542 String statusMsg = getDeploymentStatusMessage(currentStatus, 543 isStartPhase); 544 finalDeploymentStatus.addSubStage(currentStatus); 545 if(statusMsg == null) { 546 fireProgressEvent(StateType.RUNNING, 547 localStrings.getString("enterprise.deployment.client.action_completed", action), 548 aTarget); 549 return true; 550 } 551 if(rollback != null) { 552 com.sun.enterprise.deployment.backend.DeploymentStatus tmp = new 553 com.sun.enterprise.deployment.backend.DeploymentStatus(); 554 if(!rollback.rollback(dasConnection, tmp)) { 555 fireProgressEvent(StateType.RUNNING, 556 localStrings.getString("enterprise.deployment.client.action_failed", "Rollback failed"), 557 aTarget); 558 tmp.setStageStatus(com.sun.enterprise.deployment.backend.DeploymentStatus.FAILURE); 559 tmp.setStageStatusMessage(localStrings.getString("enterprise.deployment.client.action_failed", "Rollback failed")); 560 } else { 561 fireProgressEvent(StateType.RUNNING, 562 localStrings.getString("enterprise.deployment.client.action_completed", "Rollback"), 563 aTarget); 564 tmp.setStageStatus(com.sun.enterprise.deployment.backend.DeploymentStatus.SUCCESS); 565 tmp.setStageStatusMessage(localStrings.getString("enterprise.deployment.client.action_completed", "Rollback")); 566 } 567 finalDeploymentStatus.addSubStage(tmp); 568 } 569 setupForAbnormalExit(localStrings.getString("enterprise.deployment.client.action_failed_with_message", action, statusMsg), aTarget); 570 return false; 571 } 572 } 573 | Popular Tags |