NamespaceNotificationsTest.java 14.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 */

/*
 *
 * @test NamespaceNotificationsTest.java 1.12
 * @summary General Namespace & Notifications test.
28
 * @bug 5072476
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
 * @author Daniel Fuchs
 * @run clean NamespaceNotificationsTest
 *            Wombat WombatMBean JMXRemoteTargetNamespace
 *            NamespaceController NamespaceControllerMBean
 * @compile -XDignore.symbol.file=true  NamespaceNotificationsTest.java
 *            Wombat.java WombatMBean.java JMXRemoteTargetNamespace.java
 *            NamespaceController.java NamespaceControllerMBean.java
 * @run main NamespaceNotificationsTest
 */
import com.sun.jmx.remote.util.EventClientConnection;
import java.lang.management.ManagementFactory;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import javax.management.JMX;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.MBeanServerDelegate;
import javax.management.MBeanServerFactory;
import javax.management.MBeanServerNotification;
import javax.management.MalformedObjectNameException;
import javax.management.Notification;
import javax.management.NotificationEmitter;
import javax.management.NotificationListener;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.loading.MLet;
import javax.management.namespace.JMXNamespace;
import javax.management.namespace.JMXNamespaces;
import javax.management.remote.JMXAddressable;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

/**
 *
 * @author Sun Microsystems, Inc.
 */
public class NamespaceNotificationsTest {

    /**
     * A logger for this class.
     **/
    private static final Logger LOG =
            Logger.getLogger(NamespaceNotificationsTest.class.getName());

    /** Creates a new instance of NamespaceNotificationsTest */
    public NamespaceNotificationsTest() {
    }


    public static JMXServiceURL export(MBeanServer server)
    throws Exception {
        final JMXServiceURL in = new JMXServiceURL("rmi",null,0);
        final JMXConnectorServer cs =
                JMXConnectorServerFactory.newJMXConnectorServer(in,null,null);
        final ObjectName csname = ObjectName.
                getInstance(cs.getClass().getPackage().getName()+
                ":type="+cs.getClass().getSimpleName());
        server.registerMBean(cs,csname);
        cs.start();
        return cs.getAddress();
    }

    public static class Counter {
        int count;
        public synchronized int count() {
            count++;
            notifyAll();
            return count;
        }
        public synchronized int peek() {
            return count;
        }
        public synchronized int waitfor(int max, long timeout)
        throws InterruptedException {
            final long start = System.currentTimeMillis();
            while (count < max && timeout > 0) {
                final long rest = timeout -
                        (System.currentTimeMillis() - start);
                if (rest <= 0) break;
                wait(rest);
            }
            return count;
        }
    }

    public static class CounterListener
            implements NotificationListener {
        final private Counter counter;
        public CounterListener(Counter counter) {
            this.counter = counter;
        }
        public void handleNotification(Notification notification,
                Object handback) {
            System.out.println("Received notif from " + handback +
                    ":\n\t" + notification);
            if (!notification.getSource().equals(handback)) {
                System.err.println("OhOh... Unexpected source: \n\t"+
                        notification.getSource()+"\n\twas expecting:\n\t"+
                        handback);
            }
            counter.count();
        }
    }

    public static void simpleTest(String[] args) {
        try {
            final MBeanServer server1 =
                    ManagementFactory.getPlatformMBeanServer();
            final JMXServiceURL url1 = export(server1);

            final MBeanServer server2 =
                    MBeanServerFactory.createMBeanServer("server2");
            final JMXServiceURL url2 = export(server2);

            final MBeanServer server3 =
                    MBeanServerFactory.createMBeanServer("server3");
            final JMXServiceURL url3 = export(server3);

            final ObjectInstance ncinst =
                    NamespaceController.createInstance(server1);

            final NamespaceControllerMBean nc =
                    JMX.newMBeanProxy(server1,ncinst.getObjectName(),
                    NamespaceControllerMBean.class);

            final Map<String,Object> options = new HashMap<String,Object>();
            options.put(JMXRemoteTargetNamespace.CREATE_EVENT_CLIENT,"true");

            final String mount1 =
                    nc.mount(url1,"server1",options);
            final String mount2 = nc.mount(url2,"server1//server2",
                    options);
            final String mount3 = nc.mount(url3,
                    "server1//server2//server3",
                    options);
            final String mount13 = nc.mount(
                    url1,
                    "server3",
                    "server2//server3",
                    options);
            final String mount21 = nc.mount(url1,"server2//server1",
                    options);
            final String mount31 = nc.mount(
                    url1,
                    "server3//server1",
                    "server1",
                    options);
            final String mount32 = nc.mount(
                    url1,
                    "server3//server2",
                    "server2",
                    options);


            final ObjectName deep =
                    new ObjectName("server1//server2//server3//bush:type=Wombat,name=kanga");
            server1.createMBean(Wombat.class.getName(),deep);

            System.err.println("There's a wombat in the bush!");

            final Counter counter = new Counter();

            final NotificationListener listener =
                    new CounterListener(counter);

            final JMXConnector jc = JMXConnectorFactory.connect(url1);
            final MBeanServerConnection aconn =
                    EventClientConnection.getEventConnectionFor(
                        jc.getMBeanServerConnection(),null);
            aconn.addNotificationListener(deep,listener,null,deep);


209 210 211 212
            MBeanServerConnection iconn =
                    JMXNamespaces.narrowToNamespace(aconn, "server1//server1");
            MBeanServerConnection bconn =
                    JMXNamespaces.narrowToNamespace(aconn, "server3");
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
            final ObjectName shallow =
                    new ObjectName("bush:"+
                    deep.getKeyPropertyListString());
            final WombatMBean proxy =
                    JMX.newMBeanProxy(EventClientConnection.getEventConnectionFor(
                        bconn,null),shallow,WombatMBean.class,true);

            ((NotificationEmitter)proxy).
                    addNotificationListener(listener,null,shallow);
            proxy.setCaption("I am a new Wombat!");
            System.err.println("New caption: "+proxy.getCaption());
            final int rcvcount = counter.waitfor(2,3000);
            if (rcvcount != 2)
                throw new RuntimeException("simpleTest failed: "+
                        "received count is " +rcvcount);

            System.err.println("simpleTest passed: got "+rcvcount+
                    " notifs");

        } catch (RuntimeException x) {
            throw x;
        } catch (Exception x) {
            throw new RuntimeException("simpleTest failed: " + x,x);
        }
    }

    public static class LocalNamespace extends
            JMXNamespace {
        LocalNamespace() {
            super(MBeanServerFactory.newMBeanServer());
        }

    }

    public static class ContextObject<K,V> {
        public final K name;
        public final V object;
        public ContextObject(K name, V object) {
            this.name = name;
            this.object = object;
        }
        private Object[] data() {
            return new Object[] {name,object};
        }

        @Override
        public boolean equals(Object x) {
            if (x instanceof ContextObject)
                return Arrays.deepEquals(data(),((ContextObject<?,?>)x).data());
            return false;
        }
        @Override
        public int hashCode() {
            return Arrays.deepHashCode(data());
        }
    }

    private static <K,V> ContextObject<K,V> context(K k, V v) {
        return new ContextObject<K,V>(k,v);
    }

    private static ObjectName name(String name) {
        try {
            return new ObjectName(name);
        } catch(MalformedObjectNameException x) {
            throw new IllegalArgumentException(name,x);
        }
    }

    public static void simpleTest2() {
        try {
            System.out.println("\nsimpleTest2: STARTING\n");
            final LocalNamespace foo = new LocalNamespace();
            final LocalNamespace joe = new LocalNamespace();
            final LocalNamespace bar = new LocalNamespace();
            final MBeanServer server = MBeanServerFactory.newMBeanServer();

            server.registerMBean(foo,JMXNamespaces.getNamespaceObjectName("foo"));
            server.registerMBean(joe,JMXNamespaces.getNamespaceObjectName("foo//joe"));
            server.registerMBean(bar,JMXNamespaces.getNamespaceObjectName("foo//bar"));
            final BlockingQueue<ContextObject<String,MBeanServerNotification>> queue =
                    new ArrayBlockingQueue<ContextObject<String,MBeanServerNotification>>(20);

            final NotificationListener listener = new NotificationListener() {
                public void handleNotification(Notification n, Object handback) {
                    if (!(n instanceof MBeanServerNotification)) {
                        System.err.println("Error: expected MBeanServerNotification");
                        return;
                    }
                    final MBeanServerNotification mbsn =
                            (MBeanServerNotification) n;

                    // We will pass the namespace name in the handback.
                    //
                    final String namespace = (String) handback;
                    System.out.println("Received " + mbsn.getType() +
                            " for MBean " + mbsn.getMBeanName() +
                            " from name space " + namespace);
                    try {
                        queue.offer(context(namespace,mbsn),500,TimeUnit.MILLISECONDS);
                    } catch (Exception x) {
                        System.err.println("Failed to enqueue received notif: "+mbsn);
                        x.printStackTrace();
                    }
                }
            };

            server.addNotificationListener(JMXNamespaces.insertPath("foo//joe",
                    MBeanServerDelegate.DELEGATE_NAME),listener,null,"foo//joe");
            server.addNotificationListener(JMXNamespaces.insertPath("foo//bar",
                    MBeanServerDelegate.DELEGATE_NAME),listener,null,"foo//bar");
            server.createMBean(MLet.class.getName(),
                    name("foo//joe//domain:type=MLet"));
            checkQueue(queue,"foo//joe",
                    MBeanServerNotification.REGISTRATION_NOTIFICATION);
            server.createMBean(MLet.class.getName(),
                    name("foo//bar//domain:type=MLet"));
            checkQueue(queue,"foo//bar",
                    MBeanServerNotification.REGISTRATION_NOTIFICATION);
            server.unregisterMBean(
                    name("foo//joe//domain:type=MLet"));
            checkQueue(queue,"foo//joe",
                    MBeanServerNotification.UNREGISTRATION_NOTIFICATION);
            server.unregisterMBean(
                    name("foo//bar//domain:type=MLet"));
            checkQueue(queue,"foo//bar",
                    MBeanServerNotification.UNREGISTRATION_NOTIFICATION);
        } catch (RuntimeException x) {
            System.err.println("FAILED: "+x);
            throw x;
        } catch(Exception x) {
            System.err.println("FAILED: "+x);
            throw new RuntimeException("Unexpected exception: "+x,x);
        }
    }


    private static void checkQueue(
            BlockingQueue<ContextObject<String,MBeanServerNotification>> q,
                              String path, String type) {
        try {
          final ContextObject<String,MBeanServerNotification> ctxt =
                    q.poll(500,TimeUnit.MILLISECONDS);
          if (ctxt == null)
            throw new RuntimeException("Timeout expired: expected notif from "+
                    path +", type="+type);
          if (!ctxt.name.equals(path))
            throw new RuntimeException("expected notif from "+
                    path +", got "+ctxt.name);
          if (!ctxt.object.getType().equals(type))
            throw new RuntimeException(ctxt.name+": expected type="+
                    type +", got "+ctxt.object.getType());
          if (!ctxt.object.getType().equals(type))
            throw new RuntimeException(ctxt.name+": expected type="+
                    type +", got "+ctxt.object.getType());
          if (!ctxt.object.getMBeanName().equals(name("domain:type=MLet")))
            throw new RuntimeException(ctxt.name+": expected MBean=domain:type=MLet"+
                    ", got "+ctxt.object.getMBeanName());
        } catch(InterruptedException x) {
            throw new RuntimeException("unexpected interruption: "+x,x);
        }
    }

    public static void main(String[] args) {
        simpleTest(args);
        simpleTest2();
    }

}