1 23 package com.sun.enterprise.management.support; 24 25 import java.util.Set ; 26 import java.util.Collections ; 27 import java.util.List ; 28 import java.util.ArrayList ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.io.IOException ; 32 33 34 import javax.management.ObjectName ; 35 import javax.management.MBeanInfo ; 36 import javax.management.JMException ; 37 38 import javax.management.MBeanServer ; 39 import javax.management.MBeanServerConnection ; 40 import javax.management.AttributeNotFoundException ; 41 import javax.management.InstanceNotFoundException ; 42 import javax.management.ReflectionException ; 43 import javax.management.MBeanException ; 44 45 import com.sun.appserv.management.base.AMX; 46 import com.sun.appserv.management.base.AMXAttributes; 47 import com.sun.appserv.management.base.Util; 48 import com.sun.appserv.management.base.QueryMgr; 49 import com.sun.appserv.management.base.XTypesMapper; 50 import com.sun.appserv.management.base.XTypes; 51 import com.sun.appserv.management.j2ee.J2EETypes; 52 import com.sun.appserv.management.j2ee.J2EETypesMapper; 53 import com.sun.appserv.management.base.QueryMgr; 54 import com.sun.enterprise.management.support.ObjectNames; 55 56 import com.sun.appserv.management.util.jmx.ObjectNameQueryImpl; 57 import com.sun.appserv.management.util.jmx.JMXUtil; 58 import com.sun.appserv.management.util.misc.RegexUtil; 59 import com.sun.appserv.management.util.misc.ArrayConversion; 60 61 import com.sun.appserv.management.util.misc.ClassUtil; 62 import com.sun.appserv.management.util.misc.GSetUtil; 63 import com.sun.appserv.management.util.misc.ListUtil; 64 import com.sun.appserv.management.util.stringifier.SmartStringifier; 65 66 67 69 public class QueryMgrImpl extends AMXNonConfigImplBase 70 { 72 private static J2EETypesMapper sJ2EETypesMapper = null; 73 74 public 75 QueryMgrImpl() 76 { 77 } 78 79 public final String 80 getGroup() 81 { 82 return( AMX.GROUP_UTILITY ); 83 } 84 85 private static Set <ObjectName > 86 checked( final Set <ObjectName > s ) 87 { 88 return Collections.checkedSet(s, ObjectName .class); 89 } 90 91 93 public static Set <ObjectName > 94 queryPropsObjectNameSet( 95 final MBeanServerConnection conn, 96 final String jmxDomainName, 97 final String props ) 98 throws IOException 99 { 100 final ObjectName pattern = 101 Util.newObjectNamePattern( jmxDomainName, props ); 102 103 final Set <ObjectName > names = JMXUtil.queryNames( conn, pattern, null ); 104 105 return( checked(names) ); 106 } 107 108 110 public static Set <ObjectName > 111 queryPropsObjectNameSet( 112 final MBeanServer server, 113 final String jmxDomainName, 114 final String props ) 115 { 116 final ObjectName pattern = 117 Util.newObjectNamePattern( jmxDomainName, props ); 118 119 final Set <ObjectName > names = JMXUtil.queryNames( server, pattern, null ); 120 121 return( checked(names) ); 122 } 123 124 public Set <ObjectName > 125 queryPropsObjectNameSet( final String props ) 126 { 127 final String myDomain = getObjectName().getDomain(); 128 129 final Set <ObjectName > names = 130 queryPropsObjectNameSet( getMBeanServer(), myDomain, props ); 131 132 return( checked(names) ); 133 } 134 135 public ObjectName 136 querySingletonJ2EETypeObjectName( final String j2eeType ) 137 { 138 return( querySingletonJ2EETypeObjectName( getMBeanServer(), getObjectName().getDomain(), j2eeType ) ); 139 } 140 141 public static ObjectName 142 querySingletonJ2EETypeObjectName( 143 final MBeanServer server, 144 final String domainName, 145 final String j2eeTypeValue ) 146 { 147 try 148 { 149 final ObjectName objectName = 150 querySingletonJ2EETypeObjectName( 151 (MBeanServerConnection )server, 152 domainName, 153 j2eeTypeValue ); 154 return( objectName ); 155 } 156 catch( IOException e ) 157 { 158 assert( false ); 159 throw new RuntimeException ( e ); 160 } 161 } 162 163 public static ObjectName 164 querySingletonJ2EETypeObjectName( 165 final MBeanServerConnection conn, 166 final String domainName, 167 final String j2eeTypeValue ) 168 throws IOException 169 { 170 final Set <ObjectName > names = 171 queryJ2EETypeObjectNameSet( conn, domainName, j2eeTypeValue ); 172 if ( names.size() != 1 ) 173 { 174 throw new IllegalArgumentException ( 175 "request was for a single Object name of type: " + j2eeTypeValue + 176 " but found several: " + SmartStringifier.toString( names ) ); 177 } 178 179 final ObjectName objectName = (ObjectName )GSetUtil.getSingleton( names ); 180 181 return( objectName ); 182 } 183 184 185 187 public static Set <ObjectName > 188 queryJ2EETypeObjectNameSet( 189 final MBeanServerConnection conn, 190 final String domainName, 191 final String j2eeTypeValue ) 192 throws IOException 193 { 194 final String prop = Util.makeJ2EETypeProp( j2eeTypeValue ) ; 195 final ObjectName pat = Util.newObjectNamePattern( domainName, prop ); 196 197 final Set <ObjectName > names = queryPatternObjectNameSet( conn, pat ); 198 199 return( checked(names) ); 200 } 201 202 204 public Set <ObjectName > 205 queryJ2EETypesObjectNameSet( final Set <String > j2eeTypes ) 206 throws IOException 207 { 208 final String [] values = GSetUtil.toStringArray( j2eeTypes ); 209 final String [] keys = new String [ j2eeTypes.size() ]; 210 211 for( int i = 0; i < keys.length; ++i ) 212 { 213 keys[ i ] = AMX.J2EE_TYPE_KEY; 214 } 215 216 return queryWildObjectNameSet( keys, values ); 217 } 218 219 221 222 229 public ObjectName 230 querySingletonJ2EEType( final String j2eeTypeValue ) 231 { 232 final Set <ObjectName > names = queryJ2EETypeObjectNameSet( j2eeTypeValue ); 233 if ( names.size() > 1 ) 234 { 235 trace( "QueryMgrImpl.getJ2EETypeObjectName: expected 1, got " + names.size() ); 236 throw new IllegalArgumentException ( j2eeTypeValue ); 237 } 238 239 final ObjectName objectName = names.size() == 0 ? 240 null :GSetUtil.getSingleton( names ); 241 242 return( objectName ); 243 } 244 245 247 public Set <ObjectName > 248 queryJ2EETypeObjectNameSet( final String j2eeTypeValue ) 249 { 250 final String prop = Util.makeJ2EETypeProp( j2eeTypeValue ) ; 251 final ObjectName pat = Util.newObjectNamePattern( getJMXDomain(), prop ); 252 253 final Set <ObjectName > names = queryPropsObjectNameSet( getMBeanServer(), 254 getJMXDomain(), prop ); 255 256 return( names ); 257 } 258 259 260 265 public Set <ObjectName > 266 queryJ2EENameObjectNameSet( final String nameValue ) 267 { 268 final String prop = Util.makeNameProp( nameValue ); 269 final Set <ObjectName > names = queryPropsObjectNameSet( getMBeanServer(), 270 getJMXDomain(), prop ); 271 272 return( names ); 273 } 274 275 276 278 public static Set <ObjectName > 279 queryPatternObjectNameSet( 280 final MBeanServerConnection conn, 281 final ObjectName pattern ) 282 throws IOException 283 { 284 final Set <ObjectName > names = JMXUtil.queryNames( conn, pattern, null ); 285 286 return checked(names); 287 } 288 289 290 292 public static Set <ObjectName > 293 queryPatternObjectNameSet( 294 final MBeanServer server, 295 final ObjectName pattern ) 296 { 297 final Set <ObjectName > names = JMXUtil.queryNames( server, pattern, null ); 298 299 return checked(names); 300 } 301 303 public Set <ObjectName > 304 queryPatternObjectNameSet( final ObjectName pattern ) 305 { 306 return( queryPatternObjectNameSet( getMBeanServer(), pattern ) ); 307 } 308 309 311 public Set <ObjectName > 312 queryPatternObjectNameSet( String domain, String props ) 313 { 314 final String myDomain = getObjectName().getDomain(); 315 316 final ObjectName pattern = Util.newObjectNamePattern( 317 domain == null ? myDomain : domain, props ); 318 return( queryPatternObjectNameSet( getMBeanServer(), pattern ) ); 319 } 320 321 322 324 public String [] 325 queryJ2EETypeNames( final String j2eeType ) 326 { 327 final String prop = Util.makeJ2EETypeProp( j2eeType ); 328 329 final Set <ObjectName > objectNameSet = queryPropsObjectNameSet( prop ); 330 final ObjectName [] objectNames = JMXUtil.objectNameSetToArray( objectNameSet ); 331 332 final String [] nameKeyValues = 333 JMXUtil.getKeyProperty( NAME_KEY, objectNames ); 334 335 return( nameKeyValues ); 336 } 337 338 339 342 public Set <ObjectName > 343 queryJ2EETypeNameObjectNameSet( 344 final String j2eeType, 345 final String name ) 346 { 347 final String props = Util.makeRequiredProps( j2eeType, name ); 348 return queryPatternObjectNameSet( Util.newObjectNamePattern( getObjectName().getDomain(), props ) ); 349 } 350 351 352 private static String [] 353 convertToRegex( String [] wildExprs ) 354 { 355 String [] regexExprs = null; 356 357 if ( wildExprs != null ) 358 { 359 regexExprs = new String [ wildExprs.length ]; 360 361 for( int i = 0; i < wildExprs.length; ++i ) 362 { 363 final String expr = wildExprs[ i ]; 364 365 final String regex = expr == null ? 366 null : RegexUtil.wildcardToJavaRegex( expr ); 367 368 regexExprs[ i ] = regex; 369 } 370 } 371 return( regexExprs ); 372 } 373 374 376 private Set <ObjectName > 377 matchWild( 378 final Set <ObjectName > candidates, 379 final String [] wildKeys, 380 final String [] wildValues) 381 { 382 final String [] regexNames = convertToRegex( wildKeys ); 383 final String [] regexValues = convertToRegex( wildValues ); 384 385 final ObjectNameQueryImpl query = new ObjectNameQueryImpl(); 386 final Set <ObjectName > resultSet = query.matchAll( candidates, regexNames, regexValues ); 387 388 return( resultSet ); 389 } 390 391 392 394 public Set <ObjectName > 395 queryWildObjectNameSet( 396 final String [] wildKeys, 397 final String [] wildValues ) 398 { 399 final Set <ObjectName > candidates = queryAllObjectNameSet(); 400 401 return( matchWild( candidates, wildKeys, wildValues ) ); 402 } 403 404 405 410 public Set <ObjectName > 411 queryAllObjectNameSet() 412 { 413 final ObjectName pat = Util.newObjectNamePattern( getJMXDomain(), "" ); 414 415 return( queryPatternObjectNameSet( pat ) ); 416 } 417 418 425 private Set <ObjectName > 426 queryInterfaceObjectNameSet( 427 final Set <ObjectName > objectNames, 428 final String interfaceName, 429 final String attributeName) 430 { 431 final Set <ObjectName > result = new HashSet <ObjectName >(); 432 433 final MBeanServer server = getMBeanServer(); 434 for( final ObjectName objectName : objectNames ) 435 { 436 try 437 { 438 if ( server.getAttribute( objectName, attributeName ).equals( interfaceName ) ) 439 { 440 result.add( objectName ); 441 } 442 } 443 catch( JMException e ) 444 { 445 } 447 } 448 449 return checked(result); 450 } 451 452 453 455 public Set <ObjectName > 456 queryInterfaceObjectNameSet( 457 final String searchInterfaceName, 458 final Set <ObjectName > candidates ) 459 throws ClassNotFoundException 460 { 461 final Set <ObjectName > result = new HashSet <ObjectName >(); 462 final Set <ObjectName > iter = candidates == null ? 463 queryAllObjectNameSet() : candidates; 464 465 final Class <?> searchInterface = ClassUtil.getClassFromName( searchInterfaceName ); 466 467 final MBeanServer server = getMBeanServer(); 468 for( final ObjectName objectName : iter ) 469 { 470 try 471 { 472 final String interfaceName = (String ) 473 server.getAttribute( objectName, AMXAttributes.ATTR_INTERFACE_NAME ); 474 475 final Class c = 476 ClassUtil.getClassFromName( interfaceName ); 477 478 if ( searchInterface.isAssignableFrom( c ) ) 479 { 480 result.add( objectName ); 481 } 482 } 483 catch( JMException e ) 484 { 485 } 487 } 488 489 return( checked(result) ); 490 } 491 492 493 494 497 private static synchronized Class 498 interfaceForJ2EEType( final String j2eeType ) 499 { 500 Class theClass = XTypesMapper.getInstance().getInterfaceForType( j2eeType ); 501 502 if ( theClass == null ) 503 { 504 theClass = J2EETypesMapper.getInstance().getInterfaceForType( j2eeType ); 505 } 506 507 return( theClass ); 508 } 509 510 511 } 512 513 514 515 516 517 518 519 520 521 522 523 | Popular Tags |