KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jmx > BaseAgent


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package jmx;
5
6 // RI imports
7
//
8
import javax.management.MalformedObjectNameException JavaDoc;
9 import javax.management.ObjectName JavaDoc;
10 import javax.management.MBeanServer JavaDoc;
11 import javax.management.MBeanServerFactory JavaDoc;
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 JavaDoc _server = null;
27
28   public BaseAgent() {
29     _server = MBeanServerFactory.createMBeanServer();
30     initHtmlAdaptor(_server);
31   }
32
33   protected void initHtmlAdaptor(MBeanServer JavaDoc server) {
34     try {
35       HtmlAdaptorServer html = new HtmlAdaptorServer();
36       ObjectName JavaDoc html_name = new ObjectName JavaDoc("Adaptor:name=html,port=8082");
37       server.registerMBean(html, html_name);
38       html.start();
39     } catch (Exception JavaDoc 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 JavaDoc buildObjectName(MBeanServer JavaDoc server, String JavaDoc beanName) throws MalformedObjectNameException JavaDoc,
48       NullPointerException JavaDoc {
49     ObjectName JavaDoc rv = null;
50     String JavaDoc domain = server.getDefaultDomain();
51     rv = new ObjectName JavaDoc(domain + ":type=" + beanName);
52     return rv;
53   }
54
55   public void registerBean(TCStandardBean bean) {
56     try {
57       ObjectName JavaDoc objName = buildObjectName(_server, bean.getClass().getName());
58       _server.registerMBean(bean, objName);
59     } catch (Exception JavaDoc e) {
60       e.printStackTrace();
61     }
62   }
63
64   public static void main(String JavaDoc[] 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 JavaDoc e) {
72       e.printStackTrace();
73     }
74   }
75
76 }
Popular Tags