aboutsummaryrefslogtreecommitdiff
path: root/src/share/classes/javax/management/namespace/MBeanServerConnectionWrapper.java
blob: 7eeda961e31756773b635df5aa45fb44c2fc3207 (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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
/*
 * 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.  Sun designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Sun in the LICENSE file that accompanied this code.
 *
 * 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.
 */

package javax.management.namespace;

import com.sun.jmx.mbeanserver.Util;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Set;

import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceAlreadyExistsException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
import javax.management.InvalidAttributeValueException;
import javax.management.ListenerNotFoundException;
import javax.management.MBeanException;
import javax.management.MBeanInfo;
import javax.management.MBeanRegistrationException;
import javax.management.MBeanServer;
import javax.management.MBeanServerConnection;
import javax.management.NotCompliantMBeanException;
import javax.management.NotificationFilter;
import javax.management.NotificationListener;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import javax.management.OperationsException;
import javax.management.QueryExp;
import javax.management.ReflectionException;
import javax.management.loading.ClassLoaderRepository;

/**
 * <p>An object of this class implements the MBeanServer interface
 * and, for each of its methods forwards the request to a wrapped
 * {@link MBeanServerConnection} object.
 * Some methods of the {@link MBeanServer} interface do not have
 * any equivalent in {@link MBeanServerConnection}. In that case, an
 * {@link UnsupportedOperationException} will be thrown.
 *
 * <p>A typical use of this class is to apply a {@link QueryExp} object locally,
 * on an MBean that resides in a remote MBeanServer. Since an
 * MBeanServerConnection is not an MBeanServer, it cannot be passed
 * to the <code>setMBeanServer()</code> method of the {@link QueryExp}
 * object. However, this object can.</p>
 *
 * @since 1.7
 */
public class MBeanServerConnectionWrapper
        implements MBeanServer {

    private final MBeanServerConnection wrapped;
    private final ClassLoader defaultCl;

    /**
     * Construct a new object that implements {@link MBeanServer} by
     * forwarding its methods to the given {@link MBeanServerConnection}.
     * This constructor is equivalent to {@link #MBeanServerConnectionWrapper(
     * MBeanServerConnection, ClassLoader) MBeanServerConnectionWrapper(wrapped,
     * null)}.
     *
     * @param wrapped the {@link MBeanServerConnection} to which methods
     * are to be forwarded.  This parameter can be null, in which case the
     * {@code MBeanServerConnection} will typically be supplied by overriding
     * {@link #getMBeanServerConnection}.
     */
    public MBeanServerConnectionWrapper(MBeanServerConnection wrapped) {
        this(wrapped, null);
    }

    /**
     * Construct a new object that implements {@link MBeanServer} by
     * forwarding its methods to the given {@link MBeanServerConnection}.
     * The {@code defaultCl} parameter specifies the value to be returned
     * by {@link #getDefaultClassLoader}.  A null value is equivalent to
     * {@link Thread#getContextClassLoader()}.
     *
     * @param wrapped the {@link MBeanServerConnection} to which methods
     * are to be forwarded.  This parameter can be null, in which case the
     * {@code MBeanServerConnection} will typically be supplied by overriding
     * {@link #getMBeanServerConnection}.
     * @param defaultCl the value to be returned by {@link
     * #getDefaultClassLoader}.  A null value is equivalent to the current
     * thread's {@linkplain Thread#getContextClassLoader()}.
     */
    public MBeanServerConnectionWrapper(MBeanServerConnection wrapped,
            ClassLoader defaultCl) {
        this.wrapped = wrapped;
        this.defaultCl = (defaultCl == null) ?
            Thread.currentThread().getContextClassLoader() : defaultCl;
    }

    /**
     * Returns an MBeanServerConnection. This method is called each time
     * an operation must be invoked on the underlying MBeanServerConnection.
     * The default implementation returns the MBeanServerConnection that
     * was supplied to the constructor of this MBeanServerConnectionWrapper.
     **/
    protected MBeanServerConnection getMBeanServerConnection() {
        return wrapped;
    }

    /**
     * Returns the default class loader passed to the constructor.  If the
     * value passed was null, then the returned value will be the
     * {@linkplain Thread#getContextClassLoader() context class loader} at the
     * time this object was constructed.
     *
     * @return the ClassLoader that was passed to the constructor.
     **/
    public ClassLoader getDefaultClassLoader() {
        return defaultCl;
    }

    /**
     * <p>This method is called each time an IOException is raised when
     * trying to forward an operation to the underlying
     * MBeanServerConnection, as a result of calling
     * {@link #getMBeanServerConnection()} or as a result of invoking the
     * operation on the returned connection.  Since the methods in
     * {@link MBeanServer} are not declared to throw {@code IOException},
     * this method must return a {@code RuntimeException} to be thrown
     * instead.  Typically, the original {@code IOException} will be in the
     * {@linkplain Throwable#getCause() cause chain} of the {@code
     * RuntimeException}.</p>
     *
     * <p>Subclasses may redefine this method if they need to perform any
     * specific handling of IOException (logging etc...).</p>
     *
     * @param x The raised IOException.
     * @param method The name of the method in which the exception was
     *        raised. This is one of the methods of the MBeanServer
     *        interface.
     *
     * @return A RuntimeException that should be thrown by the caller.
     *         In this default implementation, this is a
     *         {@link RuntimeException} wrapping <var>x</var>.
     **/
    protected RuntimeException wrapIOException(IOException x, String method) {
        return Util.newRuntimeIOException(x);
    }

    // Take care of getMBeanServerConnection returning null.
    //
    private synchronized MBeanServerConnection connection()
        throws IOException {
        final MBeanServerConnection c = getMBeanServerConnection();
        if (c == null)
            throw new IOException("MBeanServerConnection unavailable");
        return c;
    }

    //--------------------------------------------
    //--------------------------------------------
    //
    // Implementation of the MBeanServer interface
    //
    //--------------------------------------------
    //--------------------------------------------

    /**
     * Forward this method to the
     * wrapped object.
     */
    public void addNotificationListener(ObjectName name,
                                        NotificationListener listener,
                                        NotificationFilter filter,
                                        Object handback)
        throws InstanceNotFoundException {
        try {
            connection().addNotificationListener(name, listener,
                                                 filter, handback);
        } catch (IOException x) {
            throw wrapIOException(x,"addNotificationListener");
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public void addNotificationListener(ObjectName name,
                                        ObjectName listener,
                                        NotificationFilter filter,
                                        Object handback)
        throws InstanceNotFoundException {
        try {
            connection().addNotificationListener(name, listener,
                                                 filter, handback);
        } catch (IOException x) {
            throw wrapIOException(x,"addNotificationListener");
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public ObjectInstance createMBean(String className, ObjectName name)
        throws
        ReflectionException,
        InstanceAlreadyExistsException,
        MBeanRegistrationException,
        MBeanException,
        NotCompliantMBeanException {
        try {
            return connection().createMBean(className, name);
        } catch (IOException x) {
            throw wrapIOException(x,"createMBean");
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public ObjectInstance createMBean(String className, ObjectName name,
                                      Object params[], String signature[])
        throws
        ReflectionException,
        InstanceAlreadyExistsException,
        MBeanRegistrationException,
        MBeanException,
        NotCompliantMBeanException {
        try {
            return connection().createMBean(className, name,
                                            params, signature);
        } catch (IOException x) {
            throw wrapIOException(x,"createMBean");
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public ObjectInstance createMBean(String className,
                                      ObjectName name,
                                      ObjectName loaderName)
        throws
        ReflectionException,
        InstanceAlreadyExistsException,
        MBeanRegistrationException,
        MBeanException,
        NotCompliantMBeanException,
        InstanceNotFoundException {
        try {
            return connection().createMBean(className, name, loaderName);
        } catch (IOException x) {
            throw wrapIOException(x,"createMBean");
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public ObjectInstance createMBean(String className,
                                      ObjectName name,
                                      ObjectName loaderName,
                                      Object params[],
                                      String signature[])
        throws
        ReflectionException,
        InstanceAlreadyExistsException,
        MBeanRegistrationException,
        MBeanException,
        NotCompliantMBeanException,
        InstanceNotFoundException {
        try {
            return connection().createMBean(className, name, loaderName,
                                            params, signature);
        } catch (IOException x) {
            throw wrapIOException(x,"createMBean");
        }
    }

    /**
     * Throws an {@link UnsupportedOperationException}. This behavior can
     * be changed by subclasses.
     * @deprecated see {@link MBeanServer#deserialize(ObjectName,byte[])
     *                 MBeanServer}
     */
    @Deprecated
    public ObjectInputStream deserialize(ObjectName name, byte[] data)
        throws InstanceNotFoundException, OperationsException {
        throw new UnsupportedOperationException("deserialize");
    }

    /**
     * Throws an {@link UnsupportedOperationException}. This behavior can
     * be changed by subclasses.
     * @deprecated see {@link MBeanServer#deserialize(String,byte[])
     *                 MBeanServer}
     */
    @Deprecated
    public ObjectInputStream deserialize(String className, byte[] data)
        throws OperationsException, ReflectionException {
        throw new UnsupportedOperationException("deserialize");
    }

    /**
     * Throws an {@link UnsupportedOperationException}. This behavior can
     * be changed by subclasses.
     * @deprecated see {@link MBeanServer#deserialize(String,ObjectName,byte[])
     *                 MBeanServer}
     */
    @Deprecated
    public ObjectInputStream deserialize(String className,
                                         ObjectName loaderName,
                                         byte[] data)
        throws
        InstanceNotFoundException,
        OperationsException,
        ReflectionException {
        throw new UnsupportedOperationException("deserialize");
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public Object getAttribute(ObjectName name, String attribute)
        throws
        MBeanException,
        AttributeNotFoundException,
        InstanceNotFoundException,
        ReflectionException {
        try {
            return connection().getAttribute(name, attribute);
        } catch (IOException x) {
            throw wrapIOException(x,"getAttribute");
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public AttributeList getAttributes(ObjectName name, String[] attributes)
        throws InstanceNotFoundException, ReflectionException {
        try {
            return connection().getAttributes(name, attributes);
        } catch (IOException x) {
            throw wrapIOException(x,"getAttributes");
        }
    }

    /**
     * Throws an {@link UnsupportedOperationException}. This behavior can
     * be changed by subclasses.
     */
    public ClassLoader getClassLoader(ObjectName loaderName)
        throws InstanceNotFoundException {
        throw new UnsupportedOperationException("getClassLoader");
    }

    /**
     * Returns the {@linkplain #getDefaultClassLoader() default class loader}.
     * This behavior can be changed by subclasses.
     */
    public ClassLoader getClassLoaderFor(ObjectName mbeanName)
        throws InstanceNotFoundException {
        return getDefaultClassLoader();
    }

    /**
     * <p>Returns a {@link ClassLoaderRepository} based on the class loader
     * returned by {@link #getDefaultClassLoader()}.</p>
     * @return a {@link ClassLoaderRepository} that contains a single
     *         class loader, returned by {@link #getDefaultClassLoader()}.
     **/
    public ClassLoaderRepository getClassLoaderRepository() {
        // We return a new ClassLoaderRepository each time this method is
        // called. This is by design, because there's no guarantee that
        // getDefaultClassLoader() will always return the same class loader.
        return Util.getSingleClassLoaderRepository(getDefaultClassLoader());
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public String getDefaultDomain() {
        try {
            return connection().getDefaultDomain();
        } catch (IOException x) {
            throw wrapIOException(x,"getDefaultDomain");
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public String[] getDomains() {
        try {
            return connection().getDomains();
        } catch (IOException x) {
            throw wrapIOException(x,"getDomains");
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public Integer getMBeanCount() {
        try {
            return connection().getMBeanCount();
        } catch (IOException x) {
            throw wrapIOException(x,"getMBeanCount");
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public MBeanInfo getMBeanInfo(ObjectName name)
        throws
        InstanceNotFoundException,
        IntrospectionException,
        ReflectionException {
        try {
            return connection().getMBeanInfo(name);
        } catch (IOException x) {
            throw wrapIOException(x,"getMBeanInfo");
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public ObjectInstance getObjectInstance(ObjectName name)
        throws InstanceNotFoundException {
        try {
            return connection().getObjectInstance(name);
        } catch (IOException x) {
            throw wrapIOException(x,"getObjectInstance");
        }
    }

    /**
     * Throws an {@link UnsupportedOperationException}. This behavior can
     * be changed by subclasses.
     */
    public Object instantiate(String className)
        throws ReflectionException, MBeanException {
        throw new UnsupportedOperationException("instantiate");
    }

    /**
     * Throws an {@link UnsupportedOperationException}. This behavior can
     * be changed by subclasses.
     */
    public Object instantiate(String className,
                              Object params[],
                              String signature[])
        throws ReflectionException, MBeanException {
        throw new UnsupportedOperationException("instantiate");
    }

    /**
     * Throws an {@link UnsupportedOperationException}. This behavior can
     * be changed by subclasses.
     */
    public Object instantiate(String className, ObjectName loaderName)
        throws ReflectionException, MBeanException,
               InstanceNotFoundException {
        throw new UnsupportedOperationException("instantiate");
    }

    /**
     * Throws an {@link UnsupportedOperationException}. This behavior can
     * be changed by subclasses.
     */
    public Object instantiate(String className, ObjectName loaderName,
                              Object params[], String signature[])
        throws ReflectionException, MBeanException,
               InstanceNotFoundException {
        throw new UnsupportedOperationException("instantiate");
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public Object invoke(ObjectName name, String operationName,
                         Object params[], String signature[])
        throws
        InstanceNotFoundException,
        MBeanException,
        ReflectionException {
        try {
            return connection().invoke(name,operationName,params,signature);
        } catch (IOException x) {
            throw wrapIOException(x,"invoke");
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public boolean isInstanceOf(ObjectName name, String className)
        throws InstanceNotFoundException {
        try {
            return connection().isInstanceOf(name, className);
        } catch (IOException x) {
            throw wrapIOException(x,"isInstanceOf");
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public boolean isRegistered(ObjectName name) {
        try {
            return connection().isRegistered(name);
        } catch (IOException x) {
            throw wrapIOException(x,"isRegistered");
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     * If an IOException is raised, returns an empty Set.
     */
    public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) {
        try {
            return connection().queryMBeans(name, query);
        } catch (IOException x) {
            throw wrapIOException(x,"queryMBeans");
            //return Collections.emptySet();
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     * If an IOException is raised, returns an empty Set.
     */
    public Set<ObjectName> queryNames(ObjectName name, QueryExp query) {
        try {
            return connection().queryNames(name, query);
        } catch (IOException x) {
            throw wrapIOException(x,"queryNames");
            //return Collections.emptySet();
        }
    }

    /**
     * Throws an {@link UnsupportedOperationException}. This behavior can
     * be changed by subclasses.
     */
    public ObjectInstance registerMBean(Object object, ObjectName name)
        throws
        InstanceAlreadyExistsException,
        MBeanRegistrationException,
        NotCompliantMBeanException {
        throw new UnsupportedOperationException("registerMBean");
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public void removeNotificationListener(ObjectName name,
                                           NotificationListener listener)
        throws InstanceNotFoundException, ListenerNotFoundException {
        try {
            connection().removeNotificationListener(name, listener);
        } catch (IOException x) {
            throw wrapIOException(x,"removeNotificationListener");
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public void removeNotificationListener(ObjectName name,
                                           NotificationListener listener,
                                           NotificationFilter filter,
                                           Object handback)
        throws InstanceNotFoundException, ListenerNotFoundException {
        try {
            connection().removeNotificationListener(name, listener,
                                                    filter, handback);
        } catch (IOException x) {
            throw wrapIOException(x,"removeNotificationListener");
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public void removeNotificationListener(ObjectName name,
                                           ObjectName listener)
        throws InstanceNotFoundException, ListenerNotFoundException {
        try {
            connection().removeNotificationListener(name, listener);
        } catch (IOException x) {
            throw wrapIOException(x,"removeNotificationListener");
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public void removeNotificationListener(ObjectName name,
                                           ObjectName listener,
                                           NotificationFilter filter,
                                           Object handback)
        throws InstanceNotFoundException, ListenerNotFoundException {
        try {
            connection().removeNotificationListener(name, listener,
                                                    filter, handback);
        } catch (IOException x) {
            throw wrapIOException(x,"removeNotificationListener");
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public void setAttribute(ObjectName name, Attribute attribute)
        throws
        InstanceNotFoundException,
        AttributeNotFoundException,
        InvalidAttributeValueException,
        MBeanException,
        ReflectionException {
        try {
            connection().setAttribute(name, attribute);
        } catch (IOException x) {
            throw wrapIOException(x,"setAttribute");
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public AttributeList setAttributes(ObjectName name,
                                       AttributeList attributes)
        throws InstanceNotFoundException, ReflectionException {
        try {
            return connection().setAttributes(name, attributes);
        } catch (IOException x) {
            throw wrapIOException(x,"setAttributes");
        }
    }

    /**
     * Forward this method to the
     * wrapped object.
     */
    public void unregisterMBean(ObjectName name)
        throws InstanceNotFoundException, MBeanRegistrationException {
        try {
            connection().unregisterMBean(name);
        } catch (IOException x) {
            throw wrapIOException(x,"unregisterMBean");
        }
    }

    //----------------
    // PRIVATE METHODS
    //----------------

}