directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dran...@apache.org
Subject directory-kerby git commit: DIRKRB-423. Ensure json backend file to be re-loaded only when its updated by others
Date Fri, 25 Sep 2015 23:13:10 GMT
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 <kai.zheng@intel.com>
Authored: Sat Sep 26 07:12:49 2015 +0800
Committer: Kai Zheng <kai.zheng@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);


Mime
View raw message