01: package vicazh.hyperpool;
02:
03: import javax.management.*;
04: import javax.swing.*;
05:
06: /**
07: * This class is the superclass of all graphic services and selectors
08: *
09: * @author Victor Zhigunov
10: * @version 0.4.0
11: */
12: abstract public class IElement extends IUnit implements ElementMBean,
13: NotificationListener {
14: private Class<?> interfaceClass;
15: private String name;
16:
17: /**
18: * @param interfaceClass
19: * interface class
20: * @param name
21: * service name
22: */
23: public IElement(Class<?> interfaceClass, String name) {
24: this .interfaceClass = interfaceClass;
25: this .name = name;
26: }
27:
28: public String toString() {
29: return name;
30: }
31:
32: /**
33: * Update components
34: */
35: protected void updateUI() {
36: }
37:
38: private final long ID = System.currentTimeMillis();
39:
40: public void setAttribute(String name, Object value)
41: throws Exception {
42: setAttribute(name, ID, value);
43: }
44:
45: public void setAttribute(String name) throws Exception {
46: setAttribute(name, null);
47: }
48:
49: private boolean b;
50:
51: public void handleNotification(final Notification notification,
52: Object handback) {
53: if (!b
54: && (!notification.getType().equals(ElementMBean.INIT) || ((AttributeNotification) notification).sourceID != ID)
55: || notification.getType().equals(ElementMBean.INIT)
56: && ((AttributeNotification) notification).sourceID != ID)
57: return;
58: b = true;
59: SwingUtilities.invokeLater(new Runnable() {
60: public void run() {
61: if (((AttributeNotification) notification).sourceID == ID)
62: currentNotification(
63: notification.getType(),
64: ((AttributeNotification) notification).value);
65: else
66: otherNotification(
67: notification.getType(),
68: ((AttributeNotification) notification).value);
69: }
70: });
71: }
72:
73: protected void currentNotification(String type, Object value) {
74: }
75:
76: protected void otherNotification(String type, Object value) {
77: }
78:
79: protected Object melement;
80:
81: public void connect(MBeanServerConnection mbsc) throws Exception {
82: ObjectName objectname = new ObjectName("hyperpool:type=" + id);
83: melement = MBeanServerInvocationHandler.newProxyInstance(mbsc,
84: objectname, interfaceClass, true);
85: mbsc.addNotificationListener(objectname, this , null, null);
86: setAttribute(ElementMBean.INIT);
87: }
88:
89: public void disconnect() throws Exception {
90: b = false;
91: }
92:
93: public void setAttribute(String name, long sourceID, Object value)
94: throws Exception {
95: ((ElementMBean) melement).setAttribute(name, sourceID, value);
96: }
97: }
|