55985
|
1 |
/*
|
|
2 |
* To change this license header, choose License Headers in Project Properties.
|
|
3 |
* To change this template file, choose Tools | Templates
|
|
4 |
* and open the template in the editor.
|
|
5 |
*/
|
|
6 |
package javax.management.remote.rest;
|
|
7 |
|
55994
|
8 |
import javax.management.MBeanServerFactoryListener;
|
|
9 |
|
|
10 |
import com.oracle.jmx.remote.rest.http.JmxRestAdapter;
|
|
11 |
import com.oracle.jmx.remote.rest.http.MBeanServerCollectionResource;
|
55985
|
12 |
import com.sun.net.httpserver.HttpServer;
|
|
13 |
import com.sun.net.httpserver.HttpsConfigurator;
|
|
14 |
import com.sun.net.httpserver.HttpsServer;
|
|
15 |
|
|
16 |
import javax.management.MBeanServer;
|
|
17 |
import javax.net.ssl.KeyManagerFactory;
|
|
18 |
import javax.net.ssl.SSLContext;
|
|
19 |
import javax.net.ssl.TrustManagerFactory;
|
|
20 |
import java.io.BufferedInputStream;
|
|
21 |
import java.io.FileInputStream;
|
|
22 |
import java.io.IOException;
|
|
23 |
import java.lang.management.ManagementFactory;
|
55994
|
24 |
import java.net.InetAddress;
|
55985
|
25 |
import java.net.InetSocketAddress;
|
55994
|
26 |
import java.net.UnknownHostException;
|
55985
|
27 |
import java.security.KeyStore;
|
|
28 |
import java.util.*;
|
55994
|
29 |
import java.util.concurrent.CopyOnWriteArrayList;
|
|
30 |
import java.util.concurrent.Executors;
|
55985
|
31 |
|
|
32 |
/**
|
|
33 |
* @author harsha
|
|
34 |
*/
|
55994
|
35 |
public class PlatformRestAdapter implements MBeanServerFactoryListener {
|
55985
|
36 |
|
|
37 |
/*
|
|
38 |
* Initializes HTTPServer with settings from config file
|
|
39 |
* acts as container for platform rest adapter
|
|
40 |
*/
|
|
41 |
private static HttpServer httpServer = null;
|
55994
|
42 |
private static List<JmxRestAdapter> restAdapters = new CopyOnWriteArrayList<>();
|
|
43 |
private static Map<String, Object> env;
|
|
44 |
|
|
45 |
private static String portStr;
|
|
46 |
private static Properties props;
|
55985
|
47 |
|
|
48 |
private PlatformRestAdapter() {
|
|
49 |
}
|
|
50 |
|
|
51 |
public static synchronized void init(String portStr, Properties props) throws IOException {
|
55994
|
52 |
PlatformRestAdapter.portStr = portStr;
|
|
53 |
PlatformRestAdapter.props = props;
|
|
54 |
|
|
55 |
if (httpServer == null) {
|
55985
|
56 |
final int port;
|
|
57 |
try {
|
|
58 |
port = Integer.parseInt(portStr);
|
|
59 |
} catch (NumberFormatException x) {
|
|
60 |
throw new IllegalArgumentException("Invalid string for port");
|
|
61 |
}
|
|
62 |
if (port < 0) {
|
|
63 |
throw new IllegalArgumentException("Invalid string for port");
|
|
64 |
}
|
|
65 |
|
|
66 |
boolean useSSL = Boolean.parseBoolean((String) props.get("com.sun.management.jmxremote.ssl"));
|
|
67 |
if (useSSL) {
|
|
68 |
final String sslConfigFileName
|
|
69 |
= props.getProperty(PropertyNames.SSL_CONFIG_FILE_NAME);
|
|
70 |
SSLContext ctx = getSSlContext(sslConfigFileName);
|
|
71 |
if (ctx != null) {
|
55994
|
72 |
HttpsServer server = HttpsServer.create(new InetSocketAddress("0.0.0.0", port), 0);
|
55985
|
73 |
server.setHttpsConfigurator(new HttpsConfigurator(ctx));
|
|
74 |
httpServer = server;
|
|
75 |
} else {
|
55994
|
76 |
httpServer = HttpServer.create(new InetSocketAddress("0.0.0.0", port), 0);
|
55985
|
77 |
}
|
|
78 |
} else {
|
55994
|
79 |
httpServer = HttpServer.create(new InetSocketAddress("0.0.0.0", port), 0);
|
55985
|
80 |
}
|
|
81 |
|
|
82 |
// Initialize rest adapter
|
55994
|
83 |
env = new HashMap<>();
|
55985
|
84 |
// Do we use authentication?
|
|
85 |
final String useAuthenticationStr
|
|
86 |
= props.getProperty(PropertyNames.USE_AUTHENTICATION,
|
|
87 |
DefaultValues.USE_AUTHENTICATION);
|
|
88 |
final boolean useAuthentication
|
|
89 |
= Boolean.valueOf(useAuthenticationStr);
|
|
90 |
|
|
91 |
String loginConfigName;
|
|
92 |
String passwordFileName;
|
|
93 |
|
|
94 |
if (useAuthentication) {
|
|
95 |
env.put("jmx.remote.x.authentication", Boolean.TRUE);
|
|
96 |
// Get non-default login configuration
|
|
97 |
loginConfigName
|
|
98 |
= props.getProperty(PropertyNames.LOGIN_CONFIG_NAME);
|
|
99 |
env.put("jmx.remote.x.login.config", loginConfigName);
|
|
100 |
|
|
101 |
if (loginConfigName == null) {
|
|
102 |
// Get password file
|
|
103 |
passwordFileName
|
|
104 |
= props.getProperty(PropertyNames.PASSWORD_FILE_NAME);
|
|
105 |
env.put("jmx.remote.x.password.file", passwordFileName);
|
|
106 |
}
|
|
107 |
}
|
|
108 |
|
55994
|
109 |
restAdapters.add(new JmxRestAdapter(httpServer, "platform", env, ManagementFactory.getPlatformMBeanServer()));
|
|
110 |
new MBeanServerCollectionResource(restAdapters, httpServer);
|
|
111 |
httpServer.setExecutor(Executors.newCachedThreadPool());
|
55985
|
112 |
httpServer.start();
|
|
113 |
}
|
|
114 |
}
|
|
115 |
|
55994
|
116 |
public synchronized static void stop() {
|
55985
|
117 |
if (httpServer != null) {
|
|
118 |
httpServer.stop(0);
|
|
119 |
httpServer = null;
|
|
120 |
}
|
|
121 |
}
|
|
122 |
|
55994
|
123 |
public synchronized static void start() throws IOException {
|
|
124 |
if(httpServer == null) {
|
|
125 |
PlatformRestAdapter.init(portStr,props);
|
|
126 |
}
|
|
127 |
}
|
|
128 |
|
|
129 |
@Override
|
|
130 |
public void onMBeanServerCreated(MBeanServer mBeanServer) {
|
|
131 |
JmxRestAdapter restAdapter = new JmxRestAdapter(httpServer, "", env, mBeanServer);
|
|
132 |
restAdapters.add(restAdapter);
|
|
133 |
}
|
|
134 |
|
|
135 |
@Override
|
|
136 |
public void onMBeanServerRemoved(MBeanServer mBeanServer) {
|
|
137 |
|
|
138 |
}
|
|
139 |
|
|
140 |
public static synchronized String getAuthority() {
|
|
141 |
if(httpServer == null) {
|
|
142 |
throw new IllegalStateException("Platform rest adapter not initialized");
|
|
143 |
}
|
|
144 |
try {
|
|
145 |
if (httpServer instanceof HttpsServer) {
|
|
146 |
return "https://" + InetAddress.getLocalHost().getHostName() + ":" + httpServer.getAddress().getPort();
|
|
147 |
}
|
|
148 |
return "http://" + InetAddress.getLocalHost().getHostName() + ":" + httpServer.getAddress().getPort();
|
|
149 |
} catch (UnknownHostException ex) {
|
|
150 |
return "http://localhost" + ":" + httpServer.getAddress().getPort();
|
|
151 |
}
|
|
152 |
}
|
|
153 |
|
|
154 |
public static synchronized String getBaseURL() {
|
|
155 |
if(httpServer == null) {
|
|
156 |
throw new IllegalStateException("Platform rest adapter not initialized");
|
|
157 |
}
|
|
158 |
try {
|
|
159 |
if (httpServer instanceof HttpsServer) {
|
|
160 |
return "https://" + InetAddress.getLocalHost().getHostName() + ":" + httpServer.getAddress().getPort() + "/jmx/servers";
|
|
161 |
}
|
|
162 |
return "http://" + InetAddress.getLocalHost().getHostName() + ":" + httpServer.getAddress().getPort() + "/jmx/servers";
|
|
163 |
} catch (UnknownHostException ex) {
|
|
164 |
return "http://localhost" + ":" + httpServer.getAddress().getPort() + "/jmx/servers";
|
|
165 |
}
|
|
166 |
}
|
|
167 |
|
55985
|
168 |
private static SSLContext getSSlContext(String sslConfigFileName) {
|
|
169 |
final String keyStore, keyStorePassword, trustStore, trustStorePassword;
|
|
170 |
|
|
171 |
try {
|
|
172 |
if (sslConfigFileName == null || sslConfigFileName.isEmpty()) {
|
|
173 |
keyStore = System.getProperty(PropertyNames.SSL_KEYSTORE_FILE);
|
|
174 |
keyStorePassword = System.getProperty(PropertyNames.SSL_KEYSTORE_PASSWORD);
|
|
175 |
trustStore = System.getProperty(PropertyNames.SSL_TRUSTSTORE_FILE);
|
|
176 |
trustStorePassword = System.getProperty(PropertyNames.SSL_TRUSTSTORE_PASSWORD);
|
|
177 |
} else {
|
|
178 |
Properties p = new Properties();
|
|
179 |
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(sslConfigFileName));
|
|
180 |
p.load(bin);
|
|
181 |
keyStore = p.getProperty(PropertyNames.SSL_KEYSTORE_FILE);
|
|
182 |
keyStorePassword = p.getProperty(PropertyNames.SSL_KEYSTORE_PASSWORD);
|
|
183 |
trustStore = p.getProperty(PropertyNames.SSL_TRUSTSTORE_FILE);
|
|
184 |
trustStorePassword = p.getProperty(PropertyNames.SSL_TRUSTSTORE_PASSWORD);
|
|
185 |
}
|
|
186 |
|
|
187 |
char[] keyStorePasswd = null;
|
|
188 |
if (keyStorePassword.length() != 0) {
|
|
189 |
keyStorePasswd = keyStorePassword.toCharArray();
|
|
190 |
}
|
|
191 |
|
|
192 |
char[] trustStorePasswd = null;
|
|
193 |
if (trustStorePassword.length() != 0) {
|
|
194 |
trustStorePasswd = trustStorePassword.toCharArray();
|
|
195 |
}
|
|
196 |
|
|
197 |
KeyStore ks = null;
|
|
198 |
if (keyStore != null) {
|
|
199 |
ks = KeyStore.getInstance(KeyStore.getDefaultType());
|
|
200 |
FileInputStream ksfis = new FileInputStream(keyStore);
|
|
201 |
ks.load(ksfis, keyStorePasswd);
|
|
202 |
|
|
203 |
}
|
|
204 |
KeyManagerFactory kmf = KeyManagerFactory.getInstance(
|
|
205 |
KeyManagerFactory.getDefaultAlgorithm());
|
|
206 |
kmf.init(ks, keyStorePasswd);
|
|
207 |
|
|
208 |
KeyStore ts = null;
|
|
209 |
if (trustStore != null) {
|
|
210 |
ts = KeyStore.getInstance(KeyStore.getDefaultType());
|
|
211 |
FileInputStream tsfis = new FileInputStream(trustStore);
|
|
212 |
ts.load(tsfis, trustStorePasswd);
|
|
213 |
}
|
|
214 |
TrustManagerFactory tmf = TrustManagerFactory.getInstance(
|
|
215 |
TrustManagerFactory.getDefaultAlgorithm());
|
|
216 |
tmf.init(ts);
|
|
217 |
|
|
218 |
SSLContext ctx = SSLContext.getInstance("SSL");
|
|
219 |
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null);
|
|
220 |
return ctx;
|
|
221 |
} catch (Exception ex) {
|
|
222 |
}
|
|
223 |
return null;
|
|
224 |
}
|
|
225 |
|
|
226 |
/**
|
|
227 |
* Default values for JMX configuration properties.
|
|
228 |
*/
|
55995
|
229 |
static interface DefaultValues {
|
55985
|
230 |
|
|
231 |
public static final String PORT = "0";
|
|
232 |
public static final String CONFIG_FILE_NAME = "management.properties";
|
55995
|
233 |
public static final String USE_SSL = "false";
|
55985
|
234 |
public static final String USE_LOCAL_ONLY = "true";
|
|
235 |
public static final String USE_REGISTRY_SSL = "false";
|
55995
|
236 |
public static final String USE_AUTHENTICATION = "false";
|
55985
|
237 |
public static final String PASSWORD_FILE_NAME = "jmxremote.password";
|
|
238 |
public static final String ACCESS_FILE_NAME = "jmxremote.access";
|
|
239 |
public static final String SSL_NEED_CLIENT_AUTH = "false";
|
|
240 |
}
|
|
241 |
|
|
242 |
/**
|
|
243 |
* Names of JMX configuration properties.
|
|
244 |
*/
|
|
245 |
public static interface PropertyNames {
|
|
246 |
|
55994
|
247 |
String PORT
|
55985
|
248 |
= "com.sun.management.jmxremote.rest.port";
|
|
249 |
public static final String HOST
|
|
250 |
= "com.sun.management.jmxremote.host";
|
|
251 |
public static final String USE_SSL
|
|
252 |
= "com.sun.management.jmxremote.ssl";
|
|
253 |
public static final String USE_AUTHENTICATION
|
|
254 |
= "com.sun.management.jmxremote.authenticate";
|
|
255 |
public static final String PASSWORD_FILE_NAME
|
|
256 |
= "com.sun.management.jmxremote.password.file";
|
|
257 |
public static final String LOGIN_CONFIG_NAME
|
|
258 |
= "com.sun.management.jmxremote.login.config";
|
|
259 |
public static final String SSL_NEED_CLIENT_AUTH
|
|
260 |
= "com.sun.management.jmxremote.ssl.need.client.auth";
|
|
261 |
public static final String SSL_CONFIG_FILE_NAME
|
|
262 |
= "com.sun.management.jmxremote.ssl.config.file";
|
|
263 |
public static final String SSL_KEYSTORE_FILE
|
|
264 |
= "javax.net.ssl.keyStore";
|
|
265 |
public static final String SSL_TRUSTSTORE_FILE
|
|
266 |
= "javax.net.ssl.trustStore";
|
|
267 |
public static final String SSL_KEYSTORE_PASSWORD
|
|
268 |
= "javax.net.ssl.keyStorePassword";
|
|
269 |
public static final String SSL_TRUSTSTORE_PASSWORD
|
|
270 |
= "javax.net.ssl.trustStorePassword";
|
|
271 |
}
|
|
272 |
}
|