1 23 24 package com.sun.enterprise.deployapi; 25 26 import java.util.Collection ; 27 import java.util.Vector ; 28 import java.util.Iterator ; 29 import javax.enterprise.deploy.spi.status.ProgressListener ; 30 import javax.enterprise.deploy.spi.status.ProgressEvent ; 31 import javax.enterprise.deploy.spi.status.ProgressObject ; 32 import javax.enterprise.deploy.spi.status.DeploymentStatus ; 33 import javax.enterprise.deploy.spi.TargetModuleID ; 34 import javax.enterprise.deploy.shared.StateType ; 35 import javax.enterprise.deploy.spi.exceptions.OperationUnsupportedException ; 36 import com.sun.enterprise.util.LocalStringManagerImpl; 37 import com.sun.enterprise.deployment.client.JESProgressObject; 38 import com.sun.enterprise.deployment.client.DeploymentClientUtils; 39 40 56 public class ProgressObjectSink implements JESProgressObject, ProgressListener { 57 58 private static String LINE_SEPARATOR = System.getProperty("line.separator"); 59 private Vector registeredPL = new Vector (); 60 private Vector deliveredEvents = new Vector (); 61 62 63 private StateType finalStateType = StateType.COMPLETED; 64 private String finalMessage; 65 66 private Vector targetModuleIDs = new Vector (); 67 private Vector sources = new Vector (); 68 69 private static LocalStringManagerImpl localStrings = 70 new LocalStringManagerImpl(ProgressObjectSink.class); 71 72 private com.sun.enterprise.deployment.backend.DeploymentStatus completedStatus = new com.sun.enterprise.deployment.backend.DeploymentStatus(); 73 private boolean completedStatusReady = false; 74 75 78 public void sinkProgressObject(ProgressObject source) { 79 82 sources.add(source); 83 source.addProgressListener(this); 84 } 85 86 90 public void handleProgressEvent(ProgressEvent progressEvent) { 91 92 ProgressEvent forwardedEvent; 93 DeploymentStatus forwardedDS = progressEvent.getDeploymentStatus(); 94 95 if (!forwardedDS.isRunning()) { 97 if (forwardedDS.isFailed()) { 99 103 finalStateType = StateType.FAILED; 104 } 105 106 Object source = progressEvent.getSource(); 109 if (source instanceof ProgressObject ) { 110 ProgressObject po = (ProgressObject ) source; 111 po.removeProgressListener(this); 112 113 sources.remove(source); 114 115 if (forwardedDS.isCompleted()) { 116 117 TargetModuleID [] ids = po.getResultTargetModuleIDs(); 118 for (int i=0;i<ids.length;i++) { 119 targetModuleIDs.add(ids[i]); 120 } 121 } 122 } else { 123 throw new RuntimeException (localStrings.getLocalString( 124 "enterprise.deployment.client.noprogressobject", 125 "Progress event does not contain a ProgressObject source" 126 )); 127 } 128 129 133 updateCompletedStatus(forwardedDS); 134 135 DeploymentStatusImpl forwardedStatus = new DeploymentStatusImpl(); 140 forwardedStatus.setState(StateType.RUNNING); 141 forwardedStatus.setMessage(forwardedDS.getMessage()); 142 forwardedStatus.setCommand(forwardedDS.getCommand()); 143 forwardedEvent = new ProgressEvent (this, progressEvent.getTargetModuleID(), forwardedStatus); 144 } else { 145 forwardedEvent = new ProgressEvent (this, progressEvent.getTargetModuleID(), 147 forwardedDS); 148 } 149 150 Collection clone; 152 ProgressEvent finalEvent = null; 153 154 synchronized(registeredPL) { 155 clone = (Collection ) registeredPL.clone(); 156 deliveredEvents.add(forwardedEvent); 157 162 if (sources.isEmpty()) { 163 prepareCompletedStatus(); 164 DeploymentStatusImpl status = new DeploymentStatusImpl(); 165 status.setState(finalStateType); 166 if (finalStateType.equals(StateType.FAILED)) { 167 status.setMessage(localStrings.getLocalString( 168 "enterprise.deployment.client.aggregatefailure", 169 "At least one operation failed" 170 )); 171 } else { 172 status.setMessage(localStrings.getLocalString( 173 "enterprise.deployment.client.aggregatesuccess", 174 "All operations completed successfully" 175 )); 176 } 177 finalEvent = new ProgressEvent (this, null, status); 178 deliveredEvents.add(finalEvent); 179 } 180 } 181 182 for (Iterator itr=clone.iterator();itr.hasNext();) { 183 ProgressListener pl = (ProgressListener ) itr.next(); 184 pl.handleProgressEvent(forwardedEvent); 185 } 186 187 190 if (finalEvent != null) { 191 for (Iterator itr=clone.iterator();itr.hasNext();) { 192 ProgressListener pl = (ProgressListener ) itr.next(); 193 pl.handleProgressEvent(finalEvent); 194 } 195 } 196 } 197 198 199 203 public void addProgressListener(ProgressListener progressListener) { 204 205 Collection clone; 206 synchronized(registeredPL) { 207 registeredPL.add(progressListener); 208 209 clone = (Collection ) deliveredEvents.clone(); 211 } 212 213 for (Iterator itr=clone.iterator();itr.hasNext();) { 214 ProgressEvent pe = (ProgressEvent ) itr.next(); 215 progressListener.handleProgressEvent(pe); 216 } 217 } 218 219 223 public void removeProgressListener(ProgressListener progressListener) { 224 registeredPL.remove(progressListener); 225 } 226 227 228 public javax.enterprise.deploy.spi.status.ClientConfiguration getClientConfiguration(TargetModuleID targetModuleID) { 229 return null; 232 } 233 234 public DeploymentStatus getDeploymentStatus() { 235 DeploymentStatusImpl status = new DeploymentStatusImpl(); 236 if (sources.isEmpty()) { 237 status.setState(finalStateType); 238 status.setMessage(finalMessage); 239 } else { 240 status.setState(StateType.RUNNING); 241 } 242 return status; 243 } 244 245 public TargetModuleID [] getResultTargetModuleIDs() { 246 247 TargetModuleID [] ids = new TargetModuleID [targetModuleIDs.size()]; 248 targetModuleIDs.copyInto(ids); 249 return ids; 250 } 251 252 public boolean isCancelSupported() { 253 254 for (Iterator itr=getSources().iterator();itr.hasNext();) { 256 ProgressObject source = (ProgressObject ) itr.next(); 257 if (!source.isCancelSupported()) { 258 return false; 259 } 260 } 261 return true; 262 } 263 264 public boolean isStopSupported() { 265 266 for (Iterator itr=getSources().iterator();itr.hasNext();) { 268 ProgressObject source = (ProgressObject ) itr.next(); 269 if (!source.isStopSupported()) { 270 return false; 271 } 272 } 273 return true; 274 } 275 276 public void cancel() throws OperationUnsupportedException { 277 if (!isCancelSupported()) { 278 throw new OperationUnsupportedException ("cancel"); 279 } 280 for (Iterator itr=getSources().iterator();itr.hasNext();) { 281 ProgressObject source = (ProgressObject ) itr.next(); 282 source.cancel(); 283 } 284 } 285 286 public void stop() throws OperationUnsupportedException { 287 if (!isStopSupported()) { 288 throw new OperationUnsupportedException ("stop"); 289 } 290 for (Iterator itr=getSources().iterator();itr.hasNext();) { 291 ProgressObject source = (ProgressObject ) itr.next(); 292 source.stop(); 293 } 294 295 } 296 297 private Collection getSources() { 298 return (Collection ) sources.clone(); 299 } 300 301 private void prepareCompletedStatus() { 302 307 int worstStatus = com.sun.enterprise.deployment.backend.DeploymentStatus.NOTINITIALIZED; 308 StringBuffer msgs = new StringBuffer (); 309 310 int newWorstStatus = aggregateStages(worstStatus, msgs, completedStatus); 311 completedStatus.setStageStatus(newWorstStatus); 312 completedStatus.setStageStatusMessage(msgs.toString()); 313 314 completedStatusReady = true; 315 } 316 317 private int aggregateStages(int worstStatusSoFar, StringBuffer msgs, com.sun.enterprise.deployment.backend.DeploymentStatus stage) { 318 323 int stageStatus = stage.getStageStatus(); 324 if (stageStatus < worstStatusSoFar) { 325 worstStatusSoFar = stageStatus; 326 msgs.delete(0,msgs.length()); 327 } 328 329 333 if (stageStatus == worstStatusSoFar) { 334 msgs.append(stage.getStageStatusMessage()).append(LINE_SEPARATOR); 335 } 336 337 340 for (Iterator it = stage.getSubStages(); it.hasNext(); ) { 341 com.sun.enterprise.deployment.backend.DeploymentStatus substage = (com.sun.enterprise.deployment.backend.DeploymentStatus) it.next(); 342 worstStatusSoFar = aggregateStages(worstStatusSoFar, msgs, substage); 343 } 344 345 return worstStatusSoFar; 346 } 347 348 352 public com.sun.enterprise.deployment.backend.DeploymentStatus getCompletedStatus() { 353 com.sun.enterprise.deployment.backend.DeploymentStatus answer = null; 354 if (completedStatusReady) { 355 answer = completedStatus; 356 } 357 return answer; 358 } 359 360 private void updateCompletedStatus(DeploymentStatus ds) { 361 366 367 com.sun.enterprise.deployment.backend.DeploymentStatus newStageStatus = null; 368 if (ds instanceof DeploymentStatusImpl) { 369 DeploymentStatusImpl dsi = (DeploymentStatusImpl) ds; 370 newStageStatus = dsi.progressObject.getCompletedStatus(); 371 } else { 372 375 newStageStatus = new com.sun.enterprise.deployment.backend.DeploymentStatus(completedStatus); 376 379 String msgKey = null; 380 int stageStatus = -1; 381 Throwable exc; 382 383 reviseStatusAndMessage(ds, newStageStatus); 384 } 385 if (newStageStatus != null) { 386 390 completedStatus.addSubStage(newStageStatus); 391 396 } else { 397 System.err.println("A newStageStatus was null"); 398 } 399 } 400 401 private void reviseStatusAndMessage(DeploymentStatus ds, com.sun.enterprise.deployment.backend.DeploymentStatus newStageStatus) { 402 String msgKey = null; 403 int stageStatus = -1; 404 405 if (ds.isCompleted()) { 406 409 msgKey = "enterprise.deployment.client.action_completed"; 410 stageStatus = com.sun.enterprise.deployment.backend.DeploymentStatus.SUCCESS; 411 } else { 412 415 msgKey = "enterprise.deployment.client.action_failed"; 416 stageStatus = com.sun.enterprise.deployment.backend.DeploymentStatus.FAILURE; 417 } 418 419 String i18msg = localStrings.getLocalString(msgKey, ds.getMessage()); 420 newStageStatus.setStageStatus(stageStatus); 421 newStageStatus.setStageStatusMessage(i18msg); 422 } 423 } 424 | Popular Tags |