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

org.bdware.doip.audit.config.FileStorage Maven / Gradle / Ivy

There is a newer version: 1.5.4
Show newest version
package org.bdware.doip.audit.config;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import org.bdware.doip.audit.EndpointConfig;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileStorage implements ConfigStorage {
    File f;

    public FileStorage(String path) {
        f = new File(path);
        if (!f.getParentFile().exists())
            f.getParentFile().mkdirs();

    }

    @Override
    public JsonObject load() {
        try {
            return JsonParser.parseReader(new FileReader(f)).getAsJsonObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public EndpointConfig loadAsEndpointConfig() {
        try {
            JsonObject originalJson = load();
            EndpointConfig endpointConfig = new Gson().fromJson(originalJson, EndpointConfig.class);
            return endpointConfig;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void store(JsonObject jsonObject) {
        try {
            FileWriter fileWriter = null;
            fileWriter = new FileWriter(f, false);
            fileWriter.write(jsonObject.toString());
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy