jdk/test/javax/management/remote/mandatory/connection/BrokenConnectionTest.java
author dbuck
Tue, 18 Aug 2015 04:29:28 -0700
changeset 32417 6859107fc6c3
parent 32018 91c91b3d50a0
child 43503 bc7f8619ab70
permissions -rw-r--r--
8133666: OperatingSystemMXBean reports abnormally high machine CPU consumption on Linux Reviewed-by: sla, mgronlun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 23010
diff changeset
     2
 * Copyright (c) 2003, 2015, 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
20761
b14f02adbe01 8025205: Intermittent test failure: javax/management/remote/mandatory/connection/BrokenConnectionTest.java
sjiang
parents: 5506
diff changeset
    26
 * @bug 4940957 8025205
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Tests behaviour when connections break
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @author Eamonn McManus
32018
91c91b3d50a0 8132094: Mark intermittently failuring core-svc tests
ykantser
parents: 31909
diff changeset
    29
 * @key intermittent
30376
2ccf2cf7ea48 8078896: Add @modules as needed to the jdk_svc tests
ykantser
parents: 23010
diff changeset
    30
 * @modules java.management
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run clean BrokenConnectionTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * @run build BrokenConnectionTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @run main BrokenConnectionTest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.nio.channels.ServerSocketChannel;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.rmi.server.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.rmi.UnmarshalException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.management.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.management.remote.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.management.remote.rmi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
// resolve ambiguity
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.lang.reflect.Proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class BrokenConnectionTest {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private static ObjectName DELEGATE_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static ObjectName BREAK_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private static ObjectName LISTENER_NAME;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        DELEGATE_NAME =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            new ObjectName("JMImplementation:type=MBeanServerDelegate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        BREAK_NAME = new ObjectName("test:type=Break");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        LISTENER_NAME = new ObjectName("test:type=Listener");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        String failed = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        final String[] protos = {"rmi", "jmxmp"};
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        for (int i = 0; i < protos.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            final String proto = protos[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            System.out.println("------- Testing for " + proto + " -------");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                if (!test(proto))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                    failed += " " + proto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                System.out.println("FAILED WITH EXCEPTION:");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                failed += " " + proto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (failed.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            System.out.println("TEST FAILED FOR:" + failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        System.out.println("Test passed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    private static boolean test(String proto) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (proto.equals("rmi"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            return rmiTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        else if (proto.equals("jmxmp"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return jmxmpTest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new AssertionError(proto);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    private static interface Breakable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        public JMXConnectorServer createConnectorServer(MBeanServer mbs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        public void setBroken(boolean broken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    private static interface TestAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        public String toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        public boolean test(MBeanServerConnection mbsc, Breakable breakable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private static abstract class Operation implements TestAction {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            return opName() + ", break, " + opName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        void init(MBeanServerConnection mbsc) throws Exception {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        abstract String opName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        public boolean test(MBeanServerConnection mbsc, Breakable breakable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            init(mbsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            operation(mbsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            System.out.println("Client ran " + opName() + " OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            breakable.setBroken(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            System.out.println("Broke connection, run " + opName() + " again");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                operation(mbsc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                System.out.println("TEST FAILED: " + opName() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                   " should fail!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                System.out.println("Got IOException as expected (" + e + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        abstract void operation(MBeanServerConnection mbsc) throws Exception;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    private static TestAction[] tests = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                return "getDefaultDomain";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                mbsc.getDefaultDomain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                return "addNotificationListener(NL)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                mbsc.addNotificationListener(DELEGATE_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                             new CountListener(), null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                return "addNotificationListener(MB)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            void init(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                mbsc.createMBean(CountListener.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                                 LISTENER_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                mbsc.addNotificationListener(DELEGATE_NAME, LISTENER_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                                             null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                return "removeNotificationListener(NL)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            void init(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                for (int i = 0; i < NLISTENERS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    NotificationListener l = new CountListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    mbsc.addNotificationListener(DELEGATE_NAME, l, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    listeners.add(l);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                NotificationListener l = (NotificationListener)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    listeners.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                mbsc.removeNotificationListener(DELEGATE_NAME, l, null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            static final int NLISTENERS = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            List/*<NotificationListener>*/ listeners = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                return "removeNotificationListener(MB)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            void init(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                mbsc.createMBean(CountListener.class.getName(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                                 LISTENER_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                    mbsc.removeNotificationListener(DELEGATE_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                                                    LISTENER_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                                                    null, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    throw new IllegalArgumentException("removeNL should not " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                                                       "have worked!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                } catch (ListenerNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                    // normal - there isn't one!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                return "createMBean(className, objectName)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                ObjectName name =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    new ObjectName("test:instance=" + nextInstance());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                mbsc.createMBean(CountListener.class.getName(), name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            private synchronized int nextInstance() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                return ++instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            private int instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                return "getAttribute";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                mbsc.getAttribute(DELEGATE_NAME, "ImplementationName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                return "getAttributes";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                mbsc.getAttribute(DELEGATE_NAME, "ImplementationName");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                return "getDomains";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                mbsc.getDomains();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                return "getMBeanCount";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                mbsc.getMBeanCount();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                return "getMBeanInfo";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                mbsc.getMBeanInfo(DELEGATE_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                return "getObjectInstance";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                mbsc.getObjectInstance(DELEGATE_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                return "invoke";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                mbsc.invoke(BREAK_NAME, "doNothing", new Object[0],
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                            new String[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                return "isInstanceOf";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                mbsc.isInstanceOf(DELEGATE_NAME, "whatsit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                return "isRegistered";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                mbsc.isRegistered(DELEGATE_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                return "queryMBeans";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                mbsc.queryMBeans(new ObjectName("*:*"), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                return "queryNames";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                mbsc.queryNames(new ObjectName("*:*"), null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                return "setAttribute";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                mbsc.setAttribute(BREAK_NAME,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                                  new Attribute("Nothing", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                return "setAttributes";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                AttributeList attrs = new AttributeList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                attrs.add(new Attribute("Nothing", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                mbsc.setAttributes(BREAK_NAME, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        new Operation() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            String opName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                return "unregisterMBean";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            void init(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                for (int i = 0; i < NBEANS; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                    ObjectName name = new ObjectName("test:instance=" + i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    mbsc.createMBean(CountListener.class.getName(), name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    names.add(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
            void operation(MBeanServerConnection mbsc) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                ObjectName name = (ObjectName) names.remove(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                mbsc.unregisterMBean(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            private static final int NBEANS = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            private List/*<ObjectName>*/ names = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        new TestAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                return "break during send for setAttribute";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            public boolean test(MBeanServerConnection mbsc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                Breakable breakable) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                Attribute attr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    new Attribute("Break", new BreakWhenSerialized(breakable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    mbsc.setAttribute(BREAK_NAME, attr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    System.out.println("TEST FAILED: setAttribute with " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                                       "BreakWhenSerializable did not fail!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    System.out.println("Got IOException as expected: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        new TestAction() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                return "break during receive for getAttribute";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            public boolean test(MBeanServerConnection mbsc,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                                Breakable breakable) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                    mbsc.getAttribute(BREAK_NAME, "Break");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    System.out.println("TEST FAILED: getAttribute of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                                       "BreakWhenSerializable did not fail!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                    System.out.println("Got IOException as expected: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        },
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    public static interface BreakMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        public BreakWhenSerialized getBreak();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        public void setBreak(BreakWhenSerialized x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
//      public void breakOnNotify();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        public void doNothing();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        public void setNothing(Object x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public static class Break
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            extends NotificationBroadcasterSupport implements BreakMBean {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        public Break(Breakable breakable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            this.breakable = breakable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        public BreakWhenSerialized getBreak() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            return new BreakWhenSerialized(breakable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        public void setBreak(BreakWhenSerialized x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            throw new IllegalArgumentException("setBreak worked but " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                                               "should not!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
//      public void breakOnNotify() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
//          Notification broken = new Notification("type", "source", 0L);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
//          broken.setUserData(new BreakWhenSerialized(breakable));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
//          sendNotification(broken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
//      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        public void doNothing() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        public void setNothing(Object x) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        private final Breakable breakable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    private static class BreakWhenSerialized implements Serializable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        BreakWhenSerialized(Breakable breakable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            this.breakable = breakable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        private void writeObject(ObjectOutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
            breakable.setBroken(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        private final transient Breakable breakable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    private static class FailureNotificationFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
            implements NotificationFilter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        public boolean isNotificationEnabled(Notification n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
            System.out.println("Filter: " + n + " (" + n.getType() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            final String failed =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                JMXConnectionNotification.FAILED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
            return (n instanceof JMXConnectionNotification
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                    && n.getType().equals(JMXConnectionNotification.FAILED));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    public static interface CountListenerMBean {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    public static class CountListener
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            implements CountListenerMBean, NotificationListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        public synchronized void handleNotification(Notification n, Object h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            count++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        int count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    private static boolean test(Breakable breakable)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        boolean alreadyMissedFailureNotif = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        String failed = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        for (int i = 1; i <= tests.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            TestAction ta = tests[i - 1];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            System.out.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
            System.out.println("Test " + i + ": " + ta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            MBeanServer mbs = MBeanServerFactory.newMBeanServer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            Break breakMBean = new Break(breakable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            mbs.registerMBean(breakMBean, BREAK_NAME);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            JMXConnectorServer cs = breakable.createConnectorServer(mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            System.out.println("Created and started connector server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            JMXServiceURL addr = cs.getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            JMXConnector cc = JMXConnectorFactory.connect(addr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            CountListener failureListener = new CountListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            NotificationFilter failureFilter = new FailureNotificationFilter();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            cc.addConnectionNotificationListener(failureListener,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                                                 failureFilter,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                                                 null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            MBeanServerConnection mbsc = cc.getMBeanServerConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            System.out.println("Client connected OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            boolean thisok = ta.test(mbsc, breakable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                System.out.println("Stopping server");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                cs.stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                System.out.println("Ignoring exception on stop: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            if (thisok) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                System.out.println("Waiting for failure notif");
20761
b14f02adbe01 8025205: Intermittent test failure: javax/management/remote/mandatory/connection/BrokenConnectionTest.java
sjiang
parents: 5506
diff changeset
   490
                // pass or test timeout. see 8025205
b14f02adbe01 8025205: Intermittent test failure: javax/management/remote/mandatory/connection/BrokenConnectionTest.java
sjiang
parents: 5506
diff changeset
   491
                do {
b14f02adbe01 8025205: Intermittent test failure: javax/management/remote/mandatory/connection/BrokenConnectionTest.java
sjiang
parents: 5506
diff changeset
   492
                    Thread.sleep(100);
b14f02adbe01 8025205: Intermittent test failure: javax/management/remote/mandatory/connection/BrokenConnectionTest.java
sjiang
parents: 5506
diff changeset
   493
                } while (failureListener.count < 1);
b14f02adbe01 8025205: Intermittent test failure: javax/management/remote/mandatory/connection/BrokenConnectionTest.java
sjiang
parents: 5506
diff changeset
   494
b14f02adbe01 8025205: Intermittent test failure: javax/management/remote/mandatory/connection/BrokenConnectionTest.java
sjiang
parents: 5506
diff changeset
   495
                Thread.sleep(1000); // if more notif coming ...
b14f02adbe01 8025205: Intermittent test failure: javax/management/remote/mandatory/connection/BrokenConnectionTest.java
sjiang
parents: 5506
diff changeset
   496
                if (failureListener.count > 1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    System.out.println("Got too many failure notifs: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                                       failureListener.count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                    thisok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            if (!thisok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
                failed = failed + " " + i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            System.out.println("Test " + i + (thisok ? " passed" : " FAILED"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            breakable.setBroken(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        if (failed.equals(""))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            System.out.println("FAILING CASES:" + failed);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    private static class BreakableRMI implements Breakable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        public JMXConnectorServer createConnectorServer(MBeanServer mbs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
            JMXServiceURL url = new JMXServiceURL("rmi", null, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            Map env = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            env.put(RMIConnectorServer.RMI_SERVER_SOCKET_FACTORY_ATTRIBUTE,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                    brssf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
            JMXConnectorServer cs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                JMXConnectorServerFactory.newJMXConnectorServer(url, env, mbs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
            cs.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            return cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        public void setBroken(boolean broken) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            brssf.setBroken(broken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        private final BreakableRMIServerSocketFactory brssf =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            new BreakableRMIServerSocketFactory();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    private static boolean rmiTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
        System.out.println("RMI broken connection test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        Breakable breakable = new BreakableRMI();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        return test(breakable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    private static class BreakableRMIServerSocketFactory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            implements RMIServerSocketFactory {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        public synchronized ServerSocket createServerSocket(int port)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            if (broken)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                throw new IOException("ServerSocket has been broken");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            BreakableServerSocket bss = new BreakableServerSocket(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            bssList.add(bss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
            return bss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        synchronized void setBroken(boolean broken) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            this.broken = broken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
//          System.out.println("BRSSF.setBroken(" + broken + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            for (Iterator it = bssList.iterator(); it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
                BreakableServerSocket bss = (BreakableServerSocket) it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
//              System.out.println((broken ? "" : "un") + "break " + bss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                bss.setBroken(broken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        private final List/*<BreakableServerSocket>*/ bssList =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        private boolean broken = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    private static class BreakableJMXMP implements Breakable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
        BreakableJMXMP() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
            bss = new BreakableServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        public JMXConnectorServer createConnectorServer(MBeanServer mbs)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                InvocationHandler scsih =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                    new SocketConnectionServerInvocationHandler(bss);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                final String mcs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                    "javax.management.remote.generic.MessageConnectionServer";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                final Class messageConnectionServerClass = Class.forName(mcs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                final Class[] proxyInterfaces = {messageConnectionServerClass};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                Object socketConnectionServer =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                    Proxy.newProxyInstance(this.getClass().getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                                           proxyInterfaces,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                                           scsih);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                Map env = new HashMap();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                env.put("jmx.remote.message.connection.server",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                        socketConnectionServer);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                final String gcs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    "javax.management.remote.generic.GenericConnectorServer";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                final Class genericConnectorServerClass = Class.forName(gcs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                final Class[] constrTypes = {Map.class, MBeanServer.class};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                final Constructor constr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    genericConnectorServerClass.getConstructor(constrTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                JMXConnectorServer cs = (JMXConnectorServer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    constr.newInstance(new Object[] {env, mbs});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                cs.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                return cs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                e.printStackTrace(System.out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                throw new AssertionError(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        public void setBroken(boolean broken) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            bss.setBroken(broken);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
        private final BreakableServerSocket bss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    private static boolean jmxmpTest() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        System.out.println("JMXMP broken connection test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            Class.forName("javax.management.remote.generic.GenericConnector");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        } catch (ClassNotFoundException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
            System.out.println("Optional classes not present, skipping test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        Breakable breakable = new BreakableJMXMP();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        return test(breakable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
    private static class BreakableServerSocket extends ServerSocket {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        BreakableServerSocket(int port) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            ss = new ServerSocket(port);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        synchronized void setBroken(boolean broken) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
            this.broken = broken;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
//          System.out.println("BSS.setBroken(" + broken + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
            if (!broken)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            for (Iterator it = sList.iterator(); it.hasNext(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                Socket s = (Socket) it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
//                  System.out.println("Break: " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
                } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                    System.out.println("Unable to close socket: " + s +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                                       ", ignoring (" + e + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                it.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        public void bind(SocketAddress endpoint) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            ss.bind(endpoint);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        public void bind(SocketAddress endpoint, int backlog)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
                throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            ss.bind(endpoint, backlog);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
        public InetAddress getInetAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            return ss.getInetAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        public int getLocalPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
            return ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
        public SocketAddress getLocalSocketAddress() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            return ss.getLocalSocketAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        public Socket accept() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
//          System.out.println("BSS.accept");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            Socket s = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
//          System.out.println("BSS.accept returned: " + s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            if (broken)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
                s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
                sList.add(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            return s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
        public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        public ServerSocketChannel getChannel() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            return ss.getChannel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        public boolean isBound() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            return ss.isBound();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        public boolean isClosed() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            return ss.isClosed();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        public void setSoTimeout(int timeout) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            ss.setSoTimeout(timeout);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        public int getSoTimeout() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
            return ss.getSoTimeout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        public void setReuseAddress(boolean on) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            ss.setReuseAddress(on);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        public boolean getReuseAddress() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
            return ss.getReuseAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
            return "BreakableServerSocket wrapping " + ss.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
        public void setReceiveBufferSize (int size) throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
            ss.setReceiveBufferSize(size);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        public int getReceiveBufferSize() throws SocketException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            return ss.getReceiveBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
        private final ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        private final List/*<Socket>*/ sList = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
        private boolean broken = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    /* We do a lot of messy reflection stuff here because we don't
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
       want to reference the optional parts of the JMX Remote API in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
       an environment (J2SE) where they won't be present.  */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
    /* This class implements the logic that allows us to pretend that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
       we have a class that looks like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
       class SocketConnectionServer implements MessageConnectionServer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
           public MessageConnection accept() throws IOException {...}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
           public JMXServiceURL getAddress() {...}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
           public void start(Map env) throws IOException {...}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
           public void stop() throws IOException {...}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    private static class SocketConnectionServerInvocationHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            implements InvocationHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        SocketConnectionServerInvocationHandler(ServerSocket ss) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            this.ss = ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        public Object invoke(Object proxy, Method method, Object[] args)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            final String mname = method.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                if (mname.equals("accept"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                    return accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                else if (mname.equals("getAddress"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                    return getAddress();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                else if (mname.equals("start"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                    start((Map) args[0]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                else if (mname.equals("stop"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                    stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                else // probably a method inherited from Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                    return method.invoke(this, args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            } catch (InvocationTargetException ite) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                Throwable t = ite.getCause();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
                if (t instanceof IOException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
                    throw (IOException)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                } else if (t instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                    throw (RuntimeException)t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                    throw ite;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        private Object/*MessageConnection*/ accept() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            System.out.println("SCSIH.accept()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            Socket s = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            Class socketConnectionClass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                Class.forName("com.sun.jmx.remote.socket.SocketConnection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            Constructor constr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                socketConnectionClass.getConstructor(new Class[] {Socket.class});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            return constr.newInstance(new Object[] {s});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
//          InvocationHandler scih = new SocketConnectionInvocationHandler(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
//          Class messageConnectionClass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
//              Class.forName("javax.management.generic.MessageConnection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
//          return Proxy.newProxyInstance(this.getClass().getClassLoader(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
//                                        new Class[] {messageConnectionClass},
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
//                                        scih);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        private JMXServiceURL getAddress() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
            System.out.println("SCSIH.getAddress()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            return new JMXServiceURL("jmxmp", null, ss.getLocalPort());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        private void start(Map env) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            System.out.println("SCSIH.start(" + env + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
        private void stop() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            System.out.println("SCSIH.stop()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
        private final ServerSocket ss;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
}