aboutsummaryrefslogtreecommitdiff
path: root/test/javax/management/namespace/JMXDomainTest.java
blob: 258cead1ab079dc321ac9c7856c5842981aa4dcf (plain)
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
28
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
209
210
211
212
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
 * 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 JMXDomainTest.java
 * @bug 5072476
 * @summary Basic test for JMXDomain.
 * @author Daniel Fuchs
 * @run clean JMXDomainTest Wombat WombatMBean
 * @run build JMXDomainTest Wombat WombatMBean
 * @run main JMXDomainTest
 */


import java.util.Collections;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.Map;
import java.util.Set;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.DynamicMBean;
import javax.management.InstanceAlreadyExistsException;
import javax.management.InstanceNotFoundException;
import javax.management.InvalidAttributeValueException;
import javax.management.ListenerNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanNotificationInfo;
import javax.management.MBeanRegistration;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MBeanServerDelegate;
import javax.management.MBeanServerFactory;
import javax.management.MBeanServerNotification;
import javax.management.NotCompliantMBeanException;
import javax.management.Notification;
import javax.management.NotificationBroadcaster;
import javax.management.NotificationEmitter;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.ReflectionException;
import javax.management.namespace.JMXDomain;
import javax.management.namespace.MBeanServerSupport;

/**
 * Test simple creation/registration of namespace.
 *
 */
public class JMXDomainTest {
    private static Map<String,Object> emptyEnvMap() {
        return Collections.emptyMap();
    }


    public static class LocalDomainRepository
            extends MBeanServerSupport {
        private final MBeanServer server;
        private final String      domain;

        public class DynamicMBeanProxy implements DynamicMBean {

            private final MBeanServer server;
            private final ObjectName name;

            public DynamicMBeanProxy(MBeanServer s, ObjectName n) {
                this.server = s;
                this.name = n;
            }

            public Object getAttribute(String attribute)
                    throws AttributeNotFoundException,
                    MBeanException, ReflectionException {
                try {
                    return server.getAttribute(name, attribute);
                } catch (RuntimeException x) {
                    throw x;
                } catch (Exception x) {
                    throw new RuntimeException(x);
                }
            }

            public void setAttribute(Attribute attribute)
                    throws AttributeNotFoundException,
                    InvalidAttributeValueException, MBeanException,
                    ReflectionException {
                try {
                    server.setAttribute(name, attribute);
                } catch (RuntimeException x) {
                    throw x;
                } catch (Exception x) {
                    throw new RuntimeException(x);
                }
            }

            public AttributeList getAttributes(String[] attributes) {
                try {
                    return server.getAttributes(name, attributes);
                } catch (RuntimeException x) {
                    throw x;
                } catch (Exception x) {
                    throw new RuntimeException(x);
                }
            }

            public AttributeList setAttributes(AttributeList attributes) {
                try {
                    return server.setAttributes(name, attributes);
                } catch (RuntimeException x) {
                    throw x;
                } catch (Exception x) {
                    throw new RuntimeException(x);
                }
            }

            public Object invoke(String actionName, Object[] params,
                    String[] signature) throws MBeanException,
                    ReflectionException {
                try {
                    return server.invoke(name, actionName, params, signature);
                } catch (RuntimeException x) {
                    throw x;
                } catch (Exception x) {
                    throw new RuntimeException(x);
                }
            }

            public MBeanInfo getMBeanInfo() {
                try {
                    return server.getMBeanInfo(name);
                } catch (RuntimeException x) {
                    throw x;
                } catch (Exception x) {
                    throw new RuntimeException(x);
                }
            }
        }

        public LocalDomainRepository(String domain) {
            this.server = MBeanServerFactory.newMBeanServer();
            this.domain = domain;
        }

        @Override
        protected Set<ObjectName> getNames() {
            try {
            final ObjectName name =
                    ObjectName.getInstance(domain+":*");
            return server.queryNames(name, null);
            } catch (RuntimeException x) {
                throw x;
            } catch (Exception x) {
                throw new RuntimeException(x);
            }
        }

        @Override
        public DynamicMBean getDynamicMBeanFor(ObjectName name)
                throws InstanceNotFoundException {
            if (server.isRegistered(name))
                return new DynamicMBeanProxy(server, name);
            throw new InstanceNotFoundException(name);
        }


        @Override
        public NotificationEmitter
                getNotificationEmitterFor(final ObjectName name)
                throws InstanceNotFoundException {
            if (server.isInstanceOf(name, NotificationEmitter.class.getName())) {
                return new NotificationEmitter() {

                    public void removeNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws ListenerNotFoundException {
                        try {
                            server.removeNotificationListener(name, listener, filter, handback);
                        } catch (InstanceNotFoundException x) {
                            throw new IllegalArgumentException(String.valueOf(name), x);
                        }
                    }

                    public void addNotificationListener(NotificationListener listener, NotificationFilter filter, Object handback) throws IllegalArgumentException {
                        try {
                            server.addNotificationListener(name, listener, filter, handback);
                        } catch (InstanceNotFoundException x) {
                            throw new IllegalArgumentException(String.valueOf(name), x);
                        }
                    }

                    public void removeNotificationListener(NotificationListener listener) throws ListenerNotFoundException {
                        try {
                            server.removeNotificationListener(name, listener);
                        } catch (InstanceNotFoundException x) {
                            throw new IllegalArgumentException(String.valueOf(name), x);
                        }
                    }

                    public MBeanNotificationInfo[] getNotificationInfo() {
                        try {
                            return server.getMBeanInfo(name).getNotifications();
                        } catch (Exception x) {
                            throw new IllegalArgumentException(String.valueOf(name), x);
                        }
                    }
                };
            }
            return null;
        }

        @Override
        public ObjectInstance registerMBean(Object object, ObjectName name)
                throws InstanceAlreadyExistsException,
                MBeanRegistrationException, NotCompliantMBeanException {
            return server.registerMBean(object, name);
        }

        @Override
        public void unregisterMBean(ObjectName name)
                throws InstanceNotFoundException,
                MBeanRegistrationException {
            server.unregisterMBean(name);
        }

        @Override
        public ObjectInstance createMBean(String className,
                ObjectName name, ObjectName loaderName, Object[] params,
                String[] signature, boolean useCLR)
                throws ReflectionException, InstanceAlreadyExistsException,
                MBeanRegistrationException, MBeanException,
                NotCompliantMBeanException, InstanceNotFoundException {
            if (useCLR && loaderName == null) {
                return server.createMBean(className, name, params, signature);
            }
            return server.createMBean(className, name, loaderName,
                    params, signature);
        }


    }

    private static MBeanServer newMBeanServer() {
        return MBeanServerFactory.newMBeanServer();
    }

    public static interface ThingMBean {}
    public static class Thing implements ThingMBean, MBeanRegistration {
        public ObjectName preRegister(MBeanServer server, ObjectName name)
                throws Exception {
            if (name == null) return new ObjectName(":type=Thing");
            else return name;
        }
        public void postRegister(Boolean registrationDone) {
        }

        public void preDeregister() throws Exception {
        }
        public void postDeregister() {
        }
    }

    /**
     * Test that it is possible to create a dummy MBean with a null
     * ObjectName - this is just a sanity check - as there are already
     * other JMX tests that check that.
     *
     * @throws java.lang.Exception
     */
    public static void testCreateWithNull() throws Exception {
        final MBeanServer server = newMBeanServer();
        final ObjectInstance oi = server.registerMBean(new Thing(),null);
        server.unregisterMBean(oi.getObjectName());
        System.out.println("testCreateWithNull PASSED");
    }

    public static void testRegisterSimple() throws Exception {
        final ObjectName name =
                JMXDomain.getDomainObjectName("gloups");
        final JMXDomain jmxDomain = new JMXDomain(
                MBeanServerFactory.newMBeanServer());
        testRegister("testRegisterSimple: ",name,jmxDomain);
    }

    public static void testRegisterPseudoVirtual()
            throws Exception {
        final ObjectName name =
                JMXDomain.getDomainObjectName("gloups");
        final JMXDomain jmxDomain = new JMXDomain(
                new LocalDomainRepository("gloups"));
        testRegister("testRegisterPseudoVirtual: ",name,jmxDomain);
    }

    public static void testRegister(final String test,
            final ObjectName name,
            final JMXDomain jmxDomain) throws Exception {
        System.out.println(test+" START");
        MBeanServer server = newMBeanServer();
        final ObjectInstance oi =
                server.registerMBean(jmxDomain,name);
        System.out.println(test+"Succesfully registered namespace: "+name);
        if (!server.isRegistered(name))
            fail(test+name+" is not registered!");
        if (!server.queryNames(new ObjectName(name.getDomain()+":*"), null).
                contains(name))
            fail(test+name+" not in queryNames");

        final Thing thing = new Thing();
        final ObjectName thingName = new ObjectName("gloups:type=Thing");
        server.registerMBean(thing,thingName);
        if (!server.isRegistered(thingName))
            fail(test+thingName+" is not registered!");
        if (!jmxDomain.getSourceServer().isRegistered(thingName))
            fail(test+thingName+" is not registered in domain!");
        if (!server.queryNames(new ObjectName(name.getDomain()+":*"), null).
                contains(thingName))
            fail(test+thingName+" not in queryNames");

        server.unregisterMBean(name);
        if (server.isRegistered(thingName))
            fail(test+thingName+" is still registered!");
        if (server.queryNames(new ObjectName(name.getDomain()+":*"), null).
                contains(thingName))
            fail(test+thingName+" still in queryNames");

        server.registerMBean(jmxDomain, name);
        if (!server.isRegistered(thingName))
            fail(test+thingName+" is not registered again!");

        System.out.println(test+" PASSED");
    }

    private static MBeanServerNotification pop(
            BlockingQueue<Notification> queue,
                                    String type,
                                    ObjectName mbean,
                                    String test)
                                    throws InterruptedException {
        final Notification n = queue.poll(1, TimeUnit.SECONDS);
        if (!(n instanceof MBeanServerNotification))
            fail(test+"expected MBeanServerNotification, got "+n);
        final MBeanServerNotification msn = (MBeanServerNotification)n;
        if (!type.equals(msn.getType()))
            fail(test+"expected "+type+", got "+msn.getType());
        if (!mbean.apply(msn.getMBeanName()))
            fail(test+"expected "+mbean+", got "+msn.getMBeanName());
        System.out.println(test+" got: "+msn);
        return msn;
    }
    private static MBeanServerNotification popADD(
            BlockingQueue<Notification> queue,
                                    ObjectName mbean,
                                    String test)
                                    throws InterruptedException {
        return pop(queue, MBeanServerNotification.REGISTRATION_NOTIFICATION,
                mbean, test);
    }

    private static MBeanServerNotification popREM(
            BlockingQueue<Notification> queue,
                                    ObjectName mbean,
                                    String test)
                                    throws InterruptedException {
        return pop(queue, MBeanServerNotification.UNREGISTRATION_NOTIFICATION,
                mbean, test);
    }


    public static void testRegisterNotifSimple() throws Exception {
        final ObjectName name =
                JMXDomain.getDomainObjectName("gloups");
        final JMXDomain jmxDomain = new JMXDomain(
                MBeanServerFactory.newMBeanServer());
        testRegisterNotif("testRegisterNotifSimple: ",name,jmxDomain);
    }

    public static void testRegisterNotifPseudoVirtual()
            throws Exception {
        final ObjectName name =
                JMXDomain.getDomainObjectName("gloups");
        final JMXDomain jmxDomain = new JMXDomain(
                new LocalDomainRepository("gloups"));
        testRegisterNotif("testRegisterNotifPseudoVirtual: ",name,jmxDomain);
    }

    public static void testRegisterNotif(final String test,
            final ObjectName name,
            final JMXDomain jmxDomain) throws Exception {
        System.out.println(test+" START");
        MBeanServer server = newMBeanServer();
        final ObjectInstance oi =
                server.registerMBean(jmxDomain,name);
        System.out.println(test+"Succesfully registered namespace: "+name);
        if (!server.isRegistered(name))
            fail(test+name+" is not registered!");

        final BlockingQueue<Notification> queue =
                new ArrayBlockingQueue<Notification>(10);

        final NotificationListener l = new NotificationListener() {

            public void handleNotification(Notification notification,
                    Object handback) {
                try {
                    if (!queue.offer(notification,5,TimeUnit.SECONDS))
                        throw new RuntimeException("timeout exceeded");
                } catch (Exception x) {
                    fail(test+"failed to handle notif", x);
                }
            }
        };

        server.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME, l,
                null, null);

        final Thing thing = new Thing();
        final ObjectName thingName = new ObjectName("gloups:type=Thing");

        server.registerMBean(thing,thingName);
        if (!jmxDomain.getSourceServer().isRegistered(thingName))
            fail(test+thingName+" is not registered in domain!");
        popADD(queue, thingName, test);
        server.unregisterMBean(thingName);
        if (jmxDomain.getSourceServer().isRegistered(thingName))
            fail(test+thingName+" is still registered in domain!");
        popREM(queue, thingName, test);
        if (queue.size() != 0)
            fail(test+queue.size()+" notifs remain in queue "+queue);

        server.unregisterMBean(name);
        popREM(queue, name, test);

        jmxDomain.getSourceServer().registerMBean(thing,thingName);
        if (server.isRegistered(thingName))
            fail(test+thingName+" is still registered in domain!");
        jmxDomain.getSourceServer().unregisterMBean(thingName);
        if (queue.size() != 0)
            fail(test+queue.size()+" notifs remain in queue "+queue);

        server.registerMBean(jmxDomain, name);
        if (!server.isRegistered(name))
            fail(test+name+" is not registered again!");
        popADD(queue, name, test);
        if (queue.size() != 0)
            fail(test+queue.size()+" notifs remain in queue "+queue);

        server.registerMBean(thing,thingName);
        if (!jmxDomain.getSourceServer().isRegistered(thingName))
            fail(test+thingName+" is not registered in domain!");
        popADD(queue, thingName, test);
        server.unregisterMBean(thingName);
        if (jmxDomain.getSourceServer().isRegistered(thingName))
            fail(test+thingName+" is still registered in domain!");
        popREM(queue, thingName, test);
        if (queue.size() != 0)
            fail(test+queue.size()+" notifs remain in queue "+queue);

        System.out.println(test+" PASSED");
    }



    private static void fail(String msg) {
        raise(new RuntimeException(msg));
    }

    private static void fail(String msg, Throwable cause) {
        raise(new RuntimeException(msg,cause));
    }

    private static void raise(RuntimeException x) {
        lastException = x;
        exceptionCount++;
        throw x;
    }

    private static volatile Exception lastException = null;
    private static volatile int       exceptionCount = 0;

    public static void main(String... args) throws Exception {
        testCreateWithNull();

        testRegisterSimple();
        testRegisterNotifSimple();

        testRegisterPseudoVirtual();
        testRegisterNotifPseudoVirtual();

        if (lastException != null)
            throw lastException;
    }
}