1 23 package com.sun.enterprise.management.support; 24 25 import java.util.Arrays ; 26 import java.util.Set ; 27 import java.util.List ; 28 import java.util.ArrayList ; 29 import java.util.Map ; 30 import java.util.HashMap ; 31 import java.util.HashSet ; 32 import java.util.Iterator ; 33 import java.util.logging.Logger ; 34 import java.lang.reflect.Constructor ; 35 36 import javax.management.ObjectName ; 37 import javax.management.MBeanServer ; 38 import javax.management.MBeanRegistrationException ; 39 import javax.management.InstanceAlreadyExistsException ; 40 import javax.management.NotCompliantMBeanException ; 41 42 import com.sun.appserv.management.base.AMX; 43 import com.sun.appserv.management.util.misc.ExceptionUtil; 44 import com.sun.appserv.management.util.misc.Output; 45 import com.sun.appserv.management.util.misc.GSetUtil; 46 import com.sun.appserv.management.util.misc.ListUtil; 47 import com.sun.appserv.management.util.misc.StringUtil; 48 import com.sun.appserv.management.util.jmx.JMXUtil; 49 import com.sun.appserv.management.base.Util; 50 import com.sun.appserv.management.base.AMXDebug; 51 import com.sun.appserv.management.DomainRoot; 52 53 56 abstract class LoaderOfOld 57 { 58 protected final Loader mLoader; 59 private final Output mDebug; 60 61 LoaderOfOld( final Loader loader ) 62 { 63 mLoader = loader; 64 65 mDebug = AMXDebug.getInstance().getOutput( this.getClass().getName() ); 66 } 67 68 protected final void 69 debug(final Object o) 70 { 71 mDebug.println( o.toString() ); 72 } 73 74 75 protected abstract Set <ObjectName > findAllOldCandidates(); 76 77 78 protected DomainRoot 79 getDomainRoot() 80 { 81 return( mLoader.getDomainRoot() ); 82 } 83 84 protected abstract Set getNeedsSupport(); 85 protected abstract Set getIgnoreTypes(); 86 protected abstract boolean isOldMBean( final ObjectName objectName ); 87 88 89 public final boolean 90 shouldSync( final ObjectName o ) 91 { 92 boolean shouldSync = isOldMBean( o ); 93 94 if ( shouldSync ) 95 { 96 final String type = o.getKeyProperty( "type" ); 97 98 if ( getNeedsSupport().contains( type ) ) 99 { 100 shouldSync = false; 101 getLogger().warning( 102 "com.sun.appserv MBean not yet supported: " + 103 StringUtil.quote(o) ); 104 } 105 else if ( isDefectiveIgnore( o ) ) 106 { 107 shouldSync = false; 108 debug( 109 "com.sun.appserv MBean was last determined to be defective " + 110 "and will not be represented in AMX: " + 111 StringUtil.quote(o) ); 112 } 113 } 114 115 return( shouldSync ); 116 } 117 118 119 123 protected boolean 124 isDefectiveIgnore( final ObjectName objectName ) 125 { 126 return false; 127 } 128 129 130 134 public final List 135 findAllOld() 136 { 137 final Set <ObjectName > all = findAllOldCandidates(); 138 final Set <ObjectName > results = new HashSet <ObjectName >(); 139 140 for( final ObjectName objectName : all) 141 { 142 if ( shouldSync( objectName ) ) 143 { 144 results.add( objectName ); 145 } 146 } 147 148 return( ListUtil.newListFromCollection( results ) ); 149 } 150 151 protected abstract ObjectName oldToNewObjectName( final ObjectName o ); 152 153 154 155 159 protected final Map <String ,Set <ObjectName >> 160 candidatesToMap( 161 final Set <ObjectName > candidates, 162 final String key) 163 { 164 final Map <String ,Set <ObjectName >> setMap = new HashMap <String ,Set <ObjectName >>(); 165 for( final ObjectName candidate : candidates ) 166 { 167 final String keyValue = candidate.getKeyProperty( key ); 168 169 Set <ObjectName > typeSet = setMap.get( keyValue ); 170 if ( typeSet == null ) 171 { 172 typeSet = new HashSet <ObjectName >(); 173 setMap.put( keyValue, typeSet ); 174 } 175 typeSet.add( candidate ); 176 } 177 178 return( setMap ); 179 } 180 181 182 183 184 protected final ObjectName 185 registerNew( 186 final Object impl, 187 final ObjectName implObjectName, 188 final ObjectName oldObjectName ) 189 throws MBeanRegistrationException , 190 InstanceAlreadyExistsException , NotCompliantMBeanException 191 { 192 if ( impl == null ) 193 { 194 final String msg = "unable to create new impl for old: " + oldObjectName; 195 debug( msg ); 196 throw new IllegalArgumentException ( msg ); 197 } 198 199 return( mLoader.registerNew( impl, implObjectName, oldObjectName ) ); 200 } 201 202 protected final void 203 trace( final Object o ) 204 { 205 debug( o ); 206 } 207 protected String 208 toString( final Object o ) 209 { 210 return( com.sun.appserv.management.util.stringifier.SmartStringifier.toString( o ) ); 211 } 212 213 protected Logger 214 getLogger() 215 { 216 return( mLoader.getMBeanLogger() ); 217 } 218 219 public String 220 getAMXJMXDomainName() 221 { 222 return( mLoader.getAMXJMXDomainName() ); 223 } 224 225 public MBeanServer 226 getMBeanServer() 227 { 228 return( mLoader.getMBeanServer() ); 229 } 230 231 232 233 private static final Class [] EMPTY_SIG = new Class [0]; 234 private static final Class [] DELEGATE_SIG = 235 new Class [] { Delegate.class }; 236 237 238 239 private Constructor 240 findConstructor( final Constructor [] constructors, final Class [] sig ) 241 { 242 Constructor constructor = null; 243 244 for( int i = 0; i < constructors.length; ++i ) 245 { 246 final Class <?>[] csig = constructors[ i ].getParameterTypes(); 247 248 if ( csig.length == sig.length ) 249 { 250 constructor = constructors[ i ]; 251 252 for( int c = 0; c < sig.length; ++c ) 253 { 254 if ( ! csig[ i ].isAssignableFrom( sig[ i ] ) ) 255 { 256 constructor = null; 257 break; 258 } 259 } 260 261 if ( constructor != null ) 262 { 263 break; 264 } 265 } 266 } 267 268 return( constructor ); 269 } 270 271 private Constructor 272 getDelegateConstructor( final Constructor [] constructors ) 273 { 274 return( findConstructor( constructors, DELEGATE_SIG ) ); 275 } 276 277 278 private Constructor 279 getEmptyConstructor( final Constructor [] constructors ) 280 { 281 return( findConstructor( constructors, EMPTY_SIG ) ); 282 } 283 284 285 protected Class 286 getImplClass( 287 final ObjectName newObjectName, 288 final ObjectName oldObjectName) 289 { 290 final String newType = Util.getJ2EEType( newObjectName ); 291 292 final TypeInfo info = TypeInfos.getInstance().getInfo( newType ); 293 assert( info != null ); 294 final Class implClass = info.getImplClass(); 295 296 return( implClass ); 297 } 298 299 protected Object 300 newImpl( 301 final ObjectName newObjectName, 302 final ObjectName oldObjectName ) 303 throws Exception 304 { 305 Object impl = null; 306 307 final Class implClass = getImplClass( newObjectName, oldObjectName ); 308 309 try 310 { 311 final Constructor [] constructors = implClass.getConstructors(); 312 Constructor constructor = null; 313 314 if ( (constructor = getDelegateConstructor( constructors )) != null ) 315 { 316 final DelegateToMBeanDelegate delegate = 317 new DelegateToMBeanDelegate( mLoader.getMBeanServer(), oldObjectName ); 318 assert( delegate != null ); 319 debug( "created Delegate with target of " + oldObjectName + 320 " for " + newObjectName ); 321 322 impl = constructor.newInstance( new Object [] { delegate } ); 323 } 324 else if ( getEmptyConstructor( constructors ) != null ) 325 { 326 impl = implClass.newInstance(); 327 } 328 else 329 { 330 assert( false ); 331 throw new Error ( "Delegate has no constructor" ); 332 } 333 } 334 catch( Exception e ) 335 { 336 final Throwable rootCause = ExceptionUtil.getRootCause( e ); 337 debug( "Loader.newImpl: exception creating new impl: " + e + "\n" + 338 ExceptionUtil.getStackTrace( rootCause ) ); 339 throw e; 340 } 341 342 return( impl ); 343 } 344 345 346 protected ObjectName 347 findExisting( 348 final Set <ObjectName > newObjectNames, 349 final ObjectName oldObjectName ) 350 { 351 ObjectName resultName = null; 352 353 if ( newObjectNames.size() == 1 ) 354 { 355 resultName = GSetUtil.getSingleton( newObjectNames ); 357 } 358 359 return( resultName ); 360 } 361 362 private final synchronized ObjectName 363 ensureNew( 364 final ObjectName newObjectName, 365 final ObjectName oldObjectName ) 366 throws MBeanRegistrationException , 367 InstanceAlreadyExistsException , NotCompliantMBeanException , Exception 368 { 369 final ObjectName pattern = Util.newObjectNamePattern( newObjectName ); 371 372 final Set <ObjectName > objectNames = JMXUtil.queryNames( getMBeanServer(), pattern, null ); 374 375 final ObjectName existingObjectName = findExisting( objectNames, oldObjectName ); 376 377 ObjectName resultName = null; 378 379 if ( existingObjectName == null ) 380 { 381 final Object impl = newImpl( newObjectName, oldObjectName ); 383 384 resultName = registerNew( impl, newObjectName, oldObjectName ); 385 } 386 else 387 { 388 resultName = existingObjectName; 389 } 390 391 assert( resultName != null ); 392 393 return( resultName ); 394 } 395 396 397 protected ObjectName 398 syncWithOld( final ObjectName oldObjectName ) 399 throws MBeanRegistrationException , 400 InstanceAlreadyExistsException , NotCompliantMBeanException , Exception 401 { 402 final ObjectName newObjectName = oldToNewObjectName( oldObjectName ); 403 debug( "\nsyncWithOld: \n" + oldObjectName + "\n=>\n" + newObjectName ); 404 405 final ObjectName resultName = ensureNew( newObjectName, oldObjectName ); 406 407 return( resultName ); 408 } 409 } 410 411 412 413 414 415 416 417 418 | Popular Tags |