jdk/test/javax/management/MBeanServer/MBeanFallbackTest.java
author ykantser
Thu, 07 May 2015 09:11:49 +0200
changeset 30376 2ccf2cf7ea48
parent 25164 06c6cc3a5930
child 44423 306c020eb154
permissions -rw-r--r--
8078896: Add @modules as needed to the jdk_svc tests Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 25164
diff changeset
     2
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
     4
 *
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
     8
 *
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    13
 * accompanied this code).
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    14
 *
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    18
 *
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    21
 * questions.
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    22
 */
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    23
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    24
import javax.management.MBeanServer;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    25
import javax.management.MBeanServerFactory;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    26
import javax.management.NotCompliantMBeanException;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    27
import javax.management.ObjectName;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    28
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    29
/*
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    30
 * @test
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    31
 * @bug 8010285
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    32
 * @summary Test fallback for private MBean interfaces.
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    33
 *          It needs to be a separate class because the "jdk.jmx.mbeans.allowNonPublic"
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    34
 *          system property must be set before c.s.j.m.MBeanAnalyzer has been loaded.
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    35
 * @author Jaroslav Bachorik
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 25164
diff changeset
    36
 * @modules java.management
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    37
 * @run clean MBeanFallbackTest
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    38
 * @run build MBeanFallbackTest
25164
06c6cc3a5930 8047073: Some javax/management/ fails with JFR
jbachorik
parents: 18805
diff changeset
    39
 * @run main/othervm -Djdk.jmx.mbeans.allowNonPublic=true MBeanFallbackTest
18805
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    40
 */
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    41
public class MBeanFallbackTest {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    42
    private static interface PrivateMBean {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    43
        public int[] getInts();
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    44
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    45
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    46
    public static class Private implements PrivateMBean {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    47
        public int[] getInts() {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    48
            return new int[]{1,2,3};
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    49
        }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    50
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    51
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    52
    private static int failures = 0;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    53
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    54
    public static void main(String[] args) throws Exception {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    55
        testPrivate(PrivateMBean.class, new Private());
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    56
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    57
        if (failures == 0)
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    58
            System.out.println("Test passed");
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    59
        else
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    60
            throw new Exception("TEST FAILURES: " + failures);
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    61
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    62
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    63
    private static void fail(String msg) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    64
        failures++;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    65
        System.out.println("FAIL: " + msg);
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    66
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    67
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    68
    private static void success(String msg) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    69
        System.out.println("OK: " + msg);
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    70
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    71
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    72
    private static void testPrivate(Class<?> iface, Object bean) throws Exception {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    73
        try {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    74
            System.out.println("Registering a private MBean " +
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    75
                                iface.getName() + " ...");
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    76
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    77
            MBeanServer mbs = MBeanServerFactory.newMBeanServer();
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    78
            ObjectName on = new ObjectName("test:type=Compliant");
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    79
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    80
            mbs.registerMBean(bean, on);
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    81
            success("Registered a private MBean - " + iface.getName());
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    82
        } catch (Exception e) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    83
            Throwable t = e;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    84
            while (t != null && !(t instanceof NotCompliantMBeanException)) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    85
                t = t.getCause();
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    86
            }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    87
            if (t != null) {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    88
                fail("MBean not registered");
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    89
            } else {
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    90
                throw e;
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    91
            }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    92
        }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    93
    }
b359f8adc8ad 8010285: Enforce the requirement of Management Interfaces being public
jbachorik
parents:
diff changeset
    94
}