예제 #1
0
  /**
   * 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");
  }
예제 #2
0
 /** @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);
 }