[portal-commits] JBoss Portal SVN: r9055 - in modules/identity/trunk/identity/src: test/resources/config and 1 other directory.

portal-commits at lists.jboss.org portal-commits at lists.jboss.org
Wed Nov 21 04:54:32 EST 2007


Author: bdaw
Date: 2007-11-21 04:54:32 -0500 (Wed, 21 Nov 2007)
New Revision: 9055

Modified:
   modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPConnectionContext.java
   modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPExtRoleModuleImpl.java
   modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPExtUserModuleImpl.java
   modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPRoleModule.java
   modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPRoleModuleImpl.java
   modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPStaticGroupMembershipModuleImpl.java
   modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPStaticRoleMembershipModuleImpl.java
   modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPUserModule.java
   modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPUserModuleImpl.java
   modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPUserProfileModuleImpl.java
   modules/identity/trunk/identity/src/test/resources/config/standardidentity-config.xml
Log:
LDAP connection pooling

Modified: modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPConnectionContext.java
===================================================================
--- modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPConnectionContext.java	2007-11-21 09:51:36 UTC (rev 9054)
+++ modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPConnectionContext.java	2007-11-21 09:54:32 UTC (rev 9055)
@@ -31,6 +31,7 @@
 import javax.naming.NamingException;
 import javax.naming.InitialContext;
 import javax.naming.ldap.InitialLdapContext;
+import javax.naming.ldap.LdapContext;
 import java.util.Hashtable;
 
 /**
@@ -43,13 +44,32 @@
 {
    private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(IdentityModuleService.class);
 
+   public final static String CONNECTION_POOL = "com.sun.jndi.ldap.connect.pool";
+   public final static String CONNECTION_POOL_DEBUG = "com.sun.jndi.ldap.connect.pool.debug";
+   public final static String CONNECTION_POOL_INITSIZE = "com.sun.jndi.ldap.connect.pool.initsize";
+   public final static String CONNECTION_POOL_MAXSIZE = "com.sun.jndi.ldap.connect.pool.maxsize";
+   public final static String CONNECTION_POOL_PREFSIZE = "com.sun.jndi.ldap.connect.pool.prefsize";
+   public final static String CONNECTION_POOL_PROTOCOL = "com.sun.jndi.ldap.connect.pool.protocol";
+   public final static String CONNECTION_POOL_TIMEOUT = "com.sun.jndi.ldap.connect.pool.timeout";
+
+   private boolean pooling;
+
+   private String poolingDebug;
+
+   private String poolingInitsize;
+
+   private String poolingMaxsize;
+
+   private String poolingPrefsize;
+
+   private String poolingProtocol;
+
+   private String poolingTimeout;
+
    private String jndiName;
 
    private ServiceJNDIBinder jndiBinder;
 
-   //private JNDI.Binding jndiBinding;
-
-
    private String name;
 
    private String contextFactory;
@@ -79,10 +99,41 @@
       env.put(Context.SECURITY_AUTHENTICATION, this.getAuthentication());
       env.put(Context.SECURITY_PRINCIPAL, this.getAdminDN());
       env.put(Context.SECURITY_CREDENTIALS, this.getAdminPassword());
+
       if (this.getProtocol() != null)
       {
          env.put(Context.SECURITY_PROTOCOL, this.getProtocol());
       }
+
+      if (isPooling())
+      {
+         env.put(CONNECTION_POOL, "true");
+         if (getPoolingDebug() != null)
+         {
+            env.put(CONNECTION_POOL_DEBUG, getPoolingDebug());
+         }
+         if (getPoolingInitsize() != null)
+         {
+            env.put(CONNECTION_POOL_INITSIZE, getPoolingInitsize());
+         }
+         if (getPoolingMaxsize() != null)
+         {
+            env.put(CONNECTION_POOL_MAXSIZE, getPoolingMaxsize());
+         }
+         if (getPoolingPrefsize() != null)
+         {
+            env.put(CONNECTION_POOL_PREFSIZE, getPoolingPrefsize());
+         }
+         if (getPoolingProtocol() != null)
+         {
+            env.put(CONNECTION_POOL_PROTOCOL, getPoolingProtocol());
+         }
+         if (getPoolingTimeout() != null)
+         {
+            env.put(CONNECTION_POOL_TIMEOUT, getPoolingTimeout());
+         }
+     }
+
       return env;
    }
 
@@ -126,7 +177,7 @@
 
       if (identityContext != null)
       {
-         identityContext.register(this, IdentityContext.TYPE_CONNECTION_CONTEXT);         
+         identityContext.register(this, IdentityContext.TYPE_CONNECTION_CONTEXT);
       }
 
    }
@@ -288,4 +339,74 @@
    {
       this.identityContext = identityContext;
    }
+
+   public boolean isPooling()
+   {
+      return pooling;
+   }
+
+   public void setPooling(boolean pooling)
+   {
+      this.pooling = pooling;
+   }
+
+   public String getPoolingDebug()
+   {
+      return poolingDebug;
+   }
+
+   public void setPoolingDebug(String poolingDebug)
+   {
+      this.poolingDebug = poolingDebug;
+   }
+
+   public String getPoolingInitsize()
+   {
+      return poolingInitsize;
+   }
+
+   public void setPoolingInitsize(String poolingInitsize)
+   {
+      this.poolingInitsize = poolingInitsize;
+   }
+
+   public String getPoolingMaxsize()
+   {
+      return poolingMaxsize;
+   }
+
+   public void setPoolingMaxsize(String poolingMaxsize)
+   {
+      this.poolingMaxsize = poolingMaxsize;
+   }
+
+   public String getPoolingPrefsize()
+   {
+      return poolingPrefsize;
+   }
+
+   public void setPoolingPrefsize(String poolingPrefsize)
+   {
+      this.poolingPrefsize = poolingPrefsize;
+   }
+
+   public String getPoolingProtocol()
+   {
+      return poolingProtocol;
+   }
+
+   public void setPoolingProtocol(String poolingProtocol)
+   {
+      this.poolingProtocol = poolingProtocol;
+   }
+
+   public String getPoolingTimeout()
+   {
+      return poolingTimeout;
+   }
+
+   public void setPoolingTimeout(String poolingTimeout)
+   {
+      this.poolingTimeout = poolingTimeout;
+   }
 }

Modified: modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPExtRoleModuleImpl.java
===================================================================
--- modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPExtRoleModuleImpl.java	2007-11-21 09:51:36 UTC (rev 9054)
+++ modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPExtRoleModuleImpl.java	2007-11-21 09:54:32 UTC (rev 9055)
@@ -29,6 +29,7 @@
 
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
+import javax.naming.ldap.InitialLdapContext;
 import javax.naming.directory.SearchControls;
 import javax.naming.directory.SearchResult;
 import javax.naming.directory.DirContext;
@@ -65,7 +66,7 @@
 
 
          Object[] filterArgs = {name};
-         
+
          List sr = searchRoles(filter, filterArgs);
          if (sr.size() > 1)
          {
@@ -114,7 +115,7 @@
          filter.append(")");
 
          List sr = searchRoles(filter.toString(), null);
-         
+
          log.debug("Roles found: " + sr.size());
          for (Iterator iterator = sr.iterator(); iterator.hasNext();)
          {
@@ -173,7 +174,7 @@
       Set rf = new HashSet();
       try
       {
-         //search all entries 
+         //search all entries
          String filter = getRoleSearchFilter();
          //* chars are escaped in filterArgs so we must replace it manually
          filter = filter.replaceAll("\\{0\\}", "*");
@@ -208,63 +209,73 @@
     */
    public List searchRoles(String filter, Object[] filterArgs) throws NamingException, IdentityException
    {
-      SearchControls controls = new SearchControls();
-      controls.setSearchScope(getSearchScope());
-      controls.setReturningObjFlag(true);
-      controls.setTimeLimit(getSearchTimeLimit());
 
-      //
-      filter = filter.replaceAll("\\\\", "\\\\\\\\");
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
 
-      log.debug("Search filter: " + filter);
-      if (log.isDebugEnabled() && filterArgs != null)
+      try
       {
-         for (int i = 0; i < filterArgs.length; i++)
-         {
-            Object filterArg = filterArgs[i];
-            log.debug("Search filterArg: {" + i + "}: " + filterArg);
-         }
-      }
-      log.debug("Search ctx: " + getRoleSearchCtxDNs());
+         SearchControls controls = new SearchControls();
+         controls.setSearchScope(getSearchScope());
+         controls.setReturningObjFlag(true);
+         controls.setTimeLimit(getSearchTimeLimit());
 
-      Set roleCtxs = getRoleSearchCtxDNs();
+         //
+         filter = filter.replaceAll("\\\\", "\\\\\\\\");
 
-      if (roleCtxs.size() == 1)
-      {
-         Enumeration results = null;
-         if (filterArgs == null)
+         log.debug("Search filter: " + filter);
+         if (log.isDebugEnabled() && filterArgs != null)
          {
-            results = getConnectionContext().createInitialContext().search(getRoleCtxDN(), filter, controls);
+            for (int i = 0; i < filterArgs.length; i++)
+            {
+               Object filterArg = filterArgs[i];
+               log.debug("Search filterArg: {" + i + "}: " + filterArg);
+            }
          }
-         else
-         {
-            results = getConnectionContext().createInitialContext().search(getRoleCtxDN(), filter, filterArgs, controls);
-         }
-         return Tools.toList(results);
+         log.debug("Search ctx: " + getRoleSearchCtxDNs());
 
+         Set roleCtxs = getRoleSearchCtxDNs();
 
-      }
-      else
-      {
-         List merged = new LinkedList();
-
-         for (Iterator iterator = roleCtxs.iterator(); iterator.hasNext();)
+         if (roleCtxs.size() == 1)
          {
-            String roleCtx = (String)iterator.next();
             Enumeration results = null;
             if (filterArgs == null)
             {
-               results = getConnectionContext().createInitialContext().search(roleCtx, filter, controls);
+               results = ldapContext.search(getRoleCtxDN(), filter, controls);
             }
             else
             {
-               results = getConnectionContext().createInitialContext().search(roleCtx, filter, filterArgs, controls);
+               results = ldapContext.search(getRoleCtxDN(), filter, filterArgs, controls);
             }
-            merged.addAll(Tools.toList(results));
+            return Tools.toList(results);
+
+
          }
+         else
+         {
+            List merged = new LinkedList();
 
-         return merged;
+            for (Iterator iterator = roleCtxs.iterator(); iterator.hasNext();)
+            {
+               String roleCtx = (String)iterator.next();
+               Enumeration results = null;
+               if (filterArgs == null)
+               {
+                  results = ldapContext.search(roleCtx, filter, controls);
+               }
+               else
+               {
+                  results = ldapContext.search(roleCtx, filter, filterArgs, controls);
+               }
+               merged.addAll(Tools.toList(results));
+            }
+
+            return merged;
+         }
       }
+      finally
+      {
+         ldapContext.close();
+      }
    }
 
 

Modified: modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPExtUserModuleImpl.java
===================================================================
--- modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPExtUserModuleImpl.java	2007-11-21 09:51:36 UTC (rev 9054)
+++ modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPExtUserModuleImpl.java	2007-11-21 09:54:32 UTC (rev 9055)
@@ -22,25 +22,25 @@
  ******************************************************************************/
 package org.jboss.portal.identity.ldap;
 
+import org.jboss.portal.common.util.Tools;
+import org.jboss.portal.identity.IdentityConfiguration;
 import org.jboss.portal.identity.IdentityException;
 import org.jboss.portal.identity.NoSuchUserException;
 import org.jboss.portal.identity.User;
-import org.jboss.portal.identity.IdentityConfiguration;
-import org.jboss.portal.common.util.Tools;
 
-import javax.naming.NamingEnumeration;
 import javax.naming.Context;
 import javax.naming.NamingException;
+import javax.naming.directory.SearchControls;
 import javax.naming.directory.SearchResult;
-import javax.naming.directory.SearchControls;
-import java.util.Set;
-import java.util.List;
-import java.util.NoSuchElementException;
-import java.util.HashSet;
+import javax.naming.ldap.InitialLdapContext;
 import java.util.Collections;
-import java.util.LinkedList;
 import java.util.Enumeration;
+import java.util.HashSet;
 import java.util.Iterator;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.NoSuchElementException;
+import java.util.Set;
 
 /**
  * @author <a href="mailto:boleslaw.dawidowicz at jboss.org">Boleslaw Dawidowicz</a>
@@ -93,7 +93,7 @@
    //findUserById(Object id) from super
 
 
-   //findUserById(String id) from super 
+   //findUserById(String id) from super
 
    public User createUser(String userName, String password) throws IdentityException, IllegalArgumentException
    {
@@ -218,60 +218,70 @@
     */
    public List searchUsers(String filter, Object[] filterArgs) throws NamingException, IdentityException
    {
-      SearchControls controls = new SearchControls();
-      controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
-      controls.setReturningObjFlag(true);
-      controls.setTimeLimit(getSearchTimeLimit());
 
-      log.debug("Search filter: " + filter);
-      if (log.isDebugEnabled() && filterArgs != null)
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
+
+      try
       {
-         for (int i = 0; i < filterArgs.length; i++)
-         {
-            Object filterArg = filterArgs[i];
-            log.debug("Search filterArg: {" + i + "}: " + filterArg);
-         }
-      }
-      log.debug("Search ctx: " + getUserSearchCtxDNs());
+         SearchControls controls = new SearchControls();
+         controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
+         controls.setReturningObjFlag(true);
+         controls.setTimeLimit(getSearchTimeLimit());
 
-      Set userCtxs = getUserSearchCtxDNs();
-
-      if (userCtxs.size() == 1)
-      {
-         Enumeration results = null;
-         if (filterArgs == null)
+         log.debug("Search filter: " + filter);
+         if (log.isDebugEnabled() && filterArgs != null)
          {
-            results = getConnectionContext().createInitialContext().search(getUserSearchCtxDN(), filter, controls);
+            for (int i = 0; i < filterArgs.length; i++)
+            {
+               Object filterArg = filterArgs[i];
+               log.debug("Search filterArg: {" + i + "}: " + filterArg);
+            }
          }
-         else
-         {
-            results = getConnectionContext().createInitialContext().search(getUserSearchCtxDN(), filter, filterArgs, controls);
-         }
-         return Tools.toList(results);
+         log.debug("Search ctx: " + getUserSearchCtxDNs());
 
+         Set userCtxs = getUserSearchCtxDNs();
 
-      }
-      else
-      {
-         List merged = new LinkedList();
-
-         for (Iterator iterator = userCtxs.iterator(); iterator.hasNext();)
+         if (userCtxs.size() == 1)
          {
-            String userCtx = (String)iterator.next();
             Enumeration results = null;
             if (filterArgs == null)
             {
-               results = getConnectionContext().createInitialContext().search(userCtx, filter, controls);
+               results = ldapContext.search(getUserSearchCtxDN(), filter, controls);
             }
             else
             {
-               results = getConnectionContext().createInitialContext().search(userCtx, filter, filterArgs, controls);
+               results = ldapContext.search(getUserSearchCtxDN(), filter, filterArgs, controls);
             }
-            merged.addAll(Tools.toList(results));
+            return Tools.toList(results);
+
+
          }
+         else
+         {
+            List merged = new LinkedList();
 
-         return merged;
+            for (Iterator iterator = userCtxs.iterator(); iterator.hasNext();)
+            {
+               String userCtx = (String)iterator.next();
+               Enumeration results = null;
+               if (filterArgs == null)
+               {
+                  results = ldapContext.search(userCtx, filter, controls);
+               }
+               else
+               {
+                  results = ldapContext.search(userCtx, filter, filterArgs, controls);
+               }
+               merged.addAll(Tools.toList(results));
+            }
+
+            return merged;
+         }
       }
+      finally
+      {
+         ldapContext.close();
+      }
    }
 
 

Modified: modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPRoleModule.java
===================================================================
--- modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPRoleModule.java	2007-11-21 09:51:36 UTC (rev 9054)
+++ modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPRoleModule.java	2007-11-21 09:54:32 UTC (rev 9055)
@@ -35,6 +35,7 @@
 import javax.naming.NamingException;
 import javax.naming.NamingEnumeration;
 import javax.naming.InitialContext;
+import javax.naming.ldap.InitialLdapContext;
 import java.util.NoSuchElementException;
 import java.util.List;
 
@@ -48,7 +49,7 @@
 
    private LDAPConnectionContext connectionContext;
 
-   
+
    public void start() throws Exception
    {
       if (getConnectionJNDIName() == null)
@@ -71,6 +72,7 @@
    public void updateDisplayName(LDAPRoleImpl ldapr, String name) throws IdentityException
    {
       String attributeName = getDisplayNameAttributeID();
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
 
       try
       {
@@ -81,12 +83,23 @@
          attr.add(name);
          attrs.put(attr);
 
-         getConnectionContext().createInitialContext().modifyAttributes(ldapr.getDn(), DirContext.REPLACE_ATTRIBUTE,attrs);
+         ldapContext.modifyAttributes(ldapr.getDn(), DirContext.REPLACE_ATTRIBUTE,attrs);
       }
       catch (NamingException e)
       {
          throw new IdentityException("Cannot set role displayName value.", e);
       }
+      finally
+      {
+         try
+         {
+            ldapContext.close();
+         }
+         catch (NamingException e)
+         {
+            throw new IdentityException("Failed to close LDAP connection", e);
+         }
+      }
 
    }
 
@@ -136,6 +149,9 @@
     */
    public Role findRoleByDN(String dn) throws IdentityException, IllegalArgumentException
    {
+
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
+
       try
       {
          log.debug("findRoleByDN(): DN = " + dn);
@@ -145,8 +161,9 @@
             throw new IdentityException("Role dn canot be null");
          }
 
-         Attributes attrs = getConnectionContext().createInitialContext().getAttributes(dn);
 
+         Attributes attrs = ldapContext.getAttributes(dn);
+
          if (attrs == null)
          {
             throw new IdentityException("Can't find role entry with DN: " + dn);
@@ -163,6 +180,18 @@
       {
          throw new IdentityException("Role search failed.", e);
       }
+      finally
+      {
+         try
+         {
+            ldapContext.close();
+         }
+         catch (NamingException e)
+         {
+            throw new IdentityException("Failed to close LDAP connection", e);
+         }
+      }
+
       return null;
    }
 
@@ -258,7 +287,7 @@
       String roleCtx = getIdentityConfiguration().getValue(IdentityConfiguration.ROLE_CONTEXT_DN);
       if (roleCtx == null)
       {
-         throw new IdentityException("Configuration option missing: " + IdentityConfiguration.ROLE_CONTEXT_DN);   
+         throw new IdentityException("Configuration option missing: " + IdentityConfiguration.ROLE_CONTEXT_DN);
       }
       return roleCtx;
    }

Modified: modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPRoleModuleImpl.java
===================================================================
--- modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPRoleModuleImpl.java	2007-11-21 09:51:36 UTC (rev 9054)
+++ modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPRoleModuleImpl.java	2007-11-21 09:54:32 UTC (rev 9055)
@@ -40,6 +40,7 @@
 import javax.naming.directory.SearchResult;
 import javax.naming.directory.DirContext;
 import javax.naming.ldap.LdapContext;
+import javax.naming.ldap.InitialLdapContext;
 import java.util.List;
 import java.util.NoSuchElementException;
 import java.util.Set;
@@ -113,7 +114,7 @@
                   .append("=")
                   .append(name)
                   .append(") ");
-         }                  
+         }
          filter.append(")");
 
          List sr = searchRoles(filter.toString(), null);
@@ -159,10 +160,12 @@
          throw new IdentityException("Role name cannot be null");
       }
 
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
+
       try
       {
          //
-         LdapContext ctx = (LdapContext)getConnectionContext().createInitialContext().lookup(getContainerDN());
+         LdapContext ctx = (LdapContext)ldapContext.lookup(getContainerDN());
 
          //We store new entry using set of attributes. This should give more flexibility then
          //extending user object from ContextDir - configure what objectClass place there
@@ -207,6 +210,17 @@
       {
          throw new IdentityException("Failed to create role", e);
       }
+      finally
+      {
+         try
+         {
+            ldapContext.close();
+         }
+         catch (NamingException e)
+         {
+            throw new IdentityException("Failed to close LDAP connection", e);
+         }
+      }
 
       return findRoleByName(name);
    }
@@ -226,16 +240,28 @@
          throw new IdentityException("Cannot obtain DN of role");
       }
 
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
+
       try
       {
-         LdapContext ctx = (LdapContext)getConnectionContext().createInitialContext();//.lookup(getContainerDN());
          log.debug("removing entry: " + ldapr.getDn());
-         ctx.unbind(ldapr.getDn());
+         ldapContext.unbind(ldapr.getDn());
       }
       catch (Exception e)
       {
          throw new IdentityException("Failed to remove role: ", e);
       }
+      finally
+      {
+         try
+         {
+            ldapContext.close();
+         }
+         catch (NamingException e)
+         {
+            throw new IdentityException("Failed to close LDAP connection", e);
+         }
+      }
    }
 
    public int getRolesCount() throws IdentityException
@@ -300,24 +326,34 @@
     */
    public List searchRoles(String filter, Object[] filterArgs) throws NamingException, IdentityException
    {
-      SearchControls controls = new SearchControls();
-      controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
-      controls.setReturningObjFlag(true);
 
-      //String filter = getUidAttributeID().concat("=").concat(userName);
-      filter = filter.replaceAll("\\\\", "\\\\\\\\");
-      log.debug("Search filter: " + filter);
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
 
-      Enumeration results = null;
-      if (filterArgs == null)
+      try
       {
-         results = getConnectionContext().createInitialContext().search(getContainerDN(), filter, controls);
+         SearchControls controls = new SearchControls();
+         controls.setSearchScope(SearchControls.ONELEVEL_SCOPE);
+         controls.setReturningObjFlag(true);
+
+         //String filter = getUidAttributeID().concat("=").concat(userName);
+         filter = filter.replaceAll("\\\\", "\\\\\\\\");
+         log.debug("Search filter: " + filter);
+
+         Enumeration results = null;
+         if (filterArgs == null)
+         {
+            results = ldapContext.search(getContainerDN(), filter, controls);
+         }
+         else
+         {
+            results = ldapContext.search(getContainerDN(), filter, filterArgs, controls);
+         }
+         return Tools.toList(results);
       }
-      else
+      finally
       {
-         results = getConnectionContext().createInitialContext().search(getContainerDN(), filter, filterArgs, controls);
+         ldapContext.close();
       }
-      return Tools.toList(results);
    }
 
    private Map getAttributesToAdd() throws IdentityException

Modified: modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPStaticGroupMembershipModuleImpl.java
===================================================================
--- modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPStaticGroupMembershipModuleImpl.java	2007-11-21 09:51:36 UTC (rev 9054)
+++ modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPStaticGroupMembershipModuleImpl.java	2007-11-21 09:54:32 UTC (rev 9055)
@@ -39,6 +39,7 @@
 import javax.naming.directory.ModificationItem;
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
+import javax.naming.ldap.InitialLdapContext;
 import java.util.Set;
 import java.util.HashSet;
 import java.util.List;
@@ -53,8 +54,8 @@
 public class LDAPStaticGroupMembershipModuleImpl extends LDAPMembershipModule
 {
    private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LDAPStaticGroupMembershipModuleImpl.class);
-   
 
+
    public Set getRoles(User user) throws IdentityException
    {
       if (user == null)
@@ -102,7 +103,7 @@
             memberName = ldapUser.getUserName();
          }
 
-         
+
          String filter = getMemberAttributeID().concat("=").concat(memberName);
          log.debug("Search filter: " + filter);
 
@@ -113,7 +114,7 @@
          {
             SearchResult res = (SearchResult)iterator.next();
             DirContext ctx = (DirContext)res.getObject();
-            roles.add(getRoleModule().createRoleInstance(res.getAttributes(),ctx.getNameInNamespace()));   
+            roles.add(getRoleModule().createRoleInstance(res.getAttributes(),ctx.getNameInNamespace()));
          }
 
 
@@ -155,6 +156,8 @@
 
       Set users = new HashSet();
 
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
+
       try
       {
          log.debug("findUsers(): role = " + ldapRole.getDn());
@@ -165,7 +168,7 @@
          }
 
          //obtain Role entry attributes from directory
-         Attributes attrs = getConnectionContext().createInitialContext().getAttributes(ldapRole.getDn());
+         Attributes attrs = ldapContext.getAttributes(ldapRole.getDn());
 
          //log.debug("Role attributes: " + attrs);
          if (attrs == null )
@@ -208,6 +211,17 @@
       {
          throw new IdentityException("Resolving Role Users failed.", e);
       }
+      finally
+      {
+         try
+         {
+            ldapContext.close();
+         }
+         catch (NamingException e)
+         {
+            throw new IdentityException("Failed to close LDAP connection", e);
+         }
+      }
 
       return users;
 
@@ -239,6 +253,8 @@
             "require the member field to be set). ");
       }
 
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
+
       try
       {
          log.debug("findUsers(): role = " + ldapRole.getDn());
@@ -270,7 +286,7 @@
                      throw new IdentityException("Illegal state - cached user doesn't exist in identity store: ", e);
                   }
                }
-               
+
                LDAPUserImpl ldapUser = (LDAPUserImpl)user;
 
                if (isUidAttributeIsDN())
@@ -292,17 +308,28 @@
 
          if (users.size() > 0)
          {
-            getConnectionContext().createInitialContext().modifyAttributes(ldapRole.getDn(), DirContext.REPLACE_ATTRIBUTE, attrs);
+            ldapContext.modifyAttributes(ldapRole.getDn(), DirContext.REPLACE_ATTRIBUTE, attrs);
          }
          else
          {
-            getConnectionContext().createInitialContext().modifyAttributes(ldapRole.getDn(), DirContext.REMOVE_ATTRIBUTE, attrs);
+            ldapContext.modifyAttributes(ldapRole.getDn(), DirContext.REMOVE_ATTRIBUTE, attrs);
          }
       }
       catch (NamingException e)
       {
          throw new IdentityException("Failed to change Role members", e);
       }
+      finally
+      {
+         try
+         {
+            ldapContext.close();
+         }
+         catch (NamingException e)
+         {
+            throw new IdentityException("Failed to close LDAP connection", e);
+         }
+      }
 
    }
 
@@ -365,6 +392,7 @@
          memberName = ldapUser.getUserName();
       }
 
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
 
       try
       {
@@ -402,7 +430,7 @@
                   Attributes newAttrs = new BasicAttributes(true);
                   //newAttrs.put(getMemberAttributeID(), attr);
                   newAttrs.put(attr);
-                  getConnectionContext().createInitialContext().modifyAttributes(roleDN, DirContext.REPLACE_ATTRIBUTE, newAttrs);
+                  ldapContext.modifyAttributes(roleDN, DirContext.REPLACE_ATTRIBUTE, newAttrs);
                }
                else
                {
@@ -424,7 +452,7 @@
             mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
                new BasicAttribute(getMemberAttributeID(), memberName));
             // Perform the requested modifications on the named object
-            getConnectionContext().createInitialContext().modifyAttributes(roleDN, mods);
+            ldapContext.modifyAttributes(roleDN, mods);
          }
 
          //and that should be all...
@@ -433,8 +461,20 @@
       {
          e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
       }
+      finally
+      {
+         try
+         {
+            ldapContext.close();
+         }
+         catch (NamingException e)
+         {
+            throw new IdentityException("Failed to close LDAP connection", e);
+         }
+      }
 
 
+
    }
 
    public Set findRoleMembers(String roleName, int offset, int limit, String userNameFilter) throws IdentityException
@@ -477,5 +517,5 @@
    }
 
 
-   
+
 }

Modified: modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPStaticRoleMembershipModuleImpl.java
===================================================================
--- modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPStaticRoleMembershipModuleImpl.java	2007-11-21 09:51:36 UTC (rev 9054)
+++ modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPStaticRoleMembershipModuleImpl.java	2007-11-21 09:54:32 UTC (rev 9055)
@@ -38,6 +38,7 @@
 import javax.naming.directory.ModificationItem;
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
+import javax.naming.ldap.InitialLdapContext;
 import java.util.Set;
 import java.util.HashSet;
 import java.util.List;
@@ -87,6 +88,8 @@
 
       Set roles = new HashSet();
 
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
+
       try
       {
          log.debug("findRoles(): role = " + ldapUser.getDn());
@@ -97,7 +100,7 @@
          }
 
          //obtain Role entry attributes from directory
-         Attributes attrs = getConnectionContext().createInitialContext().getAttributes(ldapUser.getDn());
+         Attributes attrs = ldapContext.getAttributes(ldapUser.getDn());
 
          //log.debug("User attributes: " + attrs);
          if (attrs == null )
@@ -143,6 +146,17 @@
       {
          throw new IdentityException("Resolving User Roles failed.", e);
       }
+      finally
+      {
+         try
+         {
+            ldapContext.close();
+         }
+         catch (NamingException e)
+         {
+            throw new IdentityException("Failed to close LDAP connection", e);
+         }
+      }
 
       return roles;
 
@@ -286,6 +300,7 @@
          memberOfName = ldapRole.getName();
       }
 
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
 
       try
       {
@@ -318,7 +333,7 @@
                //newAttrs.put(getMemberAttributeID(), attr);
                newAttrs.put(attr);
 
-               getConnectionContext().createInitialContext().modifyAttributes(userDN, DirContext.REPLACE_ATTRIBUTE, newAttrs);
+               ldapContext.modifyAttributes(userDN, DirContext.REPLACE_ATTRIBUTE, newAttrs);
 
                //and mark this role as done
                userDNsToAdd.remove(userDN);
@@ -335,7 +350,7 @@
             mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,
                new BasicAttribute(getMemberAttributeID(), memberOfName));
             // Perform the requested modifications on the named object
-            getConnectionContext().createInitialContext().modifyAttributes(userDN, mods);
+            ldapContext.modifyAttributes(userDN, mods);
          }
 
          //and that should be all...
@@ -344,6 +359,18 @@
       {
          throw new IdentityException("Failed to assign users", e);
       }
+      finally
+      {
+         try
+         {
+            ldapContext.close();
+         }
+         catch (NamingException e)
+         {
+            throw new IdentityException("Failed to close LDAP connection", e);
+         }
+      }
+
    }
 
    public void assignRoles(User user, Set roles) throws IdentityException
@@ -376,6 +403,8 @@
          throw new IllegalArgumentException("UserMembershipModuleImpl supports only LDAPUserImpl objects");
       }
 
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
+
       try
       {
          log.debug("findRoles(): user = " + ldapUser.getDn());
@@ -410,12 +439,23 @@
          }
          attrs.put(member);
 
-         getConnectionContext().createInitialContext().modifyAttributes(ldapUser.getDn(), DirContext.REPLACE_ATTRIBUTE, attrs);
+         ldapContext.modifyAttributes(ldapUser.getDn(), DirContext.REPLACE_ATTRIBUTE, attrs);
       }
       catch (NamingException e)
       {
          throw new IdentityException("Failed to change Role members", e);
       }
+      finally
+      {
+         try
+         {
+            ldapContext.close();
+         }
+         catch (NamingException e)
+         {
+            throw new IdentityException("Failed to close LDAP connection", e);
+         }
+      }
 
    }
 

Modified: modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPUserModule.java
===================================================================
--- modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPUserModule.java	2007-11-21 09:51:36 UTC (rev 9054)
+++ modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPUserModule.java	2007-11-21 09:54:32 UTC (rev 9055)
@@ -84,6 +84,8 @@
    {
       String attributeName = getPasswordAttributeId();
 
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
+
       try
       {
          //TODO: maybe perform a schema check if this attribute is allowed for such entry
@@ -93,12 +95,23 @@
          attr.add(password);
          attrs.put(attr);
 
-         getConnectionContext().createInitialContext().modifyAttributes(ldapu.getDn(), DirContext.REPLACE_ATTRIBUTE,attrs);
+         ldapContext.modifyAttributes(ldapu.getDn(), DirContext.REPLACE_ATTRIBUTE,attrs);
       }
       catch (NamingException e)
       {
          throw new IdentityException("Cannot set user password value.", e);
       }
+      finally
+      {
+         try
+         {
+            ldapContext.close();
+         }
+         catch (NamingException e)
+         {
+            throw new IdentityException("Failed to close LDAP connection", e);
+         }
+      }
 
    }
 
@@ -160,7 +173,7 @@
 
          //ldapu = new LDAPUserImpl(dn,getIdentityContext(), uida.get().toString());
 
-         //make DN as user ID 
+         //make DN as user ID
          ldapu = new LDAPUserImpl(dn,getIdentityContext(), dn);
          ldapu.setUserName(uida.get().toString());
 
@@ -183,6 +196,8 @@
     */
    public User findUserByDN(String dn) throws IdentityException, IllegalArgumentException, NoSuchUserException
    {
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
+
       try
       {
          log.debug("findUserByDN(): DN = " + dn);
@@ -192,7 +207,7 @@
             throw new IdentityException("User dn canot be null");
          }
 
-         Attributes attrs = getConnectionContext().createInitialContext().getAttributes(dn);
+         Attributes attrs = ldapContext.getAttributes(dn);
 
          if (attrs == null)
          {
@@ -210,6 +225,17 @@
       {
          throw new IdentityException("User search failed.", e);
       }
+      finally
+      {
+         try
+         {
+            ldapContext.close();
+         }
+         catch (NamingException e)
+         {
+            throw new IdentityException("Failed to close LDAP connection", e);
+         }
+      }
       return null;
 
    }
@@ -223,8 +249,8 @@
     * @return
     */
    public abstract List searchUsers(String filter, Object[] filterArgs) throws NamingException, IdentityException;
-   
 
+
    //**************************
    //*** Getter and Setters
    //**************************
@@ -384,5 +410,5 @@
    {
       this.connectionContext = connectionContext;
    }
-   
+
 }

Modified: modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPUserModuleImpl.java
===================================================================
--- modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPUserModuleImpl.java	2007-11-21 09:51:36 UTC (rev 9054)
+++ modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPUserModuleImpl.java	2007-11-21 09:54:32 UTC (rev 9055)
@@ -37,6 +37,7 @@
 import javax.naming.directory.SearchControls;
 import javax.naming.directory.SearchResult;
 import javax.naming.ldap.LdapContext;
+import javax.naming.ldap.InitialLdapContext;
 import java.util.List;
 import java.util.NoSuchElementException;
 import java.util.Set;
@@ -58,7 +59,7 @@
  * @author <a href="mailto:boleslaw.dawidowicz at jboss.org">Boleslaw Dawidowicz</a>
  * @version $Revision: 1.1 $
  */
-public class LDAPUserModuleImpl extends LDAPUserModule 
+public class LDAPUserModuleImpl extends LDAPUserModule
 {
    private static final org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(LDAPUserModuleImpl.class);
 
@@ -121,9 +122,9 @@
       return findUserByDN(id);
    }
 
-   
 
-   
+
+
    public User createUser(String userName, String password) throws IdentityException, IllegalArgumentException
    {
 
@@ -143,10 +144,12 @@
       log.debug("Creating user: " + userName);
 
 
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
+
       try
       {
          //
-         LdapContext ctx = (LdapContext)getConnectionContext().createInitialContext().lookup(getContainerDN());
+         LdapContext ctx = (LdapContext)ldapContext.lookup(getContainerDN());
 
          //We store new entry using set of attributes. This should give more flexibility then
          //extending user object from ContextDir - configure what objectClass place there
@@ -187,6 +190,17 @@
       {
          throw new IdentityException("Failed to create user", e);
       }
+      finally
+      {
+         try
+         {
+            ldapContext.close();
+         }
+         catch (NamingException e)
+         {
+            throw new IdentityException("Failed to close LDAP connection", e);
+         }
+      }
       User u =  findUserByUserName(userName);
 
 
@@ -212,17 +226,30 @@
          throw new IdentityException("Cannot obtain DN of user");
       }
 
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
+
       try
       {
-         LdapContext ctx = getConnectionContext().createInitialContext();//.lookup(getContainerDN());
          log.debug("removing entry: " + ldapu.getDn());
-         ctx.unbind(ldapu.getDn());
+         ldapContext.unbind(ldapu.getDn());
       }
       catch (Exception e)
       {
          throw new IdentityException("Failed to remove user: ", e);
       }
+      finally
+      {
+         try
+         {
+            ldapContext.close();
+         }
+         catch (NamingException e)
+         {
+            throw new IdentityException("Failed to close LDAP connection", e);
+         }
+      }
 
+
       //user was successfull removed so fire events
       fireUserDestroyedEvent(id, userName);
    }
@@ -349,16 +376,25 @@
 
       log.debug("Search filter: " + filter);
 
-      Enumeration results = null;
-      if (filterArgs == null)
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
+
+      try
       {
-         results = getConnectionContext().createInitialContext().search(getContainerDN(), filter, controls);
-         return Tools.toList(results);
+         Enumeration results = null;
+         if (filterArgs == null)
+         {
+            results = ldapContext.search(getContainerDN(), filter, controls);
+            return Tools.toList(results);
+         }
+         else
+         {
+            results = ldapContext.search(getContainerDN(), filter, filterArgs, controls);
+            return Tools.toList(results);
+         }
       }
-      else
+      finally
       {
-         results = getConnectionContext().createInitialContext().search(getContainerDN(), filter, filterArgs, controls);
-         return Tools.toList(results);
+         ldapContext.close();
       }
    }
 

Modified: modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPUserProfileModuleImpl.java
===================================================================
--- modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPUserProfileModuleImpl.java	2007-11-21 09:51:36 UTC (rev 9054)
+++ modules/identity/trunk/identity/src/main/java/org/jboss/portal/identity/ldap/LDAPUserProfileModuleImpl.java	2007-11-21 09:54:32 UTC (rev 9055)
@@ -37,6 +37,7 @@
 import javax.naming.directory.BasicAttribute;
 import javax.naming.directory.DirContext;
 import javax.naming.NamingException;
+import javax.naming.ldap.InitialLdapContext;
 import java.util.Set;
 import java.util.Map;
 import java.util.HashMap;
@@ -97,9 +98,11 @@
          return null;
       }
 
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
+
       try
       {
-         Attributes attrs = getConnectionContext().createInitialContext().getAttributes(ldapUser.getDn());
+         Attributes attrs = ldapContext.getAttributes(ldapUser.getDn());
 
          Attribute attr = attrs.get(attributeName);
 
@@ -116,6 +119,17 @@
       {
          throw new IdentityException("Cannot get user property value.", e);
       }
+      finally
+      {
+         try
+         {
+            ldapContext.close();
+         }
+         catch (NamingException e)
+         {
+            throw new IdentityException("Failed to close LDAP connection", e);
+         }
+      }
 
       PropertyInfo pi = getProfileInfo().getPropertyInfo(propertyName);
 
@@ -188,6 +202,8 @@
          return;
       }
 
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
+
       try
       {
          //TODO: maybe perform a schema check if this attribute is allowed for such entry
@@ -197,12 +213,23 @@
          attr.add(property);
          attrs.put(attr);
 
-         getConnectionContext().createInitialContext().modifyAttributes(ldapUser.getDn(), DirContext.REPLACE_ATTRIBUTE,attrs);
+         ldapContext.modifyAttributes(ldapUser.getDn(), DirContext.REPLACE_ATTRIBUTE,attrs);
       }
       catch (NamingException e)
       {
          throw new IdentityException("Cannot set user property value.", e);
       }
+      finally
+      {
+         try
+         {
+            ldapContext.close();
+         }
+         catch (NamingException e)
+         {
+            throw new IdentityException("Failed to close LDAP connection", e);
+         }
+      }
 
    }
 
@@ -238,13 +265,15 @@
 
       Map propertyMap = new HashMap();
 
+      InitialLdapContext ldapContext = getConnectionContext().createInitialContext();
+
       try
       {
          Map mappings = resolveAttributesMappingMap();
 
          Set props = mappings.keySet();
 
-         Attributes attrs = getConnectionContext().createInitialContext().getAttributes(ldapUser.getDn());
+         Attributes attrs = ldapContext.getAttributes(ldapUser.getDn());
 
          for (Iterator iterator = props.iterator(); iterator.hasNext();)
          {
@@ -272,6 +301,17 @@
       {
          throw new IdentityException("Cannot get user property value.", e);
       }
+      finally
+      {
+         try
+         {
+            ldapContext.close();
+         }
+         catch (NamingException e)
+         {
+            throw new IdentityException("Failed to close LDAP connection", e);
+         }
+      }
 
       return Collections.unmodifiableMap(propertyMap);
    }
@@ -296,7 +336,7 @@
    }
 
    /**
-    * Returns a map of mappings - property name/attribute name. 
+    * Returns a map of mappings - property name/attribute name.
     * @return
     * @throws IdentityException
     */

Modified: modules/identity/trunk/identity/src/test/resources/config/standardidentity-config.xml
===================================================================
--- modules/identity/trunk/identity/src/test/resources/config/standardidentity-config.xml	2007-11-21 09:51:36 UTC (rev 9054)
+++ modules/identity/trunk/identity/src/test/resources/config/standardidentity-config.xml	2007-11-21 09:54:32 UTC (rev 9055)
@@ -59,6 +59,18 @@
                <name>JNDIName</name>
                <value>java:/portal/LDAPConnectionContext</value>
             </option>
+            <option>
+               <name>pooling</name>
+               <value>true</value>
+            </option>
+            <option>
+               <name>poolingProtocol</name>
+               <value>plain ssl</value>
+            </option>
+            <option>
+               <name>poolingTimeout</name>
+               <value>300000</value>
+            </option>
          </config>
       </datasource>
    </datasources>




More information about the portal-commits mailing list