KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > modeler > JndiJmx


1 /*
2  * Copyright 2001-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17
18 package org.apache.commons.modeler;
19
20
21 import java.util.Enumeration JavaDoc;
22 import java.util.Hashtable JavaDoc;
23
24 import javax.management.AttributeChangeNotification JavaDoc;
25 import javax.management.InstanceNotFoundException JavaDoc;
26 import javax.management.MBeanException JavaDoc;
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.MBeanServerNotification JavaDoc;
29 import javax.management.Notification JavaDoc;
30 import javax.management.NotificationBroadcaster JavaDoc;
31 import javax.management.NotificationListener JavaDoc;
32 import javax.management.ObjectName JavaDoc;
33 import javax.naming.Context JavaDoc;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37
38 // EXPERIMENTAL. It may fit better in tomcat jndi impl.
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 JavaDoc {
73
74
75     private static Log log= LogFactory.getLog(JndiJmx.class);
76
77     protected Context JavaDoc componentContext;
78     protected Context JavaDoc descriptorContext;
79     protected Context JavaDoc configContext;
80
81     MBeanServer JavaDoc mserver;
82
83     /**
84      * Protected constructor to require use of the factory create method.
85      */

86     public JndiJmx() throws MBeanException JavaDoc {
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 JavaDoc ctx) {
97         this.componentContext= ctx;
98     }
99
100     /** JNDI context for component descriptors ( metadata ).
101      *
102      * @param ctx
103      */

104     public void setDescriptorContext(Context JavaDoc ctx) {
105         this.descriptorContext= ctx;
106     }
107
108     /** JNDI context where attributes will be stored for persistence
109      *
110      */

111     public void setConfigContext( Context JavaDoc ctx ) {
112         this.configContext= ctx;
113     }
114
115     // -------------------- Registration/unregistration --------------------
116
// temp - will only set in the jndi contexts
117
Hashtable JavaDoc attributes=new Hashtable JavaDoc();
118     Hashtable JavaDoc instances=new Hashtable JavaDoc();
119
120     public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
121     {
122         // register/unregister mbeans in jndi
123
if( notification instanceof MBeanServerNotification JavaDoc ) {
124             MBeanServerNotification JavaDoc msnot=(MBeanServerNotification JavaDoc)notification;
125
126             ObjectName JavaDoc oname=msnot.getMBeanName();
127
128             if( "jmx.mbean.created".equalsIgnoreCase( notification.getType() )) {
129                 try {
130                     Object JavaDoc mbean=mserver.getObjectInstance(oname);
131
132                     if( log.isDebugEnabled() )
133                         log.debug( "MBean created " + oname + " " + mbean);
134
135                     // XXX add filter support
136
if( mbean instanceof NotificationBroadcaster JavaDoc ) {
137                         // register for attribute changes
138
NotificationBroadcaster JavaDoc nb=(NotificationBroadcaster JavaDoc)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 JavaDoc 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         // set attributes in jndi
155
// if( "jmx.attribute.changed".equals( notification.getType() )) {
156
if( notification instanceof AttributeChangeNotification JavaDoc) {
157
158             AttributeChangeNotification JavaDoc anotif=(AttributeChangeNotification JavaDoc)notification;
159             String JavaDoc name=anotif.getAttributeName();
160             Object JavaDoc value=anotif.getNewValue();
161             Object JavaDoc source=anotif.getSource();
162             String JavaDoc mname=null;
163
164             Hashtable JavaDoc mbeanAtt=(Hashtable JavaDoc)attributes.get( source );
165             if( mbeanAtt==null ) {
166                 mbeanAtt=new Hashtable JavaDoc();
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 JavaDoc dumpStatus() throws Exception JavaDoc
180     {
181         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
182         Enumeration JavaDoc en=instances.keys();
183         while (en.hasMoreElements()) {
184             String JavaDoc on = (String JavaDoc) en.nextElement();
185             Object JavaDoc mbean=instances.get(on);
186             Hashtable JavaDoc mbeanAtt=(Hashtable JavaDoc)attributes.get(mbean);
187
188             sb.append( "<mbean class=\"").append(on).append("\">");
189             sb.append( "\n");
190             Enumeration JavaDoc attEn=mbeanAtt.keys();
191             while (attEn.hasMoreElements()) {
192                 String JavaDoc an = (String JavaDoc) attEn.nextElement();
193                 AttributeChangeNotification JavaDoc anotif=
194                         (AttributeChangeNotification JavaDoc)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 JavaDoc
208     {
209
210
211     }
212
213
214     public void init() throws Exception JavaDoc
215     {
216
217         MBeanServer JavaDoc mserver=(MBeanServer JavaDoc)Registry.getRegistry().getMBeanServer();
218         ObjectName JavaDoc delegate=new ObjectName JavaDoc("JMImplementation:type=MBeanServerDelegate");
219
220         // XXX need to extract info about previously loaded beans
221

222         // we'll know of all registered beans
223
mserver.addNotificationListener(delegate, this, null, null );
224
225     }
226
227 }
228
Popular Tags