1 23 package com.sun.enterprise.management.deploy; 24 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.Set ; 28 import java.util.Iterator ; 29 import java.util.Collections ; 30 31 import java.io.File ; 32 import java.io.FileOutputStream ; 33 import java.io.IOException ; 34 import java.io.Serializable ; 35 36 import javax.management.ObjectName ; 37 import javax.management.Notification ; 38 39 40 import com.sun.appserv.management.base.Utility; 41 import com.sun.appserv.management.base.Singleton; 42 import com.sun.appserv.management.base.XTypes; 43 import com.sun.appserv.management.deploy.DeploymentMgr; 44 45 import com.sun.enterprise.management.support.UniqueIDGenerator; 46 47 import com.sun.appserv.management.deploy.DeploymentSupport; 48 import com.sun.appserv.management.deploy.DeploymentSource; 49 import com.sun.appserv.management.deploy.DeploymentStatus; 50 import com.sun.appserv.management.deploy.DeploymentProgress; 51 import com.sun.appserv.management.base.UploadDownloadMgr; 52 53 import com.sun.appserv.management.util.misc.GSetUtil; 54 import com.sun.appserv.management.util.jmx.NotificationBuilder; 55 56 import com.sun.enterprise.management.support.AMXNonConfigImplBase; 57 58 59 68 public final class DeploymentMgrImpl extends AMXNonConfigImplBase 69 implements Utility, Singleton, DeploymentMgr 70 { 71 74 private final Map <Object ,DeployThread> mDeployThreads; 75 76 public void 77 remove( final String name ) 78 { 79 throw new RuntimeException ( "not applicable" ); 80 } 81 82 private final UniqueIDGenerator mDeployIDs; 83 84 private long mDeploymentCompletedNotificationSequenceNumber; 85 86 public 87 DeploymentMgrImpl( ) 88 { 89 mDeployThreads = Collections.synchronizedMap( new HashMap <Object ,DeployThread>() ); 90 mDeployIDs = new UniqueIDGenerator( "deploy:" ); 91 92 mDeploymentCompletedNotificationSequenceNumber = 0; 93 } 94 95 private final Set <String > NOTIF_TYPES = GSetUtil.newUnmodifiableStringSet( 96 DEPLOYMENT_STARTED_NOTIFICATION_TYPE, 97 DEPLOYMENT_ABORTED_NOTIFICATION_TYPE, 98 DEPLOYMENT_PROGRESS_NOTIFICATION_TYPE, 99 DEPLOYMENT_COMPLETED_NOTIFICATION_TYPE ); 100 101 protected Set <String > 102 getNotificationTypes( Set <String > existing ) 103 { 104 existing.addAll( NOTIF_TYPES ); 105 return existing; 106 } 107 108 109 private static final long SECOND_MILLIS = 60 * 1000; 110 private static final long MINUTE_MILLIS = 60 * SECOND_MILLIS; 111 private static final long DEPLOY_KEEP_ALIVE_MILLIS = 15 * MINUTE_MILLIS; 112 113 114 private DeployThread 115 removeDeployThread( final Object deployID) 116 { 117 trace( "\n###Removing deploy thread: " + deployID ); 118 return( (DeployThread)mDeployThreads.remove( deployID ) ); 119 } 120 121 127 private final void 128 staleDeployCheck() 129 { 130 final Set <Object > keySet = mDeployThreads.keySet(); 131 132 synchronized( mDeployThreads ) 133 { 134 final String [] deployIDs = GSetUtil.toStringArray( keySet ); 135 136 for( final String deployID : deployIDs ) 137 { 138 final DeployThread deployThread = (DeployThread)mDeployThreads.get( deployID ); 139 140 if ( deployThread.getMillisSinceDone() > DEPLOY_KEEP_ALIVE_MILLIS ) 141 { 142 removeDeployThread( deployID ); 143 } 144 } 145 } 146 } 147 148 private DeployThread 149 addDeployThread( 150 final Object deployID ) 151 { 152 final DeploymentCallback callback = 153 new InternalDeploymentCallback( deployID ); 154 155 final DeployThread deployThread = 156 new DeployThread( deployID, callback, null ); 157 mDeployThreads.put( deployThread.getID(), deployThread ); 158 159 return( deployThread ); 160 } 161 162 public Object 163 initDeploy() 164 { 165 final Object deployID = mDeployIDs.createID(); 166 167 final DeployThread deployThread = addDeployThread( deployID ); 168 169 return( deployID ); 170 } 171 172 public void 173 startDeploy( 174 final Object deployID, 175 final Object uploadID, 176 final Object planUploadID, 177 final Map <String ,String > options) 178 { 179 staleDeployCheck(); 180 181 final UploadDownloadMgr mgr = getUploadDownloadMgr(); 182 183 final File deployFile = mgr.takeUpload( uploadID ); 184 final File planFile = planUploadID == null ? null : mgr.takeUpload( planUploadID ); 185 186 final DeployThreadParams params = 187 new DeployThreadParams( 188 getQueryMgr(), 189 options, 190 deployFile, 191 planFile ); 192 193 startDeploy( deployID, params ); 194 } 195 196 public <T1 extends Serializable ,T2 extends Serializable > void 197 startDeploy( 198 Object deployID, 199 Map <String ,T1> sourceData, 200 Map <String ,T2> planData, 201 Map <String ,String > options) 202 { 203 final DeploymentSource source = 204 DeploymentSupport.mapToDeploymentSource( sourceData ); 205 206 final DeploymentSource plan = planData == null ? 207 null : DeploymentSupport.mapToDeploymentSource( planData ); 208 209 final DeployThreadParams params = 210 new DeployThreadParams( getQueryMgr(), options, source, plan ); 211 212 startDeploy( deployID, params ); 213 } 214 215 private void 216 startDeploy( 217 final Object deployID, 218 final DeployThreadParams params ) 219 { 220 final DeployThread deployThread = getDeployThread( deployID ); 221 if ( deployThread == null ) 222 { 223 throw new IllegalArgumentException ( deployID.toString() ); 224 } 225 deployThread.setParams( params ); 226 227 232 issueDeploymentStartedNotification( deployID ); 233 234 deployThread.start(); 235 } 236 237 private DeployThread 238 getDeployThread( Object deployID ) 239 { 240 final DeployThread deployThread = (DeployThread)mDeployThreads.get( deployID ); 241 if ( deployThread == null ) 242 { 243 final IllegalArgumentException e = new IllegalArgumentException ( "" + deployID ); 244 245 e.printStackTrace(); 246 throw e; 247 } 248 return( deployThread ); 249 } 250 251 private boolean 252 notifsAreDone( final Notification [] notifs ) 253 { 254 boolean done = false; 255 256 for( int i = notifs.length -1; i >= 0; --i ) 257 { 258 final String notifType = notifs[ notifs.length - 1].getType(); 259 260 if ( notifType.equals( DEPLOYMENT_COMPLETED_NOTIFICATION_TYPE ) || 261 notifType.equals( DEPLOYMENT_ABORTED_NOTIFICATION_TYPE ) ) 262 { 263 done = true; 264 break; 265 } 266 } 267 268 return( done ); 269 } 270 271 public Notification [] 272 takeNotifications( final Object deployID) 273 { 274 final DeployThread deployThread = getDeployThread( deployID ); 275 276 final Notification [] notifs = deployThread.takeNotifications(); 277 278 return( notifs ); 279 } 280 281 public boolean 282 abortDeploy(final Object deployID) 283 { 284 final DeployThread deployThread = getDeployThread( deployID ); 285 286 final boolean abortedSuccessfully = deployThread.quit(); 287 288 issueDeploymentAbortedNotification( deployID ); 289 290 return( abortedSuccessfully ); 291 } 292 293 294 private void 295 issueNotification( 296 final Object deployID, 297 final Notification notif ) 298 { 299 sendNotification( notif ); 301 302 trace( "\nDeploymentMgrImpl.issueNotification: sent notification for " + 303 deployID + " = " + notif.toString() ); 304 305 final DeployThread deployThread = getDeployThread( deployID ); 307 deployThread.queueNotification( notif ); 308 } 309 310 312 protected void 313 issueDeploymentStartedNotification( final Object deployID ) 314 { 315 final NotificationBuilder builder = 316 getNotificationBuilder( DEPLOYMENT_STARTED_NOTIFICATION_TYPE ); 317 318 final Notification notif = builder.buildNew( ); 319 builder.putMapData( notif, NOTIF_DEPLOYMENT_ID_KEY, (Serializable )deployID ); 320 321 issueNotification( deployID, notif ); 322 } 323 324 326 protected void 327 issueDeploymentDoneNotification( 328 final Object deployID, 329 final DeploymentStatus deploymentStatus ) 330 { 331 final NotificationBuilder builder = 332 getNotificationBuilder( DEPLOYMENT_COMPLETED_NOTIFICATION_TYPE ); 333 334 final Notification notif = builder.buildNew( ); 335 builder.putMapData( notif, NOTIF_DEPLOYMENT_ID_KEY, (Serializable )deployID ); 336 builder.putMapData( notif, NOTIF_DEPLOYMENT_COMPLETED_STATUS_KEY, (Serializable )deploymentStatus.asMap() ); 337 338 issueNotification( deployID, notif ); 339 } 340 341 343 protected void 344 issueDeploymentProgressNotification( 345 final Object deployID, 346 final DeploymentProgress progress) 347 { 348 final NotificationBuilder builder = 349 getNotificationBuilder( DEPLOYMENT_PROGRESS_NOTIFICATION_TYPE ); 350 351 final Notification notif = builder.buildNew( ); 352 builder.putMapData( notif, NOTIF_DEPLOYMENT_ID_KEY, (Serializable )deployID ); 353 builder.putMapData( notif, NOTIF_DEPLOYMENT_PROGRESS_KEY, (Serializable )progress.asMap() ); 354 355 issueNotification( deployID, notif ); 356 } 357 358 360 protected void 361 issueDeploymentAbortedNotification( final Object deployID ) 362 { 363 final NotificationBuilder builder = 364 getNotificationBuilder( DEPLOYMENT_ABORTED_NOTIFICATION_TYPE ); 365 366 final Notification notif = builder.buildNew( ); 367 builder.putMapData( notif, NOTIF_DEPLOYMENT_ID_KEY, (Serializable )deployID ); 368 369 issueNotification( deployID, notif ); 370 } 371 372 private final class InternalDeploymentCallback implements DeploymentCallback 373 { 374 final Object mDeployID; 375 376 public 377 InternalDeploymentCallback( Object deployID ) 378 { 379 mDeployID = deployID; 380 } 381 382 public void 383 deploymentDone( 384 final DeploymentStatus status ) 385 { 386 issueDeploymentDoneNotification( mDeployID, status ); 387 } 388 389 public void 390 deploymentProgress( 391 final DeploymentProgress progress ) 392 { 393 issueDeploymentProgressNotification( mDeployID, progress ); 394 } 395 } 396 397 public Map <String ,Serializable > 398 undeploy( 399 final String moduleID, 400 final Map <String ,String > optionalParams) 401 { 402 final Undeployer undeployer = new Undeployer( moduleID, optionalParams ); 403 final DeploymentStatus undeploymentStatus = undeployer.undeploy(); 404 405 return( undeploymentStatus.asMap() ); 406 } 407 408 public Map <String ,Serializable > 409 getFinalDeploymentStatus (Object deployID) 410 { 411 final DeployThread deployThread = removeDeployThread( deployID ); 412 413 if ( deployThread == null ) 414 { 415 throw new IllegalArgumentException ( deployID.toString() ); 416 } 417 418 return( deployThread.getDeploymentStatus().asMap() ); 419 } 420 421 private UploadDownloadMgr 422 getUploadDownloadMgr() 423 { 424 return( getDomainRoot().getUploadDownloadMgr() ); 425 } 426 427 428 public Object 429 initiateFileUpload( long totalSize ) 430 throws IOException 431 { 432 return initiateFileUpload( null, totalSize ); 433 } 434 435 public Object 436 initiateFileUpload( final String name, final long totalSize ) 437 throws IOException 438 { 439 debug( "initiateFileUpload(" + name + ", " + totalSize + ")" ); 440 return( getUploadDownloadMgr().initiateUpload( name, totalSize ) ); 441 } 442 443 public boolean 444 uploadBytes( 445 final Object uploadID, 446 final byte[] bytes) 447 throws IOException 448 { 449 return( getUploadDownloadMgr().uploadBytes( uploadID, bytes ) ); 450 } 451 452 453 public Object 454 initiateFileDownload( 455 final String moduleID, 456 final String filename) 457 throws IOException 458 { 459 final DownloadFileSource source = new DownloadFileSource( moduleID, filename ); 460 final File theFile = source.getDownloadFile( ); 461 final boolean deleteWhenDone = source.isTempFile(); 462 463 return( getUploadDownloadMgr().initiateDownload( theFile, deleteWhenDone ) ); 464 } 465 466 467 472 public long 473 getDownloadLength( final Object downloadID ) 474 { 475 return( getUploadDownloadMgr().getDownloadLength( downloadID ) ); 476 } 477 478 479 public byte[] 480 downloadBytes( 481 final Object downloadID, 482 final int requestSize ) 483 throws IOException 484 { 485 return( getUploadDownloadMgr().downloadBytes( downloadID, requestSize ) ); 486 } 487 488 } 489 490 491 492 493 494 495 496 | Popular Tags |