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

Loading…
Cancel
Save