aboutsummaryrefslogtreecommitdiff
path: root/src/share/classes/com/sun/jmx/namespace/RoutingServerProxy.java
blob: f58e39816d34d61acf4a83026b81bc3fda1f2296 (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
/*
 * 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 com.sun.jmx.namespace;


import com.sun.jmx.mbeanserver.Util;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.Collections;
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.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;
import javax.management.namespace.JMXNamespaces;

/**
 * A RoutingServerProxy is an MBeanServer proxy that proxies a
 * source name space in a source MBeanServer.
 * It wraps a source MBeanServer, and rewrites routing ObjectNames.
 * It is typically use for implementing 'cd' operations, and
 * will add the source name space to routing ObjectNames at input,
 * and remove it at output.
 * <p><b>
 * This API is a Sun internal API and is subject to changes without notice.
 * </b></p>
 *
 * @since 1.7
 */
// See class hierarchy and detailled explanations in RoutingProxy in this
// package.
//
public class RoutingServerProxy
        extends RoutingProxy<MBeanServer>
        implements MBeanServer {

    /**
     * Creates a new instance of RoutingServerProxy
     */
    public RoutingServerProxy(MBeanServer source,
                           String sourceNs) {
        this(source,sourceNs,"",false);
    }

    public RoutingServerProxy(MBeanServer source,
                                String sourceNs,
                                String targetNs,
                                boolean forwardsContext) {
        super(source,sourceNs,targetNs,forwardsContext);
    }

    /**
     * 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.
     * Subclasses may redefine this method if they need to perform any
     * specific handling of IOException (logging etc...).
     * @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 an
     *         {@link UndeclaredThrowableException} wrapping <var>x</var>.
     **/
    protected RuntimeException handleIOException(IOException x,
                                                 String method) {
        return Util.newRuntimeIOException(x);
    }


    //--------------------------------------------
    //--------------------------------------------
    //
    // Implementation of the MBeanServer interface
    //
    //--------------------------------------------
    //--------------------------------------------
    @Override
    public void addNotificationListener(ObjectName name,
                                        NotificationListener listener,
                                        NotificationFilter filter,
                                        Object handback)
        throws InstanceNotFoundException {
        try {
            super.addNotificationListener(name, listener,
                                                 filter, handback);
        } catch (IOException x) {
            throw handleIOException(x,"addNotificationListener");
        }
    }

    @Override
    public void addNotificationListener(ObjectName name,
                                        ObjectName listener,
                                        NotificationFilter filter,
                                        Object handback)
        throws InstanceNotFoundException {
        try {
            super.addNotificationListener(name, listener,
                                                 filter, handback);
        } catch (IOException x) {
            throw handleIOException(x,"addNotificationListener");
        }
    }

    @Override
    public ObjectInstance createMBean(String className, ObjectName name)
        throws
        ReflectionException,
        InstanceAlreadyExistsException,
        MBeanRegistrationException,
        MBeanException,
        NotCompliantMBeanException {
        try {
            return super.createMBean(className, name);
        } catch (IOException x) {
            throw handleIOException(x,"createMBean");
        }
    }

    @Override
    public ObjectInstance createMBean(String className, ObjectName name,
                                      Object params[], String signature[])
        throws
        ReflectionException,
        InstanceAlreadyExistsException,
        MBeanRegistrationException,
        MBeanException,
        NotCompliantMBeanException {
        try {
            return super.createMBean(className, name,
                                     params, signature);
        } catch (IOException x) {
            throw handleIOException(x,"createMBean");
        }
    }

    @Override
    public ObjectInstance createMBean(String className,
                                      ObjectName name,
                                      ObjectName loaderName)
        throws
        ReflectionException,
        InstanceAlreadyExistsException,
        MBeanRegistrationException,
        MBeanException,
        NotCompliantMBeanException,
        InstanceNotFoundException {
        try {
            return super.createMBean(className, name, loaderName);
        } catch (IOException x) {
            throw handleIOException(x,"createMBean");
        }
    }

    @Override
    public ObjectInstance createMBean(String className,
                                      ObjectName name,
                                      ObjectName loaderName,
                                      Object params[],
                                      String signature[])
        throws
        ReflectionException,
        InstanceAlreadyExistsException,
        MBeanRegistrationException,
        MBeanException,
        NotCompliantMBeanException,
        InstanceNotFoundException {
        try {
            return super.createMBean(className, name, loaderName,
                                            params, signature);
        } catch (IOException x) {
            throw handleIOException(x,"createMBean");
        }
    }

    /**
     * @deprecated see {@link MBeanServer#deserialize(ObjectName,byte[])
     *                 MBeanServer}
     **/
    @Deprecated
    public ObjectInputStream deserialize(ObjectName name, byte[] data)
        throws InstanceNotFoundException, OperationsException {
        final ObjectName sourceName = toSourceOrRuntime(name);
        try {
            return source().deserialize(sourceName,data);
        } catch (RuntimeException x) {
            throw makeCompliantRuntimeException(x);
        }
    }

    /**
     * @deprecated see {@link MBeanServer#deserialize(String,byte[])
     *                 MBeanServer}
     */
    @Deprecated
    public ObjectInputStream deserialize(String className, byte[] data)
        throws OperationsException, ReflectionException {
        try {
            return source().deserialize(className,data);
        } catch (RuntimeException x) {
            throw makeCompliantRuntimeException(x);
        }
    }

    /**
     * @deprecated see {@link MBeanServer#deserialize(String,ObjectName,byte[])
     *                 MBeanServer}
     */
    @Deprecated
    public ObjectInputStream deserialize(String className,
                                         ObjectName loaderName,
                                         byte[] data)
        throws
        InstanceNotFoundException,
        OperationsException,
        ReflectionException {
        try {
            return source().deserialize(className,loaderName,data);
        } catch (RuntimeException x) {
            throw makeCompliantRuntimeException(x);
        }
    }

    @Override
    public Object getAttribute(ObjectName name, String attribute)
        throws
        MBeanException,
        AttributeNotFoundException,
        InstanceNotFoundException,
        ReflectionException {
        try {
            return super.getAttribute(name, attribute);
        } catch (IOException x) {
            throw handleIOException(x,"getAttribute");
        }
    }

    @Override
    public AttributeList getAttributes(ObjectName name, String[] attributes)
        throws InstanceNotFoundException, ReflectionException {
        try {
            return super.getAttributes(name, attributes);
        } catch (IOException x) {
            throw handleIOException(x,"getAttributes");
        }
    }

    public ClassLoader getClassLoader(ObjectName loaderName)
        throws InstanceNotFoundException {
        final ObjectName sourceName = toSourceOrRuntime(loaderName);
        try {
            return source().getClassLoader(sourceName);
        } catch (RuntimeException x) {
            throw makeCompliantRuntimeException(x);
        }
    }

    public ClassLoader getClassLoaderFor(ObjectName mbeanName)
        throws InstanceNotFoundException {
        final ObjectName sourceName = toSourceOrRuntime(mbeanName);
        try {
            return source().getClassLoaderFor(sourceName);
        } catch (RuntimeException x) {
            throw makeCompliantRuntimeException(x);
        }
    }

    public ClassLoaderRepository getClassLoaderRepository() {
        try {
            return source().getClassLoaderRepository();
        } catch (RuntimeException x) {
            throw makeCompliantRuntimeException(x);
        }
    }

    @Override
    public String getDefaultDomain() {
        try {
            return super.getDefaultDomain();
        } catch (IOException x) {
            throw handleIOException(x,"getDefaultDomain");
        }
    }

    @Override
    public String[] getDomains() {
        try {
            return super.getDomains();
        } catch (IOException x) {
            throw handleIOException(x,"getDomains");
        }
    }

    @Override
    public Integer getMBeanCount() {
        try {
            return super.getMBeanCount();
        } catch (IOException x) {
            throw handleIOException(x,"getMBeanCount");
        }
    }

    @Override
    public MBeanInfo getMBeanInfo(ObjectName name)
        throws
        InstanceNotFoundException,
        IntrospectionException,
        ReflectionException {
        try {
            return super.getMBeanInfo(name);
        } catch (IOException x) {
            throw handleIOException(x,"getMBeanInfo");
        }
    }

    @Override
    public ObjectInstance getObjectInstance(ObjectName name)
        throws InstanceNotFoundException {
        try {
            return super.getObjectInstance(name);
        } catch (IOException x) {
            throw handleIOException(x,"getObjectInstance");
        }
    }

    public Object instantiate(String className)
        throws ReflectionException, MBeanException {
        try {
            return source().instantiate(className);
        } catch (RuntimeException x) {
            throw makeCompliantRuntimeException(x);
        }
    }

    public Object instantiate(String className,
                              Object params[],
                              String signature[])
        throws ReflectionException, MBeanException {
        try {
            return source().instantiate(className,
                    params,signature);
        } catch (RuntimeException x) {
            throw makeCompliantRuntimeException(x);
        }
    }

    public Object instantiate(String className, ObjectName loaderName)
        throws ReflectionException, MBeanException,
               InstanceNotFoundException {
        final ObjectName srcLoaderName = toSourceOrRuntime(loaderName);
        try {
            return source().instantiate(className,srcLoaderName);
        } catch (RuntimeException x) {
            throw makeCompliantRuntimeException(x);
        }
    }

    public Object instantiate(String className, ObjectName loaderName,
                              Object params[], String signature[])
        throws ReflectionException, MBeanException,
               InstanceNotFoundException {
        final ObjectName srcLoaderName = toSourceOrRuntime(loaderName);
        try {
            return source().instantiate(className,srcLoaderName,
                    params,signature);
        } catch (RuntimeException x) {
            throw makeCompliantRuntimeException(x);
        }
    }

    @Override
    public Object invoke(ObjectName name, String operationName,
                         Object params[], String signature[])
        throws
        InstanceNotFoundException,
        MBeanException,
        ReflectionException {
        try {
            return super.invoke(name,operationName,params,signature);
        } catch (IOException x) {
            throw handleIOException(x,"invoke");
        }
    }

    @Override
    public boolean isInstanceOf(ObjectName name, String className)
        throws InstanceNotFoundException {
        try {
            return super.isInstanceOf(name, className);
        } catch (IOException x) {
            throw handleIOException(x,"isInstanceOf");
        }
    }

    @Override
    public boolean isRegistered(ObjectName name) {
        try {
            return super.isRegistered(name);
        } catch (IOException x) {
            throw handleIOException(x,"isRegistered");
        }
    }

    @Override
    public Set<ObjectInstance> queryMBeans(ObjectName name, QueryExp query) {
        try {
            return super.queryMBeans(name, query);
        } catch (IOException x) {
            handleIOException(x,"queryMBeans");
            return Collections.emptySet();
        }
    }

    @Override
    public Set<ObjectName> queryNames(ObjectName name, QueryExp query) {
        try {
            return super.queryNames(name, query);
        } catch (IOException x) {
            handleIOException(x,"queryNames");
            return Collections.emptySet();
        }
    }

    public ObjectInstance registerMBean(Object object, ObjectName name)
        throws
        InstanceAlreadyExistsException,
        MBeanRegistrationException,
        NotCompliantMBeanException {
        final ObjectName sourceName = newSourceMBeanName(name);
        try {
            return processOutputInstance(
                    source().registerMBean(object,sourceName));
        } catch (RuntimeException x) {
            throw makeCompliantRuntimeException(x);
        }
    }

    @Override
    public void removeNotificationListener(ObjectName name,
                                           NotificationListener listener)
        throws InstanceNotFoundException, ListenerNotFoundException {
        try {
            super.removeNotificationListener(name, listener);
        } catch (IOException x) {
            throw handleIOException(x,"removeNotificationListener");
        }
    }

    @Override
    public void removeNotificationListener(ObjectName name,
                                           NotificationListener listener,
                                           NotificationFilter filter,
                                           Object handback)
        throws InstanceNotFoundException, ListenerNotFoundException {
        try {
            super.removeNotificationListener(name, listener,
                                                    filter, handback);
        } catch (IOException x) {
            throw handleIOException(x,"removeNotificationListener");
        }
    }

    @Override
    public void removeNotificationListener(ObjectName name,
                                           ObjectName listener)
        throws InstanceNotFoundException, ListenerNotFoundException {
        try {
            super.removeNotificationListener(name, listener);
        } catch (IOException x) {
            throw handleIOException(x,"removeNotificationListener");
        }
    }

    @Override
    public void removeNotificationListener(ObjectName name,
                                           ObjectName listener,
                                           NotificationFilter filter,
                                           Object handback)
        throws InstanceNotFoundException, ListenerNotFoundException {
        try {
            super.removeNotificationListener(name, listener,
                                                    filter, handback);
        } catch (IOException x) {
            throw handleIOException(x,"removeNotificationListener");
        }
    }

    @Override
    public void setAttribute(ObjectName name, Attribute attribute)
        throws
        InstanceNotFoundException,
        AttributeNotFoundException,
        InvalidAttributeValueException,
        MBeanException,
        ReflectionException {
        try {
            super.setAttribute(name, attribute);
        } catch (IOException x) {
            throw handleIOException(x,"setAttribute");
        }
    }

    @Override
    public AttributeList setAttributes(ObjectName name,
                                       AttributeList attributes)
        throws InstanceNotFoundException, ReflectionException {
        try {
            return super.setAttributes(name, attributes);
        } catch (IOException x) {
            throw handleIOException(x,"setAttributes");
        }
    }

    @Override
    public void unregisterMBean(ObjectName name)
        throws InstanceNotFoundException, MBeanRegistrationException {
        try {
           super.unregisterMBean(name);
        } catch (IOException x) {
            throw handleIOException(x,"unregisterMBean");
        }
    }

    static final RoutingProxyFactory<MBeanServer,RoutingServerProxy>
        FACTORY = new RoutingProxyFactory<MBeanServer,RoutingServerProxy>() {

        public RoutingServerProxy newInstance(MBeanServer source,
                String sourcePath, String targetPath,
                boolean forwardsContext) {
            return new RoutingServerProxy(source,sourcePath,
                    targetPath,forwardsContext);
        }

        public RoutingServerProxy newInstance(
                MBeanServer source, String sourcePath) {
            return new RoutingServerProxy(source,sourcePath);
        }
    };

    public static MBeanServer cd(MBeanServer source, String sourcePath) {
        return RoutingProxy.cd(RoutingServerProxy.class, FACTORY,
                source, sourcePath);
    }
}