use of org.json.simple.parser.ParseException in project storm by apache.
the class NormalizedResourceRequest method parseResources.
private static Map<String, Double> parseResources(String input) {
Map<String, Double> topologyResources = new HashMap<>();
JSONParser parser = new JSONParser();
try {
if (input != null) {
Object obj = parser.parse(input);
JSONObject jsonObject = (JSONObject) obj;
// Legacy resource parsing
if (jsonObject.containsKey(Config.TOPOLOGY_COMPONENT_RESOURCES_ONHEAP_MEMORY_MB)) {
Double topoMemOnHeap = ObjectReader.getDouble(jsonObject.get(Config.TOPOLOGY_COMPONENT_RESOURCES_ONHEAP_MEMORY_MB), null);
topologyResources.put(Config.TOPOLOGY_COMPONENT_RESOURCES_ONHEAP_MEMORY_MB, topoMemOnHeap);
}
if (jsonObject.containsKey(Config.TOPOLOGY_COMPONENT_RESOURCES_OFFHEAP_MEMORY_MB)) {
Double topoMemOffHeap = ObjectReader.getDouble(jsonObject.get(Config.TOPOLOGY_COMPONENT_RESOURCES_OFFHEAP_MEMORY_MB), null);
topologyResources.put(Config.TOPOLOGY_COMPONENT_RESOURCES_OFFHEAP_MEMORY_MB, topoMemOffHeap);
}
if (jsonObject.containsKey(Config.TOPOLOGY_COMPONENT_CPU_PCORE_PERCENT)) {
Double topoCpu = ObjectReader.getDouble(jsonObject.get(Config.TOPOLOGY_COMPONENT_CPU_PCORE_PERCENT), null);
topologyResources.put(Config.TOPOLOGY_COMPONENT_CPU_PCORE_PERCENT, topoCpu);
}
// If resource is also present in resources map will overwrite the above
if (jsonObject.containsKey(Config.TOPOLOGY_COMPONENT_RESOURCES_MAP)) {
Map<String, Number> rawResourcesMap = (Map<String, Number>) jsonObject.computeIfAbsent(Config.TOPOLOGY_COMPONENT_RESOURCES_MAP, (k) -> new HashMap<>());
for (Map.Entry<String, Number> stringNumberEntry : rawResourcesMap.entrySet()) {
topologyResources.put(stringNumberEntry.getKey(), stringNumberEntry.getValue().doubleValue());
}
}
}
} catch (ParseException e) {
LOG.error("Failed to parse component resources is:" + e.toString(), e);
return null;
}
return topologyResources;
}
use of org.json.simple.parser.ParseException in project jaggery by wso2.
the class JaggeryDeployerManager method readJaggeryConfig.
private static JSONObject readJaggeryConfig(Context context, Path appBase) {
String content = null;
String path = null;
if (context.getDocBase().contains(WAR_EXTENSION)) {
try {
if (!appBase.endsWith("/")) {
path = appBase + File.separator + context.getDocBase();
} else {
path = appBase + context.getDocBase();
}
ZipFile zip = new ZipFile(path);
for (Enumeration e = zip.entries(); e.hasMoreElements(); ) {
ZipEntry entry = (ZipEntry) e.nextElement();
if (entry.getName().toLowerCase().contains(JAGGERY_CONF)) {
InputStream inputStream = zip.getInputStream(entry);
content = IOUtils.toString(inputStream);
}
}
} catch (IOException e) {
log.error("Error occuered when the accessing the jaggery.conf file of " + context.getPath().substring(1), e);
}
} else {
File file = new File(appBase + context.getPath() + File.separator + JAGGERY_CONF);
try {
content = FileUtils.readFileToString(file);
} catch (IOException e) {
log.error("IOException is thrown when accessing the jaggery.conf file of " + context.getPath().substring(1), e);
}
}
JSONObject jaggeryConfig = null;
try {
JSONParser jp = new JSONParser();
jaggeryConfig = (JSONObject) jp.parse(content);
} catch (ParseException e) {
log.error("Error in parsing the jaggery.conf file", e);
}
return jaggeryConfig;
}
use of org.json.simple.parser.ParseException in project Glowstone by GlowstoneMC.
the class PlayerDataFetcher method getProfile.
/**
* Look up the GlowPlayerProfile for a given UUID.
*
* @param uuid The UUID to look up.
* @return The resulting GlowPlayerProfile, contains a null name on failure.
*/
public static GlowPlayerProfile getProfile(UUID uuid) {
InputStream is;
try {
URL url = new URL(PROFILE_URL + UuidUtils.toFlatString(uuid) + PROFILE_URL_SUFFIX);
URLConnection conn = url.openConnection();
// potentially blocking
is = conn.getInputStream();
} catch (IOException e) {
GlowServer.logger.log(Level.WARNING, "Failed to look up profile");
return new GlowPlayerProfile(null, uuid, true);
}
JSONObject json;
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
if (br.ready()) {
json = (JSONObject) new JSONParser().parse(br);
} else {
return new GlowPlayerProfile(null, uuid, true);
}
} catch (ParseException e) {
GlowServer.logger.log(Level.WARNING, "Failed to parse profile response", e);
return new GlowPlayerProfile(null, uuid, true);
} catch (IOException e) {
GlowServer.logger.log(Level.WARNING, "Failed to look up profile", e);
return new GlowPlayerProfile(null, uuid, true);
}
return GlowPlayerProfile.fromJson(json);
}
use of org.json.simple.parser.ParseException in project jstorm by alibaba.
the class Utils method fromCompressedJsonConf.
public static Map<String, Object> fromCompressedJsonConf(byte[] serialized) {
try {
ByteArrayInputStream bis = new ByteArrayInputStream(serialized);
InputStreamReader in = new InputStreamReader(new GZIPInputStream(bis));
Object ret = JSONValue.parseWithException(in);
in.close();
return (Map<String, Object>) ret;
} catch (IOException | ParseException ioe) {
throw new RuntimeException(ioe);
}
}
use of org.json.simple.parser.ParseException in project winery by eclipse.
the class MetadataJsonVisitor method visitMetadataJson.
/**
* This method provides functionality to extract the information from the metadata.json file of a chef cookbook.
* Normally chef cookbooks must have a metadata.rb file. This method is a backup and called when cookbook doesn't
* contain a metadata.rb file.
*
* @param parseResult A cookbook parse result. Must contain the path to the cookbook.
* @return Returns cookbook parse result with added information from metadata.json file.
*/
public CookbookParseResult visitMetadataJson(CookbookParseResult parseResult) {
JSONParser jsonParser = new JSONParser();
String path = parseResult.getCookbookPath();
String metadataPath = path + "/metadata.json";
try (FileReader reader = new FileReader(metadataPath)) {
// Read JSON file
JSONObject metadata = (JSONObject) jsonParser.parse(reader);
// Read and process name attribute
String name = (String) metadata.get("name");
parseResult = processCookbookName(parseResult, name);
String version = (String) metadata.get("version");
parseResult = processCookbookVersion(parseResult, version);
// Read and process description attribute
String description = (String) metadata.get("description");
parseResult = processCookbookDescription(parseResult, description);
JSONObject platforms = (JSONObject) metadata.get("platforms");
parseResult = processSupportedPlatforms(parseResult, platforms);
// Read and process cookbook dependencies
JSONObject dependencies = (JSONObject) metadata.get("dependencies");
parseResult = processCookbookDependencies(parseResult, dependencies);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
return parseResult;
}
Aggregations