1 10 11 package org.mule.management.agents; 12 13 import java.util.Iterator ; 14 import java.util.Set ; 15 16 import javax.management.InstanceNotFoundException ; 17 import javax.management.MBeanRegistrationException ; 18 import javax.management.MBeanServer ; 19 import javax.management.MBeanServerFactory ; 20 import javax.management.MalformedObjectNameException ; 21 import javax.management.ObjectInstance ; 22 import javax.management.ObjectName ; 23 24 import org.apache.log4j.jmx.HierarchyDynamicMBean; 25 import org.mule.config.i18n.Message; 26 import org.mule.config.i18n.Messages; 27 import org.mule.umo.UMOException; 28 import org.mule.umo.lifecycle.InitialisationException; 29 import org.mule.umo.manager.UMOAgent; 30 31 35 public class Log4jAgent implements UMOAgent 36 { 37 38 private String name = "Log4j Agent"; 39 private MBeanServer mBeanServer; 40 public static final String JMX_OBJECT_NAME = "log4j:type=Hierarchy"; 41 42 47 public String getName() 48 { 49 return this.name; 50 } 51 52 57 public void setName(String name) 58 { 59 this.name = name; 60 } 61 62 67 public String getDescription() 68 { 69 return "Log4j JMX Agent"; 70 } 71 72 77 public void initialise() throws InitialisationException 78 { 79 try 80 { 81 mBeanServer = (MBeanServer )MBeanServerFactory.findMBeanServer(null).get(0); 82 final ObjectName objectName = ObjectName.getInstance(JMX_OBJECT_NAME); 83 unregisterMBeansIfNecessary(); 85 mBeanServer.registerMBean(new HierarchyDynamicMBean(), objectName); 86 } 87 catch (Exception e) 88 { 89 throw new InitialisationException(new Message(Messages.FAILED_TO_START_X, "JMX Agent"), e, this); 90 } 91 } 92 93 96 protected void unregisterMBeansIfNecessary() 97 throws MalformedObjectNameException , InstanceNotFoundException , MBeanRegistrationException 98 { 99 if (mBeanServer.isRegistered(ObjectName.getInstance(JMX_OBJECT_NAME))) 100 { 101 Set log4jMBeans = mBeanServer.queryMBeans(ObjectName.getInstance("log4j*:*"), null); 103 for (Iterator it = log4jMBeans.iterator(); it.hasNext();) 104 { 105 ObjectInstance objectInstance = (ObjectInstance )it.next(); 106 ObjectName theName = objectInstance.getObjectName(); 107 mBeanServer.unregisterMBean(theName); 108 } 109 } 110 } 111 112 117 public void start() throws UMOException 118 { 119 } 121 122 127 public void stop() throws UMOException 128 { 129 } 131 132 137 public void dispose() 138 { 139 } 141 142 147 public void registered() 148 { 149 } 151 152 157 public void unregistered() 158 { 159 } 161 162 } 163 | Popular Tags |