jdk/test/javax/management/remote/mandatory/connectorServer/MBSFPreStartPostStartTest.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 30376 2ccf2cf7ea48
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6227124
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test that setting an MBeanServerForwarder on an already
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          started RMI connector server has the expected behavior.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @author Luis-Miguel Alventosa
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @run clean MBSFPreStartPostStartTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run build MBSFPreStartPostStartTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run main MBSFPreStartPostStartTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.lang.reflect.InvocationHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.lang.reflect.Method;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.management.MBeanServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.management.MBeanServerConnection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.management.MBeanServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.management.remote.JMXConnector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.management.remote.JMXConnectorServer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.management.remote.JMXConnectorServerFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.management.remote.JMXServiceURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.remote.MBeanServerForwarder;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
public class MBSFPreStartPostStartTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static class MBSFInvocationHandler implements InvocationHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        public static MBeanServerForwarder newProxyInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            final InvocationHandler handler = new MBSFInvocationHandler();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            final Class[] interfaces =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                new Class[] {MBeanServerForwarder.class};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            Object proxy = Proxy.newProxyInstance(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                                 MBeanServerForwarder.class.getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                                 interfaces,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                                 handler);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            return MBeanServerForwarder.class.cast(proxy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        public Object invoke(Object proxy, Method method, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            throws Throwable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            final String methodName = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            if (methodName.equals("getMBeanServer")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                return mbs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            if (methodName.equals("setMBeanServer")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                if (args[0] == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                    throw new IllegalArgumentException("Null MBeanServer");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                if (mbs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                    throw new IllegalArgumentException("MBeanServer object " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                                                       "already initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                mbs = (MBeanServer) args[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            flag = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            return method.invoke(mbs, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        public boolean getFlag() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        public void setFlag(boolean flag) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            this.flag = flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        private boolean flag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        private MBeanServer mbs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Run test
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public int runTest(boolean setBeforeStart) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        echo("=-=-= MBSFPreStartPostStartTest: Set MBSF " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
             (setBeforeStart ? "before" : "after") +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
             " starting the connector server =-=-=");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        JMXConnectorServer server = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        JMXConnector client = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        // Create a new MBeanServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        final MBeanServer mbs = MBeanServerFactory.createMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            // Create the JMXServiceURL
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            final JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            // Create a JMXConnectorServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            server = JMXConnectorServerFactory.newJMXConnectorServer(url,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                                                     null,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                                                     mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            // Create MBeanServerForwarder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            MBeanServerForwarder mbsf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                MBSFInvocationHandler.newProxyInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            // Set MBeanServerForwarder before start()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (setBeforeStart)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                server.setMBeanServerForwarder(mbsf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            // Start the JMXConnectorServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            // Set MBeanServerForwarder after start()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            if (!setBeforeStart)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                server.setMBeanServerForwarder(mbsf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            // Create a JMXConnector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            client = server.toJMXConnector(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            // Connect to the connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            client.connect(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            // Get non-secure MBeanServerConnection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            final MBeanServerConnection mbsc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                client.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            // Run method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            mbsc.getDefaultDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            // Check flag in MBeanServerForwarder
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            MBSFInvocationHandler mbsfih =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                (MBSFInvocationHandler) Proxy.getInvocationHandler(mbsf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            if (mbsfih.getFlag() == true) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                echo("OK: Did go into MBeanServerForwarder!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                echo("KO: Didn't go into MBeanServerForwarder!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            echo("Failed to perform operation: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            // Close the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            if (client != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                client.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            // Stop the connector server
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            if (server != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                server.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            // Release the MBeanServer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            if (mbs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                MBeanServerFactory.releaseMBeanServer(mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Print message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    private static void echo(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        System.out.println(message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * Standalone entry point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Run the test and report to stdout.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public static void main (String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        int error = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        MBSFPreStartPostStartTest test = new MBSFPreStartPostStartTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        // Set MBSF before start()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        error += test.runTest(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        // Set MBSF after start()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        error += test.runTest(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        // Check test results
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (error > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            echo(">>> Unhappy Bye, Bye!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                "Test FAILED: Unexpected error!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            echo(">>> Happy Bye, Bye!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
}