01: package distributedcache.management;
02:
03: import java.io.IOException;
04: import java.lang.management.ManagementFactory;
05: import java.lang.reflect.Field;
06: import java.nio.BufferUnderflowException;
07:
08: import javax.management.ObjectName;
09:
10: import org.xsocket.ILifeCycle;
11: import org.xsocket.MaxReadSizeExceededException;
12: import org.xsocket.Resource;
13: import org.xsocket.stream.IDataHandler;
14: import org.xsocket.stream.IHandler;
15: import org.xsocket.stream.INonBlockingConnection;
16: import org.xsocket.stream.IServerContext;
17:
18: import distributedcache.StoreService;
19:
20: public class MonitoredStoreService implements IDataHandler, ILifeCycle {
21:
22: private StoreService delegee = null;
23: private ObjectName objectName = null;
24:
25: @Resource
26: private IServerContext ctx = null;
27:
28: public MonitoredStoreService(StoreService delegee) {
29: this .delegee = delegee;
30: }
31:
32: public boolean onData(INonBlockingConnection connection)
33: throws IOException, BufferUnderflowException,
34: MaxReadSizeExceededException {
35: return delegee.onData(connection);
36: }
37:
38: public void onInit() {
39: injectContext(delegee);
40: delegee.onInit();
41:
42: try {
43: objectName = new ObjectName("domain"
44: + ":type=StoreService,name=" + this .hashCode());
45: ManagementFactory.getPlatformMBeanServer().registerMBean(
46: new IntrospectionBasedDynamicBean(delegee),
47: objectName);
48: } catch (Exception e) {
49: e.printStackTrace();
50: }
51: }
52:
53: public void onDestroy() {
54: delegee.onDestroy();
55:
56: try {
57: objectName = new ObjectName("domain"
58: + ":type=StoreService,name=" + this .hashCode());
59: ManagementFactory.getPlatformMBeanServer().unregisterMBean(
60: objectName);
61: } catch (Exception e) {
62: e.printStackTrace();
63: }
64: }
65:
66: private void injectContext(IHandler hdl) {
67: IServerContext ctx = null;
68: Field[] fields = hdl.getClass().getDeclaredFields();
69: for (Field field : fields) {
70: if ((field.getType() == IServerContext.class)
71: && (field.getAnnotation(Resource.class) != null)) {
72: field.setAccessible(true);
73: try {
74: if (ctx == null) {
75: ctx = this .ctx;
76: }
77: field.set(hdl, ctx);
78: } catch (IllegalAccessException iae) {
79: iae.printStackTrace();
80: }
81: }
82: }
83: }
84:
85: }
|