/
ExecuteScriptRequest.java
144 lines (121 loc) · 5.28 KB
/
ExecuteScriptRequest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.internal.management.request;
import com.eclipsesource.json.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import com.hazelcast.internal.management.ManagementCenterService;
import com.hazelcast.internal.management.operation.ScriptExecutorOperation;
import com.hazelcast.nio.Address;
import com.hazelcast.util.AddressUtil;
import com.hazelcast.util.ExceptionUtil;
import com.hazelcast.util.MapUtil;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import static com.hazelcast.util.JsonUtil.getArray;
import static com.hazelcast.util.JsonUtil.getString;
/**
* Request for executing scripts on the nodes from Management Center.
*/
public class ExecuteScriptRequest implements ConsoleRequest {
private String script;
private String engine;
private Set<String> targets;
public ExecuteScriptRequest() {
}
public ExecuteScriptRequest(String script, String engine, Set<String> targets) {
this.script = script;
this.engine = engine;
this.targets = targets;
}
@Override
public int getType() {
return ConsoleRequestConstants.REQUEST_TYPE_EXECUTE_SCRIPT;
}
@Override
public void writeResponse(ManagementCenterService mcs, JsonObject root) throws Exception {
Map<Address, Future<Object>> futures = MapUtil.createHashMap(targets.size());
for (String address : targets) {
AddressUtil.AddressHolder addressHolder = AddressUtil.getAddressHolder(address);
Address targetAddress = new Address(addressHolder.getAddress(), addressHolder.getPort());
futures.put(targetAddress, mcs.callOnAddress(targetAddress, new ScriptExecutorOperation(engine, script)));
}
JsonObject responseJson = new JsonObject();
StringBuilder scriptResult = new StringBuilder();
for (Map.Entry<Address, Future<Object>> entry : futures.entrySet()) {
Address address = entry.getKey();
Future<Object> future = entry.getValue();
try {
Object result = future.get();
StringBuilder sb = new StringBuilder();
if (result instanceof String) {
sb.append(result);
} else if (result instanceof List) {
List list = (List) result;
for (Object o : list) {
sb.append(o).append("\n");
}
} else if (result instanceof Map) {
Map map = (Map) result;
for (Object o : map.entrySet()) {
Map.Entry e = (Map.Entry) o;
sb.append(e.getKey()).append("->").append(e.getValue()).append("\n");
}
} else if (result == null) {
sb.append("error");
}
sb.append("\n");
addSuccessResponse(responseJson, scriptResult, address, sb.toString());
} catch (ExecutionException e) {
addErrorResponse(responseJson, scriptResult, address, e);
} catch (InterruptedException e) {
addErrorResponse(responseJson, scriptResult, address, e);
Thread.currentThread().interrupt();
}
}
responseJson.add("scriptResult", scriptResult.toString());
root.add("result", responseJson);
}
@Override
public void fromJson(JsonObject json) {
script = getString(json, "script", "");
engine = getString(json, "engine", "");
targets = new HashSet<String>();
for (JsonValue target : getArray(json, "targets", new JsonArray())) {
targets.add(target.asString());
}
}
private static void addSuccessResponse(JsonObject root, StringBuilder scriptResult,
Address address, String result) {
addResponse(root, scriptResult, address, true, result);
}
private static void addErrorResponse(JsonObject root, StringBuilder scriptResult,
Address address, Exception e) {
addResponse(root, scriptResult, address, false, ExceptionUtil.toString(e));
}
private static void addResponse(JsonObject root, StringBuilder scriptResult,
Address address, boolean success, String result) {
JsonObject json = new JsonObject();
json.add("success", success);
json.add("result", result);
root.add(address.toString(), json);
scriptResult.append(result);
}
}