Commit af78ac22 authored by Brian Major's avatar Brian Major
Browse files

ac2 - JNDI unbind in a finally block, JNDI bind re-init if lookup failure

parent 2196c8d7
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -73,6 +73,8 @@ import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

import org.apache.log4j.Logger;

/**
 * The object that is bound in JNDI to hold the LDAP pools.
 */
@@ -82,6 +84,7 @@ public class ConnectionPools
    private LdapConfig config;

    private Map<String,LdapConnectionPool> pools;
    private static final Logger logger = Logger.getLogger(ConnectionPools.class);

    private long lastPoolCheck = System.currentTimeMillis();
    private boolean isClosed;
@@ -120,8 +123,15 @@ public class ConnectionPools
        while (i.hasNext())
        {
            LdapConnectionPool next = i.next();
            try
            {
                next.shutdown();
            }
            catch (Throwable t)
            {
                logger.warn("Could not shutdown pool " + next.getName(), t);
            }
        }
        isClosed = true;
    }

+5 −0
Original line number Diff line number Diff line
@@ -185,6 +185,11 @@ public class LdapConnectionPool
        profiler.checkpoint("Pool closed.");
    }

    public String getName()
    {
        return poolName;
    }

    private LDAPConnectionPool createPool(LdapConfig config, LdapPool poolConfig, String poolName, String bindID, String bindPW)

    {
+34 −15
Original line number Diff line number Diff line
@@ -155,7 +155,14 @@ public abstract class LdapPersistence
                        pools.close();
                    }
                }

            }
        }
        catch (NamingException e)
        {
            throw new IllegalStateException("JNDI error", e);
        }
        finally
        {
            // unbind the pool
            try
            {
@@ -168,13 +175,6 @@ public abstract class LdapPersistence
            }
        }
    }
        catch (NamingException e)
        {
            throw new IllegalStateException("JNDI error", e);
        }


    }

    private void initPools()
    {
@@ -186,14 +186,33 @@ public abstract class LdapPersistence
            if (pools == null)
            {
                synchronized (jndiMonitor)
                {
                    try
                    {
                        pools = lookupPools();
                        logger.debug("Pool from second JNDI lookup: " + pools);
                    }
                    catch (Throwable t)
                    {
                        logger.warn("Failure looking up pool from JNDI, re-initializing", t);
                        pools = null;
                    }
                    if (pools == null)
                    {
                        LdapConfig config = LdapConfig.getLdapConfig();
                        pools = createPools(config);
                        InitialContext ic = new InitialContext();
                        try
                        {
                            // unbind just to be safe
                            ic.unbind(LDAP_POOL_JNDI_NAME);
                            logger.warn("Unbound previously bound pool");
                        }
                        catch (NamingException e)
                        {
                            // happens when nothing to unbind, expected
                            logger.debug("No pool to unbind");
                        }
                        ic.bind(LDAP_POOL_JNDI_NAME, pools);
                        profiler.checkpoint("Bound LDAP pools to JNDI");
                        logger.debug("Bound LDAP pools to JNDI");