1004
|
1 |
/*
|
|
2 |
* Copyright 2002-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package com.sun.jmx.event;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.lang.reflect.InvocationHandler;
|
|
30 |
import java.lang.reflect.InvocationTargetException;
|
|
31 |
import java.lang.reflect.Method;
|
|
32 |
import java.lang.reflect.Proxy;
|
|
33 |
import javax.management.MBeanServerConnection;
|
|
34 |
import javax.management.event.EventClient;
|
|
35 |
import javax.management.event.EventClientDelegate;
|
|
36 |
import javax.management.event.EventConsumer;
|
|
37 |
import javax.management.event.NotificationManager;
|
|
38 |
|
|
39 |
/**
|
|
40 |
* Override the methods related to the notification to use the
|
|
41 |
* Event service.
|
|
42 |
*/
|
|
43 |
public interface EventConnection extends MBeanServerConnection, EventConsumer {
|
|
44 |
public EventClient getEventClient();
|
|
45 |
|
|
46 |
public static class Factory {
|
|
47 |
public static EventConnection make(
|
|
48 |
final MBeanServerConnection mbsc,
|
|
49 |
final EventClient eventClient)
|
|
50 |
throws IOException {
|
|
51 |
if (!mbsc.isRegistered(EventClientDelegate.OBJECT_NAME)) {
|
|
52 |
throw new IOException(
|
|
53 |
"The server does not support the event service.");
|
|
54 |
}
|
|
55 |
InvocationHandler ih = new InvocationHandler() {
|
|
56 |
public Object invoke(Object proxy, Method method, Object[] args)
|
|
57 |
throws Throwable {
|
|
58 |
Class<?> intf = method.getDeclaringClass();
|
|
59 |
try {
|
|
60 |
if (intf.isInstance(eventClient))
|
|
61 |
return method.invoke(eventClient, args);
|
|
62 |
else
|
|
63 |
return method.invoke(mbsc, args);
|
|
64 |
} catch (InvocationTargetException e) {
|
|
65 |
throw e.getCause();
|
|
66 |
}
|
|
67 |
}
|
|
68 |
};
|
|
69 |
// It is important to declare NotificationManager.class first
|
|
70 |
// in the array below, so that the relevant addNL and removeNL
|
|
71 |
// methods will show up with method.getDeclaringClass() as
|
|
72 |
// being from that interface and not MBeanServerConnection.
|
|
73 |
return (EventConnection) Proxy.newProxyInstance(
|
|
74 |
NotificationManager.class.getClassLoader(),
|
|
75 |
new Class<?>[] {
|
|
76 |
NotificationManager.class, EventConnection.class,
|
|
77 |
},
|
|
78 |
ih);
|
|
79 |
}
|
|
80 |
}
|
|
81 |
}
|