Added code to re-establish JNDI connections when they are lost. This fixes a problem...
authorwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Sun, 24 Nov 2002 22:21:57 +0000 (22:21 +0000)
committerwassa <wassa@ab3bd59b-922f-494d-bb5f-6f0a3c29deca>
Sun, 24 Nov 2002 22:21:57 +0000 (22:21 +0000)
git-svn-id: https://subversion.switch.ch/svn/shibboleth/java-idp/trunk@357 ab3bd59b-922f-494d-bb5f-6f0a3c29deca

src/edu/internet2/middleware/shibboleth/aa/AAResponder.java

index af68450..81a60d4 100755 (executable)
@@ -58,19 +58,24 @@ package edu.internet2.middleware.shibboleth.aa;
  */
 
 
-import java.io.*;
-import java.util.*;
-import java.lang.reflect.*;
-import javax.servlet.*;
-import javax.servlet.http.*;
-import javax.naming.*;
-import javax.naming.directory.*;
-import edu.internet2.middleware.shibboleth.*;
-import edu.internet2.middleware.shibboleth.hs.*;
-import edu.internet2.middleware.eduPerson.*;
-import org.w3c.dom.*;
-import org.opensaml.*;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+import java.util.Vector;
+
+import javax.naming.CommunicationException;
+import javax.naming.NameNotFoundException;
+import javax.naming.NamingEnumeration;
+import javax.naming.NamingException;
+import javax.naming.directory.Attribute;
+import javax.naming.directory.DirContext;
+import javax.naming.directory.InitialDirContext;
+import javax.naming.directory.SearchControls;
+import javax.naming.directory.SearchResult;
+
 import org.apache.log4j.Logger;
+import org.opensaml.SAMLAttribute;
+import org.opensaml.SAMLException;
 
 public class AAResponder{
 
@@ -110,49 +115,15 @@ public class AAResponder{
                DirContext userCtx = null;
 
                try {
-                       if (searchFilter == null)
-                               searchFilter = "";
-                       int indx = searchFilter.indexOf("%s");
-                       if (indx < 0) {
-                               try {
-                                       userCtx = (DirContext) ctx.lookup(searchFilter + userName);
-                               } catch (NameNotFoundException nnfe) {
-                                       log.error(
-                                               "Could not locate a user ("
-                                                       + userName
-                                                       + ") as a result of searching with ("
-                                                       + searchFilter
-                                                       + ").");
-                                       throw new AAException("No data available for this principal.");
-                               }
-                       } else {
-                               /* This is a search filter. Search after replacing %s with uid*/
-                               StringBuffer tmp = new StringBuffer(searchFilter);
-                               tmp.delete(indx, indx + 2);
-                               tmp.insert(indx, userName);
-                               searchFilter = tmp.toString();
-                               SearchControls ctls = new SearchControls();
-                               ctls.setReturningObjFlag(true);
-                               NamingEnumeration en = ctx.search("", searchFilter, ctls);
-                               if (!en.hasMore()) {
-                                       log.error(
-                                               "Could not locate a user ("
-                                                       + userName
-                                                       + ") as a result of searching with ("
-                                                       + searchFilter
-                                                       + ").");
-                                       throw new AAException("No data available for this principal.");
-                               }
-                               userCtx = (DirContext) ((SearchResult) en.next()).getObject();
-                               if (en.hasMore()) {
-                                       log.error(
-                                               "Located multiple ("
-                                                       + userName
-                                                       + ") users as a result of searching with ("
-                                                       + searchFilter
-                                                       + ").");
-                                       throw new AAException("Cannot disambiguate data for this principal.");
+                       try {
+                               userCtx = queryDataSource(userName, searchFilter);
+                       } catch (CommunicationException ce) {
+                               synchronized (ctx) {
+                                       log.debug(ce);
+                                       log.warn("Encountered a connection problem while querying for attributes.  Re-initializing JNDI context and retrying...");
+                                       ctx = new InitialDirContext(ctx.getEnvironment());
                                }
+                               userCtx = queryDataSource(userName, searchFilter);
                        }
                } catch (NamingException e) {
                        log.error(
@@ -185,6 +156,59 @@ public class AAResponder{
        }
 
 
+       private DirContext queryDataSource(String userName, String searchFilter)
+               throws CommunicationException, NamingException, AAException {
+                       
+               DirContext userCtx = null;
+               if (searchFilter == null) {
+                       searchFilter = "";
+               }
+               int indx = searchFilter.indexOf("%s");
+               if (indx < 0) {
+                       try {
+                               userCtx = (DirContext) ctx.lookup(searchFilter + userName);
+                       } catch (NameNotFoundException nnfe) {
+                               log.error(
+                                       "Could not locate a user ("
+                                               + userName
+                                               + ") as a result of searching with ("
+                                               + searchFilter
+                                               + ").");
+                               throw new AAException("No data available for this principal.");
+                       }
+               } else {
+                       /* This is a search filter. Search after replacing %s with uid*/
+                       StringBuffer tmp = new StringBuffer(searchFilter);
+                       tmp.delete(indx, indx + 2);
+                       tmp.insert(indx, userName);
+                       searchFilter = tmp.toString();
+                       SearchControls ctls = new SearchControls();
+                       ctls.setReturningObjFlag(true);
+                       NamingEnumeration en = ctx.search("", searchFilter, ctls);
+                       if (!en.hasMore()) {
+                               log.error(
+                                       "Could not locate a user ("
+                                               + userName
+                                               + ") as a result of searching with ("
+                                               + searchFilter
+                                               + ").");
+                               throw new AAException("No data available for this principal.");
+                       }
+                       userCtx = (DirContext) ((SearchResult) en.next()).getObject();
+                       if (en.hasMore()) {
+                               log.error(
+                                       "Located multiple ("
+                                               + userName
+                                               + ") users as a result of searching with ("
+                                               + searchFilter
+                                               + ").");
+                               throw new AAException("Cannot disambiguate data for this principal.");
+                       }
+               }
+               return userCtx;
+       }
+
+
        private Set getCombinedReleaseSet(Arp admin, String sharName, String url, String userName)
                throws AAException {