1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.jetty.jmx;
20
21 import java.lang.management.ManagementFactory;
22 import java.net.InetAddress;
23 import java.net.ServerSocket;
24 import java.rmi.registry.LocateRegistry;
25 import java.rmi.registry.Registry;
26 import java.rmi.server.UnicastRemoteObject;
27 import java.util.Map;
28
29 import javax.management.MBeanServer;
30 import javax.management.ObjectName;
31 import javax.management.remote.JMXConnectorServer;
32 import javax.management.remote.JMXConnectorServerFactory;
33 import javax.management.remote.JMXServiceURL;
34
35 import org.eclipse.jetty.util.component.AbstractLifeCycle;
36 import org.eclipse.jetty.util.log.Log;
37 import org.eclipse.jetty.util.log.Logger;
38 import org.eclipse.jetty.util.thread.ShutdownThread;
39
40
41
42
43
44
45 public class ConnectorServer extends AbstractLifeCycle
46 {
47 private static final Logger LOG = Log.getLogger(ConnectorServer.class);
48
49 JMXConnectorServer _connectorServer;
50 Registry _registry;
51
52
53
54
55
56
57
58
59
60
61
62 public ConnectorServer(JMXServiceURL serviceURL, String name)
63 throws Exception
64 {
65 this(serviceURL, null, name);
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 public ConnectorServer(JMXServiceURL svcUrl, Map<String,?> environment, String name)
83 throws Exception
84 {
85 String urlPath = svcUrl.getURLPath();
86 int idx = urlPath.indexOf("rmi://");
87 if (idx > 0)
88 {
89 String hostPort = urlPath.substring(idx+6, urlPath.indexOf('/', idx+6));
90 String regHostPort = startRegistry(hostPort);
91 if (regHostPort != null) {
92 urlPath = urlPath.replace(hostPort,regHostPort);
93 svcUrl = new JMXServiceURL(svcUrl.getProtocol(), svcUrl.getHost(), svcUrl.getPort(), urlPath);
94 }
95 }
96 MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
97 _connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(svcUrl, environment, mbeanServer);
98 mbeanServer.registerMBean(_connectorServer,new ObjectName(name));
99 }
100
101
102
103
104
105 @Override
106 public void doStart()
107 throws Exception
108 {
109 _connectorServer.start();
110 ShutdownThread.register(0, this);
111
112 LOG.info("JMX Remote URL: {}", _connectorServer.getAddress().toString());
113 }
114
115
116
117
118
119 @Override
120 public void doStop()
121 throws Exception
122 {
123 ShutdownThread.deregister(this);
124 _connectorServer.stop();
125 stopRegistry();
126 }
127
128
129
130
131
132
133
134
135 private String startRegistry(String hostPath) throws Exception
136 {
137 int rmiPort = 1099;
138 String rmiHost = hostPath;
139
140 int idx = hostPath.indexOf(':');
141 if (idx > 0)
142 {
143 rmiPort = Integer.parseInt(hostPath.substring(idx + 1));
144 rmiHost = hostPath.substring(0,idx);
145 }
146
147
148 InetAddress hostAddress = InetAddress.getByName(rmiHost);
149 if(hostAddress.isLoopbackAddress())
150 {
151 if (rmiPort == 0)
152 {
153 ServerSocket socket = new ServerSocket(0);
154 rmiPort = socket.getLocalPort();
155 socket.close();
156 }
157 else
158 {
159 try
160 {
161
162 LocateRegistry.getRegistry(rmiPort).list();
163 return null;
164 }
165 catch (Exception ex)
166 {
167 LOG.ignore(ex);
168 }
169 }
170
171 _registry = LocateRegistry.createRegistry(rmiPort);
172 Thread.sleep(1000);
173
174 rmiHost = InetAddress.getLocalHost().getCanonicalHostName();
175 return rmiHost + ':' + Integer.toString(rmiPort);
176 }
177
178 return null;
179 }
180
181 private void stopRegistry()
182 {
183 if (_registry != null)
184 {
185 try
186 {
187 UnicastRemoteObject.unexportObject(_registry,true);
188 }
189 catch (Exception ex)
190 {
191 LOG.ignore(ex);
192 }
193 }
194 }
195 }