1 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 40 41 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 86 public JndiJmx() throws MBeanException { 87 super(JndiJmx.class.getName()); 88 } 89 90 91 96 public void setComponentContext(Context ctx) { 97 this.componentContext= ctx; 98 } 99 100 104 public void setDescriptorContext(Context ctx) { 105 this.descriptorContext= ctx; 106 } 107 108 111 public void setConfigContext( Context ctx ) { 112 this.configContext= ctx; 113 } 114 115 Hashtable attributes=new Hashtable (); 118 Hashtable instances=new Hashtable (); 119 120 public void handleNotification(Notification notification, Object handback) 121 { 122 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 if( mbean instanceof NotificationBroadcaster ) { 137 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 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 222 mserver.addNotificationListener(delegate, this, null, null ); 224 225 } 226 227 } 228 | Popular Tags |