Search in sources :

Example 6 with ImmutableDescriptor

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

the class UnionTest method main.

public static void main(String[] args) throws Exception {
    ImmutableDescriptor immutableEmpty = new ImmutableDescriptor();
    DescriptorSupport mutableEmpty = new DescriptorSupport();
    checkEmpty(union());
    checkEmpty(union(immutableEmpty));
    checkEmpty(union(mutableEmpty));
    checkEmpty(union(EMPTY_DESCRIPTOR, immutableEmpty, mutableEmpty));
    checkEmpty(union(null, immutableEmpty, null));
    ImmutableDescriptor immutableNumbers = new ImmutableDescriptor(new String[] { "one", "two", "three" }, new Object[] { 1, 2, 3 });
    final String[] noNames = null;
    DescriptorSupport mutableNumbers = new DescriptorSupport(immutableNumbers.getFieldNames(), immutableNumbers.getFieldValues(noNames));
    ImmutableDescriptor immutableOne = new ImmutableDescriptor(Collections.singletonMap("one", 1));
    DescriptorSupport mutableOne = new DescriptorSupport(new String[] { "one" }, new Object[] { 1 });
    ImmutableDescriptor immutableTwo = new ImmutableDescriptor(Collections.singletonMap("two", 2));
    DescriptorSupport mutableTwo = new DescriptorSupport(new String[] { "two" }, new Object[] { 2 });
    ImmutableDescriptor immutableOneTwo = new ImmutableDescriptor(new String[] { "one", "two" }, new Object[] { 1, 2 });
    checkEqual(union(immutableNumbers), immutableNumbers);
    checkEqual(union(immutableNumbers, mutableNumbers), immutableNumbers);
    checkEqual(union(mutableNumbers, immutableNumbers), immutableNumbers);
    checkEqual(union(mutableEmpty, immutableEmpty, immutableNumbers, mutableNumbers, immutableOne), immutableNumbers);
    checkEqual(union(immutableOne, immutableTwo, immutableNumbers), immutableNumbers);
    checkEquivalent(union(immutableOne, mutableNumbers), immutableNumbers);
    checkEquivalent(union(immutableOne, immutableTwo), immutableOneTwo);
    checkEquivalent(union(mutableOne, mutableTwo), immutableOneTwo);
    if (failure != null)
        throw new Exception("TEST FAILED: " + failure);
    System.out.println("TEST PASSED");
}
Also used : ImmutableDescriptor(javax.management.ImmutableDescriptor) DescriptorSupport(javax.management.modelmbean.DescriptorSupport)

Example 7 with ImmutableDescriptor

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

the class ImmutableArrayFieldTest method main.

public static void main(String[] args) throws Exception {
    boolean ok = true;
    ImmutableDescriptor d = new ImmutableDescriptor(new String[] { "strings", "ints", "booleans" }, new Object[] { new String[] { "foo" }, new int[] { 5 }, new boolean[] { false } });
    String[] strings = (String[]) d.getFieldValue("strings");
    strings[0] = "bar";
    strings = (String[]) d.getFieldValue("strings");
    if (!strings[0].equals("foo")) {
        System.out.println("FAILED: modified string array field");
        ok = false;
    }
    int[] ints = (int[]) d.getFieldValue("ints");
    ints[0] = 0;
    ints = (int[]) d.getFieldValue("ints");
    if (ints[0] != 5) {
        System.out.println("FAILED: modified int array field");
        ok = false;
    }
    boolean[] bools = (boolean[]) d.getFieldValue("booleans");
    bools[0] = true;
    bools = (boolean[]) d.getFieldValue("booleans");
    if (bools[0]) {
        System.out.println("FAILED: modified boolean array field");
        ok = false;
    }
    Object[] values = d.getFieldValues("strings", "ints", "booleans");
    ((String[]) values[0])[0] = "bar";
    ((int[]) values[1])[0] = 0;
    ((boolean[]) values[2])[0] = true;
    values = d.getFieldValues("strings", "ints", "booleans");
    if (!((String[]) values[0])[0].equals("foo") || ((int[]) values[1])[0] != 5 || ((boolean[]) values[2])[0]) {
        System.out.println("FAILED: getFieldValues modifiable: " + Arrays.deepToString(values));
    }
    if (ok)
        System.out.println("TEST PASSED");
    else
        throw new Exception("Array field values were modifiable");
}
Also used : ImmutableDescriptor(javax.management.ImmutableDescriptor)

Example 8 with ImmutableDescriptor

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

the class MXBeanNotifTest method run.

public void run(Map<String, Object> args) {
    System.out.println("MXBeanNotifTest::run: Start");
    int errorCount = 0;
    try {
        parseArgs(args);
        notifList = new ArrayBlockingQueue<Notification>(numOfNotifications);
        // JMX MbeanServer used inside single VM as if remote.
        MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
        JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
        JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
        cs.start();
        JMXServiceURL addr = cs.getAddress();
        JMXConnector cc = JMXConnectorFactory.connect(addr);
        MBeanServerConnection mbsc = cc.getMBeanServerConnection();
        // ----
        System.out.println("MXBeanNotifTest::run: Create and register the MBean");
        ObjectName objName = new ObjectName("sqe:type=Basic,protocol=rmi");
        mbsc.createMBean(BASIC_MXBEAN_CLASS_NAME, objName);
        System.out.println("---- OK\n");
        // ----
        System.out.println("MXBeanNotifTest::run: Add me as notification listener");
        mbsc.addNotificationListener(objName, this, null, null);
        // ----
        System.out.println("MXBeanNotifTest::run: Retrieve the Descriptor" + " that should be in MBeanNotificationInfo");
        TabularData tabData = (TabularData) mbsc.getAttribute(objName, "NotifDescriptorAsMapAtt");
        Map<String, String> descrMap = new HashMap<>();
        for (Iterator<?> it = tabData.values().iterator(); it.hasNext(); ) {
            CompositeData compData = (CompositeData) it.next();
            descrMap.put((String) compData.get("key"), (String) compData.get("value"));
        }
        Descriptor refNotifDescriptor = new ImmutableDescriptor(descrMap);
        System.out.println("---- OK\n");
        // ----
        // Because the MBean holding the targeted attribute is MXBean, we
        // should use for the setAttribute a converted form for the
        // attribute value as described by the MXBean mapping rules.
        // This explains all that lovely stuff for creating a
        // TabularDataSupport.
        //
        // WARNING : the MBeanInfo of the MXBean used on opposite side
        // is computed when the MBean is registered.
        // It means the Descriptor considered for the MBeanNotificationInfo
        // is not the one we set in the lines below, it is too late.
        // However, we check that set is harmless when we check
        // the MBeanNotificationInfo.
        //
        System.out.println("MXBeanNotifTest::run: Set a Map<String, String>" + " attribute");
        String typeName = "java.util.Map<java.lang.String,java.lang.String>";
        String[] keyValue = new String[] { "key", "value" };
        OpenType<?>[] openTypes = new OpenType<?>[] { SimpleType.STRING, SimpleType.STRING };
        CompositeType rowType = new CompositeType(typeName, typeName, keyValue, keyValue, openTypes);
        TabularType tabType = new TabularType(typeName, typeName, rowType, new String[] { "key" });
        TabularDataSupport convertedDescrMap = new TabularDataSupport(tabType);
        for (int i = 0; i < numOfNotifDescriptorElements; i++) {
            Object[] descrValue = { "field" + i, "value" + i };
            CompositeData data = new CompositeDataSupport(rowType, keyValue, descrValue);
            convertedDescrMap.put(data);
        }
        Attribute descrAtt = new Attribute("NotifDescriptorAsMapAtt", convertedDescrMap);
        mbsc.setAttribute(objName, descrAtt);
        System.out.println("---- OK\n");
        // ----
        System.out.println("MXBeanNotifTest::run: Compare the Descriptor from" + " the MBeanNotificationInfo against a reference");
        MBeanInfo mbInfo = mbsc.getMBeanInfo(objName);
        errorCount += checkMBeanInfo(mbInfo, refNotifDescriptor);
        System.out.println("---- DONE\n");
        // ----
        System.out.println("Check isInstanceOf(Basic)");
        if (!mbsc.isInstanceOf(objName, BASIC_MXBEAN_CLASS_NAME)) {
            errorCount++;
            System.out.println("---- ERROR isInstanceOf returned false\n");
        } else {
            System.out.println("---- OK\n");
        }
        // ----
        System.out.println("Check isInstanceOf(BasicMXBean)");
        if (!mbsc.isInstanceOf(objName, BASIC_MXBEAN_INTERFACE_NAME)) {
            errorCount++;
            System.out.println("---- ERROR isInstanceOf returned false\n");
        } else {
            System.out.println("---- OK\n");
        }
        // ----
        System.out.println("MXBeanNotifTest::run: Ask for " + numOfNotifications + " notification(s)");
        Object[] sendNotifParam = new Object[1];
        String[] sendNotifSig = new String[] { "java.lang.String" };
        for (int i = 0; i < numOfNotifications; i++) {
            // Select which type of notification we ask for
            if (i % 2 == 0) {
                sendNotifParam[0] = Basic.NOTIF_TYPE_0;
            } else {
                sendNotifParam[0] = Basic.NOTIF_TYPE_1;
            }
            // Trigger notification emission
            mbsc.invoke(objName, "sendNotification", sendNotifParam, sendNotifSig);
            // Wait for it then check it when it comes early enough
            Notification notif = notifList.poll(timeForNotificationInSeconds, TimeUnit.SECONDS);
            // notifications are delivered with, we prefer to secure it.
            if (i == 0 && notif == null) {
                System.out.println("MXBeanNotifTest::run: Wait extra " + timeForNotificationInSeconds + " second(s) the " + " very first notification");
                notif = notifList.poll(timeForNotificationInSeconds, TimeUnit.SECONDS);
            }
            if (notif == null) {
                errorCount++;
                System.out.println("---- ERROR No notification received" + " within allocated " + timeForNotificationInSeconds + " second(s) !");
            } else {
                errorCount += checkNotification(notif, (String) sendNotifParam[0], Basic.NOTIFICATION_MESSAGE, objName);
            }
        }
        int toc = 0;
        while (notifList.size() < 2 && toc < 10) {
            Thread.sleep(499);
            toc++;
        }
        System.out.println("---- DONE\n");
    } catch (Exception e) {
        Utils.printThrowable(e, true);
        throw new RuntimeException(e);
    }
    if (errorCount == 0) {
        System.out.println("MXBeanNotifTest::run: Done without any error");
    } else {
        System.out.println("MXBeanNotifTest::run: Done with " + errorCount + " error(s)");
        throw new RuntimeException("errorCount = " + errorCount);
    }
}
Also used : OpenType(javax.management.openmbean.OpenType) ImmutableDescriptor(javax.management.ImmutableDescriptor) MBeanInfo(javax.management.MBeanInfo) HashMap(java.util.HashMap) Attribute(javax.management.Attribute) CompositeData(javax.management.openmbean.CompositeData) TabularType(javax.management.openmbean.TabularType) Notification(javax.management.Notification) TabularData(javax.management.openmbean.TabularData) JMXConnector(javax.management.remote.JMXConnector) MBeanServer(javax.management.MBeanServer) JMXServiceURL(javax.management.remote.JMXServiceURL) CompositeDataSupport(javax.management.openmbean.CompositeDataSupport) JMXConnectorServer(javax.management.remote.JMXConnectorServer) ObjectName(javax.management.ObjectName) TabularDataSupport(javax.management.openmbean.TabularDataSupport) Descriptor(javax.management.Descriptor) ImmutableDescriptor(javax.management.ImmutableDescriptor) MBeanServerConnection(javax.management.MBeanServerConnection) CompositeType(javax.management.openmbean.CompositeType)

Example 9 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)

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