You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by dr...@apache.org on 2015/09/26 01:13:10 UTC

directory-kerby git commit: DIRKRB-423. Ensure json backend file to be re-loaded only when its updated by others

Repository: directory-kerby
Updated Branches:
  refs/heads/master f49e9f79e -> cd135c0a0


DIRKRB-423. Ensure json backend file to be re-loaded only when its updated by others


Project: http://git-wip-us.apache.org/repos/asf/directory-kerby/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-kerby/commit/cd135c0a
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/cd135c0a
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/cd135c0a

Branch: refs/heads/master
Commit: cd135c0a05a6783dd84c5f9e6204dfb9949d622b
Parents: f49e9f7
Author: Kai Zheng <ka...@intel.com>
Authored: Sat Sep 26 07:12:49 2015 +0800
Committer: Kai Zheng <ka...@intel.com>
Committed: Sat Sep 26 07:12:49 2015 +0800

----------------------------------------------------------------------
 .../identitybackend/JsonIdentityBackend.java    | 52 ++++++++++----------
 1 file changed, 27 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/cd135c0a/kerby-backend/json-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/JsonIdentityBackend.java
----------------------------------------------------------------------
diff --git a/kerby-backend/json-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/JsonIdentityBackend.java b/kerby-backend/json-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/JsonIdentityBackend.java
index e4eaf22..37e210d 100644
--- a/kerby-backend/json-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/JsonIdentityBackend.java
+++ b/kerby-backend/json-backend/src/main/java/org/apache/kerby/kerberos/kdc/identitybackend/JsonIdentityBackend.java
@@ -59,7 +59,7 @@ public class JsonIdentityBackend extends AbstractIdentityBackend {
     // Identities loaded from file
     private final Map<String, KrbIdentity> identities =
         new ConcurrentHashMap<>(new TreeMap<String, KrbIdentity>());
-    private long kdbFileTimeStamp;
+    private long kdbFileUpdateTime = -1;
 
     public JsonIdentityBackend() {
 
@@ -110,32 +110,37 @@ public class JsonIdentityBackend extends AbstractIdentityBackend {
             }
         }
 
-        checkAndLoad();
+        checkAndReload();
     }
 
     /**
      * Check kdb file timestamp to see if it's changed or not. If
      * necessary load the kdb again.
      */
-    private synchronized void checkAndLoad() throws KrbException {
+    private synchronized void checkAndReload() throws KrbException {
         long nowTimeStamp = jsonKdbFile.lastModified();
 
-        if (kdbFileTimeStamp == 0 || nowTimeStamp != kdbFileTimeStamp) {
+        if (kdbFileUpdateTime < 0 ||
+                nowTimeStamp != kdbFileUpdateTime) {
             //load identities
-            String existsFileJson = null;
+            String reloadedJsonContent;
             try {
-                existsFileJson = IOUtil.readFile(jsonKdbFile);
+                reloadedJsonContent = IOUtil.readFile(jsonKdbFile);
             } catch (IOException e) {
                 throw new KrbException("Failed to read file", e);
             }
 
-            Map<String, KrbIdentity> loaded = gson.fromJson(existsFileJson,
-                new TypeToken<HashMap<String, KrbIdentity>>() {
-                }.getType());
+            Map<String, KrbIdentity> reloadedEntries =
+                    gson.fromJson(reloadedJsonContent,
+                            new TypeToken<HashMap<String, KrbIdentity>>() {
+                            }.getType());
 
-            if (loaded != null) {
-                identities.putAll(loaded);
+            if (reloadedEntries != null) {
+                identities.clear();
+                identities.putAll(reloadedEntries);
             }
+
+            kdbFileUpdateTime = nowTimeStamp;
         }
     }
 
@@ -144,7 +149,7 @@ public class JsonIdentityBackend extends AbstractIdentityBackend {
      */
     @Override
     protected KrbIdentity doGetIdentity(String principalName) throws KrbException {
-        checkAndLoad();
+        checkAndReload();
         return identities.get(principalName);
     }
 
@@ -153,10 +158,10 @@ public class JsonIdentityBackend extends AbstractIdentityBackend {
      */
     @Override
     protected KrbIdentity doAddIdentity(KrbIdentity identity) throws KrbException {
-        checkAndLoad();
+        checkAndReload();
 
         identities.put(identity.getPrincipalName(), identity);
-        idsToFile(identities);
+        persistToFile();
 
         return doGetIdentity(identity.getPrincipalName());
     }
@@ -166,9 +171,9 @@ public class JsonIdentityBackend extends AbstractIdentityBackend {
      */
     @Override
     protected KrbIdentity doUpdateIdentity(KrbIdentity identity) throws KrbException {
-        checkAndLoad();
+        checkAndReload();
         identities.put(identity.getPrincipalName(), identity);
-        idsToFile(identities);
+        persistToFile();
 
         return doGetIdentity(identity.getPrincipalName());
     }
@@ -178,11 +183,11 @@ public class JsonIdentityBackend extends AbstractIdentityBackend {
      */
     @Override
     protected void doDeleteIdentity(String principalName) throws KrbException {
-        checkAndLoad();
+        checkAndReload();
         if (identities.containsKey(principalName)) {
             identities.remove(principalName);
         }
-        idsToFile(identities);
+        persistToFile();
     }
 
     /**
@@ -209,14 +214,11 @@ public class JsonIdentityBackend extends AbstractIdentityBackend {
         gson = gsonBuilder.create();
     }
 
-    /**
-     * Write identities into a file
-     * @param ids the identities to write into the json file
-     */
-    private synchronized void idsToFile(Map<String, KrbIdentity> ids) throws KrbException {
-        String newFileJson = gson.toJson(ids);
+    private synchronized void persistToFile() throws KrbException {
+        String newJsonContent = gson.toJson(identities);
         try {
-            IOUtil.writeFile(newFileJson, jsonKdbFile);
+            IOUtil.writeFile(newJsonContent, jsonKdbFile);
+            kdbFileUpdateTime = jsonKdbFile.lastModified();
         } catch (IOException e) {
             LOG.error("Error occurred while writing identities to file: " + jsonKdbFile);
             throw new KrbException("Failed to write file", e);