List of usage examples for org.json.simple JSONValue toJSONString
public static String toJSONString(Object value)
From source file:sf.net.experimaestro.manager.json.JsonObject.java
@Override public String toString() { return String.format("{%s}", Output.toString(", ", this.entrySet(), entry -> String.format("%s: %s", JSONValue.toJSONString(entry.getKey()), entry.getValue()))); }
From source file:sf.net.experimaestro.manager.json.JsonObject.java
@Override public void write(Writer out) throws IOException { out.write('{'); boolean first = true; for (Map.Entry<String, Json> entry : this.entrySet()) { if (first) first = false;/*w ww . j a v a 2 s.co m*/ else out.write(", "); out.write(JSONValue.toJSONString(entry.getKey())); out.write(":"); if (entry.getValue() == null) out.write("null"); else entry.getValue().write(out); } out.write('}'); }
From source file:sf.net.experimaestro.manager.json.JsonObject.java
@Override public void writeDescriptorString(Writer out, JsonWriterOptions options) throws IOException { if (canIgnore(options)) { out.write("null"); return;/* www . j a va 2 s .c o m*/ } if (isSimple() && options.simplifyValues) { get(Manager.XP_VALUE.toString()).writeDescriptorString(out, options); return; } Set<String> ignored_keys = new HashSet<>(); ignored_keys.add(Manager.XP_IGNORE.toString()); if (this.containsKey(Manager.XP_IGNORE.toString())) { Json value = this.get(Manager.XP_IGNORE.toString()); if (value instanceof JsonString) { ignored_keys.add(((JsonString) value).string); } else if (value instanceof JsonArray) { for (Json s : (JsonArray) value) { ignored_keys.add(s.toString()); } } else { throw new XPMRuntimeException("Cannot handle $ignore of type %s", value.getClass()); } } out.write('{'); boolean first = true; for (Map.Entry<String, Json> entry : this.entrySet()) { Json value = entry.getValue(); String key = entry.getKey(); // A key is ignored if either: // - its value is null or can be ignored // - it starts with "$" and is not XP_TYPE or XP_VALUE // - it is in the $$ignore key if ((value == null && options.ignoreNull) || value.canIgnore(options) || (options.ignore$ && key.startsWith("$") && !key.equals(XP_TYPE_STRING) && !key.equals(XP_VALUE_STRING)) || ignored_keys.contains(key)) continue; if (first) first = false; else out.write(","); out.write(JSONValue.toJSONString(key)); out.write(":"); value.writeDescriptorString(out, options); } out.write('}'); }
From source file:sk.gymdb.thinis.gcm.server.Sender.java
/** * Sends a message without retrying in case of service unavailability. See * {@link #send(Message, java.util.List, int)} for more info. * * @return multicast results if the message was sent successfully, * {@literal null} if it failed but could be retried. * * @throws IllegalArgumentException if registrationIds is {@literal null} or * empty./*from ww w. ja v a 2s.co m*/ * @throws InvalidRequestException if GCM didn't returned a 200 status. * @throws java.io.IOException if there was a JSON parsing error */ public MulticastResult sendNoRetry(Message message, List<String> registrationIds) throws IOException { if (nonNull(registrationIds).isEmpty()) { throw new IllegalArgumentException("registrationIds cannot be empty"); } Map<Object, Object> jsonRequest = new HashMap<Object, Object>(); setJsonField(jsonRequest, Constants.PARAM_TIME_TO_LIVE, message.getTimeToLive()); setJsonField(jsonRequest, Constants.PARAM_COLLAPSE_KEY, message.getCollapseKey()); setJsonField(jsonRequest, Constants.PARAM_RESTRICTED_PACKAGE_NAME, message.getRestrictedPackageName()); setJsonField(jsonRequest, Constants.PARAM_DELAY_WHILE_IDLE, message.isDelayWhileIdle()); setJsonField(jsonRequest, Constants.PARAM_DRY_RUN, message.isDryRun()); jsonRequest.put(Constants.JSON_REGISTRATION_IDS, registrationIds); Map<String, String> payload = message.getData(); if (!payload.isEmpty()) { jsonRequest.put(Constants.JSON_PAYLOAD, payload); } String requestBody = JSONValue.toJSONString(jsonRequest); logger.finest("JSON request: " + requestBody); HttpURLConnection conn; int status; try { conn = post(Constants.GCM_SEND_ENDPOINT, "application/json", requestBody); status = conn.getResponseCode(); } catch (IOException e) { logger.log(Level.FINE, "IOException posting to GCM", e); return null; } String responseBody; if (status != 200) { try { responseBody = getAndClose(conn.getErrorStream()); logger.finest("JSON error response: " + responseBody); } catch (IOException e) { // ignore the exception since it will thrown an InvalidRequestException // anyways responseBody = "N/A"; logger.log(Level.FINE, "Exception reading response: ", e); } throw new InvalidRequestException(status, responseBody); } try { responseBody = getAndClose(conn.getInputStream()); } catch (IOException e) { logger.log(Level.WARNING, "IOException reading response", e); return null; } logger.finest("JSON response: " + responseBody); JSONParser parser = new JSONParser(); JSONObject jsonResponse; try { jsonResponse = (JSONObject) parser.parse(responseBody); int success = getNumber(jsonResponse, Constants.JSON_SUCCESS).intValue(); int failure = getNumber(jsonResponse, Constants.JSON_FAILURE).intValue(); int canonicalIds = getNumber(jsonResponse, Constants.JSON_CANONICAL_IDS).intValue(); long multicastId = getNumber(jsonResponse, Constants.JSON_MULTICAST_ID).longValue(); MulticastResult.Builder builder = new MulticastResult.Builder(success, failure, canonicalIds, multicastId); @SuppressWarnings("unchecked") List<Map<String, Object>> results = (List<Map<String, Object>>) jsonResponse .get(Constants.JSON_RESULTS); if (results != null) { for (Map<String, Object> jsonResult : results) { String messageId = (String) jsonResult.get(Constants.JSON_MESSAGE_ID); String canonicalRegId = (String) jsonResult.get(Constants.TOKEN_CANONICAL_REG_ID); String error = (String) jsonResult.get(Constants.JSON_ERROR); Result result = new Result.Builder().messageId(messageId) .canonicalRegistrationId(canonicalRegId).errorCode(error).build(); builder.addResult(result); } } MulticastResult multicastResult = builder.build(); return multicastResult; } catch (ParseException e) { throw newIoException(responseBody, e); } }
From source file:storm.mesos.MesosNimbus.java
public Map<String, List<TaskInfo>> getTasksToLaunch(Topologies topologies, Map<String, Collection<WorkerSlot>> slots, Map<String, AggregatedOffers> aggregatedOffersPerNode) { Map<String, List<TaskInfo>> tasksToLaunchPerNode = new HashMap<>(); for (String topologyId : slots.keySet()) { Collection<WorkerSlot> slotList = slots.get(topologyId); TopologyDetails topologyDetails = topologies.getById(topologyId); Set<String> hostsWithSupervisors = new HashSet<>(); double executorCpu = MesosCommon.executorCpu(mesosStormConf); double executorMem = MesosCommon.executorMem(mesosStormConf); double workerCpu = MesosCommon.topologyWorkerCpu(mesosStormConf, topologyDetails); double workerMem = MesosCommon.topologyWorkerMem(mesosStormConf, topologyDetails); for (WorkerSlot slot : slotList) { // for this task we start with the assumption that we only need the worker resources, we'll add the executor resources later if needed. double requiredCpu = workerCpu; double requiredMem = workerMem; String workerHost = slot.getNodeId(); Long workerPort = Long.valueOf(slot.getPort()); AggregatedOffers aggregatedOffers = aggregatedOffersPerNode.get(slot.getNodeId()); String workerPrefix = ""; if (mesosStormConf.containsKey(MesosCommon.WORKER_NAME_PREFIX)) { workerPrefix = MesosCommon.getWorkerPrefix(mesosStormConf, topologyDetails); }// ww w . j a va 2s . c om // Account for executor resources only the first time we see this host for this topology if (!hostsWithSupervisors.contains(workerHost)) { requiredCpu += executorCpu; requiredMem += executorMem; } Map executorData = new HashMap(); executorData.put(MesosCommon.SUPERVISOR_ID, MesosCommon.supervisorId(slot.getNodeId(), topologyDetails.getId())); executorData.put(MesosCommon.ASSIGNMENT_ID, workerPrefix + slot.getNodeId()); String topologyAndNodeId = topologyDetails.getId() + " | " + slot.getNodeId(); String executorName = "storm-supervisor | " + topologyAndNodeId; String taskName = "storm-worker | " + topologyAndNodeId + ":" + slot.getPort(); String executorDataStr = JSONValue.toJSONString(executorData); String extraConfig = ""; if (!aggregatedOffers.isFit(mesosStormConf, topologyDetails, workerPort, hostsWithSupervisors.contains(workerHost))) { LOG.error(String.format( "Unable to launch worker %s for topology %s. Required cpu: %f, Required mem: %f, Required port: %d. Available aggregatedOffers : %s", workerHost, topologyDetails.getId(), requiredCpu, requiredMem, workerPort, aggregatedOffers)); continue; } List<Resource> executorResources = new ArrayList<>(); List<Resource> workerResources = new ArrayList<>(); try { // This list will hold CPU and MEM resources List<ResourceEntry> scalarResourceEntryList = null; // This list will hold PORTS resources List<ResourceEntry> rangeResourceEntryList = null; if (hostsWithSupervisors.contains(workerHost)) { // Since we already have a supervisor on this host, we don't need to account for its resources by obtaining them from // an offer, but we do need to ensure that executor resources are the same for every task, or mesos rejects the task executorResources.add( createMesosScalarResource(ResourceType.CPU, new ScalarResourceEntry(executorCpu))); executorResources.add( createMesosScalarResource(ResourceType.MEM, new ScalarResourceEntry(executorMem))); } else { // Need to account for executor resources, since this might be the first executor on this host for this topology // (we have no way to tell this without some state reconciliation or storage). scalarResourceEntryList = aggregatedOffers.reserveAndGet(ResourceType.CPU, new ScalarResourceEntry(executorCpu)); executorResources .addAll(createMesosScalarResourceList(ResourceType.CPU, scalarResourceEntryList)); scalarResourceEntryList = aggregatedOffers.reserveAndGet(ResourceType.MEM, new ScalarResourceEntry(executorMem)); executorResources .addAll(createMesosScalarResourceList(ResourceType.MEM, scalarResourceEntryList)); } String supervisorStormLocalDir = getStormLocalDirForWorkers(); extraConfig += String.format(" -c storm.local.dir=%s", supervisorStormLocalDir); // First add to the list of required worker resources the CPU resources we were able to secure scalarResourceEntryList = aggregatedOffers.reserveAndGet(ResourceType.CPU, new ScalarResourceEntry(workerCpu)); workerResources .addAll(createMesosScalarResourceList(ResourceType.CPU, scalarResourceEntryList)); // Then add to the list of required worker resources the MEM resources we were able to secure scalarResourceEntryList = aggregatedOffers.reserveAndGet(ResourceType.MEM, new ScalarResourceEntry(workerMem)); workerResources .addAll(createMesosScalarResourceList(ResourceType.MEM, scalarResourceEntryList)); // Finally add to the list of required worker resources the PORTS resources we were able to secure (which we expect to be a single port) rangeResourceEntryList = aggregatedOffers.reserveAndGet(ResourceType.PORTS, new RangeResourceEntry(workerPort, workerPort)); for (ResourceEntry resourceEntry : rangeResourceEntryList) { workerResources.add( createMesosRangeResource(ResourceType.PORTS, (RangeResourceEntry) resourceEntry)); } } catch (ResourceNotAvailableException rexp) { LOG.warn( "Unable to launch worker %s because some resources were unavailable. Required cpu: %f, Required mem: %f, Required port: %d. Available aggregatedOffers : %s", workerHost, requiredCpu, requiredMem, workerPort, aggregatedOffers); continue; } hostsWithSupervisors.add(workerHost); ExecutorInfo.Builder executorInfoBuilder = getExecutorInfoBuilder(topologyDetails, executorDataStr, executorName, executorResources, extraConfig); TaskID taskId = TaskID.newBuilder().setValue(MesosCommon.taskId(slot.getNodeId(), slot.getPort())) .build(); TaskInfo task = TaskInfo.newBuilder().setTaskId(taskId).setName(taskName) .setSlaveId(aggregatedOffers.getSlaveID()).setExecutor(executorInfoBuilder.build()) .addAllResources(workerResources).build(); List<TaskInfo> taskInfoList = tasksToLaunchPerNode.get(slot.getNodeId()); if (taskInfoList == null) { taskInfoList = new ArrayList<>(); tasksToLaunchPerNode.put(slot.getNodeId(), taskInfoList); } taskInfoList.add(task); } } return tasksToLaunchPerNode; }
From source file:storm.mesos.PrettyProtobuf.java
/** * Pretty-print mesos protobuf TaskStatus. *///from ww w. j a v a2 s . c o m public static String taskStatusToString(TaskStatus taskStatus) { Map<String, String> map = new LinkedHashMap<>(); map.put("task_id", taskStatus.getTaskId().getValue()); map.put("slave_id", taskStatus.getSlaveId().getValue()); map.put("state", taskStatus.getState().toString()); if (taskStatus.hasMessage()) { map.put("message", taskStatus.getMessage()); } return JSONValue.toJSONString(map); }
From source file:storm.mesos.PrettyProtobuf.java
/** * Pretty-print mesos protobuf TaskInfo. * * XXX(erikdw): not including command, container (+data), nor health_check. *//*from w ww . j a va2s. co m*/ public static String taskInfoToString(TaskInfo task) { Map<String, String> map = new LinkedHashMap<>(); map.put("task_id", task.getTaskId().getValue()); map.put("slave_id", task.getSlaveId().getValue()); map.putAll(resourcesToOrderedMap(task.getResourcesList())); map.put("executor_id", task.getExecutor().getExecutorId().getValue()); return JSONValue.toJSONString(map); }
From source file:storm.mesos.PrettyProtobuf.java
/** * Pretty-print mesos protobuf Offer./*from w w w.j a v a 2 s. co m*/ * * XXX(erikdw): not including slave_id, attributes, executor_ids, nor framework_id. */ public static String offerToString(Offer offer) { Map<String, String> map = new LinkedHashMap<>(); map.put("offer_id", offer.getId().getValue()); map.put("hostname", offer.getHostname()); map.putAll(resourcesToOrderedMap(offer.getResourcesList())); return JSONValue.toJSONString(map); }
From source file:storm.trident.drpc.ReturnResultsReducer.java
@Override public void complete(ReturnResultsState state, TridentCollector collector) { // only one of the multireducers will receive the tuples if (state.returnInfo != null) { String result = JSONValue.toJSONString(state.results); Map retMap = (Map) JSONValue.parse(state.returnInfo); final String host = (String) retMap.get("host"); final int port = Utils.getInt(retMap.get("port")); String id = (String) retMap.get("id"); DistributedRPCInvocations.Iface client; if (local) { client = (DistributedRPCInvocations.Iface) ServiceRegistry.getService(host); } else {// w ww .j a v a 2 s . c om List server = new ArrayList() { { add(host); add(port); } }; if (!_clients.containsKey(server)) { try { _clients.put(server, new DRPCInvocationsClient(conf, host, port)); } catch (TTransportException ex) { throw new RuntimeException(ex); } } client = _clients.get(server); } try { client.result(id, result); } catch (AuthorizationException aze) { collector.reportError(aze); } catch (TException e) { collector.reportError(e); } } }
From source file:yui.classes.YUILoader.java
/** * Used to fetch a JSON object with the required JavaScript components * @return Returns a JSON String containing urls for each JavaScript component *//*from ww w. j a v a 2 s .co m*/ public String script_json() { Map json = (Map) this.json(MODULE_TYPE.CSS, false, false, false); return JSONValue.toJSONString(json); }