1 23 package com.sun.enterprise.management.helper; 24 25 import java.io.File ; 26 import java.io.IOException ; 27 import java.io.Serializable ; 28 29 import java.util.Map ; 30 import java.util.HashMap ; 31 import java.util.Set ; 32 import java.util.HashSet ; 33 34 import javax.management.ObjectName ; 35 import javax.management.ListenerNotFoundException ; 36 import javax.management.NotificationListener ; 37 38 import com.sun.appserv.management.DomainRoot; 39 import com.sun.appserv.management.base.Util; 40 41 42 import com.sun.appserv.management.helper.Helper; 43 import com.sun.appserv.management.helper.DeployNotificationListener; 44 import com.sun.appserv.management.helper.Misc; 45 46 import com.sun.appserv.management.deploy.DeploymentMgr; 47 import com.sun.appserv.management.deploy.DeploymentProgress; 48 import com.sun.appserv.management.deploy.DeploymentStatus; 49 import static com.sun.appserv.management.deploy.DeploymentStatus.*; 50 import static com.sun.appserv.management.deploy.DeploymentMgr.*; 51 52 import com.sun.appserv.management.config.DeployedItemRefConfig; 53 import com.sun.appserv.management.config.DeployedItemRefConfigCR; 54 import com.sun.appserv.management.config.StandaloneServerConfig; 55 import com.sun.appserv.management.config.ClusterConfig; 56 import com.sun.appserv.management.config.DomainConfig; 57 58 59 60 64 public final class DeployHelper extends Helper 65 { 66 private DeployNotificationListener mListener; 67 private final DeploymentMgr mDeploymentMgr; 68 private boolean mDidAssociate; 69 70 public 71 DeployHelper( final DeploymentMgr deploymentMgr ) 72 { 73 super( deploymentMgr.getDomainRoot() ); 74 75 mDeploymentMgr = deploymentMgr; 76 mListener = null; 77 mDidAssociate = false; 78 } 79 80 85 public DeployNotificationListener 86 getDeployNotificationListener() 87 { 88 return mListener; 89 } 90 91 public final static String 92 getDefaultAppName( final String archiveName ) 93 { 94 String result = archiveName; 95 96 final int idx = archiveName.lastIndexOf( "." ); 97 if ( idx > 1 ) 98 { 99 result = archiveName.substring( 0, idx ); 100 } 101 102 return( result ); 103 } 104 105 123 public void 124 deploy( 125 final File archive, 126 final Map <String ,String > deployOptions ) 127 throws IOException 128 { 129 if ( archive == null ) 130 { 131 throw new IllegalArgumentException (); 132 } 133 134 final Object uploadID = Misc.uploadFile( 135 getDomainRoot().getUploadDownloadMgr(), 136 archive ); 137 138 final Object deployID = mDeploymentMgr.initDeploy( ); 139 mListener = new DeployNotificationListener( mDeploymentMgr, deployID ); 140 141 try 142 { 143 final String archiveName = archive.getName(); 144 145 final Map <String ,String > actualOptions = new HashMap <String ,String >(); 146 if ( deployOptions != null ) 147 { 148 actualOptions.putAll( deployOptions ); 149 } 150 if ( ! actualOptions.containsKey( DEPLOY_OPTION_NAME_KEY ) ) 151 { 152 final String appName = getDefaultAppName( archiveName ); 153 actualOptions.put( DEPLOY_OPTION_NAME_KEY, appName ); 154 } 155 156 mDeploymentMgr.startDeploy( deployID, uploadID, null, actualOptions ); 157 } 158 finally 159 { 160 mListener.cleanup(); 162 } 163 } 164 165 169 public DeploymentStatus 170 waitTillDone( final long pollMillis ) 171 { 172 if ( pollMillis > 5 * 1000 ) 174 { 175 throw new IllegalArgumentException (); 176 } 177 178 while ( ! mListener.isCompleted() ) 181 { 182 Util.sleep( pollMillis ); 183 } 184 185 final DeploymentStatus status = mListener.getDeploymentStatus(); 186 187 return status; 188 } 189 190 196 public static Set <DeployedItemRefConfigCR> 197 getTargetProxies( 198 final DomainRoot domainRoot, 199 final String [] names ) 200 { 201 final DomainConfig domainConfig = domainRoot.getDomainConfig(); 202 203 final Set <DeployedItemRefConfigCR> result = new HashSet <DeployedItemRefConfigCR>(); 204 205 final Map <String ,StandaloneServerConfig> serverConfigs = 206 domainConfig.getStandaloneServerConfigMap(); 207 208 final Map <String ,ClusterConfig> clusterConfigs = 209 domainConfig.getClusterConfigMap(); 210 211 for( final String name : names ) 212 { 213 if ( serverConfigs.containsKey( name ) ) 214 { 215 result.add( serverConfigs.get( name ) ); 216 } 217 else if ( clusterConfigs.containsKey( name ) ) 218 { 219 result.add( clusterConfigs.get( name ) ); 220 } 221 } 222 223 return result; 224 } 225 226 235 public Set <DeployedItemRefConfig> 236 createReferences( 237 final String [] targets, 238 final Map <String ,String > refOptions ) 239 { 240 if ( targets == null || targets.length == 0 ) 241 { 242 throw new IllegalArgumentException (); 243 } 244 245 if ( mListener == null || ! mListener.isCompleted() ) 246 { 247 throw new IllegalStateException (); 248 } 249 250 final DeploymentStatus status = mListener.getDeploymentStatus(); 251 252 if ( status == null || status.getStageStatus() == STATUS_CODE_FAILURE ) 253 { 254 throw new IllegalStateException (); 255 } 256 257 if ( mDidAssociate ) 258 { 259 throw new IllegalStateException (); 260 } 261 262 final Map <String ,Serializable > additionalStatus = status.getAdditionalStatus(); 264 final String moduleID = (String )additionalStatus.get( MODULE_ID_KEY ); 265 266 final Set <DeployedItemRefConfig> refs = new HashSet <DeployedItemRefConfig>(); 267 final Set <DeployedItemRefConfigCR> proxies = getTargetProxies( getDomainRoot(), targets ); 268 for( final DeployedItemRefConfigCR cr : proxies ) 269 { 270 final DeployedItemRefConfig ref = 271 cr.createDeployedItemRefConfig( moduleID, refOptions ); 272 refs.add( ref ); 273 } 274 275 return refs; 276 } 277 278 291 public DeploymentStatus 292 deploy( 293 final File archive, 294 final Map <String ,String > deployOptions, 295 final String [] targets, 296 final Map <String ,String > refOptions ) 297 throws IOException 298 { 299 deploy( archive, deployOptions ); 300 301 final DeploymentStatus status = waitTillDone( 50 ); 302 303 if ( targets != null && targets.length != 0 ) 304 { 305 createReferences( targets, refOptions ); 306 } 307 308 return status; 309 } 310 311 317 public DeploymentStatus 318 deploy( 319 final File archive, 320 final String [] targets ) 321 throws IOException 322 { 323 return deploy( archive, null, targets, null ); 324 } 325 } 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 | Popular Tags |