55997
|
1 |
/*
|
|
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.
|
|
24 |
*/
|
|
25 |
|
55994
|
26 |
package com.oracle.jmx.remote.rest.http;
|
|
27 |
|
|
28 |
import com.oracle.jmx.remote.rest.json.JSONArray;
|
|
29 |
import com.oracle.jmx.remote.rest.json.JSONElement;
|
|
30 |
import com.oracle.jmx.remote.rest.json.JSONObject;
|
|
31 |
import com.oracle.jmx.remote.rest.json.JSONPrimitive;
|
55995
|
32 |
import com.oracle.jmx.remote.rest.json.parser.JSONParser;
|
|
33 |
import com.oracle.jmx.remote.rest.json.parser.ParseException;
|
|
34 |
import com.oracle.jmx.remote.rest.mapper.JSONDataException;
|
55994
|
35 |
import com.oracle.jmx.remote.rest.mapper.JSONMapper;
|
|
36 |
import com.oracle.jmx.remote.rest.mapper.JSONMappingException;
|
|
37 |
import com.oracle.jmx.remote.rest.mapper.JSONMappingFactory;
|
|
38 |
import com.sun.net.httpserver.HttpExchange;
|
|
39 |
|
|
40 |
import javax.management.*;
|
55995
|
41 |
import javax.management.openmbean.OpenMBeanAttributeInfo;
|
|
42 |
import javax.management.openmbean.OpenMBeanParameterInfo;
|
|
43 |
import javax.management.openmbean.OpenType;
|
55994
|
44 |
import javax.management.remote.rest.PlatformRestAdapter;
|
|
45 |
import java.io.IOException;
|
55995
|
46 |
import java.net.HttpURLConnection;
|
55994
|
47 |
import java.util.*;
|
55995
|
48 |
import java.util.regex.Matcher;
|
|
49 |
import java.util.regex.Pattern;
|
56000
|
50 |
import java.util.stream.Collectors;
|
|
51 |
import java.util.stream.Stream;
|
55994
|
52 |
|
|
53 |
import static javax.management.MBeanOperationInfo.*;
|
|
54 |
|
|
55 |
public class MBeanResource implements RestResource {
|
|
56 |
|
|
57 |
private final ObjectName objectName;
|
|
58 |
private final MBeanServer mBeanServer;
|
55995
|
59 |
private static final String pathPrefix = "^/?jmx/servers/[^/]+/mbeans/[^/]+";
|
|
60 |
|
|
61 |
private final static Map<String, Class<?>> primitiveToObject = new HashMap<>();
|
|
62 |
|
|
63 |
static {
|
|
64 |
primitiveToObject.put("int", Integer.TYPE);
|
|
65 |
primitiveToObject.put("long", Long.TYPE);
|
|
66 |
primitiveToObject.put("double", Double.TYPE);
|
|
67 |
primitiveToObject.put("float", Float.TYPE);
|
|
68 |
primitiveToObject.put("boolean", Boolean.TYPE);
|
|
69 |
primitiveToObject.put("char", Character.TYPE);
|
|
70 |
primitiveToObject.put("byte", Byte.TYPE);
|
|
71 |
primitiveToObject.put("void", Void.TYPE);
|
|
72 |
primitiveToObject.put("short", Short.TYPE);
|
|
73 |
}
|
55994
|
74 |
|
56000
|
75 |
MBeanResource(MBeanServer mBeanServer, ObjectName objectName) {
|
55994
|
76 |
this.mBeanServer = mBeanServer;
|
|
77 |
this.objectName = objectName;
|
|
78 |
}
|
|
79 |
|
56000
|
80 |
@Override
|
|
81 |
public void handle(HttpExchange exchange) throws IOException {
|
|
82 |
String path = exchange.getRequestURI().getPath();
|
|
83 |
if (path.matches(pathPrefix + "/?$")) {
|
|
84 |
RestResource.super.handle(exchange);
|
|
85 |
} else if (path.matches(pathPrefix + "/info$")
|
|
86 |
&& exchange.getRequestMethod().equalsIgnoreCase("GET")) {
|
|
87 |
RestResource.super.handle(exchange);
|
|
88 |
} else if (path.matches(pathPrefix + "/[^/]+/?$")
|
|
89 |
&& exchange.getRequestMethod().equalsIgnoreCase("POST")) {
|
|
90 |
RestResource.super.handle(exchange);
|
|
91 |
} else {
|
|
92 |
HttpUtil.sendResponse(exchange, HttpResponse.REQUEST_NOT_FOUND);
|
|
93 |
}
|
|
94 |
}
|
|
95 |
|
|
96 |
@Override
|
|
97 |
public HttpResponse doGet(HttpExchange exchange) {
|
|
98 |
String path = PlatformRestAdapter.getDomain() +
|
|
99 |
exchange.getRequestURI().getPath().replaceAll("/$", "");
|
|
100 |
|
|
101 |
if (path.endsWith("info")) {
|
|
102 |
return doMBeanInfo();
|
|
103 |
}
|
|
104 |
|
|
105 |
String infoPath = path + "/info";
|
|
106 |
|
|
107 |
try {
|
|
108 |
Map<String, Object> allAttributes = getAllAttributes();
|
|
109 |
Map<String, String> _links = new LinkedHashMap<>();
|
|
110 |
_links.put("info", HttpUtil.escapeUrl(infoPath));
|
|
111 |
|
|
112 |
MBeanOperationInfo[] opInfo = mBeanServer.getMBeanInfo(objectName).getOperations();
|
|
113 |
JSONArray jarr = new JSONArray();
|
|
114 |
for (MBeanOperationInfo op : opInfo) {
|
|
115 |
JSONObject jobj1 = new JSONObject();
|
|
116 |
JSONArray jarr1 = new JSONArray();
|
|
117 |
jobj1.put("name", op.getName());
|
|
118 |
jobj1.put("href", HttpUtil.escapeUrl(path + "/" + op.getName()));
|
|
119 |
jobj1.put("method", "POST");
|
|
120 |
for (MBeanParameterInfo paramInfo : op.getSignature()) {
|
|
121 |
JSONObject jobj = new JSONObject();
|
|
122 |
jobj.put("name", paramInfo.getName());
|
|
123 |
jobj.put("type", paramInfo.getType());
|
|
124 |
jarr1.add(jobj);
|
|
125 |
}
|
|
126 |
jobj1.put("arguments", jarr1);
|
|
127 |
jobj1.put("returnType", op.getReturnType());
|
|
128 |
jarr.add(jobj1);
|
|
129 |
}
|
|
130 |
|
|
131 |
JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(allAttributes);
|
|
132 |
if (typeMapper != null) {
|
|
133 |
JSONElement jsonElement1 = typeMapper.toJsonValue(allAttributes);
|
|
134 |
JSONElement jsonElement2 = typeMapper.toJsonValue(_links);
|
|
135 |
|
|
136 |
JSONObject jobj = new JSONObject();
|
|
137 |
jobj.put("attributes", jsonElement1);
|
|
138 |
jobj.put("operations", jarr);
|
|
139 |
jobj.put("_links", jsonElement2);
|
|
140 |
return new HttpResponse(jobj.toJsonString());
|
|
141 |
} else {
|
|
142 |
return HttpResponse.SERVER_ERROR;
|
|
143 |
}
|
|
144 |
} catch (RuntimeOperationsException | IntrospectionException | ReflectionException
|
|
145 |
| JSONMappingException | MBeanException e) {
|
|
146 |
return HttpResponse.SERVER_ERROR;
|
|
147 |
} catch (InstanceNotFoundException e) {
|
|
148 |
return new HttpResponse(HttpResponse.BAD_REQUEST, "Specified MBean does not exist");
|
|
149 |
} catch (Exception e) {
|
|
150 |
return HttpResponse.SERVER_ERROR;
|
|
151 |
}
|
|
152 |
}
|
|
153 |
|
|
154 |
/*
|
|
155 |
HTTP POST for this MBean's URL allows setting of attributes and execution of operations.
|
|
156 |
POST request body can follow one of the below formats
|
|
157 |
1. { name : value}
|
|
158 |
Set a single attribute
|
|
159 |
2. { name1 : value1, name2 : value2 }
|
|
160 |
Sets multiple attributes
|
|
161 |
3. {attributes : {read : [name]} , {write : {name : value}}, operations : {op_name : {param_name:name, param_value:value}}}
|
|
162 |
This bulk operation request sets multiple attributes and executes multiple
|
|
163 |
operations on the MBean.
|
|
164 |
*/
|
|
165 |
@Override
|
|
166 |
public HttpResponse doPost(HttpExchange exchange) {
|
|
167 |
String path = exchange.getRequestURI().getPath();
|
|
168 |
String reqBody = null;
|
|
169 |
try {
|
|
170 |
if (path.matches(pathPrefix + "/?$")) { // POST to current URL
|
|
171 |
reqBody = HttpUtil.readRequestBody(exchange);
|
|
172 |
if (reqBody.isEmpty()) { // No Parameters
|
|
173 |
return HttpResponse.BAD_REQUEST;
|
|
174 |
}
|
|
175 |
|
|
176 |
JSONParser parser = new JSONParser(reqBody);
|
|
177 |
JSONElement jsonElement = parser.parse();
|
|
178 |
if (!(jsonElement instanceof JSONObject)) {
|
|
179 |
return new HttpResponse(HttpResponse.BAD_REQUEST,
|
|
180 |
"Invalid parameters : [" + reqBody + "]");
|
|
181 |
}
|
|
182 |
|
|
183 |
JSONObject jsonObject = (JSONObject) jsonElement;
|
|
184 |
|
|
185 |
// Handle bulk operation
|
|
186 |
if (jsonObject.keySet().contains("attributes") | jsonObject.keySet().contains("operations")) {
|
|
187 |
return new HttpResponse(handleBulkRequest(jsonObject).toJsonString());
|
|
188 |
} else { // Handle attribute update
|
|
189 |
Map<String, Object> stringObjectMap = setAttributes(jsonObject);
|
|
190 |
JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(stringObjectMap);
|
|
191 |
if (typeMapper != null) {
|
|
192 |
return new HttpResponse(HttpURLConnection.HTTP_OK, typeMapper.toJsonValue(stringObjectMap).toJsonString());
|
|
193 |
} else {
|
|
194 |
return new HttpResponse(HttpResponse.SERVER_ERROR, "Unable to find JSON Mapper");
|
|
195 |
}
|
|
196 |
}
|
|
197 |
} else if (path.matches(pathPrefix + "/[^/]+/?$")) { // POST to MBeanOperation
|
|
198 |
Matcher matcher = Pattern.compile(pathPrefix + "/").matcher(path);
|
|
199 |
String operation;
|
|
200 |
if (matcher.find()) {
|
|
201 |
operation = path.substring(matcher.end());
|
|
202 |
} else {
|
|
203 |
return HttpResponse.BAD_REQUEST;
|
|
204 |
}
|
|
205 |
|
|
206 |
reqBody = HttpUtil.readRequestBody(exchange);
|
|
207 |
JSONElement result;
|
|
208 |
if (reqBody.isEmpty()) { // No Parameters
|
|
209 |
result = execOperation(operation, null);
|
|
210 |
} else {
|
|
211 |
JSONParser parser = new JSONParser(reqBody);
|
|
212 |
JSONElement jsonElement = parser.parse();
|
|
213 |
if (!(jsonElement instanceof JSONObject)) {
|
|
214 |
return new HttpResponse(HttpResponse.BAD_REQUEST,
|
|
215 |
"Invalid parameters : [" + reqBody + "] for operation - " + operation);
|
|
216 |
}
|
|
217 |
result = execOperation(operation, (JSONObject) jsonElement);
|
|
218 |
}
|
|
219 |
return new HttpResponse(HttpURLConnection.HTTP_OK, result.toJsonString());
|
|
220 |
} else {
|
|
221 |
return HttpResponse.REQUEST_NOT_FOUND;
|
|
222 |
}
|
|
223 |
} catch (InstanceNotFoundException e) {
|
|
224 |
// Should never happen
|
|
225 |
} catch (JSONDataException | ParseException e) {
|
|
226 |
return new HttpResponse(HttpURLConnection.HTTP_BAD_REQUEST, "Invalid JSON : " + reqBody, e.getMessage());
|
|
227 |
} catch (IntrospectionException | JSONMappingException | MBeanException | ReflectionException | IOException e) {
|
|
228 |
return new HttpResponse(HttpResponse.SERVER_ERROR, HttpResponse.getErrorMessage(e));
|
|
229 |
} catch (IllegalArgumentException e) {
|
|
230 |
return new HttpResponse(HttpResponse.BAD_REQUEST, e.getMessage());
|
|
231 |
} catch (Exception e) {
|
|
232 |
return new HttpResponse(HttpResponse.SERVER_ERROR, HttpResponse.getErrorMessage(e));
|
|
233 |
}
|
|
234 |
return HttpResponse.REQUEST_NOT_FOUND;
|
|
235 |
}
|
|
236 |
|
|
237 |
private HttpResponse doMBeanInfo() {
|
|
238 |
try {
|
|
239 |
JSONObject mBeanInfo = getMBeanInfo(mBeanServer, objectName);
|
|
240 |
return new HttpResponse(mBeanInfo.toJsonString());
|
|
241 |
} catch (RuntimeOperationsException | IntrospectionException | ReflectionException e) {
|
|
242 |
return HttpResponse.SERVER_ERROR;
|
|
243 |
} catch (InstanceNotFoundException e) {
|
|
244 |
return new HttpResponse(HttpResponse.BAD_REQUEST, "Specified MBean does not exist");
|
|
245 |
} catch (Exception e) {
|
|
246 |
return HttpResponse.SERVER_ERROR;
|
|
247 |
}
|
|
248 |
}
|
|
249 |
|
|
250 |
JSONElement handleBulkRequest(JSONObject reqObject) {
|
|
251 |
JSONObject result = new JSONObject();
|
|
252 |
|
|
253 |
// Handle attributes
|
|
254 |
JSONElement element = reqObject.get("attributes");
|
|
255 |
if (element != null && element instanceof JSONObject) {
|
|
256 |
JSONObject attrInfo = (JSONObject) element;
|
|
257 |
JSONObject attrNode = new JSONObject();
|
|
258 |
|
|
259 |
// Read attributes
|
|
260 |
JSONElement read = attrInfo.get("get");
|
|
261 |
if (read != null) {
|
|
262 |
if (read instanceof JSONArray) {
|
|
263 |
JSONArray jsonAttrMap = (JSONArray) read;
|
|
264 |
JSONElement resultJson;
|
|
265 |
Map<String, Object> attrRead;
|
|
266 |
try {
|
|
267 |
String[] attributes = getStrings(jsonAttrMap);
|
|
268 |
attrRead = getAttributes(attributes);
|
|
269 |
JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(attrRead);
|
|
270 |
resultJson = typeMapper.toJsonValue(attrRead);
|
|
271 |
} catch (InstanceNotFoundException | ReflectionException | JSONMappingException | MBeanException e) {
|
|
272 |
resultJson = new JSONPrimitive("<ERROR: Unable to retrieve value>");
|
|
273 |
} catch (JSONDataException e) {
|
|
274 |
resultJson = new JSONPrimitive("Invalid JSON : " + e.getMessage());
|
|
275 |
}
|
|
276 |
attrNode.put("get", resultJson);
|
|
277 |
} else {
|
|
278 |
attrInfo.put("get", new JSONPrimitive("Invalid JSON : " + read.toJsonString()));
|
|
279 |
}
|
|
280 |
}
|
|
281 |
|
|
282 |
// Write attributes
|
|
283 |
JSONElement write = attrInfo.get("set");
|
|
284 |
|
|
285 |
if (write != null) {
|
|
286 |
if (write instanceof JSONObject) {
|
|
287 |
JSONElement resultJson;
|
|
288 |
JSONObject jsonAttrMap = (JSONObject) write;
|
|
289 |
try {
|
|
290 |
Map<String, Object> attrMap = setAttributes(jsonAttrMap);
|
|
291 |
JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(attrMap);
|
|
292 |
resultJson = typeMapper.toJsonValue(attrMap);
|
|
293 |
} catch (JSONDataException ex) {
|
|
294 |
resultJson = new JSONPrimitive("Invalid JSON : " + write.toJsonString());
|
|
295 |
} catch (JSONMappingException | IntrospectionException | InstanceNotFoundException | ReflectionException e) {
|
|
296 |
resultJson = new JSONPrimitive("<ERROR: Unable to retrieve value>");
|
|
297 |
}
|
|
298 |
attrNode.put("set", resultJson);
|
|
299 |
} else {
|
|
300 |
attrNode.put("set", new JSONPrimitive("Invalid JSON : " + write.toJsonString()));
|
|
301 |
}
|
|
302 |
}
|
|
303 |
result.put("attributes", attrNode);
|
|
304 |
}
|
|
305 |
|
|
306 |
// Execute operations
|
|
307 |
element = reqObject.get("operations");
|
|
308 |
if (element != null) {
|
|
309 |
JSONArray operationList;
|
|
310 |
if (element instanceof JSONPrimitive // Single no-arg operation
|
|
311 |
|| element instanceof JSONObject) { // single/mulitple operations
|
|
312 |
operationList = new JSONArray();
|
|
313 |
operationList.add(element);
|
|
314 |
} else if (element instanceof JSONArray) { // List of no-arg/with-arg operation
|
|
315 |
operationList = (JSONArray) element;
|
|
316 |
} else {
|
|
317 |
operationList = new JSONArray();
|
|
318 |
}
|
|
319 |
JSONObject opResult = new JSONObject();
|
|
320 |
operationList.forEach((elem) -> {
|
|
321 |
if (elem instanceof JSONPrimitive
|
|
322 |
&& ((JSONPrimitive) elem).getValue() instanceof String) { // no-arg operation
|
|
323 |
String opName = (String) ((JSONPrimitive) elem).getValue();
|
|
324 |
try {
|
|
325 |
JSONElement obj = execOperation(opName, null);
|
|
326 |
opResult.put(opName, obj);
|
|
327 |
} catch (IllegalArgumentException e) {
|
|
328 |
opResult.put(opName, e.getMessage());
|
|
329 |
} catch (IntrospectionException | InstanceNotFoundException
|
|
330 |
| MBeanException | ReflectionException e) {
|
|
331 |
opResult.put(opName, "<ERROR while executing operation>");
|
|
332 |
}
|
|
333 |
} else if (elem instanceof JSONObject) {
|
|
334 |
((JSONObject) elem).keySet().forEach((opName) -> {
|
|
335 |
try {
|
|
336 |
JSONElement jsonElement = ((JSONObject) elem).get(opName);
|
|
337 |
if (jsonElement instanceof JSONObject) {
|
|
338 |
JSONElement obj = execOperation(opName, (JSONObject) jsonElement);
|
|
339 |
opResult.put(opName, obj);
|
|
340 |
} else {
|
|
341 |
opResult.put(opName, new JSONPrimitive("Invalid parameter JSON"));
|
|
342 |
}
|
|
343 |
} catch (IllegalArgumentException e) {
|
|
344 |
opResult.put(opName, e.getMessage());
|
|
345 |
} catch (IntrospectionException | InstanceNotFoundException
|
|
346 |
| MBeanException | ReflectionException e) {
|
|
347 |
opResult.put(opName, "<ERROR while executing operation>");
|
|
348 |
}
|
|
349 |
});
|
|
350 |
}
|
|
351 |
});
|
|
352 |
result.put("operations", opResult);
|
|
353 |
}
|
|
354 |
return result;
|
|
355 |
}
|
|
356 |
|
|
357 |
private JSONObject getMBeanInfo(MBeanServer mbeanServer, ObjectName mbean)
|
|
358 |
throws InstanceNotFoundException, IntrospectionException, ReflectionException {
|
|
359 |
|
55994
|
360 |
JSONObject jobj = new JSONObject();
|
|
361 |
MBeanInfo mBeanInfo = mbeanServer.getMBeanInfo(mbean);
|
|
362 |
if (mBeanInfo == null) {
|
|
363 |
return jobj;
|
|
364 |
}
|
56000
|
365 |
jobj.put("name", mbean.toString());
|
|
366 |
jobj.put("className", mBeanInfo.getClassName());
|
55994
|
367 |
jobj.put("description", mBeanInfo.getDescription());
|
|
368 |
|
|
369 |
// Populate Attribute Info
|
|
370 |
MBeanAttributeInfo[] attributes = mBeanInfo.getAttributes();
|
|
371 |
JSONArray jarr = new JSONArray();
|
|
372 |
for (MBeanAttributeInfo attr : attributes) {
|
|
373 |
String access;
|
|
374 |
if (attr.isReadable()) {
|
|
375 |
if (attr.isWritable()) {
|
|
376 |
access = "read/write";
|
|
377 |
} else {
|
|
378 |
access = "read-only";
|
|
379 |
}
|
|
380 |
} else if (attr.isWritable()) {
|
|
381 |
access = "write-only";
|
|
382 |
} else {
|
|
383 |
access = "no-access";
|
|
384 |
}
|
|
385 |
JSONObject jobj1 = new JSONObject();
|
|
386 |
jobj1.put("name", attr.getName());
|
|
387 |
jobj1.put("type", attr.getType());
|
|
388 |
jobj1.put("access", access);
|
|
389 |
jobj1.put("description", attr.getDescription());
|
|
390 |
jobj1.put("descriptor", getDescriptorJSON(attr.getDescriptor()));
|
|
391 |
jarr.add(jobj1);
|
|
392 |
}
|
|
393 |
jobj.put("attributeInfo", jarr);
|
|
394 |
|
56000
|
395 |
|
55994
|
396 |
// Add constructor Info
|
|
397 |
MBeanConstructorInfo[] constructorInfo = mBeanInfo.getConstructors();
|
|
398 |
jarr = new JSONArray();
|
|
399 |
for (MBeanConstructorInfo constructor : constructorInfo) {
|
|
400 |
JSONObject jobj1 = new JSONObject();
|
|
401 |
jobj1.put("name", constructor.getName());
|
|
402 |
JSONArray jarr1 = new JSONArray();
|
|
403 |
for (MBeanParameterInfo paramInfo : constructor.getSignature()) {
|
|
404 |
jarr1.add(getParamJSON(paramInfo));
|
|
405 |
}
|
|
406 |
jobj1.put("signature", jarr1);
|
|
407 |
jobj1.put("description", constructor.getDescription());
|
|
408 |
if (constructor.getDescriptor().getFieldNames().length > 1) {
|
|
409 |
jobj1.put("descriptor", getDescriptorJSON(constructor.getDescriptor()));
|
|
410 |
}
|
|
411 |
jarr.add(jobj1);
|
|
412 |
}
|
|
413 |
jobj.put("constructorInfo", jarr);
|
|
414 |
|
|
415 |
MBeanOperationInfo[] opInfo = mBeanInfo.getOperations();
|
|
416 |
jarr = new JSONArray();
|
|
417 |
|
|
418 |
for (MBeanOperationInfo op : opInfo) {
|
|
419 |
String impactString;
|
|
420 |
switch (op.getImpact()) {
|
|
421 |
case ACTION:
|
|
422 |
impactString = "action";
|
|
423 |
break;
|
|
424 |
case ACTION_INFO:
|
|
425 |
impactString = "action/info";
|
|
426 |
break;
|
|
427 |
case INFO:
|
|
428 |
impactString = "info";
|
|
429 |
break;
|
|
430 |
case UNKNOWN:
|
|
431 |
impactString = "unknown";
|
|
432 |
break;
|
|
433 |
default:
|
|
434 |
impactString = "(" + op.getImpact() + ")";
|
|
435 |
}
|
|
436 |
|
|
437 |
JSONObject jobj1 = new JSONObject();
|
|
438 |
|
|
439 |
jobj1.put("name", op.getName());
|
|
440 |
JSONArray jarr1 = new JSONArray();
|
|
441 |
for (MBeanParameterInfo paramInfo : op.getSignature()) {
|
|
442 |
jarr1.add(getParamJSON(paramInfo));
|
|
443 |
}
|
|
444 |
jobj1.put("signature", jarr1);
|
|
445 |
jobj1.put("returnType", op.getReturnType());
|
|
446 |
jobj1.put("impact", impactString);
|
|
447 |
jobj1.put("description", op.getDescription());
|
|
448 |
if (op.getDescriptor().getFieldNames().length > 1) {
|
|
449 |
jobj1.put("descriptor", getDescriptorJSON(op.getDescriptor()));
|
|
450 |
}
|
|
451 |
jarr.add(jobj1);
|
|
452 |
}
|
|
453 |
jobj.put("operationInfo", jarr);
|
|
454 |
|
|
455 |
MBeanNotificationInfo[] notifications = mBeanInfo.getNotifications();
|
|
456 |
jarr = new JSONArray();
|
|
457 |
|
|
458 |
for (MBeanNotificationInfo notification : notifications) {
|
|
459 |
JSONObject jobj1 = new JSONObject();
|
|
460 |
jobj1.put("name", notification.getName());
|
|
461 |
JSONArray jarr1 = new JSONArray();
|
|
462 |
for (String notifType : notification.getNotifTypes()) {
|
|
463 |
jarr1.add(new JSONPrimitive(notifType));
|
|
464 |
}
|
|
465 |
jobj1.put("notifTypes", jarr1);
|
|
466 |
jobj1.put("description", notification.getDescription());
|
|
467 |
if (notification.getDescriptor().getFieldNames().length > 1) {
|
|
468 |
jobj1.put("descriptor", getDescriptorJSON(notification.getDescriptor()));
|
|
469 |
}
|
|
470 |
jarr.add(jobj1);
|
|
471 |
}
|
|
472 |
jobj.put("notificationInfo", jarr);
|
|
473 |
|
|
474 |
jobj.put("descriptor", getDescriptorJSON(mBeanInfo.getDescriptor()));
|
|
475 |
return jobj;
|
|
476 |
}
|
|
477 |
|
|
478 |
private JSONObject getParamJSON(MBeanParameterInfo mParamInfo) {
|
|
479 |
JSONObject jobj1 = new JSONObject();
|
56000
|
480 |
jobj1.put("name", mParamInfo.getName());
|
|
481 |
jobj1.put("type", mParamInfo.getType());
|
55994
|
482 |
if (mParamInfo.getDescription() != null && !mParamInfo.getDescription().isEmpty()) {
|
|
483 |
jobj1.put("description", mParamInfo.getDescription());
|
|
484 |
}
|
|
485 |
if (mParamInfo.getDescriptor() != null && mParamInfo.getDescriptor().getFieldNames().length > 1) {
|
|
486 |
jobj1.put("descriptor", getDescriptorJSON(mParamInfo.getDescriptor()));
|
|
487 |
}
|
|
488 |
return jobj1;
|
|
489 |
}
|
|
490 |
|
|
491 |
private JSONObject getDescriptorJSON(Descriptor descriptor) {
|
|
492 |
JSONObject jobj2 = new JSONObject();
|
56000
|
493 |
String[] descNames = descriptor.getFieldNames();
|
|
494 |
for (String descName : descNames) {
|
|
495 |
Object fieldValue = descriptor.getFieldValue(descName);
|
|
496 |
jobj2.put(descName, fieldValue != null ? fieldValue.toString() : null);
|
55994
|
497 |
}
|
|
498 |
return jobj2;
|
|
499 |
}
|
|
500 |
|
56000
|
501 |
private Map<String, Object> getAttributes(String[] attrs) throws
|
|
502 |
InstanceNotFoundException, ReflectionException, MBeanException {
|
|
503 |
|
55995
|
504 |
Map<String, Object> result = new LinkedHashMap<>();
|
56000
|
505 |
|
55996
|
506 |
if (attrs == null || attrs.length == 0) {
|
|
507 |
return result;
|
|
508 |
}
|
56000
|
509 |
|
55996
|
510 |
AttributeList attrVals = mBeanServer.getAttributes(objectName, attrs);
|
56000
|
511 |
List<String> missingAttrs = Arrays.asList(attrs);
|
|
512 |
attrVals.asList().forEach(a -> {
|
|
513 |
missingAttrs.remove(a.getName());
|
|
514 |
result.put(a.getName(), a.getValue());
|
|
515 |
});
|
55995
|
516 |
|
56000
|
517 |
for (String attr : missingAttrs) {
|
|
518 |
try {
|
|
519 |
mBeanServer.getAttribute(objectName, attr);
|
|
520 |
result.put(attr, "< Error: No such attribute >");
|
|
521 |
} catch (RuntimeException ex) {
|
|
522 |
if (ex.getCause() instanceof UnsupportedOperationException) {
|
|
523 |
result.put(attr, "< Attribute not supported >");
|
|
524 |
} else if (ex.getCause() instanceof IllegalArgumentException) {
|
|
525 |
result.put(attr, "< Invalid attributes >");
|
|
526 |
}
|
|
527 |
} catch (AttributeNotFoundException e) {
|
|
528 |
result.put(attr, "< Attribute not found >");
|
55994
|
529 |
}
|
|
530 |
}
|
|
531 |
return result;
|
|
532 |
}
|
|
533 |
|
56000
|
534 |
private Map<String, Object> getAllAttributes() throws InstanceNotFoundException,
|
|
535 |
ReflectionException, MBeanException, IntrospectionException {
|
|
536 |
|
|
537 |
MBeanInfo mInfo = mBeanServer.getMBeanInfo(objectName);
|
|
538 |
String[] attrs = Stream.of(mInfo.getAttributes())
|
|
539 |
.map(MBeanAttributeInfo::getName)
|
|
540 |
.toArray(String[]::new);
|
|
541 |
|
|
542 |
return getAttributes(attrs);
|
|
543 |
}
|
|
544 |
|
55996
|
545 |
private Map<String, Object> setAttributes(JSONObject attrMap) throws JSONDataException,
|
|
546 |
IntrospectionException, InstanceNotFoundException, ReflectionException {
|
56000
|
547 |
|
55995
|
548 |
if (attrMap == null || attrMap.isEmpty()) {
|
|
549 |
throw new JSONDataException("Null arguments for set attribute");
|
|
550 |
}
|
56000
|
551 |
|
|
552 |
MBeanInfo mBeanInfo = mBeanServer.getMBeanInfo(objectName);
|
55995
|
553 |
Map<String, Object> result = new HashMap<>();
|
56000
|
554 |
|
55995
|
555 |
for (String attrName : attrMap.keySet()) {
|
56000
|
556 |
MBeanAttributeInfo attrInfo = Arrays.stream(mBeanInfo.getAttributes()).
|
|
557 |
filter(a -> a.getName().equals(attrName)).findFirst().orElse(null);
|
55995
|
558 |
if (attrInfo == null) {
|
|
559 |
result.put(attrName, "<Attribute not found>");
|
|
560 |
} else if (!attrInfo.isWritable()) {
|
|
561 |
result.put(attrName, "<Attribute is read-only>");
|
|
562 |
} else {
|
|
563 |
JSONMapper mapper;
|
|
564 |
if (attrInfo instanceof OpenMBeanAttributeInfo) {
|
|
565 |
OpenType<?> type = ((OpenMBeanAttributeInfo) attrInfo).getOpenType();
|
|
566 |
mapper = JSONMappingFactory.INSTANCE.getTypeMapper(type);
|
|
567 |
} else {
|
|
568 |
Class<?> inputCls = primitiveToObject.get(attrInfo.getType());
|
|
569 |
try {
|
|
570 |
if (inputCls == null) {
|
|
571 |
inputCls = Class.forName(attrInfo.getType());
|
|
572 |
}
|
|
573 |
} catch (ClassNotFoundException | ClassCastException ex) {
|
56000
|
574 |
throw new IllegalArgumentException("Invalid parameters : "
|
|
575 |
+ attrMap.get(attrName).toJsonString() + " cannot be mapped to : " + attrInfo.getType());
|
55995
|
576 |
}
|
|
577 |
mapper = JSONMappingFactory.INSTANCE.getTypeMapper(inputCls);
|
|
578 |
}
|
|
579 |
try {
|
|
580 |
Object attrValue = mapper.toJavaObject(attrMap.get(attrName));
|
|
581 |
Attribute attrObj = new Attribute(attrName, attrValue);
|
|
582 |
mBeanServer.setAttribute(objectName, attrObj);
|
|
583 |
result.put(attrName, "success");
|
|
584 |
} catch (InvalidAttributeValueException | JSONDataException e) {
|
|
585 |
result.put(attrName, "<Invalid value for the attribute>");
|
|
586 |
} catch (AttributeNotFoundException e) {
|
|
587 |
result.put(attrName, "<Attribute not found>");
|
55996
|
588 |
} catch (ReflectionException | InstanceNotFoundException | MBeanException e) {
|
|
589 |
result.put(attrName, "<ERROR: Unable to retrieve value>");
|
55995
|
590 |
}
|
|
591 |
}
|
|
592 |
}
|
|
593 |
return result;
|
|
594 |
}
|
|
595 |
|
56000
|
596 |
private Object mapJsonElemToType(JSONElement jsonElement, MBeanParameterInfo type) {
|
55995
|
597 |
if (type instanceof OpenMBeanParameterInfo) {
|
|
598 |
OpenType<?> openType = ((OpenMBeanParameterInfo) type).getOpenType();
|
|
599 |
JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(openType);
|
|
600 |
try {
|
|
601 |
return typeMapper.toJavaObject(jsonElement);
|
|
602 |
} catch (JSONDataException ex) {
|
|
603 |
throw new IllegalArgumentException("Invalid JSON String : " + jsonElement.toJsonString() + " for arguments");
|
|
604 |
}
|
|
605 |
} else {
|
|
606 |
Class<?> inputCls = primitiveToObject.get(type.getType());
|
|
607 |
try {
|
|
608 |
if (inputCls == null) {
|
|
609 |
inputCls = Class.forName(type.getType());
|
|
610 |
}
|
|
611 |
} catch (ClassNotFoundException | ClassCastException ex) {
|
|
612 |
throw new IllegalArgumentException("Invalid parameters : " + jsonElement.toJsonString() + " cannot be mapped to : " + type.getType());
|
|
613 |
}
|
|
614 |
JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(inputCls);
|
|
615 |
if (typeMapper == null) {
|
|
616 |
throw new IllegalArgumentException("Invalid parameters : " + jsonElement.toJsonString() + " cannot be mapped to : " + type.getType());
|
|
617 |
}
|
|
618 |
try {
|
|
619 |
return typeMapper.toJavaObject(jsonElement);
|
|
620 |
} catch (JSONDataException ex) {
|
|
621 |
throw new IllegalArgumentException("Invalid JSON String : " + jsonElement.toJsonString() + " for arguments");
|
|
622 |
}
|
|
623 |
}
|
|
624 |
}
|
|
625 |
|
56000
|
626 |
private Map<String, Object> getOperationParameters(Map<String, JSONElement> jsonValues, Map<String, MBeanParameterInfo> typeMap) {
|
55995
|
627 |
if (jsonValues.size() != typeMap.size()) {
|
|
628 |
throw new IllegalArgumentException("Invalid parameters : expected - " + typeMap.size() + " parameters, got - " + jsonValues.size());
|
|
629 |
}
|
55996
|
630 |
if (!jsonValues.keySet().equals(typeMap.keySet())) {
|
55995
|
631 |
throw new IllegalArgumentException("Invalid parameters - expected : " + Arrays.toString(typeMap.keySet().toArray()));
|
|
632 |
}
|
56000
|
633 |
Map<String, Object> parameters = new LinkedHashMap<>(); // Order of parameters should be same as typeMap
|
|
634 |
if (typeMap.isEmpty() && jsonValues.isEmpty()) {
|
55995
|
635 |
return parameters;
|
|
636 |
}
|
56000
|
637 |
typeMap.keySet().forEach((name) -> {
|
55995
|
638 |
MBeanParameterInfo type = typeMap.get(name);
|
|
639 |
JSONElement jsonVal = jsonValues.get(name);
|
56000
|
640 |
Object obj = mapJsonElemToType(jsonVal, type);
|
55995
|
641 |
parameters.put(name, obj);
|
56000
|
642 |
});
|
55995
|
643 |
return parameters;
|
|
644 |
}
|
|
645 |
|
|
646 |
private JSONElement execOperation(String opstr, JSONObject params)
|
|
647 |
throws MBeanException, IntrospectionException, ReflectionException, InstanceNotFoundException {
|
|
648 |
if (params == null) {
|
|
649 |
params = new JSONObject();
|
|
650 |
}
|
|
651 |
MBeanInfo mBeanInfo;
|
|
652 |
try {
|
|
653 |
mBeanInfo = mBeanServer.getMBeanInfo(objectName);
|
|
654 |
} catch (InstanceNotFoundException ex) {
|
|
655 |
throw new IllegalArgumentException("MBean does not exist");
|
|
656 |
}
|
|
657 |
|
56000
|
658 |
List<MBeanOperationInfo> mBeanOperationInfos = Arrays.stream(mBeanInfo.getOperations()).
|
|
659 |
filter(a -> a.getName().equals(opstr)).collect(Collectors.toList());
|
55995
|
660 |
|
56000
|
661 |
if (mBeanOperationInfos.isEmpty()) {
|
55995
|
662 |
throw new IllegalArgumentException("Invalid Operation String");
|
|
663 |
}
|
|
664 |
|
|
665 |
String[] signature = null;
|
|
666 |
Object[] parameters = null;
|
|
667 |
|
56000
|
668 |
IllegalArgumentException exception = null;
|
|
669 |
for (MBeanOperationInfo mBeanOperationInfo : mBeanOperationInfos) {
|
|
670 |
MBeanParameterInfo[] sig = mBeanOperationInfo.getSignature();
|
|
671 |
try {
|
|
672 |
Map<String, MBeanParameterInfo> typeMap = new LinkedHashMap<>(); // Order of parameters is important
|
|
673 |
Arrays.stream(sig).forEach(e -> typeMap.put(e.getName(), e));
|
|
674 |
parameters = getOperationParameters(params, typeMap).values().toArray();
|
|
675 |
signature = Stream.of(sig).map(MBeanParameterInfo::getType).toArray(String[]::new);
|
|
676 |
exception = null;
|
|
677 |
break;
|
|
678 |
} catch (IllegalArgumentException ex) {
|
|
679 |
exception = ex;
|
55995
|
680 |
}
|
|
681 |
}
|
56000
|
682 |
if (exception != null) {
|
|
683 |
throw exception;
|
|
684 |
}
|
55995
|
685 |
|
56000
|
686 |
Object invoke;
|
55995
|
687 |
try {
|
|
688 |
invoke = mBeanServer.invoke(objectName, opstr, parameters, signature);
|
|
689 |
if (invoke != null) {
|
|
690 |
JSONMapper typeMapper = JSONMappingFactory.INSTANCE.getTypeMapper(invoke);
|
|
691 |
if (typeMapper != null) {
|
|
692 |
return typeMapper.toJsonValue(invoke);
|
|
693 |
} else {
|
|
694 |
return new JSONPrimitive("<Unable to map result to JSON>");
|
|
695 |
}
|
|
696 |
} else {
|
|
697 |
return new JSONPrimitive("void");
|
|
698 |
}
|
|
699 |
} catch (JSONMappingException e) {
|
|
700 |
return new JSONPrimitive("<Unable to map result to JSON>");
|
|
701 |
}
|
|
702 |
}
|
|
703 |
|
|
704 |
private String[] getStrings(JSONArray jsonArray) throws JSONDataException {
|
|
705 |
List<String> attributes = new ArrayList<>();
|
|
706 |
for (JSONElement element : jsonArray) {
|
|
707 |
if (element instanceof JSONPrimitive && ((JSONPrimitive) element).getValue() instanceof String) {
|
|
708 |
JSONPrimitive val = (JSONPrimitive) element;
|
|
709 |
attributes.add((String) val.getValue());
|
|
710 |
} else throw new JSONDataException("Expecting String, got " + element.toJsonString());
|
|
711 |
}
|
|
712 |
return attributes.toArray(new String[0]);
|
55994
|
713 |
}
|
|
714 |
}
|