1 23 24 package com.sun.enterprise.management.offline; 25 26 import java.util.List ; 27 import java.util.ArrayList ; 28 import java.util.Map ; 29 import java.util.HashMap ; 30 import java.util.Collection ; 31 import java.util.Collections ; 32 import java.util.Arrays ; 33 34 import java.lang.reflect.Constructor ; 35 import java.lang.reflect.Proxy ; 36 37 import javax.management.ObjectName ; 38 import javax.management.MBeanServer ; 39 import javax.management.MBeanInfo ; 40 import javax.management.MBeanAttributeInfo ; 41 import javax.management.Notification ; 42 import javax.management.MBeanServerNotification ; 43 import javax.management.AttributeNotFoundException ; 44 import javax.management.InstanceNotFoundException ; 45 import javax.management.MBeanRegistrationException ; 46 import javax.management.InstanceAlreadyExistsException ; 47 import javax.management.NotCompliantMBeanException ; 48 import javax.management.MBeanRegistration ; 49 import javax.management.NotificationListener ; 50 import javax.management.relation.MBeanServerNotificationFilter ; 51 52 53 import com.sun.appserv.management.DomainRoot; 54 import com.sun.appserv.management.base.AMX; 55 import com.sun.appserv.management.base.Util; 56 import com.sun.appserv.management.base.AMXDebug; 57 import com.sun.appserv.management.base.XTypes; 58 import com.sun.appserv.management.base.Util; 59 60 import com.sun.appserv.management.util.misc.ListUtil; 61 import com.sun.appserv.management.util.misc.CollectionUtil; 62 import com.sun.appserv.management.util.misc.ExceptionUtil; 63 import com.sun.appserv.management.util.misc.StringUtil; 64 import com.sun.appserv.management.util.misc.CollectionUtil; 65 import com.sun.appserv.management.util.jmx.JMXUtil; 66 67 import com.sun.appserv.management.client.ProxyFactory; 68 69 import com.sun.appserv.management.config.ConfigElement; 70 71 import com.sun.enterprise.management.config.AMXConfigImplBase; 72 import com.sun.enterprise.management.support.OldConfigTypes; 73 import com.sun.enterprise.management.support.ObjectNames; 74 import com.sun.enterprise.management.support.BootUtil; 75 import com.sun.enterprise.management.support.Delegate; 76 import com.sun.enterprise.management.support.TypeInfo; 77 import com.sun.enterprise.management.support.TypeInfos; 78 import com.sun.enterprise.management.support.SystemInfoImpl; 79 import com.sun.enterprise.management.support.AMXDebugSupport; 80 81 import com.sun.appserv.management.util.jmx.MBeanProxyHandler; 82 import com.sun.appserv.management.util.jmx.MBeanServerConnectionSource; 83 import com.sun.appserv.management.util.jmx.stringifier.MBeanInfoStringifier; 84 import com.sun.appserv.management.util.jmx.stringifier.NotificationStringifier; 85 86 87 import com.sun.enterprise.config.ConfigBean; 88 import com.sun.enterprise.config.ConfigContext; 89 import com.sun.enterprise.config.ConfigException; 90 91 import com.sun.enterprise.config.serverbeans.ServerXPathHelper; 92 93 96 final class AMXLoader implements NotificationListener 97 { 98 private final MBeanServer mServer; 99 private final String mJMXDomain; 100 101 private final ConfigDelegateFactory mDelegateFactory; 102 103 private ObjectNames mObjectNames; 104 105 private final Map <ObjectName ,ConfigBean> mObjectNameToConfigBean; 106 107 108 public 109 AMXLoader( 110 final MBeanServer server, 111 final ConfigDelegateFactory delegateFactory ) 112 throws Exception 113 { 114 mServer = server; 115 116 new AMXDebugSupport( mServer ); 117 118 mDelegateFactory = delegateFactory; 119 mJMXDomain = BootUtil.getInstance().getAMXJMXDomainName(); 120 121 mObjectNames = ObjectNames.getInstance( getAMXJMXDomainName() ); 122 123 mObjectNameToConfigBean = Collections.synchronizedMap( new HashMap <ObjectName ,ConfigBean>() ); 124 125 loadSystemInfo( server ); 126 127 final MBeanServerNotificationFilter filter = 128 new MBeanServerNotificationFilter (); 129 filter.enableAllObjectNames(); 130 JMXUtil.listenToMBeanServerDelegate( mServer, this, filter, null ); 131 } 132 133 protected void 134 sdebug( Object o ) 135 { 136 debug( o ); 137 System.out.println( "" + o ); 138 } 139 140 public String 141 getAMXJMXDomainName() 142 { 143 return( BootUtil.getInstance().getAMXJMXDomainName() ); 144 } 145 146 private ConfigContext 147 getConfigContext() 148 { 149 return mDelegateFactory.getConfigContext(); 150 } 151 152 private void 153 debug( final Object o ) 154 { 155 AMXDebug.getInstance().getOutput( "AMXLoader" ).println( o ); 156 } 157 158 public void 159 handleNotification( 160 final Notification notifIn, 161 final Object handback) 162 { 163 final String type = notifIn.getType(); 164 165 if ( notifIn instanceof MBeanServerNotification ) 166 { 167 final ObjectName objectName = ((MBeanServerNotification )notifIn).getMBeanName(); 168 169 if ( type.equals( MBeanServerNotification.REGISTRATION_NOTIFICATION ) ) 170 { 171 handleMBeanRegistered( objectName ); 172 } 173 else if ( type.equals( MBeanServerNotification.UNREGISTRATION_NOTIFICATION ) ) 174 { 175 handleMBeanUnregistered( objectName ); 176 } 177 } 178 } 179 180 181 public void 182 handleMBeanRegistered( final ObjectName objectName ) 183 { 184 } 185 186 public void 187 handleMBeanUnregistered( final ObjectName objectName ) 188 { 189 mObjectNameToConfigBean.remove( objectName ); 190 } 191 192 193 protected Class 194 getImplClass( final String j2eeType) 195 { 196 final TypeInfo info = TypeInfos.getInstance().getInfo( j2eeType ); 197 assert( info != null ); 198 199 return( info.getImplClass() ); 200 } 201 202 private static final Class [] DELEGATE_CONSTRUCTOR_SIG = new Class [] 203 { 204 Delegate.class, 205 }; 206 207 protected Object 208 newImpl( 209 final String j2eeType, 210 final Delegate delegate ) 211 throws Exception 212 { 213 Object impl = null; 214 215 final Class implClass = getImplClass( j2eeType ); 216 217 try 218 { 219 Constructor constructor = implClass.getConstructor( DELEGATE_CONSTRUCTOR_SIG ); 220 221 if ( constructor != null ) 222 { 223 impl = constructor.newInstance( new Object [] { delegate } ); 224 } 225 } 226 catch( Exception e ) 227 { 228 final Throwable rootCause = ExceptionUtil.getRootCause( e ); 229 debug( "newImpl: exception creating new impl: " + e + "\n" + 230 ExceptionUtil.getStackTrace( rootCause ) ); 231 throw e; 232 } 233 234 return( impl ); 235 } 236 237 238 private String 239 oldTypeToJ2EEType( final String type ) 240 { 241 return OldConfigTypes.getInstance().oldTypeToJ2EEType( type ); 242 } 243 244 public ObjectName 245 loadAMX( final ConfigBean configBean ) 246 throws Exception 247 { 248 final ConfigBeanHelperFactory factory = 249 ConfigBeanHelperFactory.getInstance( mDelegateFactory.getConfigContext() ); 250 251 final ConfigBeanHelper helper = factory.getHelper( configBean ); 252 253 final String xPath = helper.getXPath(); 254 final List <String []> propsList = 255 helper.getAllObjectNameProps( OldConfigTypes.getIgnoreTypes() ); 256 257 final String [] firstPair = propsList.iterator().next(); 258 final String type = firstPair[ 0 ]; 259 final String foundName = firstPair[ 1 ]; 260 if ( type == null || 261 OldConfigTypes.getIgnoreTypes().contains( type ) ) 262 { 263 return null; 264 } 265 266 final String j2eeType = oldTypeToJ2EEType( type ); 267 final String name = foundName == null ? 268 ObjectNames.getSingletonName( j2eeType ) : foundName; 269 String props = Util.makeRequiredProps( j2eeType, name); 270 272 for( final String [] pair : propsList ) 273 { 274 final String ancestorType = pair[ 0 ]; 275 final String ancestorNameFound = pair[ 1 ]; 276 if ( ! OldConfigTypes.getIgnoreTypes().contains( ancestorType ) ) 277 { 278 final String ancestorJ2EEType = oldTypeToJ2EEType( ancestorType ); 279 final String ancestorName = ancestorNameFound == null ? 280 ObjectNames.getSingletonName( j2eeType ) : ancestorNameFound; 281 final String prop = Util.makeProp( ancestorJ2EEType, ancestorName ); 282 283 props = Util.concatenateProps( props, prop ); 284 } 285 286 } 287 288 290 ObjectName objectName = Util.newObjectName( mJMXDomain, props ); 291 292 final ConfigDelegate delegate = 293 mDelegateFactory.createConfigDelegate( configBean ); 294 295 final AMXConfigImplBase amx = (AMXConfigImplBase)newImpl( j2eeType, delegate ); 296 297 mObjectNameToConfigBean.put( objectName, configBean ); 298 objectName = mServer.registerMBean( amx, objectName ).getObjectName(); 299 300 return objectName; 301 } 302 303 304 305 private Map <String ,Object > 306 getConfigBeanAttributes( final ConfigBean configBean ) 307 { 308 final Map <String ,Object > pairs = new HashMap <String ,Object >(); 309 310 try 311 { 312 final ConfigDelegate delegate = 313 mDelegateFactory.createConfigDelegate( configBean ); 314 315 final MBeanAttributeInfo [] attrInfos = delegate.getMBeanInfo().getAttributes(); 316 for( final MBeanAttributeInfo attrInfo : attrInfos ) 317 { 318 final String name = attrInfo.getName(); 319 Object value = null; 320 try 321 { 322 value = delegate.getAttribute( name ); 323 } 324 catch( AttributeNotFoundException e ) 325 { 326 value = "<NOT FOUND>"; 327 } 328 pairs.put( name, value ); 329 } 330 } 331 catch( Exception e ) 332 { 333 pairs.put( "EXCEPTION", e.getClass().getName() + ": " + e.getMessage() ); 334 } 335 336 return pairs; 337 } 338 339 340 private void 341 addBeanHierarchy( 342 final List <ConfigBean> configBeans, 343 final ConfigBean configBean ) 344 { 345 if ( configBean != null ) 347 { 348 configBeans.add( configBean ); 349 350 final ConfigBean[] children = configBean.getAllChildBeans(); 351 if ( children != null ) 352 { 353 for( final ConfigBean child : children ) 354 { 355 addBeanHierarchy( configBeans, child ); 356 } 357 } 358 } 359 } 360 361 private String 362 configBeansToString( final List <ConfigBean> configBeans ) 363 { 364 final String [] xPaths = new String [ configBeans.size() ]; 365 int i = 0; 366 for( final ConfigBean configBean : configBeans ) 367 { 368 xPaths[ i ] = "" + configBean.getXPath(); 369 ++i; 370 } 371 Arrays.sort( xPaths ); 372 373 final String NEWLINE = System.getProperty( "line.separator" ); 374 return StringUtil.toString( NEWLINE, (Object [])xPaths ); 375 } 376 377 378 private ObjectName 379 configBeanToAMX( final ConfigBean configBean ) 380 { 381 assert( configBean != null ); 382 383 ObjectName objectName = null; 384 try 385 { 386 objectName = loadAMX( configBean ); 387 } 388 catch( Exception e ) 389 { 390 final Throwable rootCause = ExceptionUtil.getRootCause( e ); 391 392 sdebug( "Exception of type " + rootCause.getClass().getName() + 393 " trying to create AMXConfig for " + 394 configBean.getXPath() + 395 ": " + rootCause.getMessage() ); 396 sdebug( ExceptionUtil.getStackTrace( rootCause ) ); 397 } 398 399 return objectName; 400 } 401 402 private List <ObjectName > 403 configBeansToAMX( final List <ConfigBean> configBeans ) 404 { 405 final List <ObjectName > objectNames = new ArrayList <ObjectName >(); 406 for( final ConfigBean configBean : configBeans ) 407 { 408 final ObjectName objectName = configBeanToAMX( configBean ); 409 if ( objectName != null ) 410 { 411 objectNames.add( objectName ); 412 } 413 } 414 415 return objectNames; 416 } 417 418 public void 419 loadAll() 420 throws ConfigException 421 { 422 loadDomainRoot(); 423 424 final ConfigContext configContext = mDelegateFactory.getConfigContext(); 425 assert( configContext != null ); 426 427 final ConfigBean domainConfigBean = 428 configContext.exactLookup( ServerXPathHelper.XPATH_DOMAIN ); 429 assert( domainConfigBean != null ); 430 431 final List <ConfigBean> configBeans = new ArrayList <ConfigBean>(); 432 addBeanHierarchy( configBeans, domainConfigBean ); 433 debug( configBeansToString( configBeans ) ); 434 435 final List <ObjectName > objectNames = configBeansToAMX( configBeans ); 437 438 debug( "loadAll: loaded " + objectNames.size() + "MBeans:" ); 439 debug( CollectionUtil.toString( JMXUtil.objectNamesToStrings( objectNames ), "\n") ); 440 } 441 442 private void 443 loadDomainRoot() 444 { 445 try 446 { 447 final TypeInfo info = 448 TypeInfos.getInstance().getInfo( XTypes.DOMAIN_ROOT ); 449 final Class implClass = info.getImplClass(); 450 451 final ObjectName objectName = mObjectNames.getDomainRootObjectName( ); 452 453 final Object impl = implClass.newInstance(); 454 mServer.registerMBean( impl, objectName ); 455 } 456 catch( Exception e ) 457 { 458 debug( ExceptionUtil.toString( e ) ); 459 throw new Error ( e ); 460 } 461 } 462 463 464 public ObjectName 465 loadSystemInfo( final MBeanServer server ) 466 throws Exception 467 { 468 final BootUtil bootUtil = BootUtil.getInstance(); 469 470 final SystemInfoImpl systemInfo = new SystemInfoImpl( server, bootUtil ); 471 472 final ObjectName tempName = 473 mObjectNames.getSingletonObjectName( systemInfo.J2EE_TYPE ); 474 475 final ObjectName objectName = 476 mServer.registerMBean( systemInfo, tempName ).getObjectName(); 477 debug( "loaded SystemInfo as " + objectName ); 478 return( objectName ); 479 } 480 481 private ProxyFactory 482 getProxyFactory() 483 { 484 return ProxyFactory.getInstance( mServer ); 485 } 486 487 public DomainRoot 488 getDomainRoot() 489 { 490 return( getProxyFactory().getDomainRoot() ); 491 } 492 } 493 494 495 496 497 498 499 500 501 | Popular Tags |