Search in sources :

Example 76 with ParseException

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;
}
Also used : Acker(org.apache.storm.daemon.Acker) Logger(org.slf4j.Logger) JSONParser(org.json.simple.parser.JSONParser) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ComponentCommon(org.apache.storm.generated.ComponentCommon) WorkerResources(org.apache.storm.generated.WorkerResources) ObjectReader(org.apache.storm.utils.ObjectReader) Constants(org.apache.storm.Constants) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) Map(java.util.Map) Config(org.apache.storm.Config) HashMap(java.util.HashMap) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) JSONObject(org.json.simple.JSONObject) ParseException(org.json.simple.parser.ParseException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 77 with ParseException

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;
}
Also used : ZipFile(java.util.zip.ZipFile) JSONObject(org.json.simple.JSONObject) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) JSONParser(org.json.simple.parser.JSONParser) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException) ZipFile(java.util.zip.ZipFile) File(java.io.File)

Example 78 with ParseException

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);
}
Also used : JSONObject(org.json.simple.JSONObject) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) JSONParser(org.json.simple.parser.JSONParser) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) URLConnection(java.net.URLConnection)

Example 79 with ParseException

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);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) ComponentObject(backtype.storm.generated.ComponentObject) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException) Map(java.util.Map) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap)

Example 80 with ParseException

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;
}
Also used : JSONObject(org.json.simple.JSONObject) FileNotFoundException(java.io.FileNotFoundException) JSONParser(org.json.simple.parser.JSONParser) FileReader(java.io.FileReader) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException)

Aggregations

ParseException (org.json.simple.parser.ParseException)259 JSONObject (org.json.simple.JSONObject)193 JSONParser (org.json.simple.parser.JSONParser)186 JSONArray (org.json.simple.JSONArray)84 IOException (java.io.IOException)72 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)50 HashMap (java.util.HashMap)41 ArrayList (java.util.ArrayList)34 Map (java.util.Map)23 HashSet (java.util.HashSet)18 API (org.wso2.carbon.apimgt.api.model.API)18 BufferedReader (java.io.BufferedReader)13 APIProvider (org.wso2.carbon.apimgt.api.APIProvider)13 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)13 List (java.util.List)12 File (java.io.File)11 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)11 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)11 InputStreamReader (java.io.InputStreamReader)10 URL (java.net.URL)10