/**
  * Return availability of this resource
  *
  * @see org.rhq.core.pluginapi.inventory.ResourceComponent#getAvailability()
  */
 @Override
 public AvailabilityType getAvailability() {
   boolean trace = log.isTraceEnabled();
   EmsConnection conn = getConnection();
   try {
     conn.refresh();
     EmsBean bean = queryCacheBean();
     if (bean != null && bean.getAttribute("CacheStatus").getValue().equals("RUNNING")) {
       bean.refreshAttributes();
       if (trace)
         log.trace(
             "Cache "
                 + cacheName
                 + " within "
                 + cacheManagerName
                 + " cache manager is running and attributes could be refreshed, so it's up.");
       return AvailabilityType.UP;
     }
     if (trace)
       log.trace(
           "Cache status for "
               + cacheName
               + " within "
               + cacheManagerName
               + " cache manager is anything other than running, so it's down.");
     return AvailabilityType.DOWN;
   } catch (Exception e) {
     if (trace)
       log.trace("There was an exception checking availability, so cache status is down.", e);
     return AvailabilityType.DOWN;
   }
 }
  private Set<DiscoveredResourceDetails> createDiscoveredResource(
      ResourceDiscoveryContext<JMXComponent<?>> ctx,
      Configuration pluginConfiguration,
      String objectName) {
    boolean trace = log.isTraceEnabled();
    JMXComponent<?> parentComponent = ctx.getParentResourceComponent();
    EmsConnection conn = parentComponent.getEmsConnection();
    if (conn != null) {
      if (trace) log.trace("Connection to ems server established: " + conn);

      // Run query for manager_object
      ObjectNameQueryUtility queryUtility = new ObjectNameQueryUtility(objectName);
      List<EmsBean> beans = conn.queryBeans(queryUtility.getTranslatedQuery());
      if (trace)
        log.trace("Querying [" + queryUtility.getTranslatedQuery() + "] returned beans: " + beans);

      Set<DiscoveredResourceDetails> discoveredResources = new HashSet<DiscoveredResourceDetails>();
      for (EmsBean bean : beans) {
        // Filter out spurious beans
        if (CacheManagerComponent.isCacheManagerComponent(bean)) {
          String managerName = bean.getBeanName().getCanonicalName();
          String resourceName = bean.getAttribute("Name").getValue().toString();
          String version = bean.getAttribute("Version").getValue().toString();
          /* A discovered resource must have a unique key, that must stay the same when the resource is discovered the next time */
          if (trace)
            log.trace(
                "Add resource with version '" + version + "' and type " + ctx.getResourceType());
          DiscoveredResourceDetails detail =
              new DiscoveredResourceDetails(
                  ctx.getResourceType(), // Resource type
                  resourceName, // Resource key
                  resourceName, // Resource name
                  version, // Resource version
                  "A cache manager within Infinispan", // Description
                  pluginConfiguration, // Plugin config
                  null // Process info from a process scan
                  );
          if (log.isInfoEnabled()) {
            log.info(
                String.format(
                    "Discovered Infinispan instance with key %s and name %s",
                    resourceName, managerName));
          }
          discoveredResources.add(detail);
        } else {
          log.warn(
              String.format(
                  "MBeanServer returned spurious object %s",
                  bean.getBeanName().getCanonicalName()));
        }
      }
      return discoveredResources;
    } else {
      log.debug("Unable to establish connection");
      return null;
    }
  }
 /**
  * Gather measurement data
  *
  * @see
  *     org.rhq.core.pluginapi.measurement.MeasurementFacet#getValues(org.rhq.core.domain.measurement.MeasurementReport,
  *     java.util.Set)
  */
 @Override
 public void getValues(MeasurementReport report, Set<MeasurementScheduleRequest> metrics) {
   boolean trace = log.isTraceEnabled();
   if (trace) log.trace("Get values metrics");
   for (MeasurementScheduleRequest req : metrics) {
     if (trace) log.trace("Inspect metric " + req);
     String metric = req.getName();
     try {
       EmsBean bean = queryComponentBean(metric);
       if (bean != null) {
         if (trace) log.trace("Retrieved mbean with name " + bean.getBeanName());
         bean.refreshAttributes();
         String attName = metric.substring(metric.indexOf(".") + 1);
         EmsAttribute att = bean.getAttribute(attName);
         // Attribute values are of various data types
         if (att != null) {
           Object o = att.getValue();
           Class<?> attrType = att.getTypeClass();
           DataType type = req.getDataType();
           if (type == DataType.MEASUREMENT) {
             if (o != null) {
               MeasurementDataNumeric res = constructNumericMeasure(attrType, o, req);
               if (res != null) report.addData(res);
             } else {
               if (log.isDebugEnabled())
                 log.debug("Metric (" + req.getName() + ") has null value, do not add to report");
             }
           } else if (type == DataType.TRAIT) {
             String value = (String) o;
             if (trace) log.trace("Metric (" + req.getName() + ") is trait with value " + value);
             MeasurementDataTrait res = new MeasurementDataTrait(req, value);
             report.addData(res);
           }
         } else {
           if (log.isWarnEnabled()) {
             log.warn("Attribute " + attName + " not found");
           }
         }
       }
     } catch (Exception e) {
       if (log.isWarnEnabled()) {
         log.warn("getValues failed for " + metric, e);
       }
     }
   }
 }
  /**
   * Invoke operations on the Cache MBean instance
   *
   * @param fullOpName Name of the operation
   * @param parameters Parameters of the Operation
   * @return OperationResult object if successful
   * @throws Exception If operation was not successful
   */
  @Override
  public OperationResult invokeOperation(String fullOpName, Configuration parameters)
      throws Exception {
    boolean trace = log.isTraceEnabled();
    EmsBean bean = queryComponentBean(fullOpName);
    String opName = fullOpName.substring(fullOpName.indexOf(".") + 1);
    EmsOperation ops = bean.getOperation(opName);
    Collection<PropertySimple> simples = parameters.getSimpleProperties().values();
    if (trace) log.trace("Parameters, as simple properties, are " + simples);
    Object[] realParams = new Object[simples.size()];
    int i = 0;
    for (PropertySimple property : simples) {
      // Since parameters are typed in UI, passing them as Strings is the only reasonable way of
      // dealing with this
      realParams[i++] = property.getStringValue();
    }

    if (ops == null) throw new Exception("Operation " + fullOpName + " can't be found");

    Object result = ops.invoke(realParams);
    if (trace) log.trace("Returning operation result containing " + result.toString());
    return new OperationResult(result.toString());
  }