1 22 package com.scalagent.jmx; 23 24 import java.io.*; 25 import java.util.*; 26 import java.lang.reflect.Method ; 27 28 import javax.management.*; 29 30 import org.objectweb.util.monolog.api.BasicLevel; 31 import org.objectweb.util.monolog.api.Logger; 32 import org.objectweb.util.monolog.api.LoggerFactory; 33 34 import fr.dyade.aaa.util.management.*; 35 36 39 public class JMXServer implements MXServer { 40 public MBeanServer mxserver = null; 41 public String domain = null; 42 43 public JMXServer(MBeanServer mxserver, 44 String domain) { 45 this.mxserver = mxserver; 46 this.domain = domain; 47 MXWrapper.setMXServer(this); 48 } 49 50 public JMXServer() { 51 try { 52 Class clazz = Class.forName("java.lang.management.ManagementFactory"); 54 Method method = clazz.getMethod("getPlatformMBeanServer", null); 55 this.mxserver = (MBeanServer) method.invoke(null, null); 56 } catch (Exception exc) { 57 this.mxserver = MBeanServerFactory.createMBeanServer("AgentServer"); 59 } 60 this.domain = "AgentServer"; 61 MXWrapper.setMXServer(this); 62 } 63 64 public void registerMBean(Object bean, 65 String domain, 66 String name) throws Exception { 67 if (mxserver == null) return; 68 69 StringBuffer strbuf = new StringBuffer (); 70 strbuf.append(domain); 71 if (name != null) 72 strbuf.append(':').append(name); 73 74 try { 75 mxserver.registerMBean(bean, new ObjectName(strbuf.toString())); 76 } catch (InstanceAlreadyExistsException exc) { 77 throw exc; 79 } catch (MBeanRegistrationException exc) { 80 throw exc; 83 } catch (NotCompliantMBeanException exc) { 84 throw exc; 86 } catch (RuntimeOperationsException exc) { 87 throw exc; 89 } 90 } 91 92 public void unregisterMBean(String domain, 93 String name) throws Exception { 94 if (mxserver == null) return; 95 96 StringBuffer strbuf = new StringBuffer (); 97 strbuf.append(domain); 98 strbuf.append(':').append(name); 99 100 try { 101 mxserver.unregisterMBean(new ObjectName(strbuf.toString())); 102 } catch (InstanceNotFoundException exc) { 103 throw exc; 105 } catch (MBeanRegistrationException exc) { 106 throw exc; 109 } catch (RuntimeOperationsException exc) { 110 throw exc; 112 } 113 } 114 } 115 | Popular Tags |