1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.modeler;
20
21
22 import java.util.Enumeration;
23 import java.util.Hashtable;
24
25 import javax.management.AttributeChangeNotification;
26 import javax.management.InstanceNotFoundException;
27 import javax.management.MBeanException;
28 import javax.management.MBeanServer;
29 import javax.management.MBeanServerNotification;
30 import javax.management.Notification;
31 import javax.management.NotificationBroadcaster;
32 import javax.management.NotificationListener;
33 import javax.management.ObjectName;
34 import javax.naming.Context;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
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 public class JndiJmx extends BaseModelMBean implements NotificationListener {
74
75
76 private static Log log= LogFactory.getLog(JndiJmx.class);
77
78 protected Context componentContext;
79 protected Context descriptorContext;
80 protected Context configContext;
81
82 MBeanServer mserver;
83
84
85
86
87 public JndiJmx() throws MBeanException {
88 super(JndiJmx.class.getName());
89 }
90
91
92
93
94
95
96
97 public void setComponentContext(Context ctx) {
98 this.componentContext= ctx;
99 }
100
101
102
103
104
105 public void setDescriptorContext(Context ctx) {
106 this.descriptorContext= ctx;
107 }
108
109
110
111
112 public void setConfigContext( Context ctx ) {
113 this.configContext= ctx;
114 }
115
116
117
118 Hashtable attributes=new Hashtable();
119 Hashtable instances=new Hashtable();
120
121 public void handleNotification(Notification notification, Object handback)
122 {
123
124 if( notification instanceof MBeanServerNotification ) {
125 MBeanServerNotification msnot=(MBeanServerNotification)notification;
126
127 ObjectName oname=msnot.getMBeanName();
128
129 if( "jmx.mbean.created".equalsIgnoreCase( notification.getType() )) {
130 try {
131 Object mbean=mserver.getObjectInstance(oname);
132
133 if( log.isDebugEnabled() )
134 log.debug( "MBean created " + oname + " " + mbean);
135
136
137 if( mbean instanceof NotificationBroadcaster ) {
138
139 NotificationBroadcaster nb=(NotificationBroadcaster)mbean;
140 nb.addNotificationListener(this, null, null);
141 if( log.isDebugEnabled() )
142 log.debug( "Add attribute change listener");
143 }
144
145 instances.put( oname.toString(), mbean );
146 } catch( InstanceNotFoundException ex ) {
147 log.error( "Instance not found for the created object", ex );
148 }
149 }
150 if( "jmx.mbean.deleted".equalsIgnoreCase( notification.getType() )) {
151 instances.remove(oname.toString());
152 }
153 }
154
155
156
157 if( notification instanceof AttributeChangeNotification) {
158
159 AttributeChangeNotification anotif=(AttributeChangeNotification)notification;
160 String name=anotif.getAttributeName();
161 Object value=anotif.getNewValue();
162 Object source=anotif.getSource();
163 String mname=null;
164
165 Hashtable mbeanAtt=(Hashtable)attributes.get( source );
166 if( mbeanAtt==null ) {
167 mbeanAtt=new Hashtable();
168 attributes.put( source, mbeanAtt);
169 if( log.isDebugEnabled())
170 log.debug("First attribute for " + source );
171 }
172 mbeanAtt.put( name, anotif );
173
174 log.debug( "Attribute change notification " + name + " " + value + " " + source );
175
176 }
177
178 }
179
180 public String dumpStatus() throws Exception
181 {
182 StringBuffer sb=new StringBuffer();
183 Enumeration en=instances.keys();
184 while (en.hasMoreElements()) {
185 String on = (String) en.nextElement();
186 Object mbean=instances.get(on);
187 Hashtable mbeanAtt=(Hashtable)attributes.get(mbean);
188
189 sb.append( "<mbean class=\"").append(on).append("\">");
190 sb.append( "\n");
191 Enumeration attEn=mbeanAtt.keys();
192 while (attEn.hasMoreElements()) {
193 String an = (String) attEn.nextElement();
194 AttributeChangeNotification anotif=
195 (AttributeChangeNotification)mbeanAtt.get(an);
196 sb.append(" <attribute name=\"").append(an).append("\" ");
197 sb.append("value=\"").append(anotif.getNewValue()).append("\">");
198 sb.append( "\n");
199 }
200
201
202 sb.append( "</mbean>");
203 sb.append( "\n");
204 }
205 return sb.toString();
206 }
207
208 public void replay() throws Exception
209 {
210
211
212 }
213
214
215 public void init() throws Exception
216 {
217
218 MBeanServer mserver=(MBeanServer)Registry.getRegistry().getMBeanServer();
219 ObjectName delegate=new ObjectName("JMImplementation:type=MBeanServerDelegate");
220
221
222
223
224 mserver.addNotificationListener(delegate, this, null, null );
225
226 }
227
228 }