/** * Gets a JSON object from the given string. * * @param jsonString the string that holds a serialised JSON object. * @return the deserialised JSON object * @throws DMPException */ public static ObjectNode getJSON(final String jsonString) throws DMPException { try { return DMPPersistenceUtil.MAPPER.readValue(jsonString, ObjectNode.class); } catch (final IOException e) { throw new DMPException("something went wrong while parsing the JSON string '" + jsonString + "'\n" + e.getMessage()); } }
/** * Gets a JSON array from the given string. * * @param jsonString the string that holds a serialised JSON array. * @return the deserialised JSON array * @throws DMPException */ public static ArrayNode getJSONArray(final String jsonString) throws DMPException { try { return DMPPersistenceUtil.MAPPER.readValue(jsonString, ArrayNode.class); } catch (final IOException e) { throw new DMPException("something went wrong while parsing the JSON string '" + jsonString + "'\n" + e.getMessage()); } }
private static String executeProcess(final Process process, final String command) throws InterruptedException, IOException, DMPException { final int exitStatus = process.waitFor(); if (exitStatus != 0) { throw new DMPException("couldn't execute command '" + command + "' sucessfully - exit status is '" + exitStatus + "'"); } final StringBuilder sb = new StringBuilder(); final BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line = reader.readLine(); while (line != null) { sb.append(line); line = reader.readLine(); } CmdUtil.LOG.debug("got result from command execution '" + command + "' = '" + sb.toString() + "'"); return sb.toString(); }
public static void runCommand(final String command, final String output) throws Exception { String[] osSpecificCommand; if (System.getenv("OS") != null && System.getenv("OS").startsWith("Windows")) { osSpecificCommand = new String[] { "cmd.exe", "/c", command }; try { CmdUtil.runProcess(osSpecificCommand, output); } catch (IOException | InterruptedException e) { CmdUtil.LOG.error(e.getMessage(), e); } } else { final Process process = Runtime.getRuntime().exec(command); final int exitStatus = process.waitFor(); if (exitStatus != 0) { throw new DMPException("couldn't execute command '" + command + "' sucessfully - exit status is '" + exitStatus + "'"); } final BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); final List<String> lines = Lists.newArrayList(); String line = reader.readLine(); while (line != null) { lines.add(line); line = reader.readLine(); } FileUtils.writeLines(new File(output), "UTF-8", lines); } }