author | hb |
Mon, 01 Jan 2018 23:59:14 +0530 | |
branch | jmx-rest-api |
changeset 56002 | 60ab3b595a8e |
parent 55998 | 54779691e11f |
child 56003 | 4f7f76f6be2f |
permissions | -rw-r--r-- |
55985 | 1 |
/* |
55997 | 2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. |
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
55985 | 24 |
*/ |
55997 | 25 |
|
55985 | 26 |
package javax.management.remote.rest; |
27 |
||
55994 | 28 |
import com.oracle.jmx.remote.rest.http.MBeanServerCollectionResource; |
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
29 |
import com.oracle.jmx.remote.rest.http.MBeanServerResource; |
55985 | 30 |
import com.sun.net.httpserver.HttpServer; |
31 |
import com.sun.net.httpserver.HttpsConfigurator; |
|
32 |
import com.sun.net.httpserver.HttpsServer; |
|
33 |
||
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
34 |
import javax.management.*; |
55985 | 35 |
import javax.net.ssl.KeyManagerFactory; |
36 |
import javax.net.ssl.SSLContext; |
|
37 |
import javax.net.ssl.TrustManagerFactory; |
|
38 |
import java.io.BufferedInputStream; |
|
39 |
import java.io.FileInputStream; |
|
40 |
import java.io.IOException; |
|
41 |
import java.lang.management.ManagementFactory; |
|
55994 | 42 |
import java.net.InetAddress; |
55985 | 43 |
import java.net.InetSocketAddress; |
55994 | 44 |
import java.net.UnknownHostException; |
55985 | 45 |
import java.security.KeyStore; |
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
46 |
import java.util.HashMap; |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
47 |
import java.util.List; |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
48 |
import java.util.Map; |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
49 |
import java.util.Properties; |
56002 | 50 |
import java.util.concurrent.*; |
51 |
import java.util.concurrent.atomic.AtomicInteger; |
|
55985 | 52 |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
53 |
/** |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
54 |
* This is the root class that initializes the HTTPServer and |
55997 | 55 |
* REST adapter for platform mBeanServer. |
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
56 |
* |
55997 | 57 |
* @since 11 |
55985 | 58 |
*/ |
55994 | 59 |
public class PlatformRestAdapter implements MBeanServerFactoryListener { |
55985 | 60 |
|
61 |
/* |
|
62 |
* Initializes HTTPServer with settings from config file |
|
63 |
* acts as container for platform rest adapter |
|
64 |
*/ |
|
65 |
private static HttpServer httpServer = null; |
|
55997 | 66 |
|
67 |
// Save configuration to be used for other MBeanServers |
|
55994 | 68 |
private static Map<String, Object> env; |
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
69 |
private static List<MBeanServerResource> restAdapters = new CopyOnWriteArrayList<>(); |
56002 | 70 |
private static final int maxThreadCount = 5; |
55997 | 71 |
|
55985 | 72 |
private PlatformRestAdapter() { |
73 |
} |
|
74 |
||
56002 | 75 |
private static class HttpThreadFactory implements ThreadFactory { |
76 |
||
77 |
private static final AtomicInteger poolNumber = new AtomicInteger(1); |
|
78 |
private final ThreadGroup group; |
|
79 |
private final AtomicInteger threadNumber = new AtomicInteger(1); |
|
80 |
private final String namePrefix = "http-thread-"; |
|
81 |
||
82 |
HttpThreadFactory() { |
|
83 |
SecurityManager s = System.getSecurityManager(); |
|
84 |
group = (s != null) ? s.getThreadGroup() : |
|
85 |
Thread.currentThread().getThreadGroup(); |
|
86 |
} |
|
87 |
||
88 |
public Thread newThread(Runnable r) { |
|
89 |
Thread t = new Thread(group, r, |
|
90 |
namePrefix + threadNumber.getAndIncrement(), |
|
91 |
0); |
|
92 |
if (t.isDaemon()) |
|
93 |
t.setDaemon(false); |
|
94 |
if (t.getPriority() != Thread.NORM_PRIORITY) |
|
95 |
t.setPriority(Thread.NORM_PRIORITY); |
|
96 |
return t; |
|
97 |
} |
|
98 |
} |
|
99 |
||
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
100 |
/** |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
101 |
* Starts the HTTP server with confiuration specified in properties. |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
102 |
* The configuration properties are Interface name/IP, port and SSL configuration |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
103 |
* By default the server binds to address '0.0.0.0' and port '0'. SSL is off by default. [TODO]The |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
104 |
* keyStore will be created one if not configured and the private key and a public certificate will |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
105 |
* be generated[/TODO]. |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
106 |
* Below properties are used to configure the HTTP server. |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
107 |
* com.sun.management.jmxremote.rest.port |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
108 |
* com.sun.management.jmxremote.rest.host |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
109 |
* com.sun.management.jmxremote.ssl |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
110 |
* com.sun.management.jmxremote.ssl.config.file |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
111 |
* javax.net.ssl.keyStore |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
112 |
* javax.net.ssl.trustStore |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
113 |
* javax.net.ssl.keyStorePassword |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
114 |
* javax.net.ssl.trustStorePassword |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
115 |
* @param properties Config properties for the HTTP server. |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
116 |
* If null or if any properties are not specified, default values will be assumed. |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
117 |
* @throws IOException If the server could not be created |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
118 |
*/ |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
119 |
public static synchronized void init(Properties properties) throws IOException { |
55994 | 120 |
if (httpServer == null) { |
56002 | 121 |
if(properties == null || properties.isEmpty()) { |
122 |
properties = new Properties(); |
|
123 |
properties.setProperty("com.sun.management.jmxremote.ssl", "false"); |
|
124 |
properties.setProperty("com.sun.management.jmxremote.authenticate", "false"); |
|
125 |
properties.setProperty("com.sun.management.jmxremote.rest.port", "0"); |
|
126 |
} |
|
55985 | 127 |
final int port; |
128 |
try { |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
129 |
port = Integer.parseInt(properties.getProperty(PropertyNames.PORT, DefaultValues.PORT)); |
55985 | 130 |
} catch (NumberFormatException x) { |
131 |
throw new IllegalArgumentException("Invalid string for port"); |
|
132 |
} |
|
133 |
if (port < 0) { |
|
134 |
throw new IllegalArgumentException("Invalid string for port"); |
|
135 |
} |
|
136 |
||
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
137 |
String host = properties.getProperty(PropertyNames.HOST, DefaultValues.HOST); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
138 |
|
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
139 |
boolean useSSL = Boolean.parseBoolean(properties.getProperty( |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
140 |
PropertyNames.USE_SSL, DefaultValues.USE_SSL)); |
55985 | 141 |
if (useSSL) { |
142 |
final String sslConfigFileName |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
143 |
= properties.getProperty(PropertyNames.SSL_CONFIG_FILE_NAME); |
55985 | 144 |
SSLContext ctx = getSSlContext(sslConfigFileName); |
145 |
if (ctx != null) { |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
146 |
HttpsServer server = HttpsServer.create(new InetSocketAddress(host, port), 0); |
55985 | 147 |
server.setHttpsConfigurator(new HttpsConfigurator(ctx)); |
148 |
httpServer = server; |
|
149 |
} else { |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
150 |
httpServer = HttpServer.create(new InetSocketAddress(host, port), 0); |
55985 | 151 |
} |
152 |
} else { |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
153 |
httpServer = HttpServer.create(new InetSocketAddress(host, port), 0); |
55985 | 154 |
} |
155 |
||
55994 | 156 |
new MBeanServerCollectionResource(restAdapters, httpServer); |
56002 | 157 |
httpServer.setExecutor(Executors.newFixedThreadPool(maxThreadCount, new HttpThreadFactory())); |
55985 | 158 |
httpServer.start(); |
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
159 |
startDefaultRestAdapter(properties); |
55985 | 160 |
} |
161 |
} |
|
162 |
||
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
163 |
private static void startDefaultRestAdapter(Properties properties) { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
164 |
env = new HashMap<>(); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
165 |
// Do we use authentication? |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
166 |
final String useAuthenticationStr |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
167 |
= properties.getProperty(PropertyNames.USE_AUTHENTICATION, |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
168 |
DefaultValues.USE_AUTHENTICATION); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
169 |
final boolean useAuthentication |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
170 |
= Boolean.valueOf(useAuthenticationStr); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
171 |
|
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
172 |
String loginConfigName; |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
173 |
String passwordFileName; |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
174 |
|
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
175 |
if (useAuthentication) { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
176 |
env.put("jmx.remote.x.authentication", Boolean.TRUE); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
177 |
// Get non-default login configuration |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
178 |
loginConfigName |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
179 |
= properties.getProperty(PropertyNames.LOGIN_CONFIG_NAME); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
180 |
env.put("jmx.remote.x.login.config", loginConfigName); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
181 |
|
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
182 |
if (loginConfigName == null) { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
183 |
// Get password file |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
184 |
passwordFileName |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
185 |
= properties.getProperty(PropertyNames.PASSWORD_FILE_NAME); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
186 |
env.put("jmx.remote.x.password.file", passwordFileName); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
187 |
} |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
188 |
} |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
189 |
MBeanServerResource adapter = new MBeanServerResource(httpServer, ManagementFactory.getPlatformMBeanServer(), "platform", env); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
190 |
adapter.start(); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
191 |
restAdapters.add(adapter); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
192 |
} |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
193 |
|
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
194 |
/** |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
195 |
* Wraps the mbeanServer in a REST adapter. The mBeanServer will be accessible over REST APIs |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
196 |
* at supplied context. env parameter configures authentication parameters for the MBeanServer. |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
197 |
* |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
198 |
* @param mbeanServer The MBeanServer to be wrapped in REST adapter |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
199 |
* @param context The context in HTTP server under which this MBeanServer will be available over REST |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
200 |
* If it is null or empty, a context will be generated |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
201 |
* @param env configures authemtication parameters for accessing the MBeanServer over this adapter |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
202 |
* If null, configuration from default rest adapter will be used. |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
203 |
* Below is the list of properties. |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
204 |
* <p> |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
205 |
* jmx.remote.x.authentication : enable/disable user authentication |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
206 |
* jmx.remote.authenticator : Instance of a JMXAuthenticator |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
207 |
* jmx.remote.x.login.config : JAAS login conguration |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
208 |
* jmx.remote.x.password.file : file name for default JAAS login configuration |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
209 |
* @return an Instance of REST adapter that allows to start/stop the adapter |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
210 |
*/ |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
211 |
public static synchronized JmxRestAdapter newRestAdapter(MBeanServer mbeanServer, String context, Map<String, ?> env) { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
212 |
if (httpServer == null) { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
213 |
throw new IllegalStateException("Platform Adapter not initialized"); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
214 |
} |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
215 |
|
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
216 |
MBeanServerResource server = restAdapters.stream() |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
217 |
.filter(s -> areMBeanServersEqual(s.getMBeanServer(), mbeanServer)) |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
218 |
.findFirst() |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
219 |
.get(); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
220 |
if (server == null) { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
221 |
MBeanServerResource adapter = new MBeanServerResource(httpServer, mbeanServer, context, env); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
222 |
adapter.start(); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
223 |
restAdapters.add(adapter); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
224 |
return adapter; |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
225 |
} else { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
226 |
throw new IllegalArgumentException("MBeanServer already registered at " + server.getUrl()); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
227 |
} |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
228 |
} |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
229 |
|
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
230 |
private static boolean areMBeanServersEqual(MBeanServer server1, MBeanServer server2) { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
231 |
MBeanServerDelegateMBean bean1 = JMX.newMBeanProxy(server1, MBeanServerDelegate.DELEGATE_NAME, MBeanServerDelegateMBean.class); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
232 |
MBeanServerDelegateMBean bean2 = JMX.newMBeanProxy(server2, MBeanServerDelegate.DELEGATE_NAME, MBeanServerDelegateMBean.class); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
233 |
return bean1.getMBeanServerId().equalsIgnoreCase(bean2.getMBeanServerId()); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
234 |
} |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
235 |
|
55994 | 236 |
public synchronized static void stop() { |
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
237 |
restAdapters.forEach(r -> r.stop()); |
56002 | 238 |
restAdapters.clear(); |
55985 | 239 |
if (httpServer != null) { |
56002 | 240 |
ExecutorService executor = (ExecutorService) httpServer.getExecutor(); |
241 |
executor.shutdownNow(); |
|
242 |
try { |
|
243 |
executor.awaitTermination(30, TimeUnit.SECONDS); |
|
244 |
} catch (InterruptedException e) { |
|
245 |
} |
|
55985 | 246 |
httpServer.stop(0); |
247 |
httpServer = null; |
|
248 |
} |
|
249 |
} |
|
250 |
||
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
251 |
/* |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
252 |
This auto addition of MBeanServer to rest adapter must be controlled by a system property |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
253 |
com.sun.management.jmxremote.mbeanserver.autoadd=true/false |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
254 |
This allows application MBeans to be be availble over REST without any changes to application code. |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
255 |
*/ |
55994 | 256 |
@Override |
257 |
public void onMBeanServerCreated(MBeanServer mBeanServer) { |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
258 |
try { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
259 |
newRestAdapter(mBeanServer, "", env); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
260 |
} catch (IllegalArgumentException | IllegalStateException e) { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
261 |
} |
55994 | 262 |
} |
263 |
||
264 |
@Override |
|
265 |
public void onMBeanServerRemoved(MBeanServer mBeanServer) { |
|
266 |
||
267 |
} |
|
268 |
||
55997 | 269 |
public static synchronized String getDomain() { |
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
270 |
if (httpServer == null) { |
55994 | 271 |
throw new IllegalStateException("Platform rest adapter not initialized"); |
272 |
} |
|
273 |
try { |
|
274 |
if (httpServer instanceof HttpsServer) { |
|
275 |
return "https://" + InetAddress.getLocalHost().getHostName() + ":" + httpServer.getAddress().getPort(); |
|
276 |
} |
|
277 |
return "http://" + InetAddress.getLocalHost().getHostName() + ":" + httpServer.getAddress().getPort(); |
|
278 |
} catch (UnknownHostException ex) { |
|
279 |
return "http://localhost" + ":" + httpServer.getAddress().getPort(); |
|
280 |
} |
|
281 |
} |
|
282 |
||
283 |
public static synchronized String getBaseURL() { |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
284 |
return getDomain() + "/jmx/servers"; |
55994 | 285 |
} |
286 |
||
55985 | 287 |
private static SSLContext getSSlContext(String sslConfigFileName) { |
288 |
final String keyStore, keyStorePassword, trustStore, trustStorePassword; |
|
289 |
||
290 |
try { |
|
291 |
if (sslConfigFileName == null || sslConfigFileName.isEmpty()) { |
|
292 |
keyStore = System.getProperty(PropertyNames.SSL_KEYSTORE_FILE); |
|
293 |
keyStorePassword = System.getProperty(PropertyNames.SSL_KEYSTORE_PASSWORD); |
|
294 |
trustStore = System.getProperty(PropertyNames.SSL_TRUSTSTORE_FILE); |
|
295 |
trustStorePassword = System.getProperty(PropertyNames.SSL_TRUSTSTORE_PASSWORD); |
|
296 |
} else { |
|
297 |
Properties p = new Properties(); |
|
298 |
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(sslConfigFileName)); |
|
299 |
p.load(bin); |
|
300 |
keyStore = p.getProperty(PropertyNames.SSL_KEYSTORE_FILE); |
|
301 |
keyStorePassword = p.getProperty(PropertyNames.SSL_KEYSTORE_PASSWORD); |
|
302 |
trustStore = p.getProperty(PropertyNames.SSL_TRUSTSTORE_FILE); |
|
303 |
trustStorePassword = p.getProperty(PropertyNames.SSL_TRUSTSTORE_PASSWORD); |
|
304 |
} |
|
305 |
||
306 |
char[] keyStorePasswd = null; |
|
307 |
if (keyStorePassword.length() != 0) { |
|
308 |
keyStorePasswd = keyStorePassword.toCharArray(); |
|
309 |
} |
|
310 |
||
311 |
char[] trustStorePasswd = null; |
|
312 |
if (trustStorePassword.length() != 0) { |
|
313 |
trustStorePasswd = trustStorePassword.toCharArray(); |
|
314 |
} |
|
315 |
||
316 |
KeyStore ks = null; |
|
317 |
if (keyStore != null) { |
|
318 |
ks = KeyStore.getInstance(KeyStore.getDefaultType()); |
|
319 |
FileInputStream ksfis = new FileInputStream(keyStore); |
|
320 |
ks.load(ksfis, keyStorePasswd); |
|
321 |
||
322 |
} |
|
323 |
KeyManagerFactory kmf = KeyManagerFactory.getInstance( |
|
324 |
KeyManagerFactory.getDefaultAlgorithm()); |
|
325 |
kmf.init(ks, keyStorePasswd); |
|
326 |
||
327 |
KeyStore ts = null; |
|
328 |
if (trustStore != null) { |
|
329 |
ts = KeyStore.getInstance(KeyStore.getDefaultType()); |
|
330 |
FileInputStream tsfis = new FileInputStream(trustStore); |
|
331 |
ts.load(tsfis, trustStorePasswd); |
|
332 |
} |
|
333 |
TrustManagerFactory tmf = TrustManagerFactory.getInstance( |
|
334 |
TrustManagerFactory.getDefaultAlgorithm()); |
|
335 |
tmf.init(ts); |
|
336 |
||
337 |
SSLContext ctx = SSLContext.getInstance("SSL"); |
|
338 |
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
|
339 |
return ctx; |
|
340 |
} catch (Exception ex) { |
|
341 |
} |
|
342 |
return null; |
|
343 |
} |
|
344 |
||
345 |
/** |
|
346 |
* Default values for JMX configuration properties. |
|
347 |
*/ |
|
55995 | 348 |
static interface DefaultValues { |
55985 | 349 |
|
350 |
public static final String PORT = "0"; |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
351 |
public static final String HOST = "0.0.0.0"; |
55995 | 352 |
public static final String USE_SSL = "false"; |
353 |
public static final String USE_AUTHENTICATION = "false"; |
|
55985 | 354 |
public static final String PASSWORD_FILE_NAME = "jmxremote.password"; |
355 |
} |
|
356 |
||
357 |
/** |
|
358 |
* Names of JMX configuration properties. |
|
359 |
*/ |
|
360 |
public static interface PropertyNames { |
|
361 |
||
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
362 |
public static final String PORT |
55985 | 363 |
= "com.sun.management.jmxremote.rest.port"; |
364 |
public static final String HOST |
|
365 |
= "com.sun.management.jmxremote.host"; |
|
366 |
public static final String USE_SSL |
|
367 |
= "com.sun.management.jmxremote.ssl"; |
|
368 |
public static final String SSL_CONFIG_FILE_NAME |
|
369 |
= "com.sun.management.jmxremote.ssl.config.file"; |
|
370 |
public static final String SSL_KEYSTORE_FILE |
|
371 |
= "javax.net.ssl.keyStore"; |
|
372 |
public static final String SSL_TRUSTSTORE_FILE |
|
373 |
= "javax.net.ssl.trustStore"; |
|
374 |
public static final String SSL_KEYSTORE_PASSWORD |
|
375 |
= "javax.net.ssl.keyStorePassword"; |
|
376 |
public static final String SSL_TRUSTSTORE_PASSWORD |
|
377 |
= "javax.net.ssl.trustStorePassword"; |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
378 |
public static final String USE_AUTHENTICATION |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
379 |
= "com.sun.management.jmxremote.authenticate"; |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
380 |
public static final String PASSWORD_FILE_NAME |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
381 |
= "com.sun.management.jmxremote.password.file"; |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
382 |
public static final String LOGIN_CONFIG_NAME |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
383 |
= "com.sun.management.jmxremote.login.config"; |
55985 | 384 |
} |
385 |
} |