Search in sources :

Example 1 with ScriptExecutorOperation

use of com.hazelcast.internal.management.operation.ScriptExecutorOperation in project hazelcast by hazelcast.

the class ExecuteScriptRequest method writeResponse.

@Override
public void writeResponse(ManagementCenterService mcs, JsonObject root) throws Exception {
    JsonObject jsonResult = new JsonObject();
    ArrayList results;
    if (targetAllMembers) {
        Set<Member> members = mcs.getHazelcastInstance().getCluster().getMembers();
        ArrayList<Object> list = new ArrayList<Object>(members.size());
        for (Member member : members) {
            list.add(mcs.callOnMember(member, new ScriptExecutorOperation(engine, script, bindings)));
        }
        results = list;
    } else {
        ArrayList<Object> list = new ArrayList<Object>(targets.size());
        for (String address : targets) {
            AddressUtil.AddressHolder addressHolder = AddressUtil.getAddressHolder(address);
            Address targetAddress = new Address(addressHolder.getAddress(), addressHolder.getPort());
            list.add(mcs.callOnAddress(targetAddress, new ScriptExecutorOperation(engine, script, bindings)));
        }
        results = list;
    }
    StringBuilder sb = new StringBuilder();
    for (Object result : results) {
        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 entry = (Map.Entry) o;
                sb.append(entry.getKey()).append("->").append(entry.getValue()).append("\n");
            }
        } else if (result == null) {
            sb.append("error");
        }
        sb.append("\n");
    }
    jsonResult.add("scriptResult", sb.toString());
    root.add("result", jsonResult);
}
Also used : AddressUtil(com.hazelcast.util.AddressUtil) Address(com.hazelcast.nio.Address) ArrayList(java.util.ArrayList) JsonObject(com.eclipsesource.json.JsonObject) JsonUtil.getString(com.hazelcast.util.JsonUtil.getString) ScriptExecutorOperation(com.hazelcast.internal.management.operation.ScriptExecutorOperation) JsonObject(com.eclipsesource.json.JsonObject) ArrayList(java.util.ArrayList) List(java.util.List) Member(com.hazelcast.core.Member) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

JsonObject (com.eclipsesource.json.JsonObject)1 Member (com.hazelcast.core.Member)1 ScriptExecutorOperation (com.hazelcast.internal.management.operation.ScriptExecutorOperation)1 Address (com.hazelcast.nio.Address)1 AddressUtil (com.hazelcast.util.AddressUtil)1 JsonUtil.getString (com.hazelcast.util.JsonUtil.getString)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1