org.bdware.doip.audit.config.FileStorage Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of doip-audit-tool Show documentation
Show all versions of doip-audit-tool Show documentation
doip audit tool developed by bdware
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();
}
}
}