/** * Get full MBean name with given bean name, domain and session * * @param bean Name of bean. It can be NULL so that session#getBean() is returned * @param domain Domain for bean * @param session Current session * @return Full qualified name of MBean * @throws JMException Thrown when given MBean name is malformed * @throws IOException */ public static String getBeanName(String bean, String domain, Session session) throws JMException, IOException { Validate.notNull(session, "Session can't be NULL"); if (bean == null) { return session.getBean(); } if (SyntaxUtils.isNull(bean)) { return null; } MBeanServerConnection con = session.getConnection().getServerConnection(); if (bean.indexOf(':') != -1) { try { ObjectName name = new ObjectName(bean); con.getMBeanInfo(name); return bean; } catch (MalformedObjectNameException e) { } catch (InstanceNotFoundException e) { } } String domainName = DomainCommand.getDomainName(domain, session); if (domainName == null) { throw new IllegalArgumentException( "Please specify domain using either -d option or domain command"); } try { ObjectName name = new ObjectName(domainName + ":" + bean); con.getMBeanInfo(name); return domainName + ":" + bean; } catch (MalformedObjectNameException e) { } catch (InstanceNotFoundException e) { } throw new IllegalArgumentException("Bean name " + bean + " isn't valid"); }
/** * Get list of candidate beans * * @param session Session * @return List of bean names * @throws MalformedObjectNameException * @throws IOException */ static List<String> getCandidateBeanNames(Session session) throws MalformedObjectNameException { try { ArrayList<String> results = new ArrayList<String>(BeansCommand.getBeans(session, null)); String domain = session.getDomain(); if (domain != null) { List<String> beans = BeansCommand.getBeans(session, domain); for (String bean : beans) { results.add(bean.substring(domain.length() + 1)); } } return results; } catch (IOException e) { throw new RuntimeIOException("Couldn't find candidate bean names", e); } }
/** @inheritDoc */ @Override public void execute() throws IOException, JMException { Session session = getSession(); if (bean == null) { if (session.getBean() == null) { session.output.println(SyntaxUtils.NULL); } else { session.output.println(session.getBean()); } return; } String beanName = getBeanName(bean, domain, session); if (beanName == null) { session.setBean(null); session.output.printMessage("bean is unset"); return; } ObjectName name = new ObjectName(beanName); MBeanServerConnection con = session.getConnection().getServerConnection(); con.getMBeanInfo(name); session.setBean(beanName); session.output.printMessage("bean is set to " + beanName); }