Browse Source

Local cache (json backend) is working.

master
Stanislav Usenkov 10 years ago
parent
commit
a3ef80b58e
  1. 88
      src/main/java/ru/simsonic/rscPermissions/Backends/BackendJson.java

88
src/main/java/ru/simsonic/rscPermissions/Backends/BackendJson.java

@ -1,10 +1,13 @@
package ru.simsonic.rscPermissions.Backends;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import ru.simsonic.rscPermissions.DataTypes.DatabaseContents;
import ru.simsonic.rscPermissions.DataTypes.RowEntity;
@ -18,51 +21,68 @@ public class BackendJson
{
this.workingDir = workingDir;
}
private final static String entitiesFile = "entities.json";
private final static String permissionsFile = "permissions.json";
private final static String inheritanceFile = "inheritance.json";
public synchronized DatabaseContents retrieveContents()
{
final Gson gson = new Gson();
final DatabaseContents result = new DatabaseContents();
result.entities = fetchEntities();
result.permissions = fetchPermissions();
result.inheritance = fetchInheritance();
// Entities
try(JsonReader jr = new JsonReader(new InputStreamReader(new FileInputStream(
new File(workingDir, entitiesFile)), Charset.forName("UTF-8"))))
{
result.entities = gson.fromJson(jr, RowEntity[].class);
} catch(IOException ex) {
}
// Permissions
try(JsonReader jr = new JsonReader(new InputStreamReader(new FileInputStream(
new File(workingDir, permissionsFile)), Charset.forName("UTF-8"))))
{
result.permissions = gson.fromJson(jr, RowPermission[].class);
} catch(IOException ex) {
}
// Inheritance
try(JsonReader jr = new JsonReader(new InputStreamReader(new FileInputStream(
new File(workingDir, inheritanceFile)), Charset.forName("UTF-8"))))
{
result.inheritance = gson.fromJson(jr, RowInheritance[].class);
} catch(IOException ex) {
}
return result;
}
private final static String localEntitiesFile = "entities.json";
private final static String localPermissionsFile = "permissions.json";
private final static String localInheritanceFile = "inheritance.json";
private RowEntity[] fetchEntities()
public synchronized void saveContents(DatabaseContents contents)
{
final Gson gson = new Gson();
try
// Entities
if(contents.entities == null)
contents.entities = new RowEntity[] {};
try(JsonWriter jw = new JsonWriter(new OutputStreamWriter(new FileOutputStream(
new File(workingDir, entitiesFile)), Charset.forName("UTF-8"))))
{
JsonReader jr = new JsonReader(new InputStreamReader(new FileInputStream(
new File(workingDir, localEntitiesFile)), Charset.forName("UTF-8")));
return gson.fromJson(jr, RowEntity[].class);
} catch(FileNotFoundException ex) {
jw.setIndent("\t");
gson.toJson(contents.entities, RowEntity[].class, jw);
} catch(IOException ex) {
}
return null;
}
private RowPermission[] fetchPermissions()
{
final Gson gson = new Gson();
try
// Permissions
if(contents.permissions == null)
contents.permissions = new RowPermission[] {};
try(JsonWriter jw = new JsonWriter(new OutputStreamWriter(new FileOutputStream(
new File(workingDir, permissionsFile)), Charset.forName("UTF-8"))))
{
JsonReader jr = new JsonReader(new InputStreamReader(new FileInputStream(
new File(workingDir, localEntitiesFile)), Charset.forName("UTF-8")));
return gson.fromJson(jr, RowPermission[].class);
} catch(FileNotFoundException ex) {
jw.setIndent("\t");
gson.toJson(contents.permissions, RowPermission[].class, jw);
} catch(IOException ex) {
}
return null;
}
private RowInheritance[] fetchInheritance()
{
final Gson gson = new Gson();
try
// Inheritance
if(contents.inheritance == null)
contents.inheritance = new RowInheritance[] {};
try(JsonWriter jw = new JsonWriter(new OutputStreamWriter(new FileOutputStream(
new File(workingDir, inheritanceFile)), Charset.forName("UTF-8"))))
{
JsonReader jr = new JsonReader(new InputStreamReader(new FileInputStream(
new File(workingDir, localEntitiesFile)), Charset.forName("UTF-8")));
return gson.fromJson(jr, RowInheritance[].class);
} catch(FileNotFoundException ex) {
jw.setIndent("\t");
gson.toJson(contents.inheritance, RowInheritance[].class, jw);
} catch(IOException ex) {
}
return null;
}
}

Loading…
Cancel
Save