1 //
2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
8 //
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
11 //
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
14 //
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
17 //
18
19 package org.eclipse.jetty.monitor.jmx;
20
21 import java.io.IOException;
22 import java.lang.management.ManagementFactory;
23
24 import javax.management.MBeanServer;
25 import javax.management.MBeanServerConnection;
26 import javax.management.remote.JMXConnector;
27 import javax.management.remote.JMXConnectorFactory;
28 import javax.management.remote.JMXConnectorServer;
29 import javax.management.remote.JMXConnectorServerFactory;
30 import javax.management.remote.JMXServiceURL;
31
32 import org.eclipse.jetty.util.log.Log;
33 import org.eclipse.jetty.util.log.Logger;
34
35
36 /* ------------------------------------------------------------ */
37 /**
38 * ServerConnection
39 *
40 * Provides ability to create a connection to either an external
41 * JMX server, or a loopback connection to the internal one.
42 */
43 public class ServiceConnection
44 {
45 private static final Logger LOG = Log.getLogger(ServiceConnection.class);
46
47 private String _serviceUrl;
48 private MBeanServer _server;
49 private JMXConnectorServer _connectorServer;
50 private JMXConnector _serverConnector;
51 private MBeanServerConnection _serviceConnection;
52
53 /* ------------------------------------------------------------ */
54 /**
55 * Construct a loopback connection to an internal server
56 *
57 * @throws IOException if unable to construct service connection
58 */
59 public ServiceConnection()
60 throws IOException
61 {
62 this(null);
63 }
64
65 /* ------------------------------------------------------------ */
66 /**
67 * Construct a connection to specified server
68 *
69 * @param url URL of JMX server
70 * @throws IOException if unable to construct service connection
71 */
72 public ServiceConnection(String url)
73 throws IOException
74 {
75 _serviceUrl = url;
76 }
77
78 /**
79 * Retrieve an external URL for the JMX server
80 *
81 * @return service URL
82 */
83 public String getServiceUrl()
84 {
85 return _serviceUrl;
86 }
87
88 /* ------------------------------------------------------------ */
89 /**
90 * Retrieve a connection to MBean server
91 *
92 * @return connection to MBean server
93 */
94 public MBeanServerConnection getConnection()
95 {
96 return _serviceConnection;
97 }
98
99 public void connect()
100 throws IOException
101 {
102 if (_serviceConnection == null)
103 {
104 if (_serviceUrl == null)
105 openLoopbackConnection();
106 else
107 openServerConnection(_serviceUrl);
108 }
109 }
110 /* ------------------------------------------------------------ */
111 /**
112 * Open a loopback connection to local JMX server
113 *
114 * @throws IOException
115 */
116 private void openLoopbackConnection()
117 throws IOException
118 {
119 _server = ManagementFactory.getPlatformMBeanServer();
120
121 JMXServiceURL serviceUrl = new JMXServiceURL("service:jmx:rmi://");
122 _connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(serviceUrl, null, _server);
123 _connectorServer.start();
124
125 _serviceUrl = _connectorServer.getAddress().toString();
126
127 _serverConnector = JMXConnectorFactory.connect(_connectorServer.getAddress());
128 _serviceConnection = _serverConnector.getMBeanServerConnection();
129 }
130
131 /* ------------------------------------------------------------ */
132 /**
133 * Open a connection to remote JMX server
134 *
135 * @param url
136 * @throws IOException
137 */
138 private void openServerConnection(String url)
139 throws IOException
140 {
141 _serviceUrl = url;
142
143 JMXServiceURL serviceUrl = new JMXServiceURL(_serviceUrl);
144 _serverConnector = JMXConnectorFactory.connect(serviceUrl);
145 _serviceConnection = _serverConnector.getMBeanServerConnection();
146 }
147
148 /* ------------------------------------------------------------ */
149 /**
150 * Close the connections
151 */
152 public void disconnect()
153 {
154 try
155 {
156 if (_serverConnector != null)
157 {
158 _serverConnector.close();
159 _serviceConnection = null;
160 }
161 if (_connectorServer != null)
162 {
163 _connectorServer.stop();
164 _connectorServer = null;
165 }
166 }
167 catch (Exception ex)
168 {
169 LOG.debug(ex);
170 }
171 }
172 }