1 23 package com.sun.appserv.management.deploy; 24 25 import java.util.Map ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.ArrayList ; 29 import java.util.Collections ; 30 31 import java.io.Serializable ; 32 33 import com.sun.appserv.management.util.misc.TypeCast; 34 import com.sun.appserv.management.base.OperationStatusBase; 35 36 37 import com.sun.appserv.management.base.AMXDebug; 38 39 43 public final class DeploymentStatusImpl 44 extends OperationStatusBase 45 implements DeploymentStatus 46 { 47 transient DeploymentStatus mParent; 48 49 public 50 DeploymentStatusImpl( final DeploymentStatus src ) 51 { 52 this( src.asMap(), true ); 53 } 54 55 private <T extends Serializable > 56 DeploymentStatusImpl( 57 final Map <String ,T> m, 58 final boolean doValidate ) 59 { 60 super( m, DEPLOYMENT_STATUS_CLASS_NAME ); 61 62 checkValidType( m, DEPLOYMENT_STATUS_CLASS_NAME ); 63 mParent = null; 64 65 convertSubStages(); 66 if ( doValidate && ! validate() ) 67 { 68 throw new IllegalArgumentException ( toString() ); 69 } 70 } 71 72 93 public <T extends Serializable > 94 DeploymentStatusImpl( final Map <String ,T> m ) 95 { 96 this( m, true ); 97 convertSubStages(); 98 } 99 100 109 public <T extends Serializable > 110 DeploymentStatusImpl( 111 final int stageStatus, 112 final String stageStatusMessage, 113 final String stageDescription, 114 final Map <String ,T> optional ) 115 { 116 super( null, DEPLOYMENT_STATUS_CLASS_NAME ); 117 118 putAll( optional ); 119 120 setStageStatus( stageStatus ); 121 setStageStatusMessage( stageStatusMessage == null ? "" : stageStatusMessage ); 122 setStageDescription( stageDescription == null ? "" : stageDescription ); 123 124 convertSubStages(); 125 if ( ! validate() ) 126 { 127 throw new IllegalArgumentException ( ); 128 } 129 } 130 131 private void 132 convertSubStages() 133 { 134 final List <?> value = List .class.cast( getField( SUB_STAGES_KEY ) ); 135 if ( value != null && value.size() != 0 ) 136 { 137 final List <DeploymentStatus> ssList = new ArrayList <DeploymentStatus>(); 138 139 for( final Object o : value ) 140 { 141 DeploymentStatus ds = null; 142 if ( o instanceof Map ) 143 { 144 final Map <String ,Serializable > m = TypeCast.asMap(o); 145 ds = new DeploymentStatusImpl(m,true); 146 } 147 else if ( o instanceof DeploymentStatus ) 148 { 149 final DeploymentStatus in = (DeploymentStatus)o; 151 ds = new DeploymentStatusImpl( in ); 152 } 153 else 154 { 155 throw new IllegalArgumentException (); 156 } 157 ds.setParent(this); 158 ssList.add( ds ); 159 } 160 putField( SUB_STAGES_KEY, (Serializable )ssList ); 161 } 162 } 163 164 166 @Override 167 protected Serializable 168 asMapHook( final String key, final Serializable value ) 169 { 170 Serializable result = value; 171 172 if ( SUB_STAGES_KEY.equals( key ) ) 173 { 174 final List <?> l = List .class.cast( value ); 176 if ( l != null && l.size() != 0 ) 177 { 178 final List <DeploymentStatus> lds = 179 TypeCast.checkList( l, DeploymentStatus.class ); 180 181 final ArrayList <Map <String ,Serializable >> maps = 182 new ArrayList <Map <String ,Serializable >>(); 183 184 for( final DeploymentStatus ds : lds ) 185 { 186 maps.add( ds.asMap() ); 187 } 188 result = maps; 189 } 190 } 191 else 192 { 193 result = super.asMapHook(key, value ); 194 } 195 196 return result; 197 } 198 199 protected boolean 200 validate() 201 { 202 boolean valid = getInteger( STAGE_STATUS_KEY ) != null; 203 assert( valid ) : "STAGE_STATUS_KEY missing"; 204 if ( valid ) 205 { 206 if ( getString( STAGE_STATUS_MESSAGE_KEY ) == null ) 207 { 208 putField( STAGE_STATUS_MESSAGE_KEY, "N/A" ); 209 } 210 } 211 212 if ( valid ) 213 { 214 final List <?> value = List .class.cast( getField( SUB_STAGES_KEY ) ); 215 final List <DeploymentStatus> subStages = 216 TypeCast.checkList( value, DeploymentStatus.class ); 217 } 218 219 return( valid ); 220 } 221 222 223 public String 224 getStageDescription() 225 { 226 return( getString( STAGE_DESCRIPTION_KEY ) ); 227 } 228 229 public void 230 setStageDescription( final String description ) 231 { 232 putField( STAGE_DESCRIPTION_KEY, description ); 233 } 234 235 236 public String 237 getStageStatusMessage() 238 { 239 return( getString( STAGE_STATUS_MESSAGE_KEY ) ); 240 } 241 242 243 public void 244 setStageStatusMessage( final String message ) 245 { 246 putField( STAGE_STATUS_MESSAGE_KEY, message ); 247 } 248 249 250 public int 251 getStageStatus() 252 { 253 return( getStatusCode() ); 254 } 255 256 public void 257 setStageStatus( int status ) 258 { 259 setStatusCode( status ); 260 } 261 262 private List <DeploymentStatus> 263 getDeploymentStatusField() 264 { 265 final List <?> value = List .class.cast( getField( SUB_STAGES_KEY ) ); 266 final List <DeploymentStatus> subStages = 267 TypeCast.checkList( value, DeploymentStatus.class ); 268 return subStages; 269 } 270 271 public void 272 addSubStage( final DeploymentStatus status ) 273 { 274 status.setParent( this ); 275 276 List <DeploymentStatus> subStages = getDeploymentStatusField(); 277 if ( subStages == null ) 278 { 279 subStages = new ArrayList <DeploymentStatus>(); 280 281 putField( SUB_STAGES_KEY, (Serializable )subStages ); 282 } 283 284 subStages.add( status ); 285 } 286 287 public Iterator <Map <String ,Serializable >> 288 getSubStages() 289 { 290 List <Map <String ,Serializable >> maps = null; 291 292 final List <DeploymentStatus> subStages = getSubStagesList(); 293 if ( subStages != null ) 294 { 295 maps = new ArrayList <Map <String ,Serializable >>(); 296 for( final DeploymentStatus ds : subStages ) 297 { 298 maps.add( ds.asMap() ); 299 } 300 } 301 else 302 { 303 maps = Collections.emptyList(); 304 } 305 306 return maps.iterator(); 307 } 308 309 public List <DeploymentStatus> 310 getSubStagesList() 311 { 312 List <DeploymentStatus> subStages = getDeploymentStatusField(); 313 TypeCast.checkList( subStages, DeploymentStatus.class ); 314 315 if ( subStages == null ) 316 { 317 subStages = Collections.emptyList(); 318 } 319 else 320 { 321 subStages = Collections.unmodifiableList( subStages ); 322 } 323 324 return subStages; 325 } 326 327 public DeploymentStatus 328 getParent() 329 { 330 return( mParent ); 331 } 332 333 public void 334 setParent( final DeploymentStatus parent ) 335 { 336 mParent = parent; 337 } 338 339 public Throwable 340 getStageThrowable() 341 { 342 return( getThrowable( ) ); 343 } 344 345 public void 346 setStageThrowable( Throwable t) 347 { 348 setThrowable( t ); 349 } 350 351 352 public Map <String ,Serializable > 353 getAdditionalStatus() 354 { 355 return( getMap( ADDITIONAL_STATUS_KEY ) ); 356 } 357 358 359 public void 360 setAdditionalStatus( final Map <String ,Serializable > additionalStatus ) 361 { 362 if ( ! (additionalStatus instanceof Serializable ) ) 363 { 364 throw new IllegalArgumentException ( "Class is not Serializable: " + 365 additionalStatus.getClass().getName() ); 366 } 367 368 putField( ADDITIONAL_STATUS_KEY, (Serializable )additionalStatus ); 369 } 370 371 372 public boolean 373 equals( final Object rhs) 374 { 375 boolean equal = false; 376 377 if ( rhs instanceof DeploymentStatus && ! (rhs instanceof DeploymentStatusImpl) ) 378 { 379 equal = super.equals( new DeploymentStatusImpl( (DeploymentStatus)rhs ) ); 380 } 381 else 382 { 383 equal = super.equals( rhs ); 384 } 385 386 return( equal ); 387 } 388 } 389 390 391 392 393 394 395 396 397 | Popular Tags |