@Test
  public void testJsonObjectInComplexValue() throws Exception {
    ModelNode description = createDescription(ModelType.OBJECT);
    ModelNode complexValueType = new ModelNode();
    complexValueType.get("value", DESCRIPTION).set("A  value");
    complexValueType.get("value", TYPE).set(ModelType.OBJECT);
    description.get(VALUE_TYPE).set(complexValueType);

    TypeConverter converter = getConverter(description);

    CompositeType type = assertCast(CompositeType.class, converter.getOpenType());
    Set<String> keys = type.keySet();
    Assert.assertEquals(1, keys.size());

    Assert.assertEquals(SimpleType.STRING, type.getType("value"));

    ModelNode node = new ModelNode();
    node.get("value", "long").set(1L);
    node.get("value", "string").set("test");

    CompositeData data = assertCast(CompositeData.class, converter.fromModelNode(node));
    Assert.assertEquals(type, data.getCompositeType());
    Assert.assertEquals(
        ModelNode.fromJSONString(node.toJSONString(false)), converter.toModelNode(data));
  }
  @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);
  }
  @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);
  }
  /**
   * 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 OpenType<?> assertCompositeType(
     CompositeType composite, String name, String type, String description, boolean validateType) {
   Assert.assertTrue(composite.keySet().contains(name));
   if (validateType) {
     Assert.assertEquals(type, composite.getType(name).getTypeName());
   }
   Assert.assertEquals(description, composite.getDescription(name));
   return composite.getType(name);
 }
Пример #6
0
  /**
   * Exports all the numeric attributes of beans matching the supplied MXBean name pattern. Also,
   * discovers the numeric properties of the attributes of type CompositeData and registers them as
   * well.
   *
   * @param beanNamePattern the bean name pattern used to discover the beans.
   * @see javax.management.ObjectName for bean name pattern syntax
   * @see java.lang.management for the list of java platform mbeans
   * @throws JMException if there are errors querying MBeans or information on them
   */
  public void exportNumericAttributes(ObjectName beanNamePattern) throws JMException {
    MBeanServer beanServer = ManagementFactory.getPlatformMBeanServer();
    Set<ObjectName> beanNames = beanServer.queryNames(beanNamePattern, null);

    // Iterate through all beans and register their numeric properties with Stats
    for (ObjectName beanName : beanNames) {
      MBeanInfo beanInfo = beanServer.getMBeanInfo(beanName);

      // Iterate through all bean attributes
      for (MBeanAttributeInfo attributeInfo : beanInfo.getAttributes()) {
        if (attributeInfo.isReadable()) {
          // Figure out the open type for the attribute
          OpenType<?> openType = null;
          if (attributeInfo instanceof OpenMBeanAttributeInfo) {
            openType = ((OpenMBeanAttributeInfo) attributeInfo).getOpenType();
          } else {
            // Sometimes the open mbean info is available in the descriptor
            Object obj = attributeInfo.getDescriptor().getFieldValue("openType");
            if (obj != null && obj instanceof OpenType) {
              openType = (OpenType) obj;
            }
          }
          // once the open type is found, figure out if it's a numeric or composite type
          if (openType != null) {
            if (NUMERIC_TYPES.contains(openType)) {
              // numeric attribute types are registered with callbacks that simply
              // return their value
              addStatIfMatches(
                  getStatName(beanName, attributeInfo.getName()),
                  new MBeanLongAttributeFetcher(beanServer, beanName, attributeInfo.getName()));
            } else if (openType instanceof CompositeType) {
              // for composite types, we figure out which properties of the composite type
              // are numeric and register callbacks to fetch those composite type attributes
              CompositeType compositeType = (CompositeType) openType;

              for (String key : compositeType.keySet()) {
                if (NUMERIC_TYPES.contains(compositeType.getType(key))) {
                  addStatIfMatches(
                      getStatName(beanName, attributeInfo.getName(), key),
                      new MBeanLongCompositeValueFetcher(
                          beanServer, beanName, attributeInfo.getName(), key));
                }
              }
            }
          }
        }
      }
    }
  }
Пример #7
0
  @Override
  public TabularData getConnectionList() throws JMException {
    CompositeType rowType = JdbcConnectionStat.Entry.getCompositeType();
    String[] indexNames = rowType.keySet().toArray(new String[rowType.keySet().size()]);

    TabularType tabularType =
        new TabularType("ConnectionListStatistic", "ConnectionListStatistic", rowType, indexNames);
    TabularData data = new TabularDataSupport(tabularType);

    for (Map.Entry<Long, JdbcConnectionStat.Entry> entry : getConnections().entrySet()) {
      data.put(entry.getValue().getCompositeData());
    }

    return data;
  }
Пример #8
0
  @Override
  public TabularData getSqlList() throws JMException {
    Map<String, JdbcSqlStat> sqlStatMap = this.getSqlStatMap();
    CompositeType rowType = JdbcSqlStat.getCompositeType();
    String[] indexNames = rowType.keySet().toArray(new String[rowType.keySet().size()]);

    TabularType tabularType =
        new TabularType("SqlListStatistic", "SqlListStatistic", rowType, indexNames);
    TabularData data = new TabularDataSupport(tabularType);

    for (Map.Entry<String, JdbcSqlStat> entry : sqlStatMap.entrySet()) {
      data.put(entry.getValue().getCompositeData());
    }

    return data;
  }
  @Test
  public void testComplexList() throws Exception {
    ModelNode description = createDescription(ModelType.LIST);
    ModelNode complexValueType = new ModelNode();
    complexValueType.get("int-value", DESCRIPTION).set("An int value");
    complexValueType.get("int-value", TYPE).set(ModelType.INT);
    complexValueType.get("list-int-value", DESCRIPTION).set("An int list value");
    complexValueType.get("list-int-value", TYPE).set(ModelType.LIST);
    complexValueType.get("list-int-value", VALUE_TYPE).set(ModelType.INT);
    description.get(VALUE_TYPE).set(complexValueType);

    TypeConverter converter = getConverter(description);

    ArrayType<CompositeType> arrayType = assertCast(ArrayType.class, converter.getOpenType());
    CompositeType type = assertCast(CompositeType.class, arrayType.getElementOpenType());
    Set<String> keys = type.keySet();
    Assert.assertEquals(2, keys.size());
    assertCompositeType(type, "int-value", Integer.class.getName(), "An int value");
    assertCompositeType(type, "list-int-value", Integer[].class.getName(), "An int list value");

    ModelNode node = new ModelNode();
    ModelNode entry = new ModelNode();
    entry.get("int-value").set(1);
    entry.get("list-int-value").add(2);
    entry.get("list-int-value").add(3);
    node.add(entry);
    entry = new ModelNode();
    entry.get("int-value").set(4);
    entry.get("list-int-value").add(5);
    entry.get("list-int-value").add(6);
    node.add(entry);

    CompositeData[] datas = assertCast(CompositeData[].class, converter.fromModelNode(node));
    Assert.assertEquals(datas[0].getCompositeType(), datas[1].getCompositeType());
    Assert.assertEquals(type, datas[0].getCompositeType());

    Assert.assertEquals(node, converter.toModelNode(datas));
  }
  private Object unmarshall(Object value) {
    if (value instanceof ObjectName) {
      ObjectName name = (ObjectName) value;

      return new MBean(_server, name);
    } else if (value instanceof ObjectName[]) {
      ObjectName[] names = (ObjectName[]) value;

      MBean[] mbeans = new MBean[names.length];

      for (int i = 0; i < names.length; i++) mbeans[i] = new MBean(_server, names[i]);

      return mbeans;
    } else if (value instanceof CompositeData) {
      CompositeData compositeValue = (CompositeData) value;

      CompositeType type = compositeValue.getCompositeType();

      if (type != null) {
        String typeName = type.getTypeName();

        try {
          ClassLoader loader = Thread.currentThread().getContextClassLoader();

          Class typeClass = Class.forName(typeName, false, loader);

          Method from = typeClass.getMethod("from", new Class[] {CompositeData.class});

          if (from != null) return from.invoke(null, compositeValue);
        } catch (Exception e) {
          log.log(Level.FINER, e.toString(), e);
        }
      }

      return new CompositeDataBean(compositeValue);
    } else return value;
  }
Пример #11
0
 /**
  * Subclasses could provide here a different description for the given counter.
  *
  * @param oname
  * @param attrName
  * @param ct
  * @param item
  * @return
  */
 protected String[] getCounterNameAndDescription(
     ObjectName oname, String attrName, CompositeType ct, String item) {
   return new String[] {item, ct.getDescription(item)};
 }
  @Test
  public void testComplexValue() throws Exception {
    ModelNode description = createDescription(ModelType.OBJECT);
    ModelNode complexValueType = new ModelNode();
    complexValueType.get("int-value", DESCRIPTION).set("An int value");
    complexValueType.get("int-value", TYPE).set(ModelType.INT);
    complexValueType.get("bigint-value", DESCRIPTION).set("A biginteger value");
    complexValueType.get("bigint-value", TYPE).set(ModelType.BIG_INTEGER);
    complexValueType.get("bigdecimal-value", DESCRIPTION).set("A bigdecimal value");
    complexValueType.get("bigdecimal-value", TYPE).set(ModelType.BIG_DECIMAL);
    complexValueType.get("boolean-value", DESCRIPTION).set("A boolean value");
    complexValueType.get("boolean-value", TYPE).set(ModelType.BOOLEAN);
    complexValueType.get("bytes-value", DESCRIPTION).set("A bytes value");
    complexValueType.get("bytes-value", TYPE).set(ModelType.BYTES);
    complexValueType.get("double-value", DESCRIPTION).set("A double value");
    complexValueType.get("double-value", TYPE).set(ModelType.DOUBLE);
    complexValueType.get("string-value", DESCRIPTION).set("A string value");
    complexValueType.get("string-value", TYPE).set(ModelType.STRING);
    complexValueType.get("long-value", DESCRIPTION).set("A long value");
    complexValueType.get("long-value", TYPE).set(ModelType.LONG);
    complexValueType.get("type-value", DESCRIPTION).set("A type value");
    complexValueType.get("type-value", TYPE).set(ModelType.TYPE);
    complexValueType.get("list-int-value", DESCRIPTION).set("An int list value");
    complexValueType.get("list-int-value", TYPE).set(ModelType.LIST);
    complexValueType.get("list-int-value", VALUE_TYPE).set(ModelType.INT);
    complexValueType.get("map-int-value", DESCRIPTION).set("An int map value");
    complexValueType.get("map-int-value", TYPE).set(ModelType.OBJECT);
    complexValueType.get("map-int-value", VALUE_TYPE).set(ModelType.INT);
    description.get(VALUE_TYPE).set(complexValueType);

    TypeConverter converter = getConverter(description);

    CompositeType type = assertCast(CompositeType.class, converter.getOpenType());
    Set<String> keys = type.keySet();
    Assert.assertEquals(11, keys.size());
    assertCompositeType(type, "int-value", Integer.class.getName(), "An int value");
    assertCompositeType(type, "bigint-value", BigInteger.class.getName(), "A biginteger value");
    assertCompositeType(type, "bigdecimal-value", BigDecimal.class.getName(), "A bigdecimal value");
    assertCompositeType(type, "boolean-value", Boolean.class.getName(), "A boolean value");
    assertCompositeType(type, "bytes-value", byte[].class.getName(), "A bytes value");
    assertCompositeType(type, "double-value", Double.class.getName(), "A double value");
    assertCompositeType(type, "string-value", String.class.getName(), "A string value");
    assertCompositeType(type, "long-value", Long.class.getName(), "A long value");
    assertCompositeType(type, "type-value", String.class.getName(), "A type value");
    assertCompositeType(type, "list-int-value", Integer[].class.getName(), "An int list value");
    assertMapType(
        assertCast(
            TabularType.class,
            assertCompositeType(
                type, "map-int-value", TabularType.class.getName(), "An int map value", false)),
        SimpleType.STRING,
        SimpleType.INTEGER);

    ModelNode node = new ModelNode();
    node.get("int-value").set(1);
    node.get("bigint-value").set(BigInteger.valueOf(2));
    node.get("bigdecimal-value").set(BigDecimal.valueOf(3));
    node.get("boolean-value").set(Boolean.TRUE);
    node.get("bytes-value").set(new byte[] {4, 5});
    node.get("double-value").set(Double.valueOf(6));
    node.get("string-value").set("Seven");
    node.get("long-value").set(Long.valueOf(8));
    node.get("type-value").set(ModelType.INT);
    node.get("list-int-value").add(9);
    node.get("list-int-value").add(10);
    node.get("map-int-value", "one").set(11);
    node.get("map-int-value", "two").set(12);

    CompositeData data = assertCast(CompositeData.class, converter.fromModelNode(node));
    Assert.assertEquals(type, data.getCompositeType());
    Assert.assertEquals(node, converter.toModelNode(data));

    // Another test testing missing data in fromModelNode();
    node = new ModelNode();
    node.get("int-value").set(1);
    data = assertCast(CompositeData.class, converter.fromModelNode(node));
    Assert.assertEquals(node, converter.toModelNode(data));

    // And another test testing missing data in fromModelNode();
    node = new ModelNode();
    node.get("boolean-value").set(true);
    data = assertCast(CompositeData.class, converter.fromModelNode(node));
    Assert.assertEquals(node, converter.toModelNode(data));
  }
Пример #13
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());
     }
   }
 }