directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject git commit: Used reentrantReadWriteLock instead of ReentrantLocks, to allow concurrent access to the set when we just do a set.contains().
Date Sat, 01 Nov 2014 06:40:34 GMT
Repository: directory-fortress-core
Updated Branches:
  refs/heads/master b6258df4c -> f6faf5485


Used reentrantReadWriteLock instead of ReentrantLocks, to allow
concurrent access to the set when we just do a set.contains().

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

Branch: refs/heads/master
Commit: f6faf5485f063711834d624adf3c5eaecc4ff8a0
Parents: b6258df
Author: Emmanuel L├ęcharny <elecharny@symas.com>
Authored: Sat Nov 1 07:40:17 2014 +0100
Committer: Emmanuel L├ęcharny <elecharny@symas.com>
Committed: Sat Nov 1 07:40:17 2014 +0100

----------------------------------------------------------------------
 .../directory/fortress/core/rbac/OrgUnitP.java  | 114 ++++++++++---------
 1 file changed, 58 insertions(+), 56 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-fortress-core/blob/f6faf548/src/main/java/org/apache/directory/fortress/core/rbac/OrgUnitP.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/directory/fortress/core/rbac/OrgUnitP.java b/src/main/java/org/apache/directory/fortress/core/rbac/OrgUnitP.java
index 3d2aa36..4c7a85d 100755
--- a/src/main/java/org/apache/directory/fortress/core/rbac/OrgUnitP.java
+++ b/src/main/java/org/apache/directory/fortress/core/rbac/OrgUnitP.java
@@ -22,8 +22,8 @@ package org.apache.directory.fortress.core.rbac;
 
 import java.util.List;
 import java.util.Set;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -63,8 +63,8 @@ public final class OrgUnitP
     private static final Logger LOG = LoggerFactory.getLogger( CLS_NM );
 
     // these fields are used to synchronize access to the above static pools:
-    private static final Lock userPoolLock = new ReentrantLock();
-    private static final Lock permPoolLock = new ReentrantLock();
+    private static final ReadWriteLock userPoolLock = new ReentrantReadWriteLock();
+    private static final ReadWriteLock permPoolLock = new ReentrantReadWriteLock();
     private static Cache ouCache;
 
     // DAO class for OU data sets must be initializer before the other statics:
@@ -93,7 +93,7 @@ public final class OrgUnitP
      * @param entity
      * @return
      */
-    final boolean isValid( OrgUnit entity )
+    /* No Qualifier */final boolean isValid( OrgUnit entity )
     {
         boolean result = false;
         
@@ -101,7 +101,7 @@ public final class OrgUnitP
         {
             try
             {
-                userPoolLock.lock();
+                userPoolLock.readLock().lock();
                 Set<String> userPool = getUserSet( entity );
 
                 if ( userPool != null )
@@ -111,14 +111,14 @@ public final class OrgUnitP
             }
             finally
             {
-                userPoolLock.unlock();
+                userPoolLock.readLock().unlock();
             }
         }
         else
         {
             try
             {
-                permPoolLock.lock();
+                permPoolLock.readLock().lock();
                 Set<String> permPool = getPermSet( entity );
 
                 if ( permPool != null )
@@ -128,7 +128,7 @@ public final class OrgUnitP
             }
             finally
             {
-                permPoolLock.unlock();
+                permPoolLock.readLock().unlock();
             }
         }
         
@@ -137,16 +137,17 @@ public final class OrgUnitP
 
 
     /**
-     * @param orgUnit
-     * @return
+     * Loads the User set for an orgUnit
+     * @param orgUnit The orgUnit
+     * @return The set of associated User
      */
-    private static Set<String> loadOrgSet( OrgUnit orgUnit )
+    private static Set<String> loadUserSet( OrgUnit orgUnit )
     {
-        Set<String> ouSet = null;
+        Set<String> ouUserSet = null;
         
         try
         {
-            ouSet = oDao.getOrgs( orgUnit );
+            ouUserSet = oDao.getOrgs( orgUnit );
         }
         catch ( SecurityException se )
         {
@@ -154,17 +155,34 @@ public final class OrgUnitP
             LOG.info( warning, se );
         }
         
-        if ( orgUnit.getType() == OrgUnit.Type.USER )
+        ouCache.put( getKey( USER_OUS, orgUnit.getContextId() ), ouUserSet );
+
+        return ouUserSet;
+    }
+
+
+    /**
+     * Loads the Perm set for an orgUnit
+     * @param orgUnit The orgUnit
+     * @return The set of associated Perms
+     */
+    private static Set<String> loadPermSet( OrgUnit orgUnit )
+    {
+        Set<String> ouPermSet = null;
+        
+        try
         {
-            // TODO:  add context id to this cache
-            ouCache.put( getKey( USER_OUS, orgUnit.getContextId() ), ouSet );
+            ouPermSet = oDao.getOrgs( orgUnit );
         }
-        else
+        catch ( SecurityException se )
         {
-            ouCache.put( getKey( PERM_OUS, orgUnit.getContextId() ), ouSet );
+            String warning = "loadOrgSet static initializer caught SecurityException=" +
se;
+            LOG.info( warning, se );
         }
+        
+        ouCache.put( getKey( PERM_OUS, orgUnit.getContextId() ), ouPermSet );
 
-        return ouSet;
+        return ouPermSet;
     }
 
 
@@ -175,23 +193,15 @@ public final class OrgUnitP
      */
     private static Set<String> getPermSet( OrgUnit orgUnit )
     {
-        try
-        {
-            permPoolLock.lock();
-            @SuppressWarnings("unchecked")
-            Set<String> permSet = ( Set<String> ) ouCache.get( getKey( PERM_OUS,
orgUnit.getContextId() ) );
+        @SuppressWarnings("unchecked")
+        Set<String> permSet = ( Set<String> ) ouCache.get( getKey( PERM_OUS,
orgUnit.getContextId() ) );
 
-            if ( permSet == null )
-            {
-                permSet = loadOrgSet( orgUnit );
-            }
-            
-            return permSet;
-        }
-        finally
+        if ( permSet == null )
         {
-            permPoolLock.unlock();
+            permSet = loadPermSet( orgUnit );
         }
+        
+        return permSet;
     }
 
 
@@ -202,23 +212,15 @@ public final class OrgUnitP
      */
     private static Set<String> getUserSet( OrgUnit orgUnit )
     {
-        try
-        {
-            userPoolLock.lock();
-            @SuppressWarnings("unchecked")
-            Set<String> userSet = ( Set<String> ) ouCache.get( getKey( USER_OUS,
orgUnit.getContextId() ) );
+        @SuppressWarnings("unchecked")
+        Set<String> userSet = ( Set<String> ) ouCache.get( getKey( USER_OUS,
orgUnit.getContextId() ) );
 
-            if ( userSet == null )
-            {
-                userSet = loadOrgSet( orgUnit );
-            }
-            
-            return userSet;
-        }
-        finally
+        if ( userSet == null )
         {
-            userPoolLock.unlock();
+            userSet = loadUserSet( orgUnit );
         }
+        
+        return userSet;
     }
 
 
@@ -271,7 +273,7 @@ public final class OrgUnitP
         {
             try
             {
-                userPoolLock.lock();
+                userPoolLock.writeLock().lock();
 
                 Set<String> userPool = getUserSet( entity );
                 
@@ -282,14 +284,14 @@ public final class OrgUnitP
             }
             finally
             {
-                userPoolLock.unlock();
+                userPoolLock.writeLock().unlock();
             }
         }
         else
         {
             try
             {
-                permPoolLock.lock();
+                permPoolLock.writeLock().lock();
 
                 Set<String> permPool = getPermSet( entity );
                 
@@ -300,7 +302,7 @@ public final class OrgUnitP
             }
             finally
             {
-                permPoolLock.unlock();
+                permPoolLock.writeLock().unlock();
             }
         }
         
@@ -358,7 +360,7 @@ public final class OrgUnitP
         {
             try
             {
-                userPoolLock.lock();
+                userPoolLock.writeLock().lock();
                 Set<String> userPool = getUserSet( entity );
 
                 if ( userPool != null )
@@ -368,14 +370,14 @@ public final class OrgUnitP
             }
             finally
             {
-                userPoolLock.unlock();
+                userPoolLock.writeLock().unlock();
             }
         }
         else
         {
             try
             {
-                permPoolLock.lock();
+                permPoolLock.writeLock().lock();
                 Set<String> permPool = getPermSet( entity );
 
                 if ( permPool != null )
@@ -385,7 +387,7 @@ public final class OrgUnitP
             }
             finally
             {
-                permPoolLock.unlock();
+                permPoolLock.writeLock().unlock();
             }
         }
         


Mime
View raw message