1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.modeler;
19
20
21 import java.util.Enumeration;
22 import java.util.Hashtable;
23
24 import javax.management.AttributeChangeNotification;
25 import javax.management.InstanceNotFoundException;
26 import javax.management.MBeanException;
27 import javax.management.MBeanServer;
28 import javax.management.MBeanServerNotification;
29 import javax.management.Notification;
30 import javax.management.NotificationBroadcaster;
31 import javax.management.NotificationListener;
32 import javax.management.ObjectName;
33 import javax.naming.Context;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38
39
40
41 /***
42 *
43 * Link between JNDI and JMX. JNDI can be used for persistence ( it is
44 * an API for storing hierarchical data and a perfect fit for that ), as
45 * well as an alternate view of the MBean registry.
46 *
47 * If this component is enabled, all MBeans will be registered in JNDI, and
48 * all attributes that are set via JMX can be stored in a DirContext.
49 *
50 * This acts as a "recorder" for creation of mbeans and attribute changes
51 * done via JMX.
52 *
53 * XXX How can we control ( filter ) which mbeans will be registere ? Or
54 * attributes ?
55 * XXX How can we get the beans and attributes loaded before jndijmx ?
56 *
57 * The intended use:
58 * - do whatever you want to start the application
59 * - load JndiJmx as an mbean
60 * - make changes via JMX. All changes are recorded
61 * - you can use JndiJmx to save the changes in a Jndi context.
62 * - you can use JndiJmx to load changes from a JndiContext and replay them.
63 *
64 * The main benefit is that only changed attributes are saved, and the Jndi
65 * layer can preserve most of the original structure of the config file. The
66 * alternative is to override the config files with config info extracted
67 * from the live objects - but it's very hard to save only what was actually
68 * changed and preserve structure and comments.
69 *
70 * @author Costin Manolache
71 */
72 public class JndiJmx extends BaseModelMBean implements NotificationListener {
73
74
75 private static Log log= LogFactory.getLog(JndiJmx.class);
76
77 protected Context componentContext;
78 protected Context descriptorContext;
79 protected Context configContext;
80
81 MBeanServer mserver;
82
83 /***
84 * Protected constructor to require use of the factory create method.
85 */
86 public JndiJmx() throws MBeanException {
87 super(JndiJmx.class.getName());
88 }
89
90
91 /*** If a JNDI context is set, all components
92 * will be registered in the context.
93 *
94 * @param ctx
95 */
96 public void setComponentContext(Context ctx) {
97 this.componentContext= ctx;
98 }
99
100 /*** JNDI context for component descriptors ( metadata ).
101 *
102 * @param ctx
103 */
104 public void setDescriptorContext(Context ctx) {
105 this.descriptorContext= ctx;
106 }
107
108 /*** JNDI context where attributes will be stored for persistence
109 *
110 */
111 public void setConfigContext( Context ctx ) {
112 this.configContext= ctx;
113 }
114
115
116
117 Hashtable attributes=new Hashtable();
118 Hashtable instances=new Hashtable();
119
120 public void handleNotification(Notification notification, Object handback)
121 {
122
123 if( notification instanceof MBeanServerNotification ) {
124 MBeanServerNotification msnot=(MBeanServerNotification)notification;
125
126 ObjectName oname=msnot.getMBeanName();
127
128 if( "jmx.mbean.created".equalsIgnoreCase( notification.getType() )) {
129 try {
130 Object mbean=mserver.getObjectInstance(oname);
131
132 if( log.isDebugEnabled() )
133 log.debug( "MBean created " + oname + " " + mbean);
134
135
136 if( mbean instanceof NotificationBroadcaster ) {
137
138 NotificationBroadcaster nb=(NotificationBroadcaster)mbean;
139 nb.addNotificationListener(this, null, null);
140 if( log.isDebugEnabled() )
141 log.debug( "Add attribute change listener");
142 }
143
144 instances.put( oname.toString(), mbean );
145 } catch( InstanceNotFoundException ex ) {
146 log.error( "Instance not found for the created object", ex );
147 }
148 }
149 if( "jmx.mbean.deleted".equalsIgnoreCase( notification.getType() )) {
150 instances.remove(oname.toString());
151 }
152 }
153
154
155
156 if( notification instanceof AttributeChangeNotification) {
157
158 AttributeChangeNotification anotif=(AttributeChangeNotification)notification;
159 String name=anotif.getAttributeName();
160 Object value=anotif.getNewValue();
161 Object source=anotif.getSource();
162 String mname=null;
163
164 Hashtable mbeanAtt=(Hashtable)attributes.get( source );
165 if( mbeanAtt==null ) {
166 mbeanAtt=new Hashtable();
167 attributes.put( source, mbeanAtt);
168 if( log.isDebugEnabled())
169 log.debug("First attribute for " + source );
170 }
171 mbeanAtt.put( name, anotif );
172
173 log.debug( "Attribute change notification " + name + " " + value + " " + source );
174
175 }
176
177 }
178
179 public String dumpStatus() throws Exception
180 {
181 StringBuffer sb=new StringBuffer();
182 Enumeration en=instances.keys();
183 while (en.hasMoreElements()) {
184 String on = (String) en.nextElement();
185 Object mbean=instances.get(on);
186 Hashtable mbeanAtt=(Hashtable)attributes.get(mbean);
187
188 sb.append( "<mbean class=\"").append(on).append("\">");
189 sb.append( "\n");
190 Enumeration attEn=mbeanAtt.keys();
191 while (attEn.hasMoreElements()) {
192 String an = (String) attEn.nextElement();
193 AttributeChangeNotification anotif=
194 (AttributeChangeNotification)mbeanAtt.get(an);
195 sb.append(" <attribute name=\"").append(an).append("\" ");
196 sb.append("value=\"").append(anotif.getNewValue()).append("\">");
197 sb.append( "\n");
198 }
199
200
201 sb.append( "</mbean>");
202 sb.append( "\n");
203 }
204 return sb.toString();
205 }
206
207 public void replay() throws Exception
208 {
209
210
211 }
212
213
214 public void init() throws Exception
215 {
216
217 MBeanServer mserver=(MBeanServer)Registry.getRegistry().getMBeanServer();
218 ObjectName delegate=new ObjectName("JMImplementation:type=MBeanServerDelegate");
219
220
221
222
223 mserver.addNotificationListener(delegate, this, null, null );
224
225 }
226
227 }