[Carbon-commits] [Carbon Platform] svn commit r47444 - branches/carbon-platform/2.0.2/carbon/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/hybrid/actdir

dimuthul at wso2.com dimuthul at wso2.com
Mon Oct 19 19:52:24 PDT 2009


Author: dimuthul
Date: Mon Oct 19 19:52:24 2009
New Revision: 47444
URL: http://wso2.org/svn/browse/wso2?view=rev&revision=47444

Log:
Closing the connection.



Modified:
   branches/carbon-platform/2.0.2/carbon/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/hybrid/actdir/ActDirUtil.java

Modified: branches/carbon-platform/2.0.2/carbon/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/hybrid/actdir/ActDirUtil.java
URL: http://wso2.org/svn/browse/wso2/branches/carbon-platform/2.0.2/carbon/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/hybrid/actdir/ActDirUtil.java?rev=47444&r1=47443&r2=47444&view=diff
==============================================================================
--- branches/carbon-platform/2.0.2/carbon/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/hybrid/actdir/ActDirUtil.java	(original)
+++ branches/carbon-platform/2.0.2/carbon/org.wso2.carbon.user.core/src/main/java/org/wso2/carbon/user/core/hybrid/actdir/ActDirUtil.java	Mon Oct 19 19:52:24 2009
@@ -2,64 +2,88 @@
 
 import javax.naming.NamingEnumeration;
 import javax.naming.NamingException;
+import javax.naming.directory.DirContext;
 import javax.naming.directory.SearchControls;
 import javax.naming.directory.SearchResult;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.wso2.carbon.user.core.UserStoreException;
+import org.wso2.carbon.user.core.util.JNDIUtil;
 
 public class ActDirUtil {
 
     private static Log log = LogFactory.getLog(ActDirUtil.class);
-    
-    public static String getUserCN(ActDirRealmConfig config, String loginName) throws NamingException{
+
+    public static String getUserCN(ActDirRealmConfig config, String loginName)
+            throws UserStoreException {
         String name = null;
         SearchControls searchCtls = new SearchControls();
         searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
 
-        String searchFilter = "(&(objectClass=user)(sAMAccountName="+loginName+"))";
+        String searchFilter = "(&(objectClass=user)(sAMAccountName=" + loginName + "))";
         String searchBase = config.getSearchBase();
-                
-        NamingEnumeration answer = config.getContext().search(searchBase, searchFilter,
-                searchCtls);
-        int count = 0;
-        SearchResult userObj = null;
-        while (answer.hasMoreElements()) {
-            SearchResult sr = (SearchResult) answer.next();
-            if(count>0){
-                log.error("More than one user exist for the same name");
+
+        DirContext dirContext = null;
+
+        try {
+            dirContext = config.getContext();
+            NamingEnumeration answer = dirContext.search(searchBase, searchFilter, searchCtls);
+
+            int count = 0;
+            SearchResult userObj = null;
+            while (answer.hasMoreElements()) {
+                SearchResult sr = (SearchResult) answer.next();
+                if (count > 0) {
+                    log.error("More than one user exist for the same name");
+                }
+                count++;
+                userObj = sr;
             }
-            count++;
-            userObj = sr;
-        }
-        if(userObj != null){
-            name = userObj.getNameInNamespace();
+            if (userObj != null) {
+                name = userObj.getNameInNamespace();
+            }
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+            throw new UserStoreException(e.getMessage(), e);
+        } finally {
+            JNDIUtil.closeContext(dirContext);
         }
+
         return name;
     }
-    
-    public static String getUserLoginName(ActDirRealmConfig config, String CN) throws NamingException{
+
+    public static String getUserLoginName(ActDirRealmConfig config, String CN)
+            throws UserStoreException {
         String name = null;
         SearchControls searchCtls = new SearchControls();
         searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
 
-        String searchFilter = "(&(objectClass=user)(CN="+CN+"))";
+        String searchFilter = "(&(objectClass=user)(CN=" + CN + "))";
         String searchBase = config.getSearchBase();
         String returnedAtts[] = { "sAMAccountName" };
         searchCtls.setReturningAttributes(returnedAtts);
-        NamingEnumeration answer = config.getContext().search(searchBase, searchFilter,
-                searchCtls);
-        int count = 0;
-        SearchResult userObj = null;
-        while (answer.hasMoreElements()) {
-            SearchResult sr = (SearchResult) answer.next();
-            if(count>0){
-                log.error("More than one user exist for the same name");
+        DirContext dirContext = null;
+        try {
+            dirContext = config.getContext();
+            NamingEnumeration answer = dirContext.search(searchBase, searchFilter, searchCtls);
+            int count = 0;
+            SearchResult userObj = null;
+            while (answer.hasMoreElements()) {
+                SearchResult sr = (SearchResult) answer.next();
+                if (count > 0) {
+                    log.error("More than one user exist for the same name");
+                }
+                count++;
+                name = (String) sr.getAttributes().get("sAMAccountName").get();
             }
-            count++;
-            name = (String)sr.getAttributes().get("sAMAccountName").get();
+        } catch (Exception e) {
+            log.error(e.getMessage(), e);
+            throw new UserStoreException(e.getMessage(), e);
+        } finally {
+            JNDIUtil.closeContext(dirContext);
         }
-        
+
         return name;
     }
 }



More information about the Carbon-commits mailing list