jdk/test/javax/management/context/ContextForwarderTest.java
author emcmanus
Fri, 07 Nov 2008 11:48:07 +0100
changeset 1570 4165709c91e3
permissions -rw-r--r--
5072267: A way to communicate client context such as locale to the JMX server Summary: Support for client contexts and also for localization of descriptions Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1570
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
     1
/*
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
     2
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
     4
 *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
     8
 *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    13
 * accompanied this code).
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    14
 *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    18
 *
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    21
 * have any questions.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    22
 */
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    23
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    24
/*
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    25
 * @test
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    26
 * @bug 5072267
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    27
 * @summary Test that a context forwarder can be created and then installed.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    28
 * @author Eamonn McManus
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    29
 */
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    30
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    31
/* The specific thing we're testing for is that the forwarder can be created
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    32
 * with a null "next", and then installed with a real "next".  An earlier
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    33
 * defect meant that in this case the simulated jmx.context// namespace had a
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    34
 * null handler that never changed.
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    35
 */
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    36
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    37
import java.util.HashMap;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    38
import java.util.Map;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    39
import java.util.Set;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    40
import java.util.TreeSet;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    41
import javax.management.ClientContext;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    42
import javax.management.MBeanServer;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    43
import javax.management.MBeanServerConnection;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    44
import javax.management.MBeanServerFactory;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    45
import javax.management.ObjectName;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    46
import javax.management.remote.JMXConnector;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    47
import javax.management.remote.JMXConnectorFactory;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    48
import javax.management.remote.JMXConnectorServer;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    49
import javax.management.remote.JMXConnectorServerFactory;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    50
import javax.management.remote.JMXServiceURL;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    51
import javax.management.remote.MBeanServerForwarder;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    52
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    53
public class ContextForwarderTest {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    54
    private static String failure;
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    55
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    56
    public static void main(String[] args) throws Exception {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    57
        MBeanServer mbs = MBeanServerFactory.newMBeanServer();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    58
        JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    59
        Map<String, String> env = new HashMap<String, String>();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    60
        env.put(JMXConnectorServer.CONTEXT_FORWARDER, "false");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    61
        JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    62
                url, env, mbs);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    63
        MBeanServerForwarder sysMBSF = cs.getSystemMBeanServerForwarder();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    64
        MBeanServerForwarder mbsf = ClientContext.newContextForwarder(mbs, sysMBSF);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    65
        sysMBSF.setMBeanServer(mbsf);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    66
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    67
        int localCount = mbs.getMBeanCount();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    68
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    69
        cs.start();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    70
        try {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    71
            JMXConnector cc = JMXConnectorFactory.connect(cs.getAddress());
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    72
            MBeanServerConnection mbsc = cc.getMBeanServerConnection();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    73
            mbsc = ClientContext.withContext(mbsc, "foo", "bar");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    74
            int contextCount = mbsc.getMBeanCount();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    75
            if (localCount + 1 != contextCount) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    76
                fail("Local MBean count %d, context MBean count %d",
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    77
                        localCount, contextCount);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    78
            }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    79
            Set<ObjectName> localNames =
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    80
                    new TreeSet<ObjectName>(mbs.queryNames(null, null));
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    81
            ObjectName contextNamespaceObjectName =
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    82
                    new ObjectName(ClientContext.NAMESPACE + "//:type=JMXNamespace");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    83
            if (!localNames.add(contextNamespaceObjectName))
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    84
                fail("Local names already contained context namespace handler");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    85
            Set<ObjectName> contextNames = mbsc.queryNames(null, null);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    86
            if (!localNames.equals(contextNames)) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    87
                fail("Name set differs locally and in context: " +
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    88
                        "local: %s; context: %s", localNames, contextNames);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    89
            }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    90
        } finally {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    91
            cs.stop();
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    92
        }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    93
        if (failure != null)
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    94
            throw new Exception("TEST FAILED: " + failure);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    95
        else
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    96
            System.out.println("TEST PASSED");
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    97
    }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    98
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
    99
    private static void fail(String msg, Object... params) {
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   100
        failure = String.format(msg, params);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   101
        System.out.println("FAIL: " + failure);
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   102
    }
4165709c91e3 5072267: A way to communicate client context such as locale to the JMX server
emcmanus
parents:
diff changeset
   103
}