| 352 | } |
| 353 | |
| 354 | protected static Object[] doBeans(final MBeanServerConnection mbsc, final ObjectName objName, |
| 355 | final String[] command, final boolean oneBeanOnly) throws Exception { |
| 356 | Object[] result = null; |
| 357 | Set beans = mbsc.queryMBeans(objName, null); |
| 358 | if (beans.isEmpty()) { |
| 359 | // No bean found. Check if we are to create a bean? |
| 360 | if (command.length == 1 && notEmpty(command[0]) && command[0].startsWith(CREATE_CMD_PREFIX)) { |
| 361 | String className = command[0].substring(CREATE_CMD_PREFIX.length()); |
| 362 | mbsc.createMBean(className, objName); |
| 363 | } else { |
| 364 | // TODO: Is there a better JMX exception that RE for this |
| 365 | // scenario? |
| 366 | throw new RuntimeException(objName.getCanonicalName() + " not registered."); |
| 367 | } |
| 368 | } else if (beans.size() == 1) { |
| 369 | result = doBean(mbsc, (ObjectInstance) beans.iterator().next(), command); |
| 370 | } else { |
| 371 | if (oneBeanOnly) { |
| 372 | throw new RuntimeException("Only supposed to be one bean " + "query result"); |
| 373 | } |
| 374 | // This is case of multiple beans in query results. |
| 375 | // Print name of each into a StringBuffer. Return as one |
| 376 | // result. |
| 377 | StringBuffer buffer = new StringBuffer(); |
| 378 | for (Iterator i = beans.iterator(); i.hasNext();) { |
| 379 | Object obj = i.next(); |
| 380 | if (obj instanceof ObjectName) { |
| 381 | buffer.append((((ObjectName) obj).getCanonicalName())); |
| 382 | } else if (obj instanceof ObjectInstance) { |
| 383 | buffer.append((((ObjectInstance) obj).getObjectName().getCanonicalName())); |
| 384 | } else { |
| 385 | throw new RuntimeException("Unexpected object type: " + obj); |
| 386 | } |
| 387 | buffer.append("\n"); |
| 388 | } |
| 389 | result = new String[] { buffer.toString() }; |
| 390 | } |
| 391 | return result; |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Get attribute or run operation against passed bean <code>instance</code>. |