diff options
Diffstat (limited to 'pki/base/common/src/com/netscape/cmscore/ldapconn/LdapBoundConnFactory.java')
-rw-r--r-- | pki/base/common/src/com/netscape/cmscore/ldapconn/LdapBoundConnFactory.java | 378 |
1 files changed, 186 insertions, 192 deletions
diff --git a/pki/base/common/src/com/netscape/cmscore/ldapconn/LdapBoundConnFactory.java b/pki/base/common/src/com/netscape/cmscore/ldapconn/LdapBoundConnFactory.java index a8a107acb..08932ef54 100644 --- a/pki/base/common/src/com/netscape/cmscore/ldapconn/LdapBoundConnFactory.java +++ b/pki/base/common/src/com/netscape/cmscore/ldapconn/LdapBoundConnFactory.java @@ -17,7 +17,6 @@ // --- END COPYRIGHT BLOCK --- package com.netscape.cmscore.ldapconn; - import netscape.ldap.LDAPConnection; import netscape.ldap.LDAPException; import netscape.ldap.LDAPSocketFactory; @@ -30,12 +29,10 @@ import com.netscape.certsrv.ldap.ELdapServerDownException; import com.netscape.certsrv.ldap.ILdapBoundConnFactory; import com.netscape.certsrv.logging.ILogger; - /** - * Factory for getting LDAP Connections to a LDAP server with the same - * LDAP authentication. - * XXX not sure how useful this is given that LDAPConnection itself can - * be shared by multiple threads and cloned. + * Factory for getting LDAP Connections to a LDAP server with the same LDAP + * authentication. XXX not sure how useful this is given that LDAPConnection + * itself can be shared by multiple threads and cloned. */ public class LdapBoundConnFactory implements ILdapBoundConnFactory { protected int mMinConns = 5; @@ -52,10 +49,10 @@ public class LdapBoundConnFactory implements ILdapBoundConnFactory { public static final String PROP_ERROR_IF_DOWN = "errorIfDown"; - private int mNumConns = 0; // number of available conns in array - private int mTotal = 0; // total num conns + private int mNumConns = 0; // number of available conns in array + private int mTotal = 0; // total num conns - private boolean doCloning=true; + private boolean doCloning = true; private LdapBoundConnection mMasterConn = null; // master connection object. private BoundConnection mConns[]; @@ -70,8 +67,8 @@ public class LdapBoundConnFactory implements ILdapBoundConnFactory { private boolean mDefErrorIfDown = false; /** - * Constructor for initializing from the config store. - * must be followed by init(IConfigStore) + * Constructor for initializing from the config store. must be followed by + * init(IConfigStore) */ public LdapBoundConnFactory() { } @@ -94,51 +91,52 @@ public class LdapBoundConnFactory implements ILdapBoundConnFactory { /** * Constructor for LdapBoundConnFactory + * * @param minConns minimum number of connections to have available - * @param maxConns max number of connections to have available. This is - * the maximum number of clones of this connection or separate connections one wants to allow. + * @param maxConns max number of connections to have available. This is the + * maximum number of clones of this connection or separate + * connections one wants to allow. * @param serverInfo server connection info - host, port, etc. */ - public LdapBoundConnFactory(int minConns, int maxConns, - LdapConnInfo connInfo, LdapAuthInfo authInfo) throws ELdapException { + public LdapBoundConnFactory(int minConns, int maxConns, + LdapConnInfo connInfo, LdapAuthInfo authInfo) throws ELdapException { init(minConns, maxConns, connInfo, authInfo); } /** * Constructor for initialize */ - public void init(IConfigStore config) - throws ELdapException, EBaseException { + public void init(IConfigStore config) throws ELdapException, EBaseException { CMS.debug("LdapBoundConnFactory: init "); - LdapConnInfo connInfo = - new LdapConnInfo(config.getSubStore(PROP_LDAPCONNINFO)); + LdapConnInfo connInfo = new LdapConnInfo( + config.getSubStore(PROP_LDAPCONNINFO)); mErrorIfDown = config.getBoolean(PROP_ERROR_IF_DOWN, mDefErrorIfDown); - doCloning = config.getBoolean("doCloning",true); + doCloning = config.getBoolean("doCloning", true); CMS.debug("LdapBoundConnFactory:doCloning " + doCloning); init(config.getInteger(PROP_MINCONNS, mMinConns), - config.getInteger(PROP_MAXCONNS, mMaxConns), - connInfo, - new LdapAuthInfo(config.getSubStore(PROP_LDAPAUTHINFO), - connInfo.getHost(), connInfo.getPort(), connInfo.getSecure())); + config.getInteger(PROP_MAXCONNS, mMaxConns), + connInfo, + new LdapAuthInfo(config.getSubStore(PROP_LDAPAUTHINFO), + connInfo.getHost(), connInfo.getPort(), connInfo + .getSecure())); } /** - * initialize parameters obtained from either constructor or - * config store + * initialize parameters obtained from either constructor or config store + * * @param minConns minimum number of connection handls to have available. * @param maxConns maximum total number of connections to ever have. * @param connInfo ldap connection info. * @param authInfo ldap authentication info. - * @exception ELdapException if any error occurs. + * @exception ELdapException if any error occurs. */ - private void init(int minConns, int maxConns, - LdapConnInfo connInfo, LdapAuthInfo authInfo) - throws ELdapException { - if (minConns <= 0 || maxConns <= 0 || minConns > maxConns) + private void init(int minConns, int maxConns, LdapConnInfo connInfo, + LdapAuthInfo authInfo) throws ELdapException { + if (minConns <= 0 || maxConns <= 0 || minConns > maxConns) throw new ELdapException( CMS.getUserMessage("CMS_LDAP_INVALID_NUMCONN_PARAMETERS")); if (connInfo == null || authInfo == null) @@ -152,16 +150,14 @@ public class LdapBoundConnFactory implements ILdapBoundConnFactory { mConns = new BoundConnection[mMaxConns]; // Create connection handle and make initial connection - CMS.debug( - "init: before makeConnection errorIfDown is " + mErrorIfDown); + CMS.debug("init: before makeConnection errorIfDown is " + mErrorIfDown); makeConnection(mErrorIfDown); - CMS.debug( - "initializing with mininum " + mMinConns + " and maximum " + mMaxConns + - " connections to " + - "host " + mConnInfo.getHost() + " port " + mConnInfo.getPort() + - ", secure connection, " + mConnInfo.getSecure() + - ", authentication type " + mAuthInfo.getAuthType()); + CMS.debug("initializing with mininum " + mMinConns + " and maximum " + + mMaxConns + " connections to " + "host " + + mConnInfo.getHost() + " port " + mConnInfo.getPort() + + ", secure connection, " + mConnInfo.getSecure() + + ", authentication type " + mAuthInfo.getAuthType()); // initalize minimum number of connection handles available. makeMinimum(); @@ -169,6 +165,7 @@ public class LdapBoundConnFactory implements ILdapBoundConnFactory { /** * makes the initial master connection used to clone others.. + * * @exception ELdapException if any error occurs. */ protected void makeConnection(boolean errorIfDown) throws ELdapException { @@ -179,32 +176,36 @@ public class LdapBoundConnFactory implements ILdapBoundConnFactory { if (e.getLDAPResultCode() == LDAPException.UNAVAILABLE) { // need to intercept this because message from LDAP is // "DSA is unavailable" which confuses with DSA PKI. - log(ILogger.LL_FAILURE, - CMS.getLogMessage("CMSCORE_LDAPCONN_CONNECT_SERVER", - mConnInfo.getHost(), + log(ILogger.LL_FAILURE, CMS.getLogMessage( + "CMSCORE_LDAPCONN_CONNECT_SERVER", mConnInfo.getHost(), Integer.toString(mConnInfo.getPort()))); if (errorIfDown) { - throw new ELdapServerDownException( - CMS.getUserMessage("CMS_LDAP_SERVER_UNAVAILABLE", - mConnInfo.getHost(), "" + mConnInfo.getPort())); + throw new ELdapServerDownException(CMS.getUserMessage( + "CMS_LDAP_SERVER_UNAVAILABLE", mConnInfo.getHost(), + "" + mConnInfo.getPort())); } } else { - log(ILogger.LL_FAILURE, - CMS.getLogMessage("CMSCORE_LDAPCONN_FAILED_SERVER", e.toString())); - throw new ELdapException( - CMS.getUserMessage("CMS_LDAP_CONNECT_TO_LDAP_SERVER_FAILED", - mConnInfo.getHost(), "" + (Integer.valueOf(mConnInfo.getPort())), e.toString())); + log(ILogger.LL_FAILURE, + CMS.getLogMessage("CMSCORE_LDAPCONN_FAILED_SERVER", + e.toString())); + throw new ELdapException(CMS.getUserMessage( + "CMS_LDAP_CONNECT_TO_LDAP_SERVER_FAILED", + mConnInfo.getHost(), + "" + (Integer.valueOf(mConnInfo.getPort())), + e.toString())); } } } - /** * makes subsequent connections if cloning is not used . + * * @exception ELdapException if any error occurs. */ - private LdapBoundConnection makeNewConnection(boolean errorIfDown) throws ELdapException { - CMS.debug("LdapBoundConnFactory:In makeNewConnection: errorIfDown " + errorIfDown); + private LdapBoundConnection makeNewConnection(boolean errorIfDown) + throws ELdapException { + CMS.debug("LdapBoundConnFactory:In makeNewConnection: errorIfDown " + + errorIfDown); LdapBoundConnection conn = null; try { conn = new BoundConnection(mConnInfo, mAuthInfo); @@ -212,47 +213,48 @@ public class LdapBoundConnFactory implements ILdapBoundConnFactory { if (e.getLDAPResultCode() == LDAPException.UNAVAILABLE) { // need to intercept this because message from LDAP is // "DSA is unavailable" which confuses with DSA PKI. - log(ILogger.LL_FAILURE, - CMS.getLogMessage("CMSCORE_LDAPCONN_CONNECT_SERVER", - mConnInfo.getHost(), + log(ILogger.LL_FAILURE, CMS.getLogMessage( + "CMSCORE_LDAPCONN_CONNECT_SERVER", mConnInfo.getHost(), Integer.toString(mConnInfo.getPort()))); if (errorIfDown) { - throw new ELdapServerDownException( - CMS.getUserMessage("CMS_LDAP_SERVER_UNAVAILABLE", - mConnInfo.getHost(), "" + mConnInfo.getPort())); + throw new ELdapServerDownException(CMS.getUserMessage( + "CMS_LDAP_SERVER_UNAVAILABLE", mConnInfo.getHost(), + "" + mConnInfo.getPort())); } } else { log(ILogger.LL_FAILURE, - CMS.getLogMessage("CMSCORE_LDAPCONN_FAILED_SERVER", e.toString())); - throw new ELdapException( - CMS.getUserMessage("CMS_LDAP_CONNECT_TO_LDAP_SERVER_FAILED", - mConnInfo.getHost(), "" + (Integer.valueOf(mConnInfo.getPort())), e.toString())); + CMS.getLogMessage("CMSCORE_LDAPCONN_FAILED_SERVER", + e.toString())); + throw new ELdapException(CMS.getUserMessage( + "CMS_LDAP_CONNECT_TO_LDAP_SERVER_FAILED", + mConnInfo.getHost(), + "" + (Integer.valueOf(mConnInfo.getPort())), + e.toString())); } } return conn; } + /** * makes the minumum number of connections */ private void makeMinimum() throws ELdapException { - if (mMasterConn == null || mMasterConn.isConnected() == false) + if (mMasterConn == null || mMasterConn.isConnected() == false) return; int increment; if (mNumConns < mMinConns && mTotal <= mMaxConns) { increment = Math.min(mMinConns - mNumConns, mMaxConns - mTotal); - CMS.debug( - "increasing minimum connections by " + increment); + CMS.debug("increasing minimum connections by " + increment); for (int i = increment - 1; i >= 0; i--) { - if(doCloning == true) { + if (doCloning == true) { mConns[i] = (BoundConnection) mMasterConn.clone(); - } - else { + } else { mConns[i] = (BoundConnection) makeNewConnection(true); } - + } mTotal += increment; mNumConns += increment; @@ -262,132 +264,129 @@ public class LdapBoundConnFactory implements ILdapBoundConnFactory { } /** - * gets a conenction from this factory. - * All connections obtained from the factory must be returned by - * returnConn() method. - * The best thing to do is to put returnConn in a finally clause so it - * always gets called. For example, + * gets a conenction from this factory. All connections obtained from the + * factory must be returned by returnConn() method. The best thing to do is + * to put returnConn in a finally clause so it always gets called. For + * example, + * * <pre> - * LDAPConnection c = null; - * try { - * c = factory.getConn(); - * myclass.do_something_with_c(c); - * } - * catch (ELdapException e) { - * handle_error_here(); - * } - * finally { - * factory.returnConn(c); - * } + * LDAPConnection c = null; + * try { + * c = factory.getConn(); + * myclass.do_something_with_c(c); + * } catch (ELdapException e) { + * handle_error_here(); + * } finally { + * factory.returnConn(c); + * } * </pre> */ - public LDAPConnection getConn() - throws ELdapException { + public LDAPConnection getConn() throws ELdapException { return getConn(true); } /** - * Returns a LDAP connection - a clone of the master connection. - * All connections should be returned to the factory using returnConn() - * to recycle connection objects. - * If not returned the limited max number is affected but if that - * number is large not much harm is done. - * Returns null if maximum number of connections reached. - * The best thing to do is to put returnConn in a finally clause so it - * always gets called. For example, + * Returns a LDAP connection - a clone of the master connection. All + * connections should be returned to the factory using returnConn() to + * recycle connection objects. If not returned the limited max number is + * affected but if that number is large not much harm is done. Returns null + * if maximum number of connections reached. The best thing to do is to put + * returnConn in a finally clause so it always gets called. For example, + * * <pre> - * LDAPConnection c = null; - * try { - * c = factory.getConn(); - * myclass.do_something_with_c(c); - * } - * catch (ELdapException e) { - * handle_error_here(); - * } - * finally { - * factory.returnConn(c); - * } + * LDAPConnection c = null; + * try { + * c = factory.getConn(); + * myclass.do_something_with_c(c); + * } catch (ELdapException e) { + * handle_error_here(); + * } finally { + * factory.returnConn(c); + * } * </pre> - */ - public synchronized LDAPConnection getConn(boolean waitForConn) - throws ELdapException { + */ + public synchronized LDAPConnection getConn(boolean waitForConn) + throws ELdapException { boolean waited = false; - CMS.debug("In LdapBoundConnFactory::getConn()"); - if(mMasterConn != null) + CMS.debug("In LdapBoundConnFactory::getConn()"); + if (mMasterConn != null) CMS.debug("masterConn is connected: " + mMasterConn.isConnected()); else CMS.debug("masterConn is null."); if (mMasterConn == null || !mMasterConn.isConnected()) { try { - makeConnection(true); - } catch (ELdapException e) { + makeConnection(true); + } catch (ELdapException e) { mMasterConn = null; - CMS.debug("Can't create master connection in LdapBoundConnFactory::getConn! " + e.toString()); + CMS.debug("Can't create master connection in LdapBoundConnFactory::getConn! " + + e.toString()); throw e; } } - if (mNumConns == 0) + if (mNumConns == 0) makeMinimum(); if (mNumConns == 0) { if (!waitForConn) return null; try { CMS.debug("getConn: out of ldap connections"); - log(ILogger.LL_WARN, - "Ran out of ldap connections available " + - "in ldap connection pool to " + - mConnInfo.getHost() + ":" + mConnInfo.getPort() + ". " + - "This could be a temporary condition or an indication of " + - "something more serious that can cause the server to " + - "hang."); + log(ILogger.LL_WARN, + "Ran out of ldap connections available " + + "in ldap connection pool to " + + mConnInfo.getHost() + + ":" + + mConnInfo.getPort() + + ". " + + "This could be a temporary condition or an indication of " + + "something more serious that can cause the server to " + + "hang."); waited = true; - while (mNumConns == 0) + while (mNumConns == 0) wait(); } catch (InterruptedException e) { } - } + } mNumConns--; LDAPConnection conn = mConns[mNumConns]; boolean isConnected = false; - if(conn != null) { + if (conn != null) { isConnected = conn.isConnected(); } CMS.debug("getConn: conn is connected " + isConnected); - //If masterConn is still alive, lets try to bring this one - //back to life + // If masterConn is still alive, lets try to bring this one + // back to life - if((isConnected == false) && (mMasterConn != null) - && (mMasterConn.isConnected() == true)) { + if ((isConnected == false) && (mMasterConn != null) + && (mMasterConn.isConnected() == true)) { CMS.debug("Attempt to bring back down connection."); - if(doCloning == true) { + if (doCloning == true) { mConns[mNumConns] = (BoundConnection) mMasterConn.clone(); - } - else { + } else { try { - mConns[mNumConns] = (BoundConnection) makeNewConnection(true); + mConns[mNumConns] = (BoundConnection) makeNewConnection(true); + } catch (ELdapException e) { + mConns[mNumConns] = null; } - catch (ELdapException e) { - mConns[mNumConns] = null; - } - } - conn = mConns[mNumConns]; + } + conn = mConns[mNumConns]; - CMS.debug("Re-animated connection: " + conn); - } + CMS.debug("Re-animated connection: " + conn); + } - mConns[mNumConns] = null; + mConns[mNumConns] = null; if (waited) { - log(ILogger.LL_WARN, - "Ldap connections are available again in ldap connection pool " + - "to " + mConnInfo.getHost() + ":" + mConnInfo.getPort()); + log(ILogger.LL_WARN, + "Ldap connections are available again in ldap connection pool " + + "to " + mConnInfo.getHost() + ":" + + mConnInfo.getPort()); } CMS.debug("getConn: mNumConns now " + mNumConns); @@ -395,22 +394,20 @@ public class LdapBoundConnFactory implements ILdapBoundConnFactory { } /** - * Teturn connection to the factory. - * This is mandatory after a getConn(). + * Teturn connection to the factory. This is mandatory after a getConn(). * The best thing to do is to put returnConn in a finally clause so it - * always gets called. For example, + * always gets called. For example, + * * <pre> - * LDAPConnection c = null; - * try { - * c = factory.getConn(); - * myclass.do_something_with_c(c); - * } - * catch (ELdapException e) { - * handle_error_here(); - * } - * finally { - * factory.returnConn(c); - * } + * LDAPConnection c = null; + * try { + * c = factory.getConn(); + * myclass.do_something_with_c(c); + * } catch (ELdapException e) { + * handle_error_here(); + * } finally { + * factory.returnConn(c); + * } * </pre> */ public synchronized void returnConn(LDAPConnection conn) { @@ -423,17 +420,16 @@ public class LdapBoundConnFactory implements ILdapBoundConnFactory { log(ILogger.LL_WARN, "returnConn: unknown connection."); /* swallow this exception but see who's doing it. */ - ELdapException e = - new ELdapException(CMS.getUserMessage("CMS_LDAP_UNKNOWN_RETURNED_CONN")); + ELdapException e = new ELdapException( + CMS.getUserMessage("CMS_LDAP_UNKNOWN_RETURNED_CONN")); } for (int i = 0; i < mNumConns; i++) { if (mConns[i] == conn) { - CMS.debug( - "returnConn: previously returned connection."); + CMS.debug("returnConn: previously returned connection."); - /* swallow this exception but see who's doing it */ - ELdapException e = - new ELdapException(CMS.getUserMessage("CMS_LDAP_BAD_RETURNED_CONN")); + /* swallow this exception but see who's doing it */ + ELdapException e = new ELdapException( + CMS.getUserMessage("CMS_LDAP_BAD_RETURNED_CONN")); } } mConns[mNumConns++] = boundconn; @@ -445,25 +441,25 @@ public class LdapBoundConnFactory implements ILdapBoundConnFactory { * handy routine for logging in this class. */ private void log(int level, String msg) { - mLogger.log(ILogger.EV_SYSTEM, ILogger.S_LDAP, level, - "In Ldap (bound) connection pool to" + - " host " + mConnInfo.getHost() + - " port " + mConnInfo.getPort() + ", " + msg); + mLogger.log( + ILogger.EV_SYSTEM, + ILogger.S_LDAP, + level, + "In Ldap (bound) connection pool to" + " host " + + mConnInfo.getHost() + " port " + mConnInfo.getPort() + + ", " + msg); } - protected void finalize() - throws Exception { + protected void finalize() throws Exception { reset(); } /** - * used for disconnecting all connections and reset everything to 0 - * as if connections were never made. used just before a subsystem - * shutdown or process exit. - * useful only if no connections are outstanding. + * used for disconnecting all connections and reset everything to 0 as if + * connections were never made. used just before a subsystem shutdown or + * process exit. useful only if no connections are outstanding. */ - public synchronized void reset() - throws ELdapException { + public synchronized void reset() throws ELdapException { if (mNumConns == mTotal) { for (int i = 0; i < mNumConns; i++) { try { @@ -477,18 +473,17 @@ public class LdapBoundConnFactory implements ILdapBoundConnFactory { log(ILogger.LL_INFO, "disconnecting masterConn"); mMasterConn.disconnect(); } catch (LDAPException e) { - log(ILogger.LL_FAILURE, - CMS.getLogMessage("CMSCORE_LDAPCONN_CANNOT_RESET", - e.toString())); + log(ILogger.LL_FAILURE, CMS.getLogMessage( + "CMSCORE_LDAPCONN_CANNOT_RESET", e.toString())); } } mMasterConn = null; mTotal = 0; mNumConns = 0; } else { - CMS.debug( - "Cannot reset factory: connections not all returned"); - throw new ELdapException(CMS.getUserMessage("CMS_LDAP_CANNOT_RESET_CONNFAC")); + CMS.debug("Cannot reset factory: connections not all returned"); + throw new ELdapException( + CMS.getUserMessage("CMS_LDAP_CANNOT_RESET_CONNFAC")); } if (mAuthInfo != null) { @@ -497,7 +492,7 @@ public class LdapBoundConnFactory implements ILdapBoundConnFactory { } /** - * return ldap connection info + * return ldap connection info */ public LdapConnInfo getConnInfo() { return mConnInfo; @@ -520,17 +515,16 @@ public class LdapBoundConnFactory implements ILdapBoundConnFactory { private static final long serialVersionUID = 1353616391879078337L; public BoundConnection(LdapConnInfo connInfo, LdapAuthInfo authInfo) - throws LDAPException { + throws LDAPException { super(connInfo, authInfo); } - - public BoundConnection(String host, int port, int version, - LDAPSocketFactory fac, - String bindDN, String bindPW) - throws LDAPException { + + public BoundConnection(String host, int port, int version, + LDAPSocketFactory fac, String bindDN, String bindPW) + throws LDAPException { super(host, port, version, fac, bindDN, bindPW); } - + /** * used only to identify the factory from which this came. */ |