1 4 package jmx; 5 6 import javax.management.MalformedObjectNameException ; 9 import javax.management.ObjectName ; 10 import javax.management.MBeanServer ; 11 import javax.management.MBeanServerFactory ; 12 13 import com.sun.jdmk.comm.HtmlAdaptorServer; 14 15 public class BaseAgent { 16 17 public static synchronized BaseAgent getInstance() { 18 if (_theInstance == null) { 19 _theInstance = new BaseAgent(); 20 } 21 return _theInstance; 22 } 23 24 private static volatile BaseAgent _theInstance = null; 25 26 private MBeanServer _server = null; 27 28 public BaseAgent() { 29 _server = MBeanServerFactory.createMBeanServer(); 30 initHtmlAdaptor(_server); 31 } 32 33 protected void initHtmlAdaptor(MBeanServer server) { 34 try { 35 HtmlAdaptorServer html = new HtmlAdaptorServer(); 36 ObjectName html_name = new ObjectName ("Adaptor:name=html,port=8082"); 37 server.registerMBean(html, html_name); 38 html.start(); 39 } catch (Exception e) { 40 System.out.println("\t!!! Could not create the HTML adaptor !!!"); 41 e.printStackTrace(); 42 return; 43 } 44 45 } 46 47 public static ObjectName buildObjectName(MBeanServer server, String beanName) throws MalformedObjectNameException , 48 NullPointerException { 49 ObjectName rv = null; 50 String domain = server.getDefaultDomain(); 51 rv = new ObjectName (domain + ":type=" + beanName); 52 return rv; 53 } 54 55 public void registerBean(TCStandardBean bean) { 56 try { 57 ObjectName objName = buildObjectName(_server, bean.getClass().getName()); 58 _server.registerMBean(bean, objName); 59 } catch (Exception e) { 60 e.printStackTrace(); 61 } 62 } 63 64 public static void main(String [] args) { 65 try { 66 BaseAgent agent = BaseAgent.getInstance(); 67 SimpleStandard ssb = new SimpleStandard(); 68 agent.registerBean(ssb); 69 System.err.println("Up and Running..."); 70 Thread.sleep(Long.MAX_VALUE); 71 } catch (Exception e) { 72 e.printStackTrace(); 73 } 74 } 75 76 } | Popular Tags |