1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.jmx;
19
20 import org.apache.log4j.Logger;
21
22 import javax.management.JMException;
23 import javax.management.MBeanServer;
24 import javax.management.MBeanServerFactory;
25 import javax.management.ObjectName;
26 import java.lang.reflect.InvocationTargetException;
27 import java.io.InterruptedIOException;
28
29
30
31
32
33
34
35
36
37
38
39 public class Agent {
40
41
42
43
44
45 static Logger log = Logger.getLogger(Agent.class);
46
47
48
49
50
51 public Agent() {
52 }
53
54
55
56
57
58
59
60
61 private static Object createServer() {
62 Object newInstance = null;
63 try {
64 newInstance = Class.forName(
65 "com.sun.jdmk.comm.HtmlAdapterServer").newInstance();
66 } catch (ClassNotFoundException ex) {
67 throw new RuntimeException(ex.toString());
68 } catch (InstantiationException ex) {
69 throw new RuntimeException(ex.toString());
70 } catch (IllegalAccessException ex) {
71 throw new RuntimeException(ex.toString());
72 }
73 return newInstance;
74 }
75
76
77
78
79
80
81
82 private static void startServer(final Object server) {
83 try {
84 server.getClass().getMethod("start", new Class[0]).
85 invoke(server, new Object[0]);
86 } catch(InvocationTargetException ex) {
87 Throwable cause = ex.getTargetException();
88 if (cause instanceof RuntimeException) {
89 throw (RuntimeException) cause;
90 } else if (cause != null) {
91 if (cause instanceof InterruptedException
92 || cause instanceof InterruptedIOException) {
93 Thread.currentThread().interrupt();
94 }
95 throw new RuntimeException(cause.toString());
96 } else {
97 throw new RuntimeException();
98 }
99 } catch(NoSuchMethodException ex) {
100 throw new RuntimeException(ex.toString());
101 } catch(IllegalAccessException ex) {
102 throw new RuntimeException(ex.toString());
103 }
104 }
105
106
107
108
109
110
111 public void start() {
112
113 MBeanServer server = MBeanServerFactory.createMBeanServer();
114 Object html = createServer();
115
116 try {
117 log.info("Registering HtmlAdaptorServer instance.");
118 server.registerMBean(html, new ObjectName("Adaptor:name=html,port=8082"));
119 log.info("Registering HierarchyDynamicMBean instance.");
120 HierarchyDynamicMBean hdm = new HierarchyDynamicMBean();
121 server.registerMBean(hdm, new ObjectName("log4j:hiearchy=default"));
122 } catch(JMException e) {
123 log.error("Problem while registering MBeans instances.", e);
124 return;
125 } catch(RuntimeException e) {
126 log.error("Problem while registering MBeans instances.", e);
127 return;
128 }
129 startServer(html);
130 }
131 }