1 23 package com.sun.appserv.management.base; 24 25 import java.util.Map ; 26 import java.util.HashMap ; 27 import java.util.Collection ; 28 import java.util.Collections ; 29 import java.io.File ; 30 import java.io.Serializable ; 31 32 33 import com.sun.appserv.management.util.misc.MapUtil; 34 import com.sun.appserv.management.util.misc.TypeCast; 35 import com.sun.appserv.management.util.misc.ObjectUtil; 36 import com.sun.appserv.management.util.jmx.OpenMBeanUtil; 37 38 39 42 43 public abstract class MapCapableBase implements MapCapable 44 { 45 private Map <String ,Serializable > mFields; 46 private boolean mWasMadeImmutable; 47 private String mClassName; 48 49 public int 50 hashCode() 51 { 52 return ObjectUtil.hashCode( mFields, mClassName) ^ 53 (mWasMadeImmutable ? 1 : 0); 54 } 55 56 protected 57 MapCapableBase( final String className ) 58 { 59 mFields = new HashMap <String ,Serializable >(); 60 mWasMadeImmutable = false; 61 mClassName = className; 62 } 63 64 protected <T extends Serializable > 65 MapCapableBase( 66 final Map <String ,T> m, 67 final String className) 68 { 69 this( className ); 70 71 mClassName = className; 72 73 if ( m != null ) 74 { 75 putAll( m ); 76 77 getFields().remove( MAP_CAPABLE_CLASS_NAME_KEY ); 79 } 80 } 81 82 protected <T extends Serializable > void 83 checkValidType( final Map <String ,T> m, final String requiredType ) 84 { 85 final String type = (String )m.get( MAP_CAPABLE_CLASS_NAME_KEY ); 86 87 if ( ! requiredType.equals( type ) ) 88 { 89 throw new IllegalArgumentException ( "Illegal MAP_CAPABLE_CLASS_NAME_KEY: " + type ); 90 } 91 } 92 93 96 protected abstract boolean validate(); 97 98 101 public String 102 getMapClassName() 103 { 104 return( mClassName ); 105 } 106 107 108 protected void 109 illegalObject( final Object o ) 110 { 111 throw new IllegalArgumentException ( 112 "Object is of illegal class and/or non-Serializable class " + 113 o.getClass().getName() ); 114 } 115 116 protected void 117 checkInJavaUtil( final Object o) 118 { 119 if ( ! o.getClass().getName().startsWith( "java.util." ) ) 120 { 121 illegalObject( o ); 122 } 123 } 124 125 136 protected final void 137 checkLegalObject( 138 final Object o, 139 final boolean allowMapCapable ) 140 { 141 if ( o != null ) 142 { 143 if ( o instanceof Collection ) 147 { 148 checkInJavaUtil( o ); 149 150 final Collection <?> oc = TypeCast.asCollection( o ); 151 for( final Object next : oc ) 152 { 153 checkLegalObject( next ); 154 } 155 } 156 else if ( o.getClass().getComponentType() != null ) 157 { 158 final Object [] ta = (Object [])o; 160 for( int i = 0; i < ta.length; ++i ) 161 { 162 checkLegalObject( ta[i] ); 163 } 164 } 165 else if ( o instanceof MapCapable ) 166 { 167 if ( allowMapCapable ) 168 { 169 checkLegalObject( ((MapCapable)o).asMap() ); 170 } 171 else 172 { 173 illegalObject( o ); 174 } 175 } 176 else if ( o instanceof Map ) 177 { 178 checkInJavaUtil( o ); 179 180 checkLegalObject( ((Map )o).keySet() ); 181 checkLegalObject( ((Map )o).values() ); 182 } 183 else if ( o instanceof Throwable ) 184 { 185 final String classname = o.getClass().getName(); 186 187 if ( ! (classname.startsWith( "java." ) || 188 classname.startsWith( "javax." )) ) 189 { 190 illegalObject( o ); 191 } 192 checkLegalObject( asT( ((Throwable )o).getCause() ) ); 193 } 194 else if ( OpenMBeanUtil.getSimpleType( o.getClass() ) == null ) 195 { 196 illegalObject( o ); 197 } 198 } 199 } 200 201 private Serializable 202 asT( final Object o ) 203 { 204 return Serializable .class.cast( o ); 205 } 206 207 protected final void 208 checkLegalObject( final Object o ) 209 { 210 checkLegalObject( o, true ); 211 } 212 213 protected boolean 214 validateNullOrOfType( 215 final String key, 216 final Class <?> theClass ) 217 { 218 final Object o = getField( key ); 219 220 return( o == null || theClass.isAssignableFrom( o.getClass() ) ); 221 } 222 223 224 protected final void 225 validateThrow() 226 { 227 if ( ! validate() ) 228 { 229 throw new IllegalArgumentException ( toString() ); 230 } 231 } 232 233 protected Serializable 234 asMapHook( final String key, final Serializable value) 235 { 236 Serializable result = value; 237 238 if ( result instanceof MapCapable ) 239 { 240 final Map <String ,Serializable > m = ((MapCapable)result).asMap(); 241 242 TypeCast.checkSerializable( m ); 243 244 result = Serializable .class.cast( m ); 245 } 246 247 return result; 248 } 249 250 public final Map <String ,Serializable > 251 asMap() 252 { 253 final Map <String ,Serializable > result = new HashMap <String ,Serializable >(); 254 255 for( final String key : getFields().keySet() ) 257 { 258 final Serializable value = getField( key ); 259 260 Serializable s = asMapHook( key, value ); 261 result.put( key, s ); 262 } 263 264 result.put( MAP_CAPABLE_CLASS_NAME_KEY, getMapClassName() ); 265 266 checkLegalObject( result, false ); 267 268 return( result ); 269 } 270 271 274 public void 275 makeImmutable() 276 { 277 if ( ! mWasMadeImmutable ) 278 { 279 mFields = Collections.unmodifiableMap( mFields ); 280 mWasMadeImmutable = true; 281 } 282 } 283 284 285 289 public void 290 putField( 291 final String key, 292 final Serializable value ) 293 { 294 checkLegalObject( value ); 295 296 final Serializable newValue = putFieldHook( key, value ); 297 getFields().put( key, newValue ); 298 } 299 300 protected Serializable 301 putFieldHook( 302 final String key, 303 final Serializable value ) 304 { 305 return( value ); 306 } 307 308 311 protected <T extends Serializable >void 312 putAll( final Map <String ,T> m ) 313 { 314 if ( m != null ) 315 { 316 for( final String key : m.keySet() ) 317 { 318 putField( key, m.get( key ) ); 319 } 320 } 321 } 322 323 324 private Map <String ,Serializable > 325 getFields() 326 { 327 return( mFields ); 328 } 329 330 protected Serializable 331 getField( String key ) 332 { 333 return( getFields().get( key ) ); 334 } 335 336 protected final Serializable 337 getObject( final String key ) 338 { 339 return( getFields().get( key ) ); 340 } 341 342 protected final String 343 getString( final String key ) 344 { 345 return( (String )getObject( key ) ); 346 } 347 348 protected final String [] 349 getStringArray( final String key ) 350 { 351 return( (String [])getObject( key ) ); 352 } 353 354 protected final Boolean 355 getBoolean( final String key ) 356 { 357 return( (Boolean )getObject( key ) ); 358 } 359 360 protected final Byte 361 getByte( final String key ) 362 { 363 return( (Byte )getObject( key ) ); 364 } 365 366 367 protected final boolean 368 getboolean( final String key ) 369 { 370 final Boolean b = getBoolean( key ); 371 372 if ( b == null ) 373 { 374 throw new IllegalArgumentException ( key ); 375 } 376 377 return( b.booleanValue() ); 378 } 379 380 protected final Integer 381 getInteger( final String key ) 382 { 383 return( (Integer )getObject( key ) ); 384 } 385 386 protected final Map <String ,Serializable > 387 getMap( final String key ) 388 { 389 return (Map <String ,Serializable >)getObject( key ); 390 } 391 392 protected final File 393 getFile( final String key ) 394 { 395 final String s = getString( key ); 396 397 return( s == null ? null : new File ( s ) ); 398 } 399 400 public boolean 401 equals( final Object o ) 402 { 403 if ( o == this ) 404 { 405 return( true ); 406 } 407 else if ( ! (o instanceof MapCapableBase) ) 408 { 409 return( false ); 410 } 411 412 boolean equals = false; 413 414 final MapCapableBase rhs = (MapCapableBase)o; 415 equals = MapUtil.mapsEqual( getFields(), rhs.getFields() ); 416 417 return( equals ); 418 } 419 420 public String 421 toString() 422 { 423 return( MapUtil.toString( getFields() ) ); 424 } 425 } 426 427 428 429 430 431 432 433 434 | Popular Tags |