예제 #1
0
  /**
   * Gets the stack trace elements.
   *
   * @param stackTrace The stack trace in composite data array
   * @return The stack trace elements
   */
  private StackTraceElement[] getStackTrace(CompositeData[] stackTrace) {
    List<StackTraceElement> list = new ArrayList<StackTraceElement>();
    for (CompositeData compositeData : stackTrace) {
      Object className = compositeData.get(CLASS_NAME);
      Object fileName = compositeData.get(FILE_NAME);
      Object lineNumber = compositeData.get(LINE_NUMBER);
      Object metohdName = compositeData.get(METHOD_NAME);
      Object nativeMethod = compositeData.get(NATIVE_METHOD);

      if ((className instanceof String)
          && (className instanceof String)
          && (fileName instanceof String)
          && (lineNumber instanceof Integer)
          && (metohdName instanceof String)
          && (nativeMethod instanceof Boolean)) {
        list.add(
            new StackTraceElement(
                (String) className,
                (String) metohdName,
                (String) fileName,
                (Boolean) nativeMethod ? -2 : (Integer) lineNumber));
      }
    }
    return list.toArray(new StackTraceElement[list.size()]);
  }
  @Test
  public void testProperPropertyValueTypeExpressionConverter() throws Exception {
    ModelNode description = createDescription(ModelType.PROPERTY, ModelType.INT);
    TypeConverter converter =
        TypeConverters.createLegacyTypeConverters(true).getConverter(description);

    CompositeType type = assertCast(CompositeType.class, converter.getOpenType());
    Set<String> keys = type.keySet();
    Assert.assertEquals(2, keys.size());
    assertCompositeType(type, "name", String.class.getName(), "The property name");
    assertCompositeType(type, "value", Integer.class.getName(), "The property value");

    CompositeData data =
        assertCast(
            CompositeData.class,
            converter.fromModelNode(
                new ModelNode().setExpression("one", "${this.should.not.exist.!!!!!:1}")));
    Assert.assertEquals(type, data.getCompositeType());
    Assert.assertEquals("one", data.get("name"));
    Assert.assertEquals(1, data.get("value"));

    data =
        new CompositeDataSupport(
            type, new String[] {"name", "value"}, new Object[] {"two", Integer.valueOf(2)});
    ModelNode newNode = converter.toModelNode(data);
    Assert.assertEquals(ModelType.PROPERTY, newNode.getType());
    Assert.assertEquals(new ModelNode().set("two", 2), newNode);

    assertToArray(converter, data);
  }
예제 #3
0
  /**
   * Gets the SWT resource elements.
   *
   * @param resourceComposites The resources in composite data array
   * @return The SWT resource elements
   */
  private Collection<ISWTResourceElement> getSWTResourceElements(
      CompositeData[] resourceComposites) {
    Map<String, ISWTResourceElement> newResourceElements =
        new HashMap<String, ISWTResourceElement>();
    for (CompositeData compositeData : resourceComposites) {
      Object name = compositeData.get(NAME);
      if (!(name instanceof String)) {
        continue;
      }

      ISWTResourceElement element = resourceElements.get(name);
      if (element == null) {
        Object stackTraceElements = compositeData.get(STACK_TRACE);
        if (!(stackTraceElements instanceof CompositeData[])) {
          continue;
        }
        element =
            new SWTResourceElement(
                (String) name, getStackTrace((CompositeData[]) stackTraceElements));
      }

      newResourceElements.put((String) name, element);
    }
    resourceElements = newResourceElements;
    return resourceElements.values();
  }
  @Test
  public void testProperPropertyTypeConverter() throws Exception {
    ModelNode description = createDescription(ModelType.PROPERTY);
    TypeConverter converter =
        TypeConverters.createLegacyTypeConverters(true).getConverter(description);

    CompositeType type = assertCast(CompositeType.class, converter.getOpenType());
    Set<String> keys = type.keySet();
    Assert.assertEquals(2, keys.size());
    assertCompositeType(type, "name", String.class.getName(), "The property name");
    assertCompositeType(type, "value", String.class.getName(), "The property value");

    CompositeData data =
        assertCast(CompositeData.class, converter.fromModelNode(new ModelNode().set("one", "uno")));
    Assert.assertEquals(type, data.getCompositeType());
    Assert.assertEquals("one", data.get("name"));
    Assert.assertEquals("uno", data.get("value"));

    data =
        new CompositeDataSupport(type, new String[] {"name", "value"}, new String[] {"two", "dos"});
    ModelNode newNode = converter.toModelNode(data);
    Assert.assertEquals(ModelType.PROPERTY, newNode.getType());
    Assert.assertEquals(new ModelNode().set("two", "dos"), newNode);

    assertToArray(converter, data);
  }
예제 #5
0
 private static String getProperty(TabularData td, String propName) {
   CompositeData cd = td.get(new Object[] {propName});
   if (cd != null) {
     String key = (String) cd.get("key");
     if (!propName.equals(key)) {
       throw new RuntimeException(
           "TEST FAILED: " + key + " property found" + " but expected to be " + propName);
     }
     return (String) cd.get("value");
   }
   return null;
 }
예제 #6
0
파일: Client.java 프로젝트: Juliens/red5
 /**
  * Allows for reconstruction via CompositeData.
  *
  * @param cd composite data
  * @return Client class instance
  */
 public static Client from(CompositeData cd) {
   Client instance = null;
   if (cd.containsKey("id")) {
     String id = (String) cd.get("id");
     instance = new Client(id, null);
     instance.setCreationTime((Long) cd.get("creationTime"));
     instance.setAttribute(PERMISSIONS, cd.get(PERMISSIONS));
   }
   if (cd.containsKey("attributes")) {
     AttributeStore attrs = (AttributeStore) cd.get("attributes");
     instance.setAttributes(attrs);
   }
   return instance;
 }
  /**
   * Adds the attribute to the given list.
   *
   * @param mBeanAttributes The attributes
   * @param root The root attribute
   * @param value The value
   */
  private void addAttributeRoots(
      List<AttributeNode> mBeanAttributes, AttributeNode root, Object value) {
    if (value instanceof CompositeData) {
      mBeanAttributes.add(root);
      CompositeData compositeData = (CompositeData) value;
      CompositeType type = compositeData.getCompositeType();
      for (String key : type.keySet()) {
        AttributeNode attribute = new AttributeNode(key, root);

        root.addChild(attribute);
        addAttributeItems(attribute, compositeData.get(key));
      }
    } else if (value instanceof TabularData) {
      mBeanAttributes.add(root);
      TabularData tabularData = (TabularData) value;
      for (Object keyList : tabularData.keySet()) {
        @SuppressWarnings("unchecked")
        Object[] keys = ((List<Object>) keyList).toArray(new Object[0]);
        AttributeNode attribute = new AttributeNode(String.valueOf(keys[0]), root);

        root.addChild(attribute);
        addAttributeItems(attribute, tabularData.get(keys));
      }
    } else if (value instanceof Long || value instanceof Integer || value instanceof Double) {
      root.setValidLeaf(true);
      root.setRgb(getRGB(root.getQualifiedName()));
      mBeanAttributes.add(root);
    }
  }
 private String format(CompositeData compositeData) {
   StringBuffer stackTraceElement = new StringBuffer("    ");
   stackTraceElement.append((String) compositeData.get("className"));
   stackTraceElement.append('.');
   stackTraceElement.append(compositeData.get("methodName"));
   stackTraceElement.append('(');
   Boolean nativeMethod = (Boolean) compositeData.get("nativeMethod");
   if (!nativeMethod.booleanValue()) {
     stackTraceElement.append(compositeData.get("fileName"));
     stackTraceElement.append(':');
     stackTraceElement.append(compositeData.get("lineNumber"));
   } else {
     stackTraceElement.append("Native Method");
   }
   stackTraceElement.append(')');
   return stackTraceElement.toString();
 }
예제 #9
0
 /**
  * Allows for reconstruction via CompositeData.
  *
  * @param cd composite data
  * @return Scope class instance
  */
 public static Scope from(CompositeData cd) {
   IScope parent = null;
   ScopeType type = ScopeType.UNDEFINED;
   String name = null;
   boolean persistent = false;
   if (cd.containsKey("parent")) {
     parent = (IScope) cd.get("parent");
   }
   if (cd.containsKey("type")) {
     type = (ScopeType) cd.get("type");
   }
   if (cd.containsKey("name")) {
     name = (String) cd.get("name");
   }
   if (cd.containsKey("persistent")) {
     persistent = (Boolean) cd.get("persistent");
   }
   return new Scope(new Builder(parent, type, name, persistent));
 }
예제 #10
0
 /**
  * Recursively extracts simple numeric values from composite data objects. The map {@code values}
  * will be populated with a path to the value as the key and the simple object as the value.
  */
 private void extractValues(String path, Map<String, Object> values, CompositeData obj) {
   for (String key : obj.getCompositeType().keySet()) {
     String newPath = (path == null) ? key : path + "." + key;
     Object value = obj.get(key);
     if (value instanceof CompositeData) {
       extractValues(newPath, values, (CompositeData) value);
     } else if (value != null) {
       values.put(newPath, value);
     }
   }
 }
예제 #11
0
  public Object getValue(String serviceURL, String object, String attribute, String key)
      throws Exception {
    JMXConnector jmxc = null;
    try {
      // pega conector do pool
      jmxc = (JMXConnector) connectionPool.borrowObject(serviceURL);
      MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();

      ObjectName name = new ObjectName(object);
      Object value = mbsc.getAttribute(name, attribute);
      if (value instanceof CompositeData) {
        if (key == null) {
          logger.warn(
              "Attribute {}:{} is CompositeData but resource did not define a 'compositeDataKey'",
              object,
              attribute);
          value = null;
        } else {
          CompositeData data = (CompositeData) value;
          value = data.get(key);
        }
      }
      return value;

    } catch (Exception e) {
      if (null != jmxc) {
        connectionPool.invalidateObject(serviceURL, jmxc);
        jmxc = null;
      }
      throw e;

    } finally {
      if (null != jmxc) {
        connectionPool.returnObject(serviceURL, jmxc);
      }
    }
  }
 /** Returns an attribute. */
 public Object __getField(String attrName) {
   return _data.get(attrName);
 }
예제 #13
0
  /**
   * {@inheritDoc}
   *
   * <p>Perform data collection.
   */
  @Override
  public CollectionSet collect(CollectionAgent agent, EventProxy eproxy, Map<String, Object> map) {
    InetAddress ipaddr = agent.getAddress();
    JMXNodeInfo nodeInfo = agent.getAttribute(NODE_INFO_KEY);
    Map<String, BeanInfo> mbeans = nodeInfo.getMBeans();
    String collDir = serviceName;

    boolean useMbeanForRrds = ParameterMap.getKeyedBoolean(map, "use-mbean-name-for-rrds", false);
    String port = ParameterMap.getKeyedString(map, "port", null);
    String friendlyName = ParameterMap.getKeyedString(map, "friendly-name", port);
    if (useFriendlyName) {
      collDir = friendlyName;
    }

    JMXCollectionSet collectionSet = new JMXCollectionSet(agent, collDir);
    collectionSet.setCollectionTimestamp(new Date());
    JMXCollectionResource collectionResource = collectionSet.getResource();

    ConnectionWrapper connection = null;

    LogUtils.debugf(
        this, "collecting %s on node ID %d", InetAddressUtils.str(ipaddr), nodeInfo.getNodeId());

    try {
      connection = getMBeanServerConnection(map, ipaddr);

      if (connection == null) {
        return collectionSet;
      }

      MBeanServerConnection mbeanServer = connection.getMBeanServer();

      int retry = ParameterMap.getKeyedInteger(map, "retry", 3);
      for (int attempts = 0; attempts <= retry; attempts++) {
        try {
          /*
           * Iterate over the mbeans, for each object name perform a
           * getAttributes, the update the RRD.
           */

          for (Iterator<BeanInfo> iter = mbeans.values().iterator(); iter.hasNext(); ) {
            BeanInfo beanInfo = iter.next();
            String mbeanName = beanInfo.getMbeanName();
            String objectName = beanInfo.getObjectName();
            String excludeList = beanInfo.getExcludes();
            // All JMX collected values are per node
            String obj = useMbeanForRrds ? mbeanName : objectName;
            AttributeGroupType attribGroupType = new AttributeGroupType(fixGroupName(obj), "all");

            List<String> attribNames = beanInfo.getAttributeNames();
            List<String> compAttribNames = beanInfo.getCompositeAttributeNames();

            for (String compAttribName : compAttribNames) {
              if (attribNames.contains(compAttribName)) {
                attribNames.remove(compAttribName);
                String[] ac = compAttribName.split("\\|", -1);
                String attrName = ac[0];
                if (!attribNames.contains(attrName)) {
                  attribNames.add(attrName);
                }
              }
            }
            // log.debug(" JMXCollector: processed the following attributes: " +
            // attribNames.toString());
            // log.debug(" JMXCollector: processed the following Composite Attributes: " +
            // compAttribNames.toString());

            String[] attrNames = attribNames.toArray(new String[attribNames.size()]);

            if (objectName.indexOf("*") == -1) {
              LogUtils.debugf(
                  this,
                  "%s Collector - getAttributes: %s, # attributes: %d, # composite attribute members: %d",
                  serviceName,
                  objectName,
                  attrNames.length,
                  compAttribNames.size());
              try {
                ObjectName oName = new ObjectName(objectName);
                if (mbeanServer.isRegistered(oName)) {
                  AttributeList attrList = mbeanServer.getAttributes(oName, attrNames);
                  Map<String, JMXDataSource> dsMap = nodeInfo.getDsMap();
                  for (Object attribute : attrList) {
                    List<String> compositeMemberKeys = new ArrayList<String>();
                    Boolean isComposite = false;
                    Attribute attrib = (Attribute) attribute;
                    for (String compAttrName : compAttribNames) {
                      String[] attribKeys = compAttrName.split("\\|", -1);
                      if (attrib.getName().equals(attribKeys[0])) {
                        compositeMemberKeys.add(attribKeys[1]);
                        isComposite = true;
                      }
                    }
                    if (isComposite) {
                      try {
                        CompositeData cd = (CompositeData) attrib.getValue();
                        for (String key : compositeMemberKeys) {
                          /*
                          value = cd.get(key);

                          log.debug(" JMXCollector - got CompositeData: " +
                                    objectName + "|" + attrib.getName() + "|" + key + " |-> " + cd.get(key).toString());
                          */
                          JMXDataSource ds =
                              dsMap.get(objectName + "|" + attrib.getName() + "|" + key);
                          JMXCollectionAttributeType attribType =
                              new JMXCollectionAttributeType(ds, null, null, attribGroupType);
                          collectionResource.setAttributeValue(attribType, cd.get(key).toString());
                        }
                      } catch (final ClassCastException cce) {
                        LogUtils.debugf(
                            this,
                            cce,
                            "%s Collection - getAttributes (try CompositeData) - ERROR: Failed to cast attribute value to type CompositeData!",
                            serviceName);
                      }
                    } else {
                      // this is a normal attribute, so fallback to default handler
                      JMXDataSource ds = dsMap.get(objectName + "|" + attrib.getName());
                      JMXCollectionAttributeType attribType =
                          new JMXCollectionAttributeType(ds, null, null, attribGroupType);
                      collectionResource.setAttributeValue(
                          attribType, attrib.getValue().toString());
                    }
                  }
                }
              } catch (final InstanceNotFoundException e) {
                LogUtils.errorf(this, e, "Unable to retrieve attributes from %s", objectName);
              }
            } else {
              /*
               * This section is for ObjectNames that use the
               * '*' wildcard
               */
              Set<ObjectName> mbeanSet = getObjectNames(mbeanServer, objectName);
              for (Iterator<ObjectName> objectNameIter = mbeanSet.iterator();
                  objectNameIter.hasNext(); ) {
                ObjectName oName = objectNameIter.next();
                LogUtils.debugf(
                    this,
                    "%s Collector - getAttributesWC: %s, # attributes: %d, alias: %s",
                    serviceName,
                    oName,
                    attrNames.length,
                    beanInfo.getKeyAlias());

                try {
                  if (excludeList == null) {
                    // the exclude list doesn't apply
                    if (mbeanServer.isRegistered(oName)) {
                      AttributeList attrList = mbeanServer.getAttributes(oName, attrNames);
                      Map<String, JMXDataSource> dsMap = nodeInfo.getDsMap();

                      for (Object attribute : attrList) {
                        Attribute attrib = (Attribute) attribute;
                        JMXDataSource ds = dsMap.get(objectName + "|" + attrib.getName());
                        JMXCollectionAttributeType attribType =
                            new JMXCollectionAttributeType(
                                ds,
                                oName.getKeyProperty(beanInfo.getKeyField()),
                                beanInfo.getKeyAlias(),
                                attribGroupType);

                        collectionResource.setAttributeValue(
                            attribType, attrib.getValue().toString());
                      }
                    }
                  } else {
                    /*
                     * filter out calls if the key field
                     * matches an entry in the exclude
                     * list
                     */
                    String keyName = oName.getKeyProperty(beanInfo.getKeyField());
                    boolean found = false;
                    StringTokenizer st = new StringTokenizer(excludeList, ",");
                    while (st.hasMoreTokens()) {
                      if (keyName.equals(st.nextToken())) {
                        found = true;
                        break;
                      }
                    }
                    if (!found) {
                      if (mbeanServer.isRegistered(oName)) {
                        AttributeList attrList = mbeanServer.getAttributes(oName, attrNames);
                        Map<String, JMXDataSource> dsMap = nodeInfo.getDsMap();

                        for (Object attribute : attrList) {
                          Attribute attrib = (Attribute) attribute;
                          JMXDataSource ds = dsMap.get(objectName + "|" + attrib.getName());
                          JMXCollectionAttributeType attribType =
                              new JMXCollectionAttributeType(
                                  ds,
                                  oName.getKeyProperty(beanInfo.getKeyField()),
                                  beanInfo.getKeyAlias(),
                                  attribGroupType);

                          collectionResource.setAttributeValue(
                              attribType, attrib.getValue().toString());
                        }
                      }
                    }
                  }
                } catch (final InstanceNotFoundException e) {
                  LogUtils.errorf(this, e, "Error retrieving attributes for %s", oName);
                }
              }
            }
          }
          break;
        } catch (final Exception e) {
          LogUtils.debugf(
              this,
              e,
              "%s Collector.collect: IOException while collecting address: %s",
              serviceName,
              agent.getAddress());
        }
      }
    } catch (final Exception e) {
      LogUtils.errorf(this, e, "Error getting MBeanServer");
    } finally {
      if (connection != null) {
        connection.close();
      }
    }

    collectionSet.setStatus(ServiceCollector.COLLECTION_SUCCEEDED);
    return collectionSet;
  }
예제 #14
0
 /**
  * create result as property with name from property prefix When result is an array and
  * isSeparateArrayResults is true, resultproperty used as prefix (<code>
  * resultproperty.0-array.length</code> and store the result array length at <code>
  * resultproperty.length</code>. Other option is that you delimit your result with a delimiter
  * (java.util.StringTokenizer is used).
  *
  * @param propertyPrefix
  * @param result
  */
 protected void createProperty(String propertyPrefix, Object result) {
   if (propertyPrefix == null) propertyPrefix = "";
   if (result instanceof CompositeDataSupport) {
     CompositeDataSupport data = (CompositeDataSupport) result;
     CompositeType compositeType = data.getCompositeType();
     Set<String> keys = compositeType.keySet();
     for (Iterator<String> iter = keys.iterator(); iter.hasNext(); ) {
       String key = iter.next();
       Object value = data.get(key);
       OpenType<?> type = compositeType.getType(key);
       if (type instanceof SimpleType<?>) {
         setProperty(propertyPrefix + "." + key, value);
       } else {
         createProperty(propertyPrefix + "." + key, value);
       }
     }
   } else if (result instanceof TabularDataSupport) {
     TabularDataSupport data = (TabularDataSupport) result;
     for (Iterator<Object> iter = data.keySet().iterator(); iter.hasNext(); ) {
       Object key = iter.next();
       for (Iterator<?> iter1 = ((List<?>) key).iterator(); iter1.hasNext(); ) {
         Object key1 = iter1.next();
         CompositeData valuedata = data.get(new Object[] {key1});
         Object value = valuedata.get("value");
         OpenType<?> type = valuedata.getCompositeType().getType("value");
         if (type instanceof SimpleType<?>) {
           setProperty(propertyPrefix + "." + key1, value);
         } else {
           createProperty(propertyPrefix + "." + key1, value);
         }
       }
     }
   } else if (result.getClass().isArray()) {
     if (isSeparatearrayresults()) {
       int size = 0;
       for (int i = 0; i < Array.getLength(result); i++) {
         if (setProperty(propertyPrefix + "." + size, Array.get(result, i))) {
           size++;
         }
       }
       if (size > 0) {
         setProperty(propertyPrefix + ".Length", Integer.toString(size));
       }
     }
   } else {
     String delim = getDelimiter();
     if (delim != null) {
       StringTokenizer tokenizer = new StringTokenizer(result.toString(), delim);
       int size = 0;
       for (; tokenizer.hasMoreTokens(); ) {
         String token = tokenizer.nextToken();
         if (setProperty(propertyPrefix + "." + size, token)) {
           size++;
         }
       }
       if (size > 0) setProperty(propertyPrefix + ".Length", Integer.toString(size));
     } else {
       setProperty(propertyPrefix, result.toString());
     }
   }
 }