KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > jmx > server > StructuralInfo


1 /*
2  * (c) Rob Gordon 2005
3  */

4 package org.oddjob.jmx.server;
5
6 import java.util.ArrayList JavaDoc;
7 import java.util.LinkedList JavaDoc;
8 import java.util.List JavaDoc;
9
10 import javax.management.MBeanAttributeInfo JavaDoc;
11 import javax.management.MBeanException JavaDoc;
12 import javax.management.MBeanNotificationInfo JavaDoc;
13 import javax.management.MBeanOperationInfo JavaDoc;
14 import javax.management.Notification JavaDoc;
15 import javax.management.ObjectName JavaDoc;
16 import javax.management.ReflectionException JavaDoc;
17
18 import org.apache.log4j.Logger;
19 import org.oddjob.Structural;
20 import org.oddjob.structural.StructuralEvent;
21 import org.oddjob.structural.StructuralListener;
22
23 public class StructuralInfo implements InterfaceInfo {
24     private static final Logger logger = Logger.getLogger(StructuralInfo.class);
25     
26     public static final String JavaDoc CHILD_ADDED_NOTIF_TYPE = "org.oddjob.childadded";
27
28     public static final String JavaDoc CHILD_REMOVED_NOTIF_TYPE = "org.oddjob.childremoved";
29
30
31     public Class JavaDoc interfaceClass() {
32         return Structural.class;
33     }
34     
35     public MBeanAttributeInfo JavaDoc[] getMBeanAttributeInfo() {
36         return new MBeanAttributeInfo JavaDoc[0];
37     }
38
39     public MBeanOperationInfo JavaDoc[] getMBeanOperationInfo() {
40         return new MBeanOperationInfo JavaDoc[0];
41     }
42     
43     public MBeanNotificationInfo JavaDoc[] getMBeanNotificationInfo() {
44
45         MBeanNotificationInfo JavaDoc[] nInfo = new MBeanNotificationInfo JavaDoc[] {
46                 new MBeanNotificationInfo JavaDoc(new String JavaDoc[] {
47                         CHILD_ADDED_NOTIF_TYPE, CHILD_REMOVED_NOTIF_TYPE },
48                         Notification JavaDoc.class.getName(), "Structural notification.")};
49         return nInfo;
50     }
51
52     public InterfaceHandler attach(Object JavaDoc target,
53             OddjobMBean ojmb) {
54         Structural structural = (Structural) target;
55         ServerStructuralHelper structuralHelper = new ServerStructuralHelper (structural, ojmb);
56         structural.addStructuralListener(structuralHelper);
57         return structuralHelper;
58     }
59     
60     class ServerStructuralHelper implements StructuralListener, InterfaceHandler {
61
62         private final Structural structural;
63         private final OddjobMBean ojmb;
64         private boolean duplicate;
65         
66         /** Child remote job nodes. */
67         private final LinkedList JavaDoc children = new LinkedList JavaDoc();
68
69         private final List JavaDoc lastNotifications = new ArrayList JavaDoc();
70
71         ServerStructuralHelper(Structural structural,
72                 OddjobMBean ojmb) {
73             this.structural = structural;
74             this.ojmb = ojmb;
75         }
76     
77         /*
78          * (non-Javadoc)
79          *
80          * @see org.oddjob.structural.StructuralListener#childAdded(org.oddjob.structural.StructuralEvent)
81          */

82         public void childAdded(final StructuralEvent e) {
83             final ServerContext childContext = new ServerContext(e.getChild(),
84                     ojmb.getContext());
85             duplicate = childContext.isDuplicate();
86             if (duplicate) {
87                 return;
88             }
89             ojmb.runSynchronized(new Runnable JavaDoc() {
90                 public void run() {
91                     ObjectName JavaDoc child = ojmb.addChild(e.getChild(), childContext);
92                     int index = e.getIndex();
93                     StructuralEvent newEvent = new StructuralEvent(new Object JavaDoc(),
94                             child, index);
95                     final Notification JavaDoc notification = new Notification JavaDoc(
96                             CHILD_ADDED_NOTIF_TYPE, ojmb,
97                             ojmb.getNextNotificationNumber());
98                     notification.setUserData(newEvent);
99                     children.add(index, child);
100                     ojmb.sendNotification(notification);
101                     lastNotifications.add(index, notification);
102                 }
103             });
104             logger.debug("Child added, ObjectName [" + e.getChild().toString() + "], index [" + e.getIndex() + "]");
105         }
106
107         /*
108          * (non-Javadoc)
109          *
110          * @see org.oddjob.structural.StructuralListener#childRemoved(org.oddjob.structural.StructuralEvent)
111          */

112         public void childRemoved(final StructuralEvent e) {
113             if (duplicate) {
114                 // a duplicate was never added.
115
return;
116             }
117             ojmb.runSynchronized(new Runnable JavaDoc() {
118                 public void run() {
119                     int index = e.getIndex();
120                     ObjectName JavaDoc child = (ObjectName JavaDoc) children.get(index);
121                     StructuralEvent newEvent = new StructuralEvent(new Object JavaDoc(),
122                             child, index);
123                     Notification JavaDoc notification = new Notification JavaDoc(
124                             CHILD_REMOVED_NOTIF_TYPE, ojmb,
125                             ojmb.getNextNotificationNumber());
126                     notification.setUserData(newEvent);
127                     children.remove(index);
128                     ojmb.removeChild(child);
129                     ojmb.sendNotification(notification);
130                     lastNotifications.remove(index);
131                 }
132             });
133             ojmb.getContext().removeChild(e.getChild());
134             logger.debug("Child removed, ObjectName [" + e.getChild().toString() + "], index [" + e.getIndex() + "]");
135         }
136
137         public Notification JavaDoc[] getLastNotifications() {
138             return (Notification JavaDoc[]) lastNotifications.toArray(new Notification JavaDoc[0]);
139         }
140
141         public Object JavaDoc invoke(String JavaDoc actionName, Object JavaDoc[] params, String JavaDoc[] signature) throws MBeanException JavaDoc, ReflectionException JavaDoc {
142             throw new ReflectionException JavaDoc(
143                     new IllegalStateException JavaDoc("invoked for an unknown method."),
144                             actionName);
145         }
146         
147         public void destroy() {
148             structural.removeStructuralListener(this);
149             // use our listener to remove children.
150
while (children.size() > 0) {
151                 StructuralEvent dummyEvent = new StructuralEvent(new Object JavaDoc(),
152                         new Object JavaDoc(), children.size() - 1);
153                 childRemoved(dummyEvent);
154             }
155         }
156
157     }
158 }
159
Popular Tags