1 23 24 28 29 31 32 package com.sun.enterprise.management.support; 33 34 import java.util.Set ; 35 import java.util.List ; 36 import java.util.Collections ; 37 import java.util.Map ; 38 import java.util.Iterator ; 39 40 import java.lang.reflect.Field ; 41 42 import javax.management.ObjectName ; 43 import javax.management.InstanceNotFoundException ; 44 import javax.management.MalformedObjectNameException ; 45 import javax.management.MBeanServer ; 46 47 import com.sun.appserv.management.base.AMX; 48 import com.sun.appserv.management.base.AMXAttributes; 49 import com.sun.appserv.management.base.AMXDebug; 50 51 import com.sun.appserv.management.base.Util; 52 import com.sun.appserv.management.DomainRoot; 53 import com.sun.appserv.management.base.QueryMgr; 54 import com.sun.appserv.management.deploy.DeploymentMgr; 55 import com.sun.appserv.management.base.XTypes; 56 import com.sun.appserv.management.config.*; 57 import com.sun.appserv.management.monitor.*; 58 import com.sun.appserv.management.j2ee.J2EETypes; 59 60 import com.sun.appserv.management.util.misc.ListUtil; 61 import com.sun.appserv.management.util.jmx.JMXUtil; 62 import com.sun.appserv.management.base.AllTypesMapper; 63 64 import com.sun.appserv.management.util.misc.ClassUtil; 65 import com.sun.appserv.management.util.misc.GSetUtil; 66 import com.sun.appserv.management.util.stringifier.SmartStringifier; 67 68 79 public final class ObjectNames 80 { 81 private final String mJMXDomain; 82 83 private 84 ObjectNames( final String jmxDomain ) 85 { 86 mJMXDomain = jmxDomain; 87 } 88 89 private void 90 debug( final Object o ) 91 { 92 AMXDebug.getInstance().getOutput( 93 "com.sun.enterprise.management.support.ObjectNames" ).println( o ); 94 } 95 96 public static ObjectNames 97 getInstance( final String jmxDomain ) 98 { 99 return( new ObjectNames( jmxDomain ) ); 100 } 101 102 103 public String 104 getJMXDomain() 105 { 106 return( mJMXDomain ); 107 } 108 109 private static final String [] EMPTY_STRING_ARRAY = new String [ 0 ]; 110 111 112 114 public static String 115 getJ2EEType( Class theInterface ) 116 { 117 return( (String )ClassUtil.getFieldValue( theInterface, "J2EE_TYPE" ) ); 118 } 119 120 121 124 private ObjectName 125 newObjectName( String props ) 126 { 127 return( Util.newObjectName( getJMXDomain(), props ) ); 128 } 129 130 public static String 131 makeWild( String props ) 132 { 133 return( Util.concatenateProps( props, JMXUtil.WILD_PROP ) ); 134 } 135 136 147 public ObjectName 148 buildContaineeObjectName( 149 final ObjectName parentObjectName, 150 final String parentFullType, 151 final String childJ2EEType, 152 final String childName ) 153 { 154 final String domain = parentObjectName.getDomain(); 155 156 String props = ""; 157 158 final TypeInfo info = TypeInfos.getInstance().getInfo( childJ2EEType ); 159 if ( info.isSubType() ) 160 { 161 final String parentProp = Util.getSelfProp( parentObjectName ); 163 164 final String [] parentFullTypes = Util.getTypeArray( parentFullType ); 166 final Set <String > ancestorKeys = GSetUtil.newSet( parentFullTypes, 0, parentFullTypes.length - 1 ); 167 final String ancestorProps = JMXUtil.getProps( parentObjectName, ancestorKeys, true ); 168 props = Util.concatenateProps( parentProp, ancestorProps ); 169 } 170 else 171 { 172 } 174 175 176 final String requiredProps = Util.makeRequiredProps( childJ2EEType, childName ); 177 final String allProps = Util.concatenateProps( requiredProps, props ); 178 179 return( Util.newObjectName( domain, allProps ) ); 180 } 181 182 public ObjectName 183 buildContaineeObjectName( 184 final ObjectName parentObjectName, 185 final String parentFullType, 186 final String childJ2EEType ) 187 { 188 final String name = getSingletonName( childJ2EEType ); 189 190 return( buildContaineeObjectName( parentObjectName, parentFullType, childJ2EEType, name ) ); 191 } 192 193 public ObjectName 194 getDomainRootObjectName() 195 { 196 return( newObjectName( Util.makeRequiredProps( XTypes.DOMAIN_ROOT, getJMXDomain() ) ) ); 197 } 198 199 202 public ObjectName 203 getSingletonObjectName( final String j2eeType ) 204 { 205 final TypeInfo info = TypeInfos.getInstance().getInfo( j2eeType ); 206 if ( info.isSubType() ) 207 { 208 throw new IllegalArgumentException ( "singletons may not be sub-types: " + j2eeType ); 209 } 210 211 final String props = Util.makeRequiredProps( j2eeType, getSingletonName( j2eeType ) ); 212 213 return( newObjectName( props ) ); 214 } 215 216 217 218 229 public static String 230 getSingletonName( String j2eeType ) 231 { 232 if ( TypeInfos.getInstance().getInfo( j2eeType ) == null ) 233 { 234 throw new IllegalArgumentException ( j2eeType ); 235 } 236 237 assert( ! AMX.NO_NAME.equals( MISSING_PARENT_NAME ) ); 238 return( AMX.NO_NAME ); 239 } 240 241 246 public static final String MISSING_PARENT_NAME = AMX.NULL_NAME; 247 248 249 250 258 public ObjectName 259 getContainerObjectNamePattern( 260 final ObjectName childObjectName, 261 final String childFullType ) 262 { 263 ObjectName parentPattern = null; 264 265 final String domain = childObjectName.getDomain(); 266 267 final String [] fullType = Util.getTypeArray( childFullType ); 268 assert( fullType.length >= 1 ); 269 270 final String childType = fullType[ fullType.length - 1 ]; 271 272 if ( fullType.length == 1 ) 273 { 274 final TypeInfo info = TypeInfos.getInstance().getInfo( childType ); 275 276 final String containedByJ2EEType = info.getContainedByJ2EEType(); 277 if ( containedByJ2EEType != null ) 278 { 279 String parentProps = ""; 280 281 if ( containedByJ2EEType.equals( XTypes.DOMAIN_ROOT ) || 284 containedByJ2EEType.equals( J2EETypes.J2EE_DOMAIN ) ) 285 { 286 parentProps = Util.makeRequiredProps( containedByJ2EEType, domain ); 287 } 288 else 289 { 290 parentProps = Util.makeRequiredProps( containedByJ2EEType, 291 getSingletonName( containedByJ2EEType ) ); 292 } 293 294 parentPattern = Util.newObjectNamePattern( domain, parentProps ); 295 } 296 else 297 { 298 parentPattern = null; } 300 } 301 else 302 { 303 314 String parentJ2EEType = null; 315 String parentName = null; 316 Set <String > remainingKeys = Collections.emptySet(); 317 for( int i = fullType.length - 2; i >= 0; --i ) 318 { 319 final String tempType = fullType[ i ]; 320 final String tempName = childObjectName.getKeyProperty( tempType ); 321 assert( tempName != null ) : "missing ObjectName property: " + tempType; 322 if ( ! MISSING_PARENT_NAME.equals( tempName ) ) 323 { 324 parentJ2EEType = tempType; 325 parentName = tempName; 326 final int numItems = i; 327 remainingKeys = GSetUtil.newSet( fullType, 0, numItems ); 328 break; 329 } 330 } 331 339 final String parentProps = Util.makeRequiredProps( parentJ2EEType, parentName ); 340 final String ancestorProps = JMXUtil.getProps( childObjectName, remainingKeys ); 341 342 final String props = Util.concatenateProps( parentProps, ancestorProps ); 343 344 parentPattern = Util.newObjectNamePattern( domain, props ); 345 } 346 347 return( parentPattern ); 348 } 349 350 351 private static String 352 getFullType( final MBeanServer server, final ObjectName objectName ) 353 { 354 try 355 { 356 final String fullType = (String ) 357 server.getAttribute( objectName, AMXAttributes.ATTR_FULL_TYPE ); 358 359 return( fullType ); 360 } 361 catch( Exception e ) 362 { 363 throw new RuntimeException ( e ); 364 } 365 } 366 367 374 public ObjectName 375 getContainerObjectName( 376 final MBeanServer server, 377 final ObjectName containedObjectName ) 378 throws InstanceNotFoundException 379 { 380 debug( "getContainerObjectName: containedObjectName = " + containedObjectName ); 381 382 final String containedFullType = getFullType( server, containedObjectName ); 383 384 debug( "getContainerObjectName: containedFullType = " + containedFullType ); 385 386 ObjectName parentPattern = getContainerObjectNamePattern( containedObjectName, containedFullType ); 387 ObjectName containingObjectName = null; 388 389 if ( parentPattern != null ) 390 { 391 debug( "getContainerObjectName: parentPattern = " + parentPattern ); 392 final Set <ObjectName > names = JMXUtil.queryNames( server, parentPattern, null ); 393 394 if ( names.size() == 0 ) 395 { 396 throw new InstanceNotFoundException ( parentPattern.toString() ); 397 } 398 399 containingObjectName = (ObjectName )GSetUtil.getSingleton( names ); 400 } 401 else 402 { 403 final String j2eeType = Util.getJ2EEType( containedObjectName ); 404 405 if ( ! j2eeType.equals( XTypes.DOMAIN_ROOT ) ) 406 { 407 throw new IllegalArgumentException ( containedObjectName.toString() ); 408 } 409 } 410 411 return( containingObjectName ); 412 } 413 414 415 416 417 private static String 418 toString( final Object o ) 419 { 420 return( SmartStringifier.toString( o ) ); 421 } 422 } 423 424 425 426 427 428 429 430 431 432 | Popular Tags |