author | hb |
Tue, 02 Jan 2018 13:20:18 +0530 | |
branch | jmx-rest-api |
changeset 56003 | 4f7f76f6be2f |
parent 56002 | 60ab3b595a8e |
child 56005 | 90cff2ac77b8 |
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 |
private final ThreadGroup group; |
|
77 |
private final AtomicInteger threadNumber = new AtomicInteger(1); |
|
78 |
private final String namePrefix = "http-thread-"; |
|
79 |
||
80 |
HttpThreadFactory() { |
|
81 |
SecurityManager s = System.getSecurityManager(); |
|
82 |
group = (s != null) ? s.getThreadGroup() : |
|
83 |
Thread.currentThread().getThreadGroup(); |
|
84 |
} |
|
85 |
||
86 |
public Thread newThread(Runnable r) { |
|
87 |
Thread t = new Thread(group, r, |
|
88 |
namePrefix + threadNumber.getAndIncrement(), |
|
89 |
0); |
|
90 |
if (t.isDaemon()) |
|
91 |
t.setDaemon(false); |
|
92 |
if (t.getPriority() != Thread.NORM_PRIORITY) |
|
93 |
t.setPriority(Thread.NORM_PRIORITY); |
|
94 |
return t; |
|
95 |
} |
|
96 |
} |
|
97 |
||
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
98 |
/** |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
99 |
* Starts the HTTP server with confiuration specified in properties. |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
100 |
* The configuration properties are Interface name/IP, port and SSL configuration |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
101 |
* 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
|
102 |
* 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
|
103 |
* be generated[/TODO]. |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
104 |
* Below properties are used to configure the HTTP server. |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
105 |
* com.sun.management.jmxremote.rest.port |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
106 |
* com.sun.management.jmxremote.rest.host |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
107 |
* com.sun.management.jmxremote.ssl |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
108 |
* com.sun.management.jmxremote.ssl.config.file |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
109 |
* javax.net.ssl.keyStore |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
110 |
* javax.net.ssl.trustStore |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
111 |
* javax.net.ssl.keyStorePassword |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
112 |
* javax.net.ssl.trustStorePassword |
56003 | 113 |
* |
114 |
* @param properties Config properties for the HTTP server. |
|
115 |
* If null or if any properties are not specified, default values will be assumed. |
|
116 |
* @throws IOException If the server could not be created |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
117 |
*/ |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
118 |
public static synchronized void init(Properties properties) throws IOException { |
55994 | 119 |
if (httpServer == null) { |
56003 | 120 |
if (properties == null || properties.isEmpty()) { |
56002 | 121 |
properties = new Properties(); |
122 |
properties.setProperty("com.sun.management.jmxremote.ssl", "false"); |
|
123 |
properties.setProperty("com.sun.management.jmxremote.authenticate", "false"); |
|
124 |
properties.setProperty("com.sun.management.jmxremote.rest.port", "0"); |
|
125 |
} |
|
55985 | 126 |
final int port; |
127 |
try { |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
128 |
port = Integer.parseInt(properties.getProperty(PropertyNames.PORT, DefaultValues.PORT)); |
55985 | 129 |
} catch (NumberFormatException x) { |
130 |
throw new IllegalArgumentException("Invalid string for port"); |
|
131 |
} |
|
132 |
if (port < 0) { |
|
133 |
throw new IllegalArgumentException("Invalid string for port"); |
|
134 |
} |
|
135 |
||
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
136 |
String host = properties.getProperty(PropertyNames.HOST, DefaultValues.HOST); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
137 |
|
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
138 |
boolean useSSL = Boolean.parseBoolean(properties.getProperty( |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
139 |
PropertyNames.USE_SSL, DefaultValues.USE_SSL)); |
55985 | 140 |
if (useSSL) { |
141 |
final String sslConfigFileName |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
142 |
= properties.getProperty(PropertyNames.SSL_CONFIG_FILE_NAME); |
55985 | 143 |
SSLContext ctx = getSSlContext(sslConfigFileName); |
144 |
if (ctx != null) { |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
145 |
HttpsServer server = HttpsServer.create(new InetSocketAddress(host, port), 0); |
55985 | 146 |
server.setHttpsConfigurator(new HttpsConfigurator(ctx)); |
147 |
httpServer = server; |
|
148 |
} else { |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
149 |
httpServer = HttpServer.create(new InetSocketAddress(host, port), 0); |
55985 | 150 |
} |
151 |
} else { |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
152 |
httpServer = HttpServer.create(new InetSocketAddress(host, port), 0); |
55985 | 153 |
} |
154 |
||
55994 | 155 |
new MBeanServerCollectionResource(restAdapters, httpServer); |
56002 | 156 |
httpServer.setExecutor(Executors.newFixedThreadPool(maxThreadCount, new HttpThreadFactory())); |
55985 | 157 |
httpServer.start(); |
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
158 |
startDefaultRestAdapter(properties); |
55985 | 159 |
} |
160 |
} |
|
161 |
||
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
162 |
private static void startDefaultRestAdapter(Properties properties) { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
163 |
env = new HashMap<>(); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
164 |
// Do we use authentication? |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
165 |
final String useAuthenticationStr |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
166 |
= properties.getProperty(PropertyNames.USE_AUTHENTICATION, |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
167 |
DefaultValues.USE_AUTHENTICATION); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
168 |
final boolean useAuthentication |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
169 |
= Boolean.valueOf(useAuthenticationStr); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
170 |
|
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
171 |
String loginConfigName; |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
172 |
String passwordFileName; |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
173 |
|
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
174 |
if (useAuthentication) { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
175 |
env.put("jmx.remote.x.authentication", Boolean.TRUE); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
176 |
// Get non-default login configuration |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
177 |
loginConfigName |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
178 |
= properties.getProperty(PropertyNames.LOGIN_CONFIG_NAME); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
179 |
env.put("jmx.remote.x.login.config", loginConfigName); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
180 |
|
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
181 |
if (loginConfigName == null) { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
182 |
// Get password file |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
183 |
passwordFileName |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
184 |
= properties.getProperty(PropertyNames.PASSWORD_FILE_NAME); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
185 |
env.put("jmx.remote.x.password.file", passwordFileName); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
186 |
} |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
187 |
} |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
188 |
MBeanServerResource adapter = new MBeanServerResource(httpServer, ManagementFactory.getPlatformMBeanServer(), "platform", env); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
189 |
adapter.start(); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
190 |
restAdapters.add(adapter); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
191 |
} |
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 |
* 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
|
195 |
* at supplied context. env parameter configures authentication parameters for the MBeanServer. |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
196 |
* |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
197 |
* @param mbeanServer The MBeanServer to be wrapped in REST adapter |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
198 |
* @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
|
199 |
* If it is null or empty, a context will be generated |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
200 |
* @param env configures authemtication parameters for accessing the MBeanServer over this adapter |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
201 |
* If null, configuration from default rest adapter will be used. |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
202 |
* Below is the list of properties. |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
203 |
* <p> |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
204 |
* jmx.remote.x.authentication : enable/disable user authentication |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
205 |
* jmx.remote.authenticator : Instance of a JMXAuthenticator |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
206 |
* jmx.remote.x.login.config : JAAS login conguration |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
207 |
* jmx.remote.x.password.file : file name for default JAAS login configuration |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
208 |
* @return an Instance of REST adapter that allows to start/stop the adapter |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
209 |
*/ |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
210 |
public static synchronized JmxRestAdapter newRestAdapter(MBeanServer mbeanServer, String context, Map<String, ?> env) { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
211 |
if (httpServer == null) { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
212 |
throw new IllegalStateException("Platform Adapter not initialized"); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
213 |
} |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
214 |
|
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
215 |
MBeanServerResource server = restAdapters.stream() |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
216 |
.filter(s -> areMBeanServersEqual(s.getMBeanServer(), mbeanServer)) |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
217 |
.findFirst() |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
218 |
.get(); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
219 |
if (server == null) { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
220 |
MBeanServerResource adapter = new MBeanServerResource(httpServer, mbeanServer, context, env); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
221 |
adapter.start(); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
222 |
restAdapters.add(adapter); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
223 |
return adapter; |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
224 |
} else { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
225 |
throw new IllegalArgumentException("MBeanServer already registered at " + server.getUrl()); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
226 |
} |
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 |
private static boolean areMBeanServersEqual(MBeanServer server1, MBeanServer server2) { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
230 |
MBeanServerDelegateMBean bean1 = JMX.newMBeanProxy(server1, MBeanServerDelegate.DELEGATE_NAME, MBeanServerDelegateMBean.class); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
231 |
MBeanServerDelegateMBean bean2 = JMX.newMBeanProxy(server2, MBeanServerDelegate.DELEGATE_NAME, MBeanServerDelegateMBean.class); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
232 |
return bean1.getMBeanServerId().equalsIgnoreCase(bean2.getMBeanServerId()); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
233 |
} |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
234 |
|
55994 | 235 |
public synchronized static void stop() { |
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
236 |
restAdapters.forEach(r -> r.stop()); |
56002 | 237 |
restAdapters.clear(); |
55985 | 238 |
if (httpServer != null) { |
56002 | 239 |
ExecutorService executor = (ExecutorService) httpServer.getExecutor(); |
240 |
executor.shutdownNow(); |
|
241 |
try { |
|
242 |
executor.awaitTermination(30, TimeUnit.SECONDS); |
|
243 |
} catch (InterruptedException e) { |
|
244 |
} |
|
55985 | 245 |
httpServer.stop(0); |
246 |
httpServer = null; |
|
247 |
} |
|
248 |
} |
|
249 |
||
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
250 |
/* |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
251 |
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
|
252 |
com.sun.management.jmxremote.mbeanserver.autoadd=true/false |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
253 |
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
|
254 |
*/ |
55994 | 255 |
@Override |
256 |
public void onMBeanServerCreated(MBeanServer mBeanServer) { |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
257 |
try { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
258 |
newRestAdapter(mBeanServer, "", env); |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
259 |
} catch (IllegalArgumentException | IllegalStateException e) { |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
260 |
} |
55994 | 261 |
} |
262 |
||
263 |
@Override |
|
264 |
public void onMBeanServerRemoved(MBeanServer mBeanServer) { |
|
265 |
||
266 |
} |
|
267 |
||
55997 | 268 |
public static synchronized String getDomain() { |
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
269 |
if (httpServer == null) { |
55994 | 270 |
throw new IllegalStateException("Platform rest adapter not initialized"); |
271 |
} |
|
272 |
try { |
|
273 |
if (httpServer instanceof HttpsServer) { |
|
274 |
return "https://" + InetAddress.getLocalHost().getHostName() + ":" + httpServer.getAddress().getPort(); |
|
275 |
} |
|
276 |
return "http://" + InetAddress.getLocalHost().getHostName() + ":" + httpServer.getAddress().getPort(); |
|
277 |
} catch (UnknownHostException ex) { |
|
278 |
return "http://localhost" + ":" + httpServer.getAddress().getPort(); |
|
279 |
} |
|
280 |
} |
|
281 |
||
282 |
public static synchronized String getBaseURL() { |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
283 |
return getDomain() + "/jmx/servers"; |
55994 | 284 |
} |
285 |
||
55985 | 286 |
private static SSLContext getSSlContext(String sslConfigFileName) { |
287 |
final String keyStore, keyStorePassword, trustStore, trustStorePassword; |
|
288 |
||
289 |
try { |
|
290 |
if (sslConfigFileName == null || sslConfigFileName.isEmpty()) { |
|
291 |
keyStore = System.getProperty(PropertyNames.SSL_KEYSTORE_FILE); |
|
292 |
keyStorePassword = System.getProperty(PropertyNames.SSL_KEYSTORE_PASSWORD); |
|
293 |
trustStore = System.getProperty(PropertyNames.SSL_TRUSTSTORE_FILE); |
|
294 |
trustStorePassword = System.getProperty(PropertyNames.SSL_TRUSTSTORE_PASSWORD); |
|
295 |
} else { |
|
296 |
Properties p = new Properties(); |
|
297 |
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(sslConfigFileName)); |
|
298 |
p.load(bin); |
|
299 |
keyStore = p.getProperty(PropertyNames.SSL_KEYSTORE_FILE); |
|
300 |
keyStorePassword = p.getProperty(PropertyNames.SSL_KEYSTORE_PASSWORD); |
|
301 |
trustStore = p.getProperty(PropertyNames.SSL_TRUSTSTORE_FILE); |
|
302 |
trustStorePassword = p.getProperty(PropertyNames.SSL_TRUSTSTORE_PASSWORD); |
|
303 |
} |
|
304 |
||
305 |
char[] keyStorePasswd = null; |
|
306 |
if (keyStorePassword.length() != 0) { |
|
307 |
keyStorePasswd = keyStorePassword.toCharArray(); |
|
308 |
} |
|
309 |
||
310 |
char[] trustStorePasswd = null; |
|
311 |
if (trustStorePassword.length() != 0) { |
|
312 |
trustStorePasswd = trustStorePassword.toCharArray(); |
|
313 |
} |
|
314 |
||
315 |
KeyStore ks = null; |
|
316 |
if (keyStore != null) { |
|
317 |
ks = KeyStore.getInstance(KeyStore.getDefaultType()); |
|
318 |
FileInputStream ksfis = new FileInputStream(keyStore); |
|
319 |
ks.load(ksfis, keyStorePasswd); |
|
320 |
||
321 |
} |
|
322 |
KeyManagerFactory kmf = KeyManagerFactory.getInstance( |
|
323 |
KeyManagerFactory.getDefaultAlgorithm()); |
|
324 |
kmf.init(ks, keyStorePasswd); |
|
325 |
||
326 |
KeyStore ts = null; |
|
327 |
if (trustStore != null) { |
|
328 |
ts = KeyStore.getInstance(KeyStore.getDefaultType()); |
|
329 |
FileInputStream tsfis = new FileInputStream(trustStore); |
|
330 |
ts.load(tsfis, trustStorePasswd); |
|
331 |
} |
|
332 |
TrustManagerFactory tmf = TrustManagerFactory.getInstance( |
|
333 |
TrustManagerFactory.getDefaultAlgorithm()); |
|
334 |
tmf.init(ts); |
|
335 |
||
336 |
SSLContext ctx = SSLContext.getInstance("SSL"); |
|
337 |
ctx.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); |
|
338 |
return ctx; |
|
339 |
} catch (Exception ex) { |
|
340 |
} |
|
341 |
return null; |
|
342 |
} |
|
343 |
||
344 |
/** |
|
345 |
* Default values for JMX configuration properties. |
|
346 |
*/ |
|
55995 | 347 |
static interface DefaultValues { |
55985 | 348 |
|
349 |
public static final String PORT = "0"; |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
350 |
public static final String HOST = "0.0.0.0"; |
55995 | 351 |
public static final String USE_SSL = "false"; |
352 |
public static final String USE_AUTHENTICATION = "false"; |
|
55985 | 353 |
public static final String PASSWORD_FILE_NAME = "jmxremote.password"; |
354 |
} |
|
355 |
||
356 |
/** |
|
357 |
* Names of JMX configuration properties. |
|
358 |
*/ |
|
359 |
public static interface PropertyNames { |
|
360 |
||
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
361 |
public static final String PORT |
55985 | 362 |
= "com.sun.management.jmxremote.rest.port"; |
363 |
public static final String HOST |
|
364 |
= "com.sun.management.jmxremote.host"; |
|
365 |
public static final String USE_SSL |
|
366 |
= "com.sun.management.jmxremote.ssl"; |
|
367 |
public static final String SSL_CONFIG_FILE_NAME |
|
368 |
= "com.sun.management.jmxremote.ssl.config.file"; |
|
369 |
public static final String SSL_KEYSTORE_FILE |
|
370 |
= "javax.net.ssl.keyStore"; |
|
371 |
public static final String SSL_TRUSTSTORE_FILE |
|
372 |
= "javax.net.ssl.trustStore"; |
|
373 |
public static final String SSL_KEYSTORE_PASSWORD |
|
374 |
= "javax.net.ssl.keyStorePassword"; |
|
375 |
public static final String SSL_TRUSTSTORE_PASSWORD |
|
376 |
= "javax.net.ssl.trustStorePassword"; |
|
55998
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
377 |
public static final String USE_AUTHENTICATION |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
378 |
= "com.sun.management.jmxremote.authenticate"; |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
379 |
public static final String PASSWORD_FILE_NAME |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
380 |
= "com.sun.management.jmxremote.password.file"; |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
381 |
public static final String LOGIN_CONFIG_NAME |
54779691e11f
Code cleanup - PlatformMBeanServer, MBeanServerResource
hb
parents:
55997
diff
changeset
|
382 |
= "com.sun.management.jmxremote.login.config"; |
55985 | 383 |
} |
384 |
} |