From a381819e25430ccca6f3d5645d7422d1dda97c1c Mon Sep 17 00:00:00 2001
From: Nicolas Capponi <nicolas.capponi@forgerock.com>
Date: Fri, 17 Jun 2016 09:54:07 +0000
Subject: [PATCH] OPENDJ-2655 Optimize Group implementations by lazily creating examined groups

---
 opendj-server-legacy/src/main/java/org/opends/server/extensions/StaticGroup.java |   39 +++++++++++++++++++++++++++------------
 1 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/opendj-server-legacy/src/main/java/org/opends/server/extensions/StaticGroup.java b/opendj-server-legacy/src/main/java/org/opends/server/extensions/StaticGroup.java
index fbd0a53..e0d14ed 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/extensions/StaticGroup.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/extensions/StaticGroup.java
@@ -26,6 +26,7 @@
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Set;
+import java.util.concurrent.atomic.AtomicReference;
 import java.util.concurrent.locks.ReadWriteLock;
 import java.util.concurrent.locks.ReentrantReadWriteLock;
 
@@ -387,7 +388,7 @@
   }
 
   @Override
-  public boolean isMember(DN userDN, Set<DN> examinedGroups) throws DirectoryException
+  public boolean isMember(DN userDN, AtomicReference<Set<DN>> examinedGroups) throws DirectoryException
   {
     reloadIfNeeded();
     CompactDn compactUserDN = new CompactDn(userDN);
@@ -398,19 +399,22 @@
       {
         return true;
       }
-      else if (!examinedGroups.add(getGroupDN()))
+      if (nestedGroups.isEmpty()) {
+        return false;
+      }
+
+      // there are nested groups
+      Set<DN> groups = getExaminedGroups(examinedGroups);
+      if (!groups.add(getGroupDN()))
       {
         return false;
       }
-      else
+      for (DN nestedGroupDN : nestedGroups)
       {
-        for (DN nestedGroupDN : nestedGroups)
+        Group<? extends GroupImplementationCfg> group = getGroupManager().getGroupInstance(nestedGroupDN);
+        if (group != null && group.isMember(userDN, examinedGroups))
         {
-          Group<? extends GroupImplementationCfg> group = getGroupManager().getGroupInstance(nestedGroupDN);
-          if (group != null && group.isMember(userDN, examinedGroups))
-          {
-            return true;
-          }
+          return true;
         }
       }
     }
@@ -421,8 +425,19 @@
     return false;
   }
 
+  private Set<DN> getExaminedGroups(AtomicReference<Set<DN>> examinedGroups)
+  {
+    Set<DN> groups = examinedGroups.get();
+    if (groups == null)
+    {
+      groups = new HashSet<DN>();
+      examinedGroups.set(groups);
+    }
+    return groups;
+  }
+
   @Override
-  public boolean isMember(Entry userEntry, Set<DN> examinedGroups)
+  public boolean isMember(Entry userEntry, AtomicReference<Set<DN>> examinedGroups)
          throws DirectoryException
   {
     return isMember(userEntry.getName(), examinedGroups);
@@ -445,8 +460,8 @@
         // Check if the group itself has been removed
         if (thisGroup == null)
         {
-          throw new DirectoryException(ResultCode.NO_SUCH_ATTRIBUTE, ERR_STATICGROUP_GROUP_INSTANCE_INVALID
-              .get(groupEntryDN));
+          throw new DirectoryException(ResultCode.NO_SUCH_ATTRIBUTE,
+              ERR_STATICGROUP_GROUP_INSTANCE_INVALID.get(groupEntryDN));
         }
         else if (thisGroup != this)
         {

--
Gitblit v1.10.0