All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.javonet.sdk.tools.JsonFileResolver Maven / Gradle / Ivy

Go to download

Javonet allows you to reference and use modules or packages written in (Java/Kotlin/Groovy/Clojure, C#/VB.NET, Ruby, Perl, Python, JavaScript/TypeScript) like they were created in your technology. It works on Linux/Windows and MacOS for applications created in JVM, CLR/Netcore, Perl, Python, Ruby, NodeJS, C++ or GoLang and gives you unparalleled freedom and flexibility with native performance in building your mixed-technologies products. Let it be accessing best AI or cryptography libraries, devices SDKs, legacy client modules, internal custom packages or anything from public repositories available on NPM, Nuget, PyPI, Maven/Gradle, RubyGems or GitHub. Get free from programming languages barriers today! For more information check out our guides at https://www.javonet.com/guides/v2/

There is a newer version: 2.4.5
Show newest version
package com.javonet.sdk.tools;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class JsonFileResolver {
    private String path_;
    private JsonObject jsonObject;
    private JsonElement runtimes;

    public JsonFileResolver(String path) {
        path_ = path;

        if (!Files.exists(Paths.get(path_))) {
            throw new IllegalArgumentException("Configuration file " + path_ + " not found. Please check your configuration file.");
        }

        try {
            String jsonText = new String(Files.readAllBytes(Paths.get(path_)));
            jsonObject = JsonParser.parseString(jsonText).getAsJsonObject();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public String getLicenseKey() {
        try {
            return jsonObject.get("licenseKey").getAsString();
        } catch (Exception e) {
            throw new IllegalArgumentException("License key not found in configuration file. Please check your configuration file.");
        }
    }

    private void getRuntimes() {
        runtimes = jsonObject.get("runtimes");
    }

    private JsonElement getRuntime(String runtimeName, String configName) {
        getRuntimes();
        JsonElement runtime = runtimes.getAsJsonObject().get(runtimeName);
        if (runtime.isJsonArray()) {
            for (JsonElement item : runtime.getAsJsonArray()) {
                if (item.getAsJsonObject().get("name").getAsString().equals(configName)) {
                    return item;
                }
            }
        } else {
            if (runtime.getAsJsonObject().get("name").getAsString().equals(configName)) {
                return runtime;
            }
        }

        throw new IllegalArgumentException("Runtime config " + configName + " not found in configuration file for runtime " + runtimeName + ". Please check your configuration file.");
    }

    private String getRuntimeName(String runtimeName, String configName) {
        JsonElement runtime = getRuntime(runtimeName, configName);
        return runtime.getAsJsonObject().get("name").getAsString();
    }

    private JsonElement getChannel(String runtimeName, String configName) {
        JsonElement runtime = getRuntime(runtimeName, configName);
        return runtime.getAsJsonObject().get("channel");
    }

    public String getChannelType(String runtimeName, String configName) {
        JsonElement channel = getChannel(runtimeName, configName);
        return channel.getAsJsonObject().get("type").getAsString();
    }

    public String getChannelHost(String runtimeName, String configName) {
        JsonElement channel = getChannel(runtimeName, configName);
        return channel.getAsJsonObject().get("host").getAsString();
    }

    public int getChannelPort(String runtimeName, String configName) {
        JsonElement channel = getChannel(runtimeName, configName);
        return channel.getAsJsonObject().get("port").getAsInt();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy