Search in sources :

Example 1 with ImmutableDescriptor

use of javax.management.ImmutableDescriptor in project jdk8u_jdk by JetBrains.

the class ImmutableDescriptorSetFieldsTest method main.

public static void main(String[] args) throws Exception {
    boolean ok = true;
    ImmutableDescriptor d = new ImmutableDescriptor("k=v");
    try {
        System.out.println("Call ImmutableDescriptor.setFields(fieldNames,fieldValues) " + "with empty name in field names array");
        String[] fieldNames = { "a", "", "c" };
        Object[] fieldValues = { 1, 2, 3 };
        d.setFields(fieldNames, fieldValues);
        System.out.println("Didn't get expected exception");
        ok = false;
    } catch (RuntimeOperationsException e) {
        if (e.getCause() instanceof IllegalArgumentException) {
            System.out.println("Got expected exception:");
            ok = true;
        } else {
            System.out.println("Got unexpected exception:");
            ok = false;
        }
        e.printStackTrace(System.out);
    } catch (Exception e) {
        System.out.println("Got unexpected exception:");
        ok = false;
        e.printStackTrace(System.out);
    }
    try {
        System.out.println("Call ImmutableDescriptor.setFields(fieldNames,fieldValues) " + "with null name in field names array");
        String[] fieldNames = { "a", null, "c" };
        Object[] fieldValues = { 1, 2, 3 };
        d.setFields(fieldNames, fieldValues);
        System.out.println("Didn't get expected exception");
        ok = false;
    } catch (RuntimeOperationsException e) {
        if (e.getCause() instanceof IllegalArgumentException) {
            System.out.println("Got expected exception:");
            ok = true;
        } else {
            System.out.println("Got unexpected exception:");
            ok = false;
        }
        e.printStackTrace(System.out);
    } catch (Exception e) {
        System.out.println("Got unexpected exception:");
        ok = false;
        e.printStackTrace(System.out);
    }
    if (ok) {
        System.out.println("TEST PASSED");
    } else {
        System.out.println("TEST FAILED");
        throw new Exception("Got unexpected exceptions");
    }
}
Also used : ImmutableDescriptor(javax.management.ImmutableDescriptor) RuntimeOperationsException(javax.management.RuntimeOperationsException) RuntimeOperationsException(javax.management.RuntimeOperationsException)

Example 2 with ImmutableDescriptor

use of javax.management.ImmutableDescriptor in project jdk8u_jdk by JetBrains.

the class ImmutableDescriptorSerialTest method main.

public static void main(String[] args) throws Exception {
    System.out.println("Test that ImmutableDescriptor.EMPTY_DESCRIPTOR " + "deserializes identically");
    if (serialize(ImmutableDescriptor.EMPTY_DESCRIPTOR) != ImmutableDescriptor.EMPTY_DESCRIPTOR) {
        throw new Exception("ImmutableDescriptor.EMPTY_DESCRIPTOR did not " + "deserialize identically");
    }
    System.out.println("...OK");
    System.out.println("Test that serialization preserves case and " + "that deserialized object is case-insensitive");
    Descriptor d = new ImmutableDescriptor("a=aval", "B=Bval", "cC=cCval");
    Descriptor d1 = serialize(d);
    Set<String> keys = new HashSet(Arrays.asList(d1.getFieldNames()));
    if (keys.size() != 3 || !keys.containsAll(Arrays.asList("a", "B", "cC"))) {
        throw new Exception("Keys don't match: " + keys);
    }
    for (String key : keys) {
        String value = (String) d.getFieldValue(key);
        for (String t : Arrays.asList(key, key.toLowerCase(), key.toUpperCase())) {
            String tvalue = (String) d1.getFieldValue(t);
            if (!tvalue.equals(value)) {
                throw new Exception("Value of " + key + " for " + "deserialized object does not match: " + tvalue + " should be " + value);
            }
        }
    }
    System.out.println("...OK");
}
Also used : ImmutableDescriptor(javax.management.ImmutableDescriptor) Descriptor(javax.management.Descriptor) ImmutableDescriptor(javax.management.ImmutableDescriptor) HashSet(java.util.HashSet)

Example 3 with ImmutableDescriptor

use of javax.management.ImmutableDescriptor in project jdk8u_jdk by JetBrains.

the class DescriptorCache method get.

public ImmutableDescriptor get(ImmutableDescriptor descriptor) {
    WeakReference<ImmutableDescriptor> wr = map.get(descriptor);
    ImmutableDescriptor got = (wr == null) ? null : wr.get();
    if (got != null)
        return got;
    map.put(descriptor, new WeakReference<ImmutableDescriptor>(descriptor));
    return descriptor;
}
Also used : ImmutableDescriptor(javax.management.ImmutableDescriptor)

Example 4 with ImmutableDescriptor

use of javax.management.ImmutableDescriptor in project jdk8u_jdk by JetBrains.

the class Introspector method descriptorForAnnotations.

public static Descriptor descriptorForAnnotations(Annotation[] annots) {
    if (annots.length == 0)
        return ImmutableDescriptor.EMPTY_DESCRIPTOR;
    Map<String, Object> descriptorMap = new HashMap<String, Object>();
    for (Annotation a : annots) {
        Class<? extends Annotation> c = a.annotationType();
        Method[] elements = c.getMethods();
        boolean packageAccess = false;
        for (Method element : elements) {
            DescriptorKey key = element.getAnnotation(DescriptorKey.class);
            if (key != null) {
                String name = key.value();
                Object value;
                try {
                    // Avoid checking access more than once per annotation
                    if (!packageAccess) {
                        ReflectUtil.checkPackageAccess(c);
                        packageAccess = true;
                    }
                    value = MethodUtil.invoke(element, a, null);
                } catch (RuntimeException e) {
                    //
                    throw e;
                } catch (Exception e) {
                    // we don't expect this
                    throw new UndeclaredThrowableException(e);
                }
                value = annotationToField(value);
                Object oldValue = descriptorMap.put(name, value);
                if (oldValue != null && !equals(oldValue, value)) {
                    final String msg = "Inconsistent values for descriptor field " + name + " from annotations: " + value + " :: " + oldValue;
                    throw new IllegalArgumentException(msg);
                }
            }
        }
    }
    if (descriptorMap.isEmpty())
        return ImmutableDescriptor.EMPTY_DESCRIPTOR;
    else
        return new ImmutableDescriptor(descriptorMap);
}
Also used : ImmutableDescriptor(javax.management.ImmutableDescriptor) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) DescriptorKey(javax.management.DescriptorKey) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) AttributeNotFoundException(javax.management.AttributeNotFoundException) NotCompliantMBeanException(javax.management.NotCompliantMBeanException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) InvocationTargetException(java.lang.reflect.InvocationTargetException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException)

Example 5 with ImmutableDescriptor

use of javax.management.ImmutableDescriptor in project jdk8u_jdk by JetBrains.

the class Basic method setNotifDescriptorAsMapAtt.

/**
     * Set the Descriptor used to build the NotificationInfo
     * of emitted notifications.
     * <br>A Map<String, Object> would better fit Descriptor needs but then
     * it is not convertible according the MXBean specification so the MBean
     * registration fails.
     * As we plan to test our custom Descriptor finds its way into
     * the metadata of emitted notifications, String is good enough.
     */
public void setNotifDescriptorAsMapAtt(Map<String, String> value) {
    notifDescriptorAsMapAtt = new HashMap<String, String>(value);
    notifDescriptorAtt = new ImmutableDescriptor(value);
}
Also used : ImmutableDescriptor(javax.management.ImmutableDescriptor)

Aggregations

ImmutableDescriptor (javax.management.ImmutableDescriptor)9 HashMap (java.util.HashMap)2 Descriptor (javax.management.Descriptor)2 Annotation (java.lang.annotation.Annotation)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 HashSet (java.util.HashSet)1 WeakHashMap (java.util.WeakHashMap)1 Attribute (javax.management.Attribute)1 AttributeNotFoundException (javax.management.AttributeNotFoundException)1 DescriptorKey (javax.management.DescriptorKey)1 MBeanInfo (javax.management.MBeanInfo)1 MBeanServer (javax.management.MBeanServer)1 MBeanServerConnection (javax.management.MBeanServerConnection)1 NotCompliantMBeanException (javax.management.NotCompliantMBeanException)1 Notification (javax.management.Notification)1 ObjectName (javax.management.ObjectName)1 RuntimeOperationsException (javax.management.RuntimeOperationsException)1 DescriptorSupport (javax.management.modelmbean.DescriptorSupport)1