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