01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package jmx;
05:
06: // RI imports
07: //
08: import javax.management.MalformedObjectNameException;
09: 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(
37: "Adaptor:name=html,port=8082");
38: server.registerMBean(html, html_name);
39: html.start();
40: } catch (Exception e) {
41: System.out
42: .println("\t!!! Could not create the HTML adaptor !!!");
43: e.printStackTrace();
44: return;
45: }
46:
47: }
48:
49: public static ObjectName buildObjectName(MBeanServer server,
50: String beanName) throws MalformedObjectNameException,
51: NullPointerException {
52: ObjectName rv = null;
53: String domain = server.getDefaultDomain();
54: rv = new ObjectName(domain + ":type=" + beanName);
55: return rv;
56: }
57:
58: public void registerBean(TCStandardBean bean) {
59: try {
60: ObjectName objName = buildObjectName(_server, bean
61: .getClass().getName());
62: _server.registerMBean(bean, objName);
63: } catch (Exception e) {
64: e.printStackTrace();
65: }
66: }
67:
68: public static void main(String[] args) {
69: try {
70: BaseAgent agent = BaseAgent.getInstance();
71: SimpleStandard ssb = new SimpleStandard();
72: agent.registerBean(ssb);
73: System.err.println("Up and Running...");
74: Thread.sleep(Long.MAX_VALUE);
75: } catch (Exception e) {
76: e.printStackTrace();
77: }
78: }
79:
80: }
|