| | |
| | | import java.util.HashMap; |
| | | |
| | | import javax.naming.NamingException; |
| | | import javax.naming.ldap.Control; |
| | | import javax.net.ssl.KeyManager; |
| | | |
| | | import org.forgerock.opendj.ldap.DN; |
| | | import org.forgerock.opendj.ldap.SearchScope; |
| | | import org.forgerock.opendj.ldap.controls.ManageDsaITRequestControl; |
| | | import org.forgerock.opendj.ldap.controls.ServerSideSortRequestControl; |
| | | import org.opends.admin.ads.util.ApplicationTrustManager; |
| | | import org.opends.admin.ads.util.ConnectionWrapper; |
| | | import org.opends.admin.ads.util.PreferredConnection.Type; |
| | |
| | | |
| | | private ArrayList<ReferralAuthenticationListener> listeners; |
| | | |
| | | private Control[] requestControls = new Control[] {}; |
| | | private ServerSideSortRequestControl sortControl; |
| | | private ManageDsaITRequestControl followReferralsControl; |
| | | private ApplicationTrustManager trustManager; |
| | | private int connectTimeout = CliConstants.DEFAULT_LDAP_CONNECT_TIMEOUT; |
| | | |
| | | /** |
| | | * Returns <CODE>true</CODE> if the connection passed is registered in the |
| | | * connection pool, <CODE>false</CODE> otherwise. |
| | | * @param conn the connection. |
| | | * @return <CODE>true</CODE> if the connection passed is registered in the |
| | | * connection pool, <CODE>false</CODE> otherwise. |
| | | * Returns whether the connection passed is registered in the connection pool. |
| | | * |
| | | * @param conn |
| | | * the connection. |
| | | * @return {@code true} if the connection passed is registered in the connection pool, |
| | | * {@code false} otherwise. |
| | | */ |
| | | public boolean isConnectionRegistered(ConnectionWrapper conn) { |
| | | for (String key : connectionTable.keySet()) |
| | | { |
| | | ConnectionRecord cr = connectionTable.get(key); |
| | | if (cr.conn != null |
| | | && cr.conn.getHostPort().equals(conn.getHostPort()) |
| | | && cr.conn.getBindDn().equals(conn.getBindDn()) |
| | | && cr.conn.getBindPassword().equals(conn.getBindPassword()) |
| | | && cr.conn.getConnectionType() == conn.getConnectionType()) |
| | | final ConnectionRecord cr = connectionTable.get(key); |
| | | if (cr.conn != null) |
| | | { |
| | | return true; |
| | | final ConnectionWrapper c = cr.conn.getConnectionWrapper(); |
| | | if (c.getHostPort().equals(conn.getHostPort()) |
| | | && c.getBindDn().equals(conn.getBindDn()) |
| | | && c.getBindPassword().equals(conn.getBindPassword()) |
| | | && c.getConnectionType() == conn.getConnectionType()) |
| | | { |
| | | return true; |
| | | } |
| | | } |
| | | } |
| | | return false; |
| | |
| | | LDAPURL url = makeLDAPUrl(conn); |
| | | String key = makeKeyFromLDAPUrl(url); |
| | | ConnectionRecord cr = new ConnectionRecord(); |
| | | cr.conn = conn; |
| | | cr.conn = new ConnectionWithControls(conn, sortControl, followReferralsControl); |
| | | cr.counter = 1; |
| | | cr.disconnectAfterUse = false; |
| | | connectionTable.put(key, cr); |
| | |
| | | * @return a connection to the provided LDAP URL. |
| | | * @throws NamingException if there was an error connecting. |
| | | */ |
| | | public ConnectionWrapper getConnection(LDAPURL ldapUrl) |
| | | throws NamingException { |
| | | public ConnectionWithControls getConnection(LDAPURL ldapUrl) throws NamingException |
| | | { |
| | | String key = makeKeyFromLDAPUrl(ldapUrl); |
| | | ConnectionRecord cr; |
| | | |
| | |
| | | registerAuth = true; |
| | | } |
| | | cr.conn = createLDAPConnection(ldapUrl, authRecord); |
| | | cr.conn.getLdapContext().setRequestControls(requestControls); |
| | | if (registerAuth) |
| | | { |
| | | authTable.put(key, authRecord); |
| | |
| | | } |
| | | |
| | | /** |
| | | * Sets the request controls to be used by the connections of this connection |
| | | * pool. |
| | | * @param ctls the request controls. |
| | | * @throws NamingException if an error occurs updating the connections. |
| | | * Sets the request controls to be used by the connections of this connection pool. |
| | | * |
| | | * @param sortControl |
| | | * the sort control. |
| | | * @param followReferralsControl |
| | | * the manage dsa it control. |
| | | */ |
| | | public synchronized void setRequestControls(Control[] ctls) |
| | | throws NamingException |
| | | public synchronized void setRequestControls( |
| | | ServerSideSortRequestControl sortControl, |
| | | ManageDsaITRequestControl followReferralsControl) |
| | | { |
| | | requestControls = ctls; |
| | | this.sortControl = sortControl; |
| | | this.followReferralsControl = followReferralsControl; |
| | | for (ConnectionRecord cr : connectionTable.values()) |
| | | { |
| | | if (cr.conn != null) |
| | | { |
| | | cr.conn.getLdapContext().setRequestControls(requestControls); |
| | | cr.conn.setRequestControls(sortControl, followReferralsControl); |
| | | } |
| | | } |
| | | } |
| | |
| | | * @param conn |
| | | * the connection to be released. |
| | | */ |
| | | public synchronized void releaseConnection(ConnectionWrapper conn) { |
| | | public synchronized void releaseConnection(ConnectionWithControls conn) { |
| | | String targetKey = null; |
| | | ConnectionRecord targetRecord = null; |
| | | synchronized(this) { |
| | |
| | | * @return the key to be used in Maps for the provided connection record. |
| | | */ |
| | | private static String makeKeyFromRecord(ConnectionRecord rec) { |
| | | return (rec.conn.isLdaps() ? "LDAPS" : "LDAP") + ":" + rec.conn.getHostPort(); |
| | | ConnectionWrapper conn = rec.conn.getConnectionWrapper(); |
| | | return (conn.isLdaps() ? "LDAPS" : "LDAP") + ":" + conn.getHostPort(); |
| | | } |
| | | |
| | | /** |
| | |
| | | * @return a connection. |
| | | * @throws NamingException if an error occurs when connecting. |
| | | */ |
| | | private ConnectionWrapper createLDAPConnection(LDAPURL ldapUrl, AuthRecord ar) throws NamingException |
| | | private ConnectionWithControls createLDAPConnection(LDAPURL ldapUrl, AuthRecord ar) throws NamingException |
| | | { |
| | | final HostPort hostPort = new HostPort(ldapUrl.getHost(), ldapUrl.getPort()); |
| | | final Type connectiontype = isSecureLDAPUrl(ldapUrl) ? LDAPS : LDAP; |
| | | return new ConnectionWrapper(hostPort, connectiontype, ar.dn, ar.password, |
| | | final ConnectionWrapper conn = new ConnectionWrapper(hostPort, connectiontype, ar.dn, ar.password, |
| | | getConnectTimeout(), getTrustManager(), getKeyManager()); |
| | | return new ConnectionWithControls(conn, sortControl, followReferralsControl); |
| | | } |
| | | |
| | | /** |
| | |
| | | null, // No filter |
| | | null); // No extensions |
| | | } |
| | | |
| | | } |
| | | |
| | | /** A structure representing authentication data. */ |
| | |
| | | |
| | | /** A structure representing an active connection. */ |
| | | class ConnectionRecord { |
| | | ConnectionWrapper conn; |
| | | ConnectionWithControls conn; |
| | | int counter; |
| | | boolean disconnectAfterUse; |
| | | } |