Index: src/edu/ucsb/nceas/metacat/AuthLdap.java
===================================================================
--- src/edu/ucsb/nceas/metacat/AuthLdap.java	(revision 979)
+++ src/edu/ucsb/nceas/metacat/AuthLdap.java	(revision 980)
@@ -66,7 +66,7 @@
   private String ldapsUrl;
   private String ldapBase;
   private String referral;
-  private Context referralContext;
+  private DirContext referralContext;
   Hashtable env = new Hashtable(11);
   private Context rContext;
   private String userName;
@@ -466,7 +466,7 @@
   public String[] getGroups(String user, String password) 
          throws ConnectException
   {
-      return getGroups(user, password, null);
+      return getGroups(user, password, user);
   }
 
   /**
@@ -500,7 +500,6 @@
         SearchControls ctls = new SearchControls();
         ctls.setReturningAttributes(attrIDs);
         ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
-        
         String filter = null;
         String gfilter = "(objectClass=groupOfUniqueNames)";
         if (null == foruser) {
@@ -508,15 +507,88 @@
         } else {
             filter = "(& " + gfilter + "(uniqueMember=" + foruser + "))";
         }
-        NamingEnumeration enum = ctx.search(ldapBase, filter, ctls);
-
-        // Print the groups
+        
         Vector uvec = new Vector();
-        while (enum.hasMore()) {
-          SearchResult sr = (SearchResult)enum.next();
-          uvec.add(sr.getName()+","+ldapBase);
+        
+        try
+        {
+          NamingEnumeration enum = ctx.search(ldapBase, filter, ctls);
+          // Print the groups
+          while (enum.hasMore()) {
+            SearchResult sr = (SearchResult)enum.next();
+            uvec.addElement(sr.getName()+","+ldapBase);
+            //System.out.println("result: " + sr.getName() + "," + ldapBase);
+          }
         }
-      
+        catch(ReferralException re)
+        {
+          boolean moreReferrals = true;
+          while(moreReferrals)
+          {
+            //System.out.println("Referral: " + re.toString());
+            refExc = re;
+            Thread t = new Thread(this);
+            //System.out.println("Starting thread...");
+            t.start();
+            //System.out.println("sleeping for 7 seconds.");
+            try
+            {
+              Thread.sleep(5000);
+            }
+            catch(java.lang.InterruptedException iee)
+            {
+              //System.out.println("Main Program Sleep Interrupted");
+            }
+            //this is a manual override of ldap's hideously long time
+            //out period.
+            //System.out.println("Awake after 5 seconds.");
+            if (referralContext == null)
+            {
+              t.interrupt();
+              //System.out.println("!!!!!!!!thread interrupted!!!!!!!!!");
+              moreReferrals = false;
+            }
+            else
+            {
+              try
+              {
+                //System.out.println("searching...");
+                
+                NamingEnumeration enum = referralContext.search(ldapBase, filter, ctls);
+                //System.out.println("searching complete.");
+                while (enum.hasMore() && enum != null) {
+                  SearchResult sr = (SearchResult)enum.next();
+                  uvec.addElement(sr.getName()+","+ldapBase);
+                  //System.out.println("result: " + sr.getName() + "," + ldapBase);
+                }
+                moreReferrals = false;
+              }
+              catch (ReferralException re2)
+              {
+                moreReferrals=true;
+                refExc=re2;
+              }
+              catch (AuthenticationException ae)
+              {
+                util.debugMessage("Error running referral handler thread: " + 
+                                  ae.getMessage());
+                //check if has another referral
+                moreReferrals=refExc.skipReferral();
+                //don't get the context
+                referralContext = null;
+              }
+              catch (NamingException ne)
+              {
+                util.debugMessage("Error running referral handler thread: " + 
+                                  ne.getMessage());
+                //check if has another referral
+                moreReferrals=refExc.skipReferral();
+                //don't get context
+                referralContext = null;		
+              }
+            }
+          }
+        }
 
         // initialize groups[] and fill it
         groups = new String[uvec.size()];
@@ -526,62 +598,9 @@
 
         // Close the context when we're done
         ctx.close();
-
-    } catch (ReferralException re) {
-      try
-      {
-        refExc = re;
-        Thread t = new Thread(this);
-        util.debugMessage("Starting thread...");
-        t.start();
-        util.debugMessage("sleeping for 5 seconds.");
-        Thread.sleep(5000); 
-        //this is a manual override of ldap's hideously long time
-        //out period.
-        util.debugMessage("Awake after 5 seconds.");
-        if (referralContext == null)
-        {
-          t.interrupt();
-          return null;
-        }
-        DirContext dc = (DirContext)referralContext;
-        String[] attrIDs = {"cn"};
-        // Specify the attributes to match.
-        // Groups are objects with attribute objectclass=groupofuniquenames.
-        // and have attribute uniquemember: uid=foruser,ldapbase.
-        SearchControls ctls = new SearchControls();
-        ctls.setReturningAttributes(attrIDs);
-        ctls.setSearchScope(SearchControls.SUBTREE_SCOPE);
-        
-        String filter = null;
-        String gfilter = "(objectClass=groupOfUniqueNames)";
-        if (null == foruser) {
-            filter = gfilter;
-        } else {
-            filter = "(& " + gfilter + "(uniqueMember=" + foruser + "))";
-        }
-        NamingEnumeration enum = dc.search(ldapBase, filter, ctls);
-
-        // Print the groups
-        Vector uvec = new Vector();
-        while (enum.hasMore()) {
-          SearchResult sr = (SearchResult)enum.next();
-          uvec.add(sr.getName()+","+ldapBase);
-        }
-
-        // initialize groups[] and fill it
-        groups = new String[uvec.size()];
-        for (int i=0; i < uvec.size(); i++) {
-          groups[i] = (String)uvec.elementAt(i); 
-        }
-        referralContext.close();
-        dc.close();
-      }
-      catch (Exception e)
-      {
-        return groups;
-      }
-    } catch (NamingException e) {
+    }  
+    catch (NamingException e) 
+    {
       e.printStackTrace(System.err);
       throw new ConnectException(
       "Problem getting groups for a user in AuthLdap.getGroups:" + e);
@@ -817,6 +836,10 @@
     // Provide a user, such as: "Matt Jones", or "jones"
     String user = args[0];
     String password = args[1];
+    String foruser = null;
+    
+    if(args.length == 3)
+      foruser = args[2];
 
     AuthLdap authservice = new AuthLdap();
 
@@ -823,17 +846,26 @@
                 
     boolean isValid = false;
     try {
+      System.out.println("authenticating user " + user);
       isValid = authservice.authenticate(user, password);
       if (isValid) {
-        MetaCatUtil.debugMessage("Authentication successful for: " + user );
+        System.out.println("Authentication successful for: " + user );
       } else {
-        MetaCatUtil.debugMessage("Authentication failed for: " + user);
+        System.out.println("Authentication failed for: " + user);
       }
 
       // Get attributes for the user
       if (isValid) {
-        MetaCatUtil.debugMessage("\nGetting attributes for user....");
-        HashMap userInfo = authservice.getAttributes(user, password, user);
+        System.out.println("\nGetting attributes for user....");
+        HashMap userInfo;
+        if(foruser == null)
+        {
+          userInfo = authservice.getAttributes(user, password, user);
+        }
+        else
+        {
+          userInfo = authservice.getAttributes(user, password, foruser);
+        }
         // Print all of the attributes
         Iterator attList = (Iterator)(((Set)userInfo.keySet()).iterator());
         while (attList.hasNext()) {
@@ -842,7 +874,7 @@
           Iterator attvalues = values.iterator();
           while (attvalues.hasNext()) {
             String value = (String)attvalues.next();
-            MetaCatUtil.debugMessage(att + ": " + value);
+            System.out.println(att + ": " + value);
           }
         }
       }
@@ -849,11 +881,19 @@
 
       // get the groups
       if (isValid) {
-        MetaCatUtil.debugMessage("\nGetting all groups....");
-        String[] groups = authservice.getGroups(user, password);
-        MetaCatUtil.debugMessage("Groups found: " + groups.length);
+        System.out.println("\nGetting all groups....");
+        String[] groups;
+        if(foruser == null)
+        {
+          groups = authservice.getGroups(user, password);
+        }
+        else
+        {
+          groups = authservice.getGroups(user, password, foruser);
+        }
+        System.out.println("Groups found: " + groups.length);
         for (int i=0; i < groups.length; i++) {
-            MetaCatUtil.debugMessage("Group " + i + ": " + groups[i]);
+            System.out.println("Group " + i + ": " + groups[i]);
         }
       }
 
@@ -860,11 +900,19 @@
       // get the groups for the user
       String savedGroup = null;
       if (isValid) {
-        MetaCatUtil.debugMessage("\nGetting groups for user....");
-        String[] groups = authservice.getGroups(user, password, user);
-        MetaCatUtil.debugMessage("Groups found: " + groups.length);
+        System.out.println("\nGetting groups for user....");
+        String[] groups;
+        if(foruser == null)
+        {
+          groups = authservice.getGroups(user, password, user);
+        }
+        else
+        {
+          groups = authservice.getGroups(user, password, foruser);
+        }
+        System.out.println("Groups found: " + groups.length);
         for (int i=0; i < groups.length; i++) {
-            MetaCatUtil.debugMessage("Group " + i + ": " + groups[i]);
+            System.out.println("Group " + i + ": " + groups[i]);
             savedGroup = groups[i];
         }
       }
@@ -871,26 +919,26 @@
 
       // get the users for a group
       if (isValid) {
-        MetaCatUtil.debugMessage("\nGetting users for group....");
-        MetaCatUtil.debugMessage("Group: " + savedGroup);
+        System.out.println("\nGetting users for group....");
+        System.out.println("Group: " + savedGroup);
         String[] users = authservice.getUsers(user, password, savedGroup);
-        MetaCatUtil.debugMessage("Users found: " + users.length);
+        System.out.println("Users found: " + users.length);
         for (int i=0; i < users.length; i++) {
-            MetaCatUtil.debugMessage("User " + i + ": " + users[i]);
+            System.out.println("User " + i + ": " + users[i]);
         }
       }
 
       // get all users
       if (isValid) {
-        MetaCatUtil.debugMessage("\nGetting all users ....");
+        System.out.println("\nGetting all users ....");
         String[] users = authservice.getUsers(user, password);
-        MetaCatUtil.debugMessage("Users found: " + users.length);
+        System.out.println("Users found: " + users.length);
         
       }
 
       // get the whole list groups and users in XML format
       if (isValid) {
-        MetaCatUtil.debugMessage("\nTrying principals....");
+        System.out.println("\nTrying principals....");
         authservice = new AuthLdap();
         String out = authservice.getPrincipals(user, password);
         java.io.File f = new java.io.File("principals.xml");
@@ -900,13 +948,13 @@
         buff.flush();
         buff.close();
         fw.close();
-        MetaCatUtil.debugMessage("\nFinished getting principals.");
+        System.out.println("\nFinished getting principals.");
       }
 
     } catch (ConnectException ce) {
-      MetaCatUtil.debugMessage(ce.getMessage());
+      System.out.println(ce.getMessage());
     } catch (java.io.IOException ioe) {
-      MetaCatUtil.debugMessage("I/O Error writing to file principals.txt");
+      System.out.println("I/O Error writing to file principals.txt");
     }
   }
   
@@ -926,23 +974,44 @@
       try
       {
         //revise environment variable
-        env.put(Context.PROVIDER_URL, refExc.getReferralInfo());
+        String refInfo = null;
+        refInfo = (String)refExc.getReferralInfo();
+        //refInfo = (String)refExc.getReferralContext().getEnvironment().get(Context.PROVIDER_URL);
+        //System.out.println("refInfo: " + refInfo);
+        if(refInfo != null)
+        {
+          //System.out.println("Referral in thread to: " + 
+          //                  refInfo.toString());
+        }
+        else
+        {
+          refInfo = (String)refExc.getReferralContext().getEnvironment().get(Context.PROVIDER_URL);
+        }
+        
+        /*env.put(Context.PROVIDER_URL, refExc.getReferralInfo());
         env.put(Context.INITIAL_CONTEXT_FACTORY, 
                 "com.sun.jndi.ldap.LdapCtxFactory");
         env.put(Context.SECURITY_PRINCIPAL, userName);
         env.put(Context.SECURITY_CREDENTIALS, userPassword);
+        env.put(Context.REFERRAL, "throw");*/
+        
+        //get a context object for referral in the new envriment
+        //rContext = refExc.getReferralContext();
+        
+        env.put(Context.INITIAL_CONTEXT_FACTORY, 
+            "com.sun.jndi.ldap.LdapCtxFactory");
         env.put(Context.REFERRAL, "throw");
-        //get a context object for referral in the new envriment
-        rContext = refExc.getReferralContext(env);
+        env.put(Context.PROVIDER_URL, refInfo);
+        
+        referralContext = new InitialDirContext(env);
         //casting the context to dircontext and it will create a
         //autherntication or naming exception if DN and password is incorrect
-        referralContext=rContext;
-        refDirContext=(DirContext)rContext;
-        refDirContext.close();
+        //referralContext=rContext;
+        //refDirContext=(DirContext)rContext;
+        //refDirContext.close();
         //get context and jump out the while loop
         moreReferrals=false;
       }//try
-      //if referral have another referral excption
       catch (ReferralException re)
       {
         //keep running in while loop
@@ -950,7 +1019,6 @@
         //assign refExc to new referral exception re
         refExc=re;
       }
-      //catch a authentication exception
       catch (AuthenticationException ae)
       {
         util.debugMessage("Error running referral handler thread: " + 
@@ -960,7 +1028,6 @@
         //don't get the context
         referralContext = null;
       }
-      //catch a naming exception
       catch (NamingException ne)
       {
         util.debugMessage("Error running referral handler thread: " + 
Index: build.xml
===================================================================
--- build.xml	(revision 979)
+++ build.xml	(revision 980)
@@ -51,18 +51,18 @@
      <property name="jdbc" value="lib/jdbc7.1-1.2.jar" />
    </target>
    
-   <target name="config" depends="postgresql">
+   <target name="config" depends="oracle">
       <!-- Change the 'depends' attribute above to 'postgresql' to install 
            metacat with postgres instead of oracle -->
 
       <!-- Customize these properties for your system -->
-      <property name="tomcat" value="/home/tao/tomcat" />
-      <property name="webapps" value="/home/tao/tomcat/webapps" />
-      <property name="context" value="tao" />
-      <property name="user" value="tao"/>
-      <property name="password" value="tdataint"/>
-      <property name="server" value="epsilon.nceas.ucsb.edu:8443"/>
-      <property name="systemidserver" value="http://epsilon.nceas.ucsb.edu:8080" />
+      <property name="tomcat" value="/usr/local/devtools/jakarta-tomcat" />
+      <property name="webapps" value="/opt/tomcat/webapps" />
+      <property name="context" value="berkley" />
+      <property name="user" value="berkley"/>
+      <property name="password" value=""/>
+      <property name="server" value="dev.nceas.ucsb.edu:8091"/>
+      <property name="systemidserver" value="http://dev.nceas.ucsb.edu:8091" />
       <property name="web-base-url" 
                 value="http://knb.ecoinformatics.org"/>
       <property name="default-style" value="knb" />
