1
2
3
4
5
6
7
8
9
10
11 package org.mule.management.agents;
12
13 import org.mule.config.i18n.CoreMessages;
14 import org.mule.umo.UMOException;
15 import org.mule.umo.lifecycle.InitialisationException;
16 import org.mule.umo.manager.UMOAgent;
17 import org.mule.util.ClassUtils;
18 import org.mule.util.StringUtils;
19
20 import java.net.URI;
21
22 import javax.management.InstanceNotFoundException;
23 import javax.management.MBeanException;
24 import javax.management.MBeanServer;
25 import javax.management.MBeanServerFactory;
26 import javax.management.ObjectName;
27 import javax.management.ReflectionException;
28
29
30
31
32
33
34
35 public class JdmkAgent implements UMOAgent
36 {
37
38 public static final String CLASSNAME_ADAPTER = "com.sun.jdmk.comm.HtmlAdaptorServer";
39
40 private static final String PROTOCOL_PREFIX = "http://";
41 public static final String DEFAULT_HOSTNAME = "localhost";
42 public static final int DEFAULT_PORT = 9092;
43 public static final String DEFAULT_JMX_ADAPTOR_URL = PROTOCOL_PREFIX + DEFAULT_HOSTNAME + ":" + DEFAULT_PORT;
44
45 private String jmxAdaptorUrl;
46 private String host;
47 private String port;
48
49 private String name = "JDMK Agent";
50 private MBeanServer mBeanServer;
51 private ObjectName adaptorName;
52
53 protected Object createAdaptor() throws Exception
54 {
55 final URI uri = new URI(jmxAdaptorUrl);
56 final int port = uri.getPort();
57 return ClassUtils.instanciateClass(CLASSNAME_ADAPTER,
58 new Object[] {new Integer(port)}, this.getClass());
59 }
60
61 public String getName()
62 {
63 return this.name;
64 }
65
66 public void setName(String name)
67 {
68 this.name = name;
69 }
70
71 public String getDescription()
72 {
73 return "Jdmk Http adaptor: " + jmxAdaptorUrl;
74 }
75
76 public void start() throws UMOException
77 {
78 try
79 {
80 mBeanServer.invoke(adaptorName, "start", null, null);
81 }
82 catch (InstanceNotFoundException e)
83 {
84 throw new JmxManagementException(
85 CoreMessages.failedToStart("Jdmk agent"), adaptorName, e);
86 }
87 catch (MBeanException e)
88 {
89 throw new JmxManagementException(
90 CoreMessages.failedToStart("Jdmk agent"), adaptorName, e);
91 }
92 catch (ReflectionException e)
93 {
94
95 }
96 }
97
98 public void stop() throws UMOException
99 {
100 if (mBeanServer == null)
101 {
102 return;
103 }
104
105 try
106 {
107 mBeanServer.invoke(adaptorName, "stop", null, null);
108 }
109 catch (InstanceNotFoundException e)
110 {
111 throw new JmxManagementException(
112 CoreMessages.failedToStop("Jdmk agent"), adaptorName, e);
113 }
114 catch (MBeanException e)
115 {
116 throw new JmxManagementException(
117 CoreMessages.failedToStop("Jdmk agent"), adaptorName, e);
118 }
119 catch (ReflectionException e)
120 {
121
122 }
123 }
124
125 public void dispose()
126 {
127 try
128 {
129 stop();
130 }
131 catch (Exception e)
132 {
133
134 }
135 }
136
137 public void registered()
138 {
139
140 }
141
142 public void unregistered()
143 {
144
145 }
146
147 public void initialise() throws InitialisationException
148 {
149 try
150 {
151 mBeanServer = (MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0);
152 final Object adaptor = createAdaptor();
153 if (StringUtils.isBlank(jmxAdaptorUrl))
154 {
155 if (StringUtils.isNotBlank(host) && StringUtils.isNotBlank(port))
156 {
157 jmxAdaptorUrl = PROTOCOL_PREFIX + host + ":" + port;
158 }
159 else
160 {
161 jmxAdaptorUrl = DEFAULT_JMX_ADAPTOR_URL;
162 }
163 }
164
165 adaptorName = new ObjectName("Adaptor:class=" + adaptor.getClass().getName());
166 mBeanServer.registerMBean(adaptor, adaptorName);
167 }
168 catch (Exception e)
169 {
170 throw new InitialisationException(CoreMessages.failedToStart("Jdmk Agent"), e, this);
171 }
172 }
173
174
175
176
177 public String getJmxAdaptorUrl()
178 {
179 return jmxAdaptorUrl;
180 }
181
182
183
184
185 public void setJmxAdaptorUrl(String jmxAdaptorUrl)
186 {
187 this.jmxAdaptorUrl = jmxAdaptorUrl;
188 }
189
190
191 public String getHost()
192 {
193 return host;
194 }
195
196 public void setHost(String host)
197 {
198 this.host = host;
199 }
200
201 public String getPort()
202 {
203 return port;
204 }
205
206 public void setPort(String port)
207 {
208 this.port = port;
209 }
210 }