[exo-jcr-commits] exo-jcr SVN: r4753 - core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap.

do-not-reply at jboss.org do-not-reply at jboss.org
Mon Aug 15 03:11:31 EDT 2011


Author: areshetnyak
Date: 2011-08-15 03:11:30 -0400 (Mon, 15 Aug 2011)
New Revision: 4753

Added:
   core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/CacheHandler.java
Modified:
   core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/ADGroupDAOImpl.java
   core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/ADMembershipDAOImpl.java
   core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/ADUserDAOImpl.java
   core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/BaseDAO.java
   core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/GroupDAOImpl.java
   core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/LDAPUserPageList.java
   core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/LdapUserListAccess.java
   core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/MembershipDAOImpl.java
   core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/MembershipTypeDAOImpl.java
   core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/OrganizationServiceImpl.java
   core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/UserDAOImpl.java
   core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/UserProfileDAOImpl.java
Log:
EXOJCR-1480: Improve performance of ldap organization service

Modified: core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/ADGroupDAOImpl.java
===================================================================
--- core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/ADGroupDAOImpl.java	2011-08-12 13:45:19 UTC (rev 4752)
+++ core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/ADGroupDAOImpl.java	2011-08-15 07:11:30 UTC (rev 4753)
@@ -48,12 +48,14 @@
     *          items
     * @param ldapService {@link LDAPService}
     * @param ad See {@link ADSearchBySID}
+    * @param cacheHandler
+    *          The Cache Handler
     * @throws Exception if any errors occurs
     */
-   public ADGroupDAOImpl(LDAPAttributeMapping ldapAttrMapping, LDAPService ldapService, ADSearchBySID ad)
-      throws Exception
+   public ADGroupDAOImpl(LDAPAttributeMapping ldapAttrMapping, LDAPService ldapService, ADSearchBySID ad,
+      CacheHandler cacheHandler) throws Exception
    {
-      super(ldapAttrMapping, ldapService);
+      super(ldapAttrMapping, ldapService, cacheHandler);
       adSearch = ad;
    }
 
@@ -126,10 +128,7 @@
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
             finally
             {

Modified: core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/ADMembershipDAOImpl.java
===================================================================
--- core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/ADMembershipDAOImpl.java	2011-08-12 13:45:19 UTC (rev 4752)
+++ core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/ADMembershipDAOImpl.java	2011-08-15 07:11:30 UTC (rev 4753)
@@ -23,6 +23,7 @@
 import org.exoplatform.services.organization.Membership;
 import org.exoplatform.services.organization.OrganizationService;
 import org.exoplatform.services.organization.impl.MembershipImpl;
+import org.exoplatform.services.organization.ldap.CacheHandler.CacheType;
 
 import java.util.ArrayList;
 import java.util.Collection;
@@ -49,12 +50,14 @@
     *          items
     * @param ldapService {@link LDAPService}
     * @param ad See {@link ADSearchBySID}
+    * @param cacheHandler
+    *          The Cache Handler 
     * @throws Exception if any errors occurs
     */
    public ADMembershipDAOImpl(LDAPAttributeMapping ldapAttrMapping, LDAPService ldapService, ADSearchBySID ad,
-      OrganizationService service) throws Exception
+      OrganizationService service, CacheHandler cacheHandler) throws Exception
    {
-      super(ldapAttrMapping, ldapService, service);
+      super(ldapAttrMapping, ldapService, service, cacheHandler);
       adSearch = ad;
    }
 
@@ -65,6 +68,13 @@
    @Override
    public Membership findMembershipByUserGroupAndType(String userName, String groupId, String type) throws Exception
    {
+      MembershipImpl membership =
+         (MembershipImpl)cacheHandler.get(cacheHandler.getMembershipKey(userName, groupId, type), CacheType.MEMBERSHIP);
+      if (membership != null)
+      {
+         return membership;
+      }
+
       LdapContext ctx = ldapService.getLdapContext(true);
       String groupDN = getGroupDNFromGroupId(groupId);
       try
@@ -75,15 +85,16 @@
             {
                Collection memberships = findMemberships(ctx, userName, groupDN, type);
                if (memberships.size() > 0)
-                  return (MembershipImpl)memberships.iterator().next();
+               {
+                  membership = (MembershipImpl)memberships.iterator().next();
+                  cacheHandler.put(cacheHandler.getMembershipKey(membership), membership, CacheType.MEMBERSHIP);
+                  return membership;
+               }
                return null;
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -111,10 +122,7 @@
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -143,10 +151,7 @@
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }

Modified: core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/ADUserDAOImpl.java
===================================================================
--- core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/ADUserDAOImpl.java	2011-08-12 13:45:19 UTC (rev 4752)
+++ core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/ADUserDAOImpl.java	2011-08-15 07:11:30 UTC (rev 4753)
@@ -20,6 +20,7 @@
 
 import org.exoplatform.services.ldap.LDAPService;
 import org.exoplatform.services.organization.User;
+import org.exoplatform.services.organization.ldap.CacheHandler.CacheType;
 
 import javax.naming.Context;
 import javax.naming.NamingException;
@@ -61,11 +62,14 @@
    /**
     * @param ldapAttrMapping {@link LDAPAttributeMapping}
     * @param ldapService {@link LDAPService}
+    * @param cservice 
+    *          The Cache Handler
     * @throws Exception if any errors occurs
     */
-   public ADUserDAOImpl(LDAPAttributeMapping ldapAttrMapping, LDAPService ldapService) throws Exception
+   public ADUserDAOImpl(LDAPAttributeMapping ldapAttrMapping, LDAPService ldapService, CacheHandler cacheHandler)
+      throws Exception
    {
-      super(ldapAttrMapping, ldapService);
+      super(ldapAttrMapping, ldapService, cacheHandler);
       LDAPUserPageList.SEARCH_CONTROL = Control.CRITICAL;
    }
 
@@ -93,14 +97,13 @@
                ctx.createSubcontext(userDN, attrs);
                if (broadcast)
                   postSave(user, true);
+
+               cacheHandler.put(user.getUserName(), user, CacheType.USER);
                break;
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -118,7 +121,7 @@
     * {@inheritDoc}
     */
    @Override
-   void saveUserPassword(User user, String userDN) throws Exception
+   protected void saveUserPassword(User user, String userDN) throws Exception
    {
       Object v = ldapService.getLdapContext().getEnvironment().get(Context.SECURITY_PROTOCOL);
       if (v == null)
@@ -147,10 +150,7 @@
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }

Modified: core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/BaseDAO.java
===================================================================
--- core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/BaseDAO.java	2011-08-12 13:45:19 UTC (rev 4752)
+++ core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/BaseDAO.java	2011-08-15 07:11:30 UTC (rev 4753)
@@ -24,6 +24,7 @@
 import org.exoplatform.services.organization.Group;
 import org.exoplatform.services.organization.User;
 import org.exoplatform.services.organization.impl.GroupImpl;
+import org.exoplatform.services.organization.ldap.CacheHandler.CacheType;
 
 import java.util.ArrayList;
 import java.util.Enumeration;
@@ -82,14 +83,21 @@
    private static int maxConnectionError = -1;
 
    /**
+    *     * The Cache Handler.
+    */
+   protected final CacheHandler cacheHandler;
+
+   /**
     * @param ldapAttrMapping {@link LDAPAttributeMapping}
     * @param ldapService {@link LDAPService}
     * @throws Exception if any error occurs
     */
-   public BaseDAO(LDAPAttributeMapping ldapAttrMapping, LDAPService ldapService) throws Exception
+   public BaseDAO(LDAPAttributeMapping ldapAttrMapping, LDAPService ldapService, CacheHandler cacheHandler)
+      throws Exception
    {
       this.ldapAttrMapping = ldapAttrMapping;
       this.ldapService = ldapService;
+      this.cacheHandler = cacheHandler;
       initializeNameParser();
    }
 
@@ -141,6 +149,26 @@
    }
 
    /**
+    * Construct object name from {@link Group} id.
+    *
+    * @param groupDN group DN
+    * @return object name
+    */
+   protected String getGroupIdFromGroupDN(String groupDN) throws NamingException
+   {
+      // extract group's id, group's name and parent's group from DN
+      StringBuffer buffer = new StringBuffer();
+      String[] baseParts = explodeDN(ldapAttrMapping.groupsURL, true);
+      String[] membershipParts = explodeDN(groupDN, true);
+      for (int x = (membershipParts.length - baseParts.length - 1); x > -1; x--)
+      {
+         buffer.append("/" + membershipParts[x]);
+      }
+
+      return buffer.toString();
+   }
+
+   /**
     * Get collection of {@link Attribute} with specified name from
     * {@link Attributes}.
     * 
@@ -204,6 +232,20 @@
     */
    protected Group getGroupFromMembershipDN(LdapContext ctx, String membershipDN) throws NamingException
    {
+      String groupDN = getGroupDNFromMembershipDN(membershipDN);
+      Group group = getGroupByDN(ctx, groupDN);
+      return group;
+   }
+
+   /**
+    * Retrieve Group DN from membership DN.
+    *
+    * @param membershipDN membership Distinguished Name
+    * @return GroupDN
+    * @throws NamingException if any naming errors occurs
+    */
+   protected String getGroupDNFromMembershipDN(String membershipDN) throws NamingException
+   {
       String[] membershipParts = explodeDN(membershipDN, false);
       StringBuffer buffer = new StringBuffer();
       for (int x = 1; x < membershipParts.length; x++)
@@ -217,8 +259,7 @@
             buffer.append(membershipParts[x] + ",");
          }
       }
-      Group group = getGroupByDN(ctx, buffer.toString());
-      return group;
+      return buffer.toString();
    }
 
    /**
@@ -245,12 +286,7 @@
             }
             catch (NamingException e)
             {
-               // check is allowed to try one more time
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  // not connection exception or error occurs more than MAX_CONNECTION_ERROR
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -261,6 +297,30 @@
    }
 
    /**
+    * Re-load the ctx if the context allows it
+    * @param ctx the previous context
+    * @param err the total of errors that have already occurred
+    * @param e the last exception that occurs
+    * @return the new context if the context reload is allowed throws an exception otherwise
+    * @throws NamingException if context could not be reloaded
+    */
+   protected LdapContext reloadCtx(LdapContext ctx, int err, NamingException e) throws NamingException
+   {
+      // check is allowed to try one more time
+      if (isConnectionError(e) && err < getMaxConnectionError())
+      {
+         // release the previous context
+         ldapService.release(ctx);
+         // reload the context
+         ctx = ldapService.getLdapContext(true);
+      }
+      else
+         // not connection exception or error occurs more than MAX_CONNECTION_ERROR
+         throw e;
+      return ctx;
+   }
+
+   /**
     * Get Group what reflected to object with specified Distinguished Name.
     * 
     * @param ctx {@link LdapContext}
@@ -270,6 +330,26 @@
     */
    protected Group getGroupByDN(LdapContext ctx, String groupDN) throws NamingException
    {
+      try
+      {
+         Attributes attrs = ctx.getAttributes(groupDN);
+         return buildGroup(groupDN, attrs);
+      }
+      catch (NameNotFoundException e)
+      {
+         if (LOG.isDebugEnabled())
+            e.printStackTrace();
+         // Object with specified Distinguished Name not found. Null will be
+         // returned. This result we regard as successful, just nothing found.
+         return null;
+      }
+   }
+
+   protected Group buildGroup(String groupDN, Attributes attrs) throws NamingException
+   {
+      GroupImpl group = new GroupImpl();
+
+      // extract group's id, group's name and parent's group from DN
       StringBuffer idBuffer = new StringBuffer();
       String parentId = null;
       String[] baseParts = explodeDN(ldapAttrMapping.groupsURL, true);
@@ -280,25 +360,16 @@
          if (x == 1)
             parentId = idBuffer.toString();
       }
-      try
+      group.setGroupName(membershipParts[0]);
+      group.setId(idBuffer.toString());
+      if (attrs != null)
       {
-         Attributes attrs = ctx.getAttributes(groupDN);
-         GroupImpl group = new GroupImpl();
-         group.setGroupName(membershipParts[0]);
-         group.setId(idBuffer.toString());
          group.setDescription(ldapAttrMapping.getAttributeValueAsString(attrs, ldapAttrMapping.ldapDescriptionAttr));
          group.setLabel(ldapAttrMapping.getAttributeValueAsString(attrs, ldapAttrMapping.groupLabelAttr));
-         group.setParentId(parentId);
-         return group;
       }
-      catch (NameNotFoundException e)
-      {
-         if (LOG.isDebugEnabled())
-            LOG.debug(e.getLocalizedMessage(), e);
-         // Object with specified Distinguished Name not found. Null will be
-         // returned. This result we regard as successful, just nothing found.
-         return null;
-      }
+      group.setParentId(parentId);
+
+      return group;
    }
 
    /**
@@ -352,10 +423,7 @@
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -381,9 +449,7 @@
          answer = findUser(ctx, username, true);
          while (answer.hasMoreElements())
          {
-            String userDN = answer.next().getNameInNamespace();
-            Attributes attrs = ctx.getAttributes(userDN);
-            return ldapAttrMapping.attributesToUser(attrs);
+            return ldapAttrMapping.attributesToUser(answer.next().getAttributes());
          }
          return null;
       }
@@ -465,10 +531,7 @@
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -525,6 +588,9 @@
             removeAllSubtree(ctx, sr.getNameInNamespace());
          }
          ctx.destroySubcontext(dn);
+         String groupId = buildGroup(dn, null).getId();
+         cacheHandler.remove(groupId, CacheType.GROUP);
+         cacheHandler.remove(CacheHandler.GROUP_PREFIX + groupId, CacheType.MEMBERSHIP);
       }
       finally
       {
@@ -649,14 +715,7 @@
             }
             catch (NamingException e)
             {
-               // check is allowed to try one more time
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  // update LdapContext
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  // not connection exception or error occurs more than
-                  // MAX_CONNECTION_ERROR times
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }

Added: core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/CacheHandler.java
===================================================================
--- core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/CacheHandler.java	                        (rev 0)
+++ core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/CacheHandler.java	2011-08-15 07:11:30 UTC (rev 4753)
@@ -0,0 +1,176 @@
+/*
+ * Copyright (C) 2011 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.exoplatform.services.organization.ldap;
+
+import org.exoplatform.services.cache.CacheService;
+import org.exoplatform.services.cache.ExoCache;
+import org.exoplatform.services.organization.Group;
+import org.exoplatform.services.organization.Membership;
+import org.exoplatform.services.organization.MembershipType;
+import org.exoplatform.services.organization.User;
+
+import java.io.Serializable;
+
+/**
+ * @author <a href="abazko at exoplatform.com">Anatoliy Bazko</a>
+ * @version $Id: CacheHandler.java 34360 2009-07-22 23:58:59Z tolusha $
+ */
+public class CacheHandler
+{
+   public static final String MEMBERSHIPTYPE_PREFIX = "mt=";
+
+   public static final String GROUP_PREFIX = "g=";
+
+   public static final String USER_PREFIX = "u=";
+
+   /**
+    * Cache for Users.
+    */
+   private final ExoCache<Serializable, User> userCache;
+
+   /**
+    * Cache for MembershipTypes.
+    */
+   private final ExoCache<Serializable, MembershipType> membershipTypeCache;
+
+   /**
+    * Cache for Memberships.
+    */
+   private final ExoCache<Serializable, Membership> membershipCache;
+
+   /**
+    * Cache for Groups.
+    */
+   private final ExoCache<Serializable, Group> groupCache;
+
+   /**
+    * Constructor CacheHandler. 
+    * 
+    * @param cservice
+    *          The cache handler
+    */
+   public CacheHandler(CacheService cservice)
+   {
+      this.userCache = cservice.getCacheInstance(this.getClass().getName() + "userCache");
+      this.membershipTypeCache = cservice.getCacheInstance(this.getClass().getName() + "membershipTypeCache");
+      this.groupCache = cservice.getCacheInstance(this.getClass().getName() + "groupCache");
+      this.membershipCache = cservice.getCacheInstance(this.getClass().getName() + "membershipCache");
+   }
+
+   public void put(Serializable key, Object value, CacheType cacheType)
+   {
+      if (cacheType == CacheType.USER)
+      {
+         userCache.put(key, (User)value);
+      }
+      else if (cacheType == CacheType.GROUP)
+      {
+         groupCache.put(key, (Group)value);
+      }
+      else if (cacheType == CacheType.MEMBERSHIP)
+      {
+         membershipCache.put(key, (Membership)value);
+      }
+      else if (cacheType == CacheType.MEMBERSHIPTYPE)
+      {
+         membershipTypeCache.put(key, (MembershipType)value);
+      }
+   }
+
+   public Object get(Serializable key, CacheType cacheType)
+   {
+      if (cacheType == CacheType.USER)
+      {
+         return userCache.get(key);
+      }
+      else if (cacheType == CacheType.GROUP)
+      {
+         return groupCache.get(key);
+      }
+      else if (cacheType == CacheType.MEMBERSHIP)
+      {
+         return membershipCache.get(key);
+      }
+      else if (cacheType == CacheType.MEMBERSHIPTYPE)
+      {
+         return membershipTypeCache.get(key);
+      }
+
+      return null;
+   }
+
+   public void remove(Serializable key, CacheType cacheType)
+   {
+      if (cacheType == CacheType.USER)
+      {
+         userCache.remove(key);
+      }
+      else if (cacheType == CacheType.GROUP)
+      {
+         groupCache.remove(key);
+      }
+      else if (cacheType == CacheType.MEMBERSHIP)
+      {
+         try
+         {
+            String tKey = ((String)key).toUpperCase();
+            for (Membership m : membershipCache.getCachedObjects())
+            {
+               String mkey = getMembershipKey(m);
+               if (mkey.toUpperCase().indexOf(tKey) >= 0)
+               {
+                  membershipCache.remove(mkey);
+               }
+            }
+         }
+         catch (Exception e)
+         {
+         }
+      }
+      else if (cacheType == CacheType.MEMBERSHIPTYPE)
+      {
+         membershipTypeCache.remove(key);
+      }
+   }
+
+   public String getMembershipKey(Membership m)
+   {
+      StringBuilder key = new StringBuilder();
+      key.append(GROUP_PREFIX + m.getGroupId());
+      key.append(MEMBERSHIPTYPE_PREFIX + m.getMembershipType());
+      key.append(USER_PREFIX + m.getUserName());
+
+      return key.toString();
+   }
+
+   public String getMembershipKey(String username, String groupId, String type)
+   {
+      StringBuilder key = new StringBuilder();
+      key.append(GROUP_PREFIX + groupId);
+      key.append(MEMBERSHIPTYPE_PREFIX + type);
+      key.append(USER_PREFIX + username);
+
+      return key.toString();
+   }
+
+   static enum CacheType
+   {
+      USER, GROUP, MEMBERSHIP, MEMBERSHIPTYPE
+   }
+}

Modified: core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/GroupDAOImpl.java
===================================================================
--- core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/GroupDAOImpl.java	2011-08-12 13:45:19 UTC (rev 4752)
+++ core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/GroupDAOImpl.java	2011-08-15 07:11:30 UTC (rev 4753)
@@ -26,11 +26,14 @@
 import org.exoplatform.services.organization.GroupEventListenerHandler;
 import org.exoplatform.services.organization.GroupHandler;
 import org.exoplatform.services.organization.impl.GroupImpl;
+import org.exoplatform.services.organization.ldap.CacheHandler.CacheType;
 
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Set;
 
 import javax.naming.CompositeName;
 import javax.naming.Name;
@@ -69,11 +72,14 @@
     * @param ldapAttrMapping mapping LDAP attributes to eXo organization service
     *          items (users, groups, etc)
     * @param ldapService {@link LDAPService}
+    * @param cacheHandler
+    *          The Cache Handler
     * @throws Exception if any errors occurs
     */
-   public GroupDAOImpl(LDAPAttributeMapping ldapAttrMapping, LDAPService ldapService) throws Exception
+   public GroupDAOImpl(LDAPAttributeMapping ldapAttrMapping, LDAPService ldapService, CacheHandler cacheHandler)
+      throws Exception
    {
-      super(ldapAttrMapping, ldapService);
+      super(ldapAttrMapping, ldapService, cacheHandler);
       this.listeners = new ArrayList<GroupEventListener>(3);
    }
 
@@ -136,14 +142,13 @@
                ctx.createSubcontext(groupDN, ldapAttrMapping.groupToAttributes(child));
                if (broadcast)
                   postSave(group, true);
+               
+               cacheHandler.put(child.getId(), group, CacheType.GROUP);
                return;
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
             finally
             {
@@ -192,14 +197,13 @@
                ctx.modifyAttributes(groupDN, mods);
                if (broadcast)
                   postSave(group, true);
+
+               cacheHandler.put(group.getId(), group, CacheType.GROUP);
                return;
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -255,15 +259,13 @@
                removeAllSubtree(ctx, groupDN);
                if (broadcast)
                   postDelete(group);
+
                return group;
 
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
             finally
             {
@@ -323,10 +325,7 @@
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
             finally
             {
@@ -348,6 +347,13 @@
    {
       if (groupId == null)
          return null;
+
+      Group group = (Group)cacheHandler.get(groupId, CacheType.GROUP);
+      if (group != null)
+      {
+         return group;
+      }
+      
       String parentId = null;
       StringBuffer buffer = new StringBuffer();
       String[] groupIdParts = groupId.split("/");
@@ -366,17 +372,19 @@
             try
             {
                Attributes attrs = ctx.getAttributes(groupDN);
-               Group group = ldapAttrMapping.attributesToGroup(attrs);
+               group = ldapAttrMapping.attributesToGroup(attrs);
                ((GroupImpl)group).setId(groupId);
                ((GroupImpl)group).setParentId(parentId);
+
+               if (group != null)
+               {
+                  cacheHandler.put(groupId, group, CacheType.GROUP);
+               }
                return group;
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -402,6 +410,13 @@
    {
       if (groupId == null)
          return null;
+
+      Group group = (Group)cacheHandler.get(groupId, CacheType.GROUP);
+      if (group != null)
+      {
+         return group;
+      }
+
       String parentId = null;
       StringBuffer buffer = new StringBuffer();
       String[] groupIdParts = groupId.split("/");
@@ -415,9 +430,14 @@
       try
       {
          Attributes attrs = ctx.getAttributes(groupDN);
-         Group group = ldapAttrMapping.attributesToGroup(attrs);
+         group = ldapAttrMapping.attributesToGroup(attrs);
          ((GroupImpl)group).setId(groupId);
          ((GroupImpl)group).setParentId(parentId);
+
+         if (group != null)
+         {
+            cacheHandler.put(groupId, group, CacheType.GROUP);
+         }
          return group;
       }
       catch (NameNotFoundException e)
@@ -473,7 +493,7 @@
                   {
                      Name entryName = parser.parse(name.get(0));
                      String groupDN = entryName + "," + ldapAttrMapping.groupsURL;
-                     Group group = this.getGroupByDN(ctx, groupDN);
+                     Group group = this.buildGroup(groupDN, sr.getAttributes());
                      if (group != null)
                         addGroup(groups, group);
                   }
@@ -482,10 +502,7 @@
             }
             catch (NamingException e2)
             {
-               if (isConnectionError(e2) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e2;
+               ctx = reloadCtx(ctx, err, e2);
             }
             finally
             {
@@ -555,7 +572,7 @@
                   {
                      Name entryName = parser.parse(name.get(0));
                      String groupDN = entryName + "," + searchBase;
-                     Group group = this.getGroupByDN(ctx, groupDN);
+                     Group group = this.buildGroup(groupDN, sr.getAttributes());
                      if (group != null)
                         addGroup(groups, group);
                   }
@@ -564,10 +581,7 @@
             }
             catch (NamingException e2)
             {
-               if (isConnectionError(e2) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e2;
+               ctx = reloadCtx(ctx, err, e2);
             }
             finally
             {
@@ -613,21 +627,24 @@
                results = ctx.search(ldapAttrMapping.groupsURL, filter, constraints);
 
                // add groups for memberships matching user
-               // int total = 0;
+               Set<String> uniqueGroupsDN = new HashSet<String>();
                while (results != null && results.hasMore())
                {
                   SearchResult sr = results.next();
-                  // total++;
                   NameParser parser = ctx.getNameParser("");
                   CompositeName name = new CompositeName(sr.getName());
                   if (name.size() < 1)
                      break;
                   Name entryName = parser.parse(name.get(0));
                   String membershipDN = entryName + "," + ldapAttrMapping.groupsURL;
-                  Group group = this.getGroupFromMembershipDN(ctx, membershipDN);
-                  if (group != null)
-                     addGroup(groups, group);
+                  uniqueGroupsDN.add(this.getGroupDNFromMembershipDN(membershipDN));
                }
+               for(String groupDN : uniqueGroupsDN)
+               {
+                    Group group = this.getGroupByDN(ctx, groupDN);
+                    if (group != null)
+                        addGroup(groups, group);
+               }
                if (LOG.isDebugEnabled())
                {
                   LOG.debug("Retrieved " + groups.size() + " groups from ldap for user " + userName);
@@ -636,10 +653,7 @@
             }
             catch (NamingException e2)
             {
-               if (isConnectionError(e2) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e2;
+               ctx = reloadCtx(ctx, err, e2);
             }
             finally
             {

Modified: core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/LDAPUserPageList.java
===================================================================
--- core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/LDAPUserPageList.java	2011-08-12 13:45:19 UTC (rev 4752)
+++ core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/LDAPUserPageList.java	2011-08-15 07:11:30 UTC (rev 4753)
@@ -142,7 +142,10 @@
             catch (NamingException e)
             {
                if (BaseDAO.isConnectionError(e) && err < BaseDAO.getMaxConnectionError())
+               {
+                  ldapService.release(ctx);
                   ctx = ldapService.getLdapContext(true);
+               }
                else
                   throw e;
             }
@@ -199,7 +202,10 @@
             catch (NamingException e)
             {
                if (BaseDAO.isConnectionError(e) && err < 1)
+               {
+                  ldapService.release(ctx);
                   ctx = ldapService.getLdapContext(true);
+               }
                else
                   throw e;
             }

Modified: core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/LdapUserListAccess.java
===================================================================
--- core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/LdapUserListAccess.java	2011-08-12 13:45:19 UTC (rev 4752)
+++ core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/LdapUserListAccess.java	2011-08-15 07:11:30 UTC (rev 4753)
@@ -84,7 +84,10 @@
             catch (NamingException e)
             {
                if (BaseDAO.isConnectionError(e) && err < BaseDAO.getMaxConnectionError())
+               {
+                  ldapService.release(ctx);
                   ctx = ldapService.getLdapContext(true);
+               }
                else
                   throw e;
             }
@@ -113,7 +116,10 @@
             catch (NamingException e)
             {
                if (BaseDAO.isConnectionError(e) && err < BaseDAO.getMaxConnectionError())
+               {
+                  ldapService.release(ctx);
                   ctx = ldapService.getLdapContext(true);
+               }
                else
                   throw e;
             }

Modified: core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/MembershipDAOImpl.java
===================================================================
--- core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/MembershipDAOImpl.java	2011-08-12 13:45:19 UTC (rev 4752)
+++ core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/MembershipDAOImpl.java	2011-08-15 07:11:30 UTC (rev 4753)
@@ -30,6 +30,7 @@
 import org.exoplatform.services.organization.OrganizationService;
 import org.exoplatform.services.organization.User;
 import org.exoplatform.services.organization.impl.MembershipImpl;
+import org.exoplatform.services.organization.ldap.CacheHandler.CacheType;
 
 import java.util.ArrayList;
 import java.util.Collection;
@@ -78,13 +79,15 @@
     *          {@link LDAPService}
     * @param service 
     *          Organization service implementation covering the handler. 
+    * @param cacheHandler
+    *          The Cache Handler
     * @throws Exception
     *           if any errors occurs
     */
-   public MembershipDAOImpl(LDAPAttributeMapping ldapAttrMapping, LDAPService ldapService, OrganizationService service)
-      throws Exception
+   public MembershipDAOImpl(LDAPAttributeMapping ldapAttrMapping, LDAPService ldapService, OrganizationService service,
+      CacheHandler cacheHandler) throws Exception
    {
-      super(ldapAttrMapping, ldapService);
+      super(ldapAttrMapping, ldapService, cacheHandler);
       this.listeners = new ArrayList<MembershipEventListener>(3);
       this.service = service;
    }
@@ -147,6 +150,7 @@
                   ctx.createSubcontext(membershipDN, ldapAttrMapping.membershipToAttributes(m, userDN));
                   if (broadcast)
                      postSave(m, true);
+                  cacheHandler.put(cacheHandler.getMembershipKey(m), m, CacheType.MEMBERSHIP);
                   return;
                }
                // if contains membership
@@ -164,15 +168,13 @@
                ctx.modifyAttributes(membershipDN, mods);
                if (broadcast)
                   postSave(m, true);
+               cacheHandler.put(cacheHandler.getMembershipKey(m), m, CacheType.MEMBERSHIP);
                return;
 
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -266,6 +268,7 @@
                   ctx.modifyAttributes(membershipDN, mods);
                   if (broadcast)
                      postSave(m, true);
+                  cacheHandler.put(cacheHandler.getMembershipKey(m), m, CacheType.MEMBERSHIP);
                }
                else
                {
@@ -274,15 +277,13 @@
                   ctx.destroySubcontext(membershipDN);
                   if (broadcast)
                      postDelete(m);
+                  cacheHandler.remove(cacheHandler.getMembershipKey(m), CacheType.MEMBERSHIP);
                }
                return m;
             }
             catch (NamingException e1)
             {
-               if (isConnectionError(e1) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e1;
+               ctx = reloadCtx(ctx, err, e1);
             }
          }
       }
@@ -339,9 +340,13 @@
                            new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute(
                               ldapAttrMapping.membershipTypeMemberValue, userDN));
                         ctx.modifyAttributes(membershipDN, mods);
+                        cacheHandler.put(cacheHandler.getMembershipKey(membership), membership, CacheType.MEMBERSHIP);
                      }
                      else
+                     {
                         ctx.destroySubcontext(membershipDN);
+                        cacheHandler.remove(cacheHandler.getMembershipKey(membership), CacheType.MEMBERSHIP);
+                     }
                   }
                   catch (Exception e1)
                   {
@@ -352,10 +357,7 @@
             }
             catch (NamingException e2)
             {
-               if (isConnectionError(e2) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e2;
+               ctx = reloadCtx(ctx, err, e2);
             }
             finally
             {
@@ -386,12 +388,19 @@
     */
    public Membership findMembershipByUserGroupAndType(String userName, String groupId, String type) throws Exception
    {
+      MembershipImpl membership =
+         (MembershipImpl)cacheHandler.get(cacheHandler.getMembershipKey(userName, groupId, type), CacheType.MEMBERSHIP);
+      if (membership != null)
+      {
+         return membership;
+      }
+
       LdapContext ctx = ldapService.getLdapContext();
       try
       {
          for (int err = 0;; err++)
          {
-            Membership membership = null;
+            membership = null;
             try
             {
                String userDN = getDNFromUsername(ctx, userName);
@@ -416,14 +425,15 @@
                   membership = createMembershipObject(userName, groupId, type);
                }
 
+               if (membership != null)
+               {
+                  cacheHandler.put(cacheHandler.getMembershipKey(membership), membership, CacheType.MEMBERSHIP);
+               }
                return membership;
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -559,9 +569,9 @@
                {
                   SearchResult sr = results.next();
                   String membershipDN = sr.getNameInNamespace();
-                  Group group = getGroupFromMembershipDN(ctx, membershipDN);
+                  String groupId = getGroupIdFromGroupDN(getGroupDNFromMembershipDN(membershipDN));
                   String type = explodeDN(membershipDN, true)[0];
-                  Membership membership = createMembershipObject(userName, group.getId(), type);
+                  Membership membership = createMembershipObject(userName, groupId, type);
                   memberships.add(membership);
                }
                if (LOG.isDebugEnabled())
@@ -572,10 +582,7 @@
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
             finally
             {
@@ -640,10 +647,7 @@
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
             finally
             {

Modified: core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/MembershipTypeDAOImpl.java
===================================================================
--- core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/MembershipTypeDAOImpl.java	2011-08-12 13:45:19 UTC (rev 4752)
+++ core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/MembershipTypeDAOImpl.java	2011-08-15 07:11:30 UTC (rev 4753)
@@ -24,6 +24,7 @@
 import org.exoplatform.services.organization.MembershipType;
 import org.exoplatform.services.organization.MembershipTypeHandler;
 import org.exoplatform.services.organization.impl.MembershipTypeImpl;
+import org.exoplatform.services.organization.ldap.CacheHandler.CacheType;
 
 import java.util.ArrayList;
 import java.util.Collection;
@@ -56,11 +57,14 @@
     * @param ldapAttrMapping mapping LDAP attributes to eXo organization service
     *          items (users, groups, etc)
     * @param ldapService {@link LDAPService}
+    * @param cacheHandler
+    *          The Cache Handler
     * @throws Exception if any errors occurs
     */
-   public MembershipTypeDAOImpl(LDAPAttributeMapping ldapAttrMapping, LDAPService ldapService) throws Exception
+   public MembershipTypeDAOImpl(LDAPAttributeMapping ldapAttrMapping, LDAPService ldapService, CacheHandler cacheHandler)
+      throws Exception
    {
-      super(ldapAttrMapping, ldapService);
+      super(ldapAttrMapping, ldapService, cacheHandler);
    }
 
    /**
@@ -95,15 +99,14 @@
                   mt.setCreatedDate(now);
                   mt.setModifiedDate(now);
                   ctx.createSubcontext(membershipTypeDN, ldapAttrMapping.membershipTypeToAttributes(mt));
+
+                  cacheHandler.put(mt.getName(), mt, CacheType.MEMBERSHIPTYPE);
                }
                return mt;
             }
             catch (NamingException e1)
             {
-               if (isConnectionError(e1) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e1;
+               ctx = reloadCtx(ctx, err, e1);
             }
          }
       }
@@ -146,14 +149,13 @@
                         ldapAttrMapping.ldapDescriptionAttr, mt.getDescription()));
                }
                ctx.modifyAttributes(membershipTypeDN, mods);
+
+               cacheHandler.put(mt.getName(), mt, CacheType.MEMBERSHIPTYPE);
                return mt;
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -181,14 +183,13 @@
                MembershipType m = ldapAttrMapping.attributesToMembershipType(attrs);
                removeMembership(ctx, name);
                ctx.destroySubcontext(membershipTypeDN);
+
+               cacheHandler.remove(name, CacheType.MEMBERSHIPTYPE);
                return m;
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -209,6 +210,12 @@
     */
    public MembershipType findMembershipType(String name) throws Exception
    {
+      MembershipType mt = (MembershipType)cacheHandler.get(name, CacheType.MEMBERSHIPTYPE);
+      if (mt != null)
+      {
+         return mt;
+      }
+
       String membershipTypeDN =
          ldapAttrMapping.membershipTypeNameAttr + "=" + name + "," + ldapAttrMapping.membershipTypeURL;
       LdapContext ctx = ldapService.getLdapContext();
@@ -219,14 +226,16 @@
             try
             {
                Attributes attrs = ctx.getAttributes(membershipTypeDN);
-               return ldapAttrMapping.attributesToMembershipType(attrs);
+               mt = ldapAttrMapping.attributesToMembershipType(attrs);
+               if (mt != null)
+               {
+                  cacheHandler.put(name, mt, CacheType.MEMBERSHIPTYPE);
+               }
+               return mt;
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -274,10 +283,7 @@
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
             finally
             {
@@ -307,6 +313,8 @@
          {
             SearchResult sr = results.next();
             ctx.destroySubcontext(sr.getNameInNamespace());
+
+            cacheHandler.remove(CacheHandler.MEMBERSHIPTYPE_PREFIX + name, CacheType.MEMBERSHIP);
          }
       }
       finally

Modified: core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/OrganizationServiceImpl.java
===================================================================
--- core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/OrganizationServiceImpl.java	2011-08-12 13:45:19 UTC (rev 4752)
+++ core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/OrganizationServiceImpl.java	2011-08-15 07:11:30 UTC (rev 4753)
@@ -47,25 +47,27 @@
       LDAPAttributeMapping ldapAttrMapping =
          (LDAPAttributeMapping)params.getObjectParam("ldap.attribute.mapping").getObject();
 
+      CacheHandler cacheHandler = new CacheHandler(cservice);
+
       if (ldapService.getServerType() == LDAPService.ACTIVE_DIRECTORY_SERVER)
       {
-         userDAO_ = new ADUserDAOImpl(ldapAttrMapping, ldapService);
+         userDAO_ = new ADUserDAOImpl(ldapAttrMapping, ldapService, cacheHandler);
          //      ADSearchBySID adSearch = new ADSearchBySID(ldapAttrMapping, ldapService);
          ADSearchBySID adSearch = new ADSearchBySID(ldapAttrMapping);
-         groupDAO_ = new ADGroupDAOImpl(ldapAttrMapping, ldapService, adSearch);
-         membershipDAO_ = new ADMembershipDAOImpl(ldapAttrMapping, ldapService, adSearch, this);
+         groupDAO_ = new ADGroupDAOImpl(ldapAttrMapping, ldapService, adSearch, cacheHandler);
+         membershipDAO_ = new ADMembershipDAOImpl(ldapAttrMapping, ldapService, adSearch, this, cacheHandler);
       }
       else
       {
          //      ValueParam param = params.getValueParam("ldap.userDN.key");
          //      ldapAttrMapping.userDNKey = param.getValue();
-         userDAO_ = new UserDAOImpl(ldapAttrMapping, ldapService);
-         groupDAO_ = new GroupDAOImpl(ldapAttrMapping, ldapService);
-         membershipDAO_ = new MembershipDAOImpl(ldapAttrMapping, ldapService, this);
+         userDAO_ = new UserDAOImpl(ldapAttrMapping, ldapService, cacheHandler);
+         groupDAO_ = new GroupDAOImpl(ldapAttrMapping, ldapService, cacheHandler);
+         membershipDAO_ = new MembershipDAOImpl(ldapAttrMapping, ldapService, this, cacheHandler);
       }
       // userProfileHandler_ = new UserProfileHandlerImpl(ldapAttrMapping, ldapService) ;
       userProfileDAO_ = new UserProfileDAOImpl(hservice, cservice);
-      membershipTypeDAO_ = new MembershipTypeDAOImpl(ldapAttrMapping, ldapService);
+      membershipTypeDAO_ = new MembershipTypeDAOImpl(ldapAttrMapping, ldapService, cacheHandler);
 
       ValueParam param = params.getValueParam("ldap.userDN.key");
       if (param != null)

Modified: core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/UserDAOImpl.java
===================================================================
--- core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/UserDAOImpl.java	2011-08-12 13:45:19 UTC (rev 4752)
+++ core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/UserDAOImpl.java	2011-08-15 07:11:30 UTC (rev 4753)
@@ -21,8 +21,13 @@
 import org.exoplatform.commons.utils.LazyPageList;
 import org.exoplatform.commons.utils.ListAccess;
 import org.exoplatform.services.ldap.LDAPService;
-import org.exoplatform.services.organization.*;
+import org.exoplatform.services.organization.Query;
+import org.exoplatform.services.organization.User;
+import org.exoplatform.services.organization.UserEventListener;
+import org.exoplatform.services.organization.UserEventListenerHandler;
+import org.exoplatform.services.organization.UserHandler;
 import org.exoplatform.services.organization.impl.UserImpl;
+import org.exoplatform.services.organization.ldap.CacheHandler.CacheType;
 
 import java.util.ArrayList;
 import java.util.Collections;
@@ -53,11 +58,14 @@
     * @param ldapAttrMapping mapping LDAP attributes to eXo organization service
     *          items (users, groups, etc)
     * @param ldapService {@link LDAPService}
+    * @param cacheHandler 
+    *          The Cache Handler
     * @throws Exception if any errors occurs
     */
-   public UserDAOImpl(LDAPAttributeMapping ldapAttrMapping, LDAPService ldapService) throws Exception
+   public UserDAOImpl(LDAPAttributeMapping ldapAttrMapping, LDAPService ldapService, CacheHandler cacheHandler)
+      throws Exception
    {
-      super(ldapAttrMapping, ldapService);
+      super(ldapAttrMapping, ldapService, cacheHandler);
    }
 
    /**
@@ -104,14 +112,13 @@
                ctx.createSubcontext(userDN, attrs);
                if (broadcast)
                   postSave(user, true);
+
+               cacheHandler.put(user.getUserName(), user, CacheType.USER);
                break;
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -145,14 +152,13 @@
                ctx.modifyAttributes(userDN, mods);
                if (broadcast)
                   postSave(user, false);
+
+               cacheHandler.put(user.getUserName(), user, CacheType.USER);
                break;
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -174,7 +180,7 @@
     * @param userDN Distinguished Name
     * @throws Exception if any errors occurs
     */
-   void saveUserPassword(User user, String userDN) throws Exception
+   protected void saveUserPassword(User user, String userDN) throws Exception
    {
       ModificationItem[] mods =
          new ModificationItem[]{new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute(
@@ -191,10 +197,7 @@
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -227,14 +230,14 @@
                ctx.destroySubcontext(userDN);
                if (broadcast)
                   postDelete(user);
+
+               cacheHandler.remove(userName, CacheType.USER);
+               cacheHandler.remove(CacheHandler.USER_PREFIX + userName, CacheType.MEMBERSHIP);
                return user;
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -249,6 +252,12 @@
     */
    public User findUserByName(String userName) throws Exception
    {
+      User user = (User)cacheHandler.get(userName, CacheType.USER);
+      if (user != null)
+      {
+         return user;
+      }
+
       LdapContext ctx = ldapService.getLdapContext();
       try
       {
@@ -256,14 +265,16 @@
          {
             try
             {
-               return getUserFromUsername(ctx, userName);
+               user = getUserFromUsername(ctx, userName);
+               if (user != null)
+               {
+                  cacheHandler.put(user.getUserName(), user, CacheType.USER);
+               }
+               return user;
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -283,51 +294,6 @@
    */
    public ListAccess<User> findUsersByGroupId(String groupId) throws Exception
    {
-      //    ArrayList<User> users = new ArrayList<User>();
-      //    TreeMap<String, User> map = new TreeMap<String, User>();
-      //
-      //    LdapContext ctx = ldapService.getLdapContext();
-      //    try {
-      //      NamingEnumeration<SearchResult> results = null;
-      //      for (int err = 0;; err++) {
-      //        map.clear();
-      //        try {
-      //          String searchBase = this.getGroupDNFromGroupId(groupId);
-      //          String filter = ldapAttrMapping.membershipObjectClassFilter;
-      //          SearchControls constraints = new SearchControls();
-      //          constraints.setSearchScope(SearchControls.ONELEVEL_SCOPE);
-      //          results = ctx.search(searchBase, filter, constraints);
-      //          while (results.hasMoreElements()) {
-      //            SearchResult sr = results.next();
-      //            Attributes attrs = sr.getAttributes();
-      //            List<Object> members = this.getAttributes(attrs,
-      //                                                      ldapAttrMapping.membershipTypeMemberValue);
-      //            for (int x = 0; x < members.size(); x++) {
-      //              User user = findUserByDN(ctx, (String) members.get(x));
-      //              if (user != null)
-      //                map.put(user.getUserName(), user);
-      //            }
-      //          }
-      //          break;
-      //        } catch (NamingException e) {
-      //          if (isConnectionError(e) && err < getMaxConnectionError())
-      //            ctx = ldapService.getLdapContext(true);
-      //          else 
-      //            throw e;
-      //        } finally {
-      //          if (results != null)
-      //            results.close();
-      //        }
-      //      }
-      //    } finally {
-      //      ldapService.release(ctx);
-      //    }
-      //
-      //    for (Iterator<String> i = map.keySet().iterator(); i.hasNext();)
-      //      users.add(map.get(i.next()));
-      //    
-      //    return new ObjectPageList(users, 10);
-
       String searchBase = this.getGroupDNFromGroupId(groupId);
       String filter = ldapAttrMapping.membershipObjectClassFilter;
       return new ByGroupLdapUserListAccess(ldapAttrMapping, ldapService, searchBase, filter);
@@ -346,8 +312,6 @@
       String searchBase = ldapAttrMapping.userURL;
       String filter = ldapAttrMapping.userObjectClassFilter;
 
-      //    return new LDAPUserPageList(ldapAttrMapping, ldapService, searchBase, filter, pageSize);
-
       return new SimpleLdapUserListAccess(ldapAttrMapping, ldapService, searchBase, filter);
    }
 

Modified: core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/UserProfileDAOImpl.java
===================================================================
--- core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/UserProfileDAOImpl.java	2011-08-12 13:45:19 UTC (rev 4752)
+++ core/trunk/exo.core.component.organization.ldap/src/main/java/org/exoplatform/services/organization/ldap/UserProfileDAOImpl.java	2011-08-15 07:11:30 UTC (rev 4753)
@@ -62,9 +62,10 @@
     */
    private static final Log LOG = ExoLogger.getLogger("exo.core.component.organization.ldap.UserProfileDAOImpl");
 
-   public UserProfileDAOImpl(LDAPAttributeMapping ldapAttrMapping, LDAPService ldapService) throws Exception
+   public UserProfileDAOImpl(LDAPAttributeMapping ldapAttrMapping, LDAPService ldapService, CacheHandler cacheHandler)
+      throws Exception
    {
-      super(ldapAttrMapping, ldapService);
+      super(ldapAttrMapping, ldapService, cacheHandler);
       this.listeners = new ArrayList<UserProfileEventListener>(3);
    }
 
@@ -108,10 +109,7 @@
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -154,10 +152,7 @@
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -192,10 +187,7 @@
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }
@@ -230,10 +222,7 @@
             }
             catch (NamingException e)
             {
-               if (isConnectionError(e) && err < getMaxConnectionError())
-                  ctx = ldapService.getLdapContext(true);
-               else
-                  throw e;
+               ctx = reloadCtx(ctx, err, e);
             }
          }
       }



More information about the exo-jcr-commits mailing list