1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.module.management.agent; |
12 | |
|
13 | |
import org.mule.AbstractAgent; |
14 | |
import org.mule.api.MuleException; |
15 | |
import org.mule.api.lifecycle.InitialisationException; |
16 | |
import org.mule.config.i18n.CoreMessages; |
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 extends AbstractAgent |
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 MBeanServer mBeanServer; |
50 | |
private ObjectName adaptorName; |
51 | |
|
52 | |
|
53 | |
public JdmkAgent() |
54 | |
{ |
55 | 0 | super("jdmk-agent"); |
56 | 0 | } |
57 | |
|
58 | |
protected Object createAdaptor() throws Exception |
59 | |
{ |
60 | 0 | final URI uri = new URI(jmxAdaptorUrl); |
61 | 0 | final int port = uri.getPort(); |
62 | 0 | return ClassUtils.instanciateClass(CLASSNAME_ADAPTER, |
63 | |
new Object[] {new Integer(port)}, this.getClass()); |
64 | |
} |
65 | |
|
66 | |
public String getDescription() |
67 | |
{ |
68 | 0 | return "Jdmk Http adaptor: " + jmxAdaptorUrl; |
69 | |
} |
70 | |
|
71 | |
public void start() throws MuleException |
72 | |
{ |
73 | |
try |
74 | |
{ |
75 | 0 | mBeanServer.invoke(adaptorName, "start", null, null); |
76 | |
} |
77 | 0 | catch (InstanceNotFoundException e) |
78 | |
{ |
79 | 0 | throw new JmxManagementException( |
80 | |
CoreMessages.failedToStart("Jdmk agent"), adaptorName, e); |
81 | |
} |
82 | 0 | catch (MBeanException e) |
83 | |
{ |
84 | 0 | throw new JmxManagementException( |
85 | |
CoreMessages.failedToStart("Jdmk agent"), adaptorName, e); |
86 | |
} |
87 | 0 | catch (ReflectionException e) |
88 | |
{ |
89 | |
|
90 | 0 | } |
91 | 0 | } |
92 | |
|
93 | |
public void stop() throws MuleException |
94 | |
{ |
95 | 0 | if (mBeanServer == null) |
96 | |
{ |
97 | 0 | return; |
98 | |
} |
99 | |
|
100 | |
try |
101 | |
{ |
102 | 0 | mBeanServer.invoke(adaptorName, "stop", null, null); |
103 | |
} |
104 | 0 | catch (InstanceNotFoundException e) |
105 | |
{ |
106 | 0 | throw new JmxManagementException( |
107 | |
CoreMessages.failedToStop("Jdmk agent"), adaptorName, e); |
108 | |
} |
109 | 0 | catch (MBeanException e) |
110 | |
{ |
111 | 0 | throw new JmxManagementException( |
112 | |
CoreMessages.failedToStop("Jdmk agent"), adaptorName, e); |
113 | |
} |
114 | 0 | catch (ReflectionException e) |
115 | |
{ |
116 | |
|
117 | 0 | } |
118 | 0 | } |
119 | |
|
120 | |
public void dispose() |
121 | |
{ |
122 | |
try |
123 | |
{ |
124 | 0 | stop(); |
125 | |
} |
126 | 0 | catch (Exception e) |
127 | |
{ |
128 | |
|
129 | 0 | } |
130 | 0 | } |
131 | |
|
132 | |
public void initialise() throws InitialisationException |
133 | |
{ |
134 | |
try |
135 | |
{ |
136 | 0 | mBeanServer = (MBeanServer)MBeanServerFactory.findMBeanServer(null).get(0); |
137 | 0 | final Object adaptor = createAdaptor(); |
138 | 0 | if (StringUtils.isBlank(jmxAdaptorUrl)) |
139 | |
{ |
140 | 0 | if (StringUtils.isNotBlank(host) && StringUtils.isNotBlank(port)) |
141 | |
{ |
142 | 0 | jmxAdaptorUrl = PROTOCOL_PREFIX + host + ":" + port; |
143 | |
} |
144 | |
else |
145 | |
{ |
146 | 0 | jmxAdaptorUrl = DEFAULT_JMX_ADAPTOR_URL; |
147 | |
} |
148 | |
} |
149 | |
|
150 | 0 | adaptorName = new ObjectName("Adaptor:class=" + adaptor.getClass().getName()); |
151 | 0 | mBeanServer.registerMBean(adaptor, adaptorName); |
152 | |
} |
153 | 0 | catch (Exception e) |
154 | |
{ |
155 | 0 | throw new InitialisationException(CoreMessages.failedToStart("Jdmk Agent"), e, this); |
156 | 0 | } |
157 | 0 | } |
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
public String getJmxAdaptorUrl() |
163 | |
{ |
164 | 0 | return jmxAdaptorUrl; |
165 | |
} |
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
public void setJmxAdaptorUrl(String jmxAdaptorUrl) |
171 | |
{ |
172 | 0 | this.jmxAdaptorUrl = jmxAdaptorUrl; |
173 | 0 | } |
174 | |
|
175 | |
|
176 | |
public String getHost() |
177 | |
{ |
178 | 0 | return host; |
179 | |
} |
180 | |
|
181 | |
public void setHost(String host) |
182 | |
{ |
183 | 0 | this.host = host; |
184 | 0 | } |
185 | |
|
186 | |
public String getPort() |
187 | |
{ |
188 | 0 | return port; |
189 | |
} |
190 | |
|
191 | |
public void setPort(String port) |
192 | |
{ |
193 | 0 | this.port = port; |
194 | 0 | } |
195 | |
} |