1 23 package com.sun.enterprise.management.deploy; 24 25 import java.util.Set ; 26 import java.util.Map ; 27 import java.util.List ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.HashSet ; 31 import java.util.Collections ; 32 33 import java.io.IOException ; 34 import java.io.File ; 35 import java.io.InputStream ; 36 import java.io.FileInputStream ; 37 import java.io.FileOutputStream ; 38 import java.io.Serializable ; 39 40 import javax.management.ObjectName ; 41 import javax.management.AttributeList ; 42 import javax.management.Notification ; 43 import javax.management.NotificationListener ; 44 import javax.management.MBeanServerConnection ; 45 import javax.management.NotCompliantMBeanException ; 46 import javax.management.ListenerNotFoundException ; 47 48 import com.sun.appserv.management.deploy.DeploymentMgr; 49 import com.sun.appserv.management.deploy.DeploymentMgr; 50 import com.sun.appserv.management.deploy.DeploymentProgress; 51 import com.sun.appserv.management.deploy.DeploymentStatus; 52 import com.sun.appserv.management.deploy.DeploymentSupport; 53 54 import com.sun.appserv.management.base.XTypes; 55 import com.sun.appserv.management.config.StandaloneServerConfig; 56 import com.sun.appserv.management.config.DeployedItemRefConfig; 57 import com.sun.appserv.management.config.DeployedItemRefConfigCR; 58 59 60 import com.sun.appserv.management.util.misc.ExceptionUtil; 61 import com.sun.appserv.management.util.misc.MapUtil; 62 import com.sun.appserv.management.util.misc.GSetUtil; 63 import com.sun.appserv.management.util.misc.TypeCast; 64 65 66 import com.sun.enterprise.management.AMXTestBase; 67 import com.sun.enterprise.management.Capabilities; 68 69 import com.sun.enterprise.management.PropertyKeys; 70 import com.sun.enterprise.management.Capabilities; 71 72 73 75 public final class DeploymentMgrTest extends AMXTestBase 76 { 77 public 78 DeploymentMgrTest( ) 79 throws IOException 80 { 81 } 82 83 public static Capabilities 84 getCapabilities() 85 { 86 return getOfflineCapableCapabilities( false ); 87 } 88 89 90 private static final class MyNotificationListener 91 implements NotificationListener 92 { 93 private final Object mDeployID; 94 private boolean mIsCompleted; 95 96 public 97 MyNotificationListener( final Object deployID ) 98 { 99 mDeployID = deployID; 100 mIsCompleted = false; 101 } 102 103 public boolean 104 isCompleted() 105 { 106 return( mIsCompleted ); 107 } 108 109 public synchronized void 110 handleNotification( 111 final Notification notif, 112 final Object handback) 113 { 114 try 115 { 116 realHandleNotification( notif, handback ); 117 } 118 catch( Exception e ) 119 { 120 e.printStackTrace(); 121 } 122 } 123 124 public void 125 realHandleNotification( 126 final Notification notif, 127 final Object handback) 128 { 129 final String type = notif.getType(); 130 final Map <String ,Serializable > m = TypeCast.asMap( notif.getUserData() ); 131 final Object deployID = m.get( DeploymentMgr.NOTIF_DEPLOYMENT_ID_KEY ); 132 133 if ( deployID.equals( mDeployID ) ) 135 { 136 if ( type.equals( DeploymentMgr.DEPLOYMENT_STARTED_NOTIFICATION_TYPE ) ) 137 { 138 } 139 else if ( deployID.equals( mDeployID ) ) 140 { 141 assert( deployID != null && deployID.equals( mDeployID ) ); 142 144 if ( type.equals( DeploymentMgr.DEPLOYMENT_COMPLETED_NOTIFICATION_TYPE ) ) 145 { 146 final Map <String ,Serializable > statusData = TypeCast.asMap( 147 m.get( DeploymentMgr.NOTIF_DEPLOYMENT_COMPLETED_STATUS_KEY ) ); 148 149 final DeploymentStatus status = 150 DeploymentSupport.mapToDeploymentStatus( statusData ); 151 152 assert( mDeployID.equals( deployID ) ) : 153 "deploy ID mismatch: " + mDeployID + " != " + deployID; 154 155 mIsCompleted = true; 156 } 157 else if ( type.equals( DeploymentMgr.DEPLOYMENT_PROGRESS_NOTIFICATION_TYPE ) ) 158 { 159 final Map <String ,Serializable > progressData = TypeCast.asMap( 160 m.get( DeploymentMgr.NOTIF_DEPLOYMENT_PROGRESS_KEY ) ); 161 TypeCast.checkMap( progressData, String .class, Serializable .class); 162 163 final DeploymentProgress progress = 164 DeploymentSupport.mapToDeploymentProgress( progressData ); 165 166 } 168 else 169 { 170 assert( false ) : "Unknown deployment notification type: " + type; 171 } 172 } 173 } 174 } 175 } 176 177 178 public DeploymentMgr 179 getDeploymentMgr() 180 { 181 return( getDomainRoot().getDeploymentMgr() ); 182 } 183 184 185 public Object 186 uploadFile( final String name, final InputStream is ) 187 throws IOException 188 { 189 final DeploymentMgr mgr = getDeploymentMgr(); 190 assert( mgr != null ); 191 193 final int totalSize = is.available(); 194 final int chunkSize = 1 + 32 * 1024; 196 final Object uploadID = mgr.initiateFileUpload( name, totalSize ); 197 int remaining = totalSize; 198 boolean done = false; 199 while ( remaining != 0 ) 200 { 201 final int actual = remaining < chunkSize ? remaining : chunkSize; 202 203 final byte[] bytes = new byte[ actual ]; 204 is.read( bytes ); 205 done = mgr.uploadBytes( uploadID, bytes ); 206 remaining -= actual; 207 } 208 assert( done ); 209 210 return( uploadID ); 211 } 212 213 public Object 214 uploadFile( final File theFile ) 215 throws IOException 216 { 217 final FileInputStream is = new FileInputStream ( theFile ); 218 Object id = null; 219 220 try 221 { 222 id = uploadFile( theFile.getName(), is ); 223 } 224 finally 225 { 226 is.close(); 227 } 228 return( id ); 229 } 230 231 public void 232 testDownloadFile( 233 final String moduleID, 234 final String filename, 235 final int chunkSize ) 236 throws IOException 237 { 238 final DeploymentMgr mgr = 239 getDomainRoot().getDeploymentMgr(); 240 241 final Object id = mgr.initiateFileDownload( moduleID, filename ); 242 243 245 final int actualChunkSize = chunkSize < mgr.MAX_DOWNLOAD_CHUNK_SIZE ? 246 chunkSize : mgr.MAX_DOWNLOAD_CHUNK_SIZE; 247 248 final long length = mgr.getDownloadLength( id ); 249 long doneSoFar = 0; 250 while ( doneSoFar < length ) 251 { 252 final byte[] bytes = mgr.downloadBytes( id, actualChunkSize ); 253 254 doneSoFar += bytes.length; 255 } 256 } 257 258 private final int K = 1024; 259 private final int MEGABYTE = K * K; 260 261 public void 262 XXXXtestDownloadFile1() 263 throws IOException 264 { 265 printVerbose( "testDownloadFile1" ); 266 testDownloadFile( "moduleID", "filename", MEGABYTE ); 267 printVerbose( "testDownloadFile1 DONE" ); 268 } 269 270 public void 271 testUndeployNonExistentModule() 272 { 273 printVerbose( "testUndeployNonExistentModule" ); 274 try 275 { 276 getDeploymentMgr().undeploy( "does_not_exist", null ); 277 } 278 catch( Exception e ) 279 { 280 final Throwable t = ExceptionUtil.getRootCause( e ); 282 assert( t instanceof IllegalArgumentException ); 283 } 284 printVerbose( "testUndeployNonExistentModule DONE" ); 285 } 286 287 private DeploymentStatus 288 deploy( final File theFile ) 289 throws IOException 290 { 291 final Object uploadID = uploadFile( theFile ); 293 final DeploymentMgr mgr = getDeploymentMgr(); 294 295 final Object deployID = mgr.initDeploy( ); 296 assert( deployID instanceof String ); 297 final MyNotificationListener myListener = new MyNotificationListener( deployID); 298 299 DeploymentStatus status = null; 300 mgr.addNotificationListener( myListener, null, null ); 301 try 302 { 303 final Map <String ,String > options = new HashMap <String ,String >(); 304 options.put( DeploymentMgr.DEPLOY_OPTION_FORCE_KEY, Boolean.TRUE.toString() ); 305 options.put( DeploymentMgr.DEPLOY_OPTION_VERIFY_KEY, Boolean.TRUE.toString() ); 306 options.put( DeploymentMgr.DEPLOY_OPTION_DESCRIPTION_KEY, "test deploy" ); 307 options.put( DeploymentMgr.DEPLOY_OPTION_AVAILABILITY_ENABLED_KEY, Boolean.FALSE.toString() ); 308 options.put( DeploymentMgr.DEPLOY_OPTION_ENABLE_KEY, Boolean.TRUE.toString() ); 309 310 mgr.startDeploy( deployID, uploadID, null, null); 311 312 printVerbose( NEWLINE + "Deploying: " + quote( theFile ) + 313 ", deploy options: " + MapUtil.toString( options, ", ") ); 314 while ( ! myListener.isCompleted() ) 315 { 316 try 317 { 318 Thread.sleep( 500 ); 320 } 321 catch( InterruptedException e ) 322 { 323 } 324 } 325 326 final Map <String ,Serializable > deploymentStatusMap = mgr.getFinalDeploymentStatus( deployID ); 327 status = DeploymentSupport.mapToDeploymentStatus( deploymentStatusMap ); 328 } 329 finally 330 { 331 try 332 { 333 mgr.removeNotificationListener( myListener ); 334 } 335 catch( Exception e ) 336 { 337 } 338 } 339 340 return( status ); 341 } 342 343 protected void 344 removeAllRefs( final String moduleID ) 345 { 346 final Set <DeployedItemRefConfig> refs = 347 getQueryMgr().queryJ2EETypeNameSet( XTypes.DEPLOYED_ITEM_REF_CONFIG, moduleID ); 348 349 for( final DeployedItemRefConfig ref : refs ) 350 { 351 final DeployedItemRefConfigCR container = (DeployedItemRefConfigCR)ref.getContainer(); 352 353 container.removeDeployedItemRefConfig( ref.getName() ); 354 } 355 } 356 357 public void 358 undeploy( final String moduleID ) 359 { 360 removeAllRefs( moduleID ); 361 362 final DeploymentMgr mgr = getDeploymentMgr(); 363 364 final long start = now(); 365 final DeploymentStatus status = DeploymentSupport.mapToDeploymentStatus( mgr.undeploy( moduleID, null ) ); 366 assert( status.getStageStatus() == DeploymentStatus.STATUS_CODE_SUCCESS ); 367 368 printElapsed( "undeploy " + moduleID, start ); 369 } 370 371 private static final Set <String > ARCHIVE_SUFFIXES = 372 GSetUtil.newUnmodifiableStringSet( 373 "ear", "war", "rar", "jar" 374 ); 375 376 private void 377 addArchivesInDirectory( 378 final File dir, 379 final List <File > archives ) 380 { 381 assert dir.isDirectory(); 382 383 final File [] contents = dir.listFiles(); 384 for( final File f : contents ) 385 { 386 if ( f.isDirectory() ) 387 { 388 addArchivesInDirectory( f, archives ); 389 } 390 else 391 { 392 final String name = f.getName(); 393 final int idx = name.lastIndexOf( "." ); 394 final String suffix = (idx <= 0 ? "" : name.substring( idx + 1, name.length() )).toLowerCase(); 395 396 if ( ARCHIVE_SUFFIXES.contains(suffix ) ) 397 { 398 archives.add( f ); 399 } 400 } 401 } 402 } 403 404 public void 405 testDeployUndeployModules( ) 406 { 407 final String filesString = getEnvString( PropertyKeys.ARCHIVES_TO_DEPLOY_KEY, "" ).trim(); 408 final String [] names = filesString.split( PropertyKeys.ARCHIVES_DELIM ); 409 410 final List <File > archives = new ArrayList <File >(); 411 for( int i = 0; i < names.length; ++i ) 412 { 413 names[ i ] = names[i].trim(); 414 415 final File f = new File ( names[ i ] ); 416 if ( ! f.exists() ) 417 { 418 warning( "File " + f + " does not exist" ); 419 } 420 if ( f.isDirectory() ) 421 { 422 addArchivesInDirectory( f, archives); 423 } 424 else 425 { 426 archives.add( f ); 427 } 428 } 429 430 List <String > moduleIDs = deployModules( archives ); 431 final List <Exception > results = undeployModules( moduleIDs ); 432 433 moduleIDs = deployModules( archives ); 435 436 final StandaloneServerConfig server = 438 getDomainConfig().getStandaloneServerConfigMap().get( "server" ); 439 for( final String moduleID : moduleIDs ) 440 { 441 server.createDeployedItemRefConfig( moduleID ); 442 println( "Added ref to: " + moduleID ); 443 } 444 } 445 446 447 protected List <Exception > 448 undeployModules( final List <String > moduleIDs ) 449 { 450 final List <Exception > results = new ArrayList <Exception >(); 451 452 for ( final String moduleID : moduleIDs ) 453 { 454 if ( moduleID != null ) 455 { 456 try 457 { 458 undeploy( moduleID ); 459 println( "Undeployed: " + moduleID ); 460 results.add( null ); 461 } 462 catch( Exception e ) 463 { 464 warning( "FAILURE undeploying module " + moduleID ); 465 results.add( e ); 466 } 467 } 468 } 469 return results; 470 } 471 472 476 protected List <String > 477 deployModules( final List <File > files ) 478 { 479 final List <String > moduleIDs = new ArrayList <String >(); 480 481 if ( files.size() == 0 ) 482 { 483 warning( "testDeployUndeployModules: no modules specified via property " + 484 PropertyKeys.ARCHIVES_TO_DEPLOY_KEY + ", NO MODULES WILL BE DEPLOYED." ); 485 } 486 else 487 { 488 final Set <String > failedSet = new HashSet <String >(); 489 final Set <String > successSet = new HashSet <String >(); 490 491 for( final File theFile : files ) 492 { 493 try 494 { 495 if ( theFile.exists() ) 496 { 497 final long start = now(); 498 final DeploymentStatus status = deploy( theFile ); 499 final long elapsed = now() - start; 500 501 String msg = "Deployed: " + 502 quote( theFile ) + " in " + elapsed + "ms"; 503 if ( getVerbose() ) 504 { 505 msg = msg + ", DeploymentStatus = " + status; 506 } 507 println( msg ); 508 509 if ( status.getStageStatus() != DeploymentStatus.STATUS_CODE_SUCCESS ) 510 { 511 warning( "DeploymentMgrTest.testDeployUndeployModules: expected STATUS_CODE_SUCCESS " + 512 "for " + quote( theFile.toString() ) + 513 ", got " + status.getStageStatus() ); 514 failedSet.add( theFile.toString() ); 515 continue; 516 } 517 518 String moduleID = 519 (String )status.getAdditionalStatus().get( DeploymentStatus.MODULE_ID_KEY ); 520 521 if ( moduleID == null ) 522 { 523 moduleID =(String ) status.getAdditionalStatus().get( "moduleid" ); 524 assert( moduleID != null ); 525 526 warning( "WARNING: used 'moduleid' instead of " + 527 "DeploymentStatus.MODULE_ID_KEY (" + DeploymentStatus.MODULE_ID_KEY + 528 ") as workaround for bug #6218705" ); 529 } 530 moduleIDs.add( moduleID ); 531 532 successSet.add( theFile.toString() ); 533 } 534 else 535 { 536 warning( "testDeployUndeployModules: file " + 537 quote( theFile.toString() ) + " does not exist." ); 538 } 539 } 540 catch( Throwable t ) 541 { 542 warning( "Error deploying archive: " + quote( theFile.toString() ) ); 543 } 544 } 545 546 if ( failedSet.size() != 0 ) 547 { 548 failure( "testDeployUndeployModules: failure count = " + 549 failedSet.size() + " modules: " + toString( failedSet ) ); 550 } 551 } 552 return moduleIDs; 553 } 554 } 555 556 557 558 559 560 561 562 563 564 565 566 | Popular Tags |