directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lucasthei...@apache.org
Subject svn commit: r1590297 - in /directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api: DefaultLdapConnectionFactory.java LdapConnectionFactory.java LdapConnectionPool.java PoolableLdapConnectionFactory.java
Date Sat, 26 Apr 2014 20:56:04 GMT
Author: lucastheisen
Date: Sat Apr 26 20:56:03 2014
New Revision: 1590297

URL: http://svn.apache.org/r1590297
Log:
DIRAPI-168: Create an LdapConnectionFactory that is not pooled to create LdapConnection objects
for an LdapConnectionConfig


Added:
    directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/DefaultLdapConnectionFactory.java
  (with props)
    directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionFactory.java
  (with props)
Modified:
    directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionPool.java
    directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/PoolableLdapConnectionFactory.java

Added: directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/DefaultLdapConnectionFactory.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/DefaultLdapConnectionFactory.java?rev=1590297&view=auto
==============================================================================
--- directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/DefaultLdapConnectionFactory.java
(added)
+++ directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/DefaultLdapConnectionFactory.java
Sat Apr 26 20:56:03 2014
@@ -0,0 +1,106 @@
+package org.apache.directory.ldap.client.api;
+
+
+import java.io.IOException;
+
+import org.apache.directory.api.ldap.codec.api.LdapApiService;
+import org.apache.directory.api.ldap.model.exception.LdapException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class DefaultLdapConnectionFactory implements LdapConnectionFactory
+{
+    private static Logger LOG = LoggerFactory.getLogger( DefaultLdapConnectionFactory.class
);
+
+    private LdapApiService apiService;
+    private LdapConnectionConfig connectionConfig;
+    private long timeout;
+
+
+    public DefaultLdapConnectionFactory( LdapConnectionConfig config )
+    {
+        this.connectionConfig = config;
+        this.timeout = config.getDefaultTimeout();
+    }
+
+
+    @Override
+    public LdapConnection bindConnection( LdapConnection connection ) throws LdapException
+    {
+        try
+        {
+            connection.bind( connectionConfig.getName(), connectionConfig.getCredentials()
);
+        }
+        catch ( LdapException e )
+        {
+            LOG.error( "unable to bind connection: {}", e.getMessage() );
+            LOG.debug( "unable to bind connection:", e );
+            try
+            {
+                connection.close();
+            }
+            catch ( IOException ioe )
+            {
+                LOG.error( "unable to close failed bind connection: {}", e.getMessage() );
+                LOG.debug( "unable to close failed bind connection:", e );
+            }
+            throw e;
+        }
+        return connection;
+    }
+
+
+    @Override
+    public LdapConnection configureConnection( LdapConnection connection )
+    {
+        connection.setTimeOut( timeout );
+        connection.setBinaryAttributeDetector( connectionConfig.getBinaryAttributeDetector()
);
+        return connection;
+    }
+
+
+    @Override
+    public LdapConnection newLdapConnection() throws LdapException
+    {
+        return bindConnection( newUnboundLdapConnection() );
+    }
+
+
+    @Override
+    @SuppressWarnings("resource")
+    public LdapConnection newUnboundLdapConnection()
+    {
+        return configureConnection( apiService == null
+            ? new LdapNetworkConnection( connectionConfig )
+            : new LdapNetworkConnection( connectionConfig, apiService ) );
+    }
+
+
+    /**
+     * Sets the LdapApiService (codec) to be used by the connections created
+     * by this factory.
+     *
+     * @param apiService The codec to used by connections created by this 
+     * factory
+     */
+    public void setLdapApiService( LdapApiService apiService )
+    {
+        this.apiService = apiService;
+    }
+
+
+    /**
+     * Sets the timeout that will be used by all connections created by this
+     * factory.
+     *
+     * @param timeout The timeout in millis.
+     * 
+     * @see LdapConnection#setTimeOut(long)
+     */
+    public void setTimeOut( long timeout )
+    {
+        this.timeout = timeout;
+    }
+
+}

Propchange: directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/DefaultLdapConnectionFactory.java
------------------------------------------------------------------------------
    svn:executable = *

Added: directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionFactory.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionFactory.java?rev=1590297&view=auto
==============================================================================
--- directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionFactory.java
(added)
+++ directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionFactory.java
Sat Apr 26 20:56:03 2014
@@ -0,0 +1,63 @@
+package org.apache.directory.ldap.client.api;
+
+
+import org.apache.directory.api.ldap.model.exception.LdapException;
+
+
+public interface LdapConnectionFactory
+{
+    /**
+     * Issues a bind request on the supplied connection using the name and
+     * credentials from the LdapConnectionConfg supplied to the constructor.
+     * Returns the connection supplied for chaining.
+     * 
+     * @param connection
+     *            The connection to bind with the configuration credentials.
+     * @return The connection supplied.
+     * @throws LdapException
+     *             If the bind fails.
+     */
+    public abstract LdapConnection bindConnection( LdapConnection connection ) throws LdapException;
+
+
+    /**
+     * Applies the following configuration settings from the
+     * LdapConnectionConfig to the supplied connection:
+     * <ul>
+     * <li>timeOut</li>
+     * <li>binaryAttributeDetector</li>
+     * </ul>
+     * This method is called by newLdapConnection, so there is no need to call
+     * this on a newly created connection. This should be used for pooling where
+     * the returned connection could have been modified by the borrower in order
+     * to ensure the next borrower gets a correctly configured connection.
+     * Returns the supplied connection for chaining.
+     * 
+     * @param connection
+     *            The connection to configure
+     * @return The supplied connection.
+     */
+    public abstract LdapConnection configureConnection( LdapConnection connection );
+
+
+    /**
+     * Returns a newly created, configured, and authenticated connection. This
+     * method should be used by a connection pool to manufacture the pooled
+     * instances.
+     * 
+     * @return A newly created, configured, and authenticated LdapConnection.
+     * @throws LdapException
+     */
+    public abstract LdapConnection newLdapConnection() throws LdapException;
+    
+    
+    /**
+     * Returns a newly created connection, that has not been bound (bind) that
+     * otherwise respects LdapConnectionConfig supplied to the constructor. This
+     * is useful for authentication purposes where the consumer will use a bind
+     * operation.
+     * 
+     * @return A newly created and configured LdapConnection.
+     */
+    public abstract LdapConnection newUnboundLdapConnection();
+}

Propchange: directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionFactory.java
------------------------------------------------------------------------------
    svn:executable = *

Modified: directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionPool.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionPool.java?rev=1590297&r1=1590296&r2=1590297&view=diff
==============================================================================
--- directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionPool.java
(original)
+++ directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/LdapConnectionPool.java
Sat Apr 26 20:56:03 2014
@@ -22,6 +22,7 @@ package org.apache.directory.ldap.client
 
 
 import org.apache.commons.pool.impl.GenericObjectPool;
+import org.apache.directory.api.ldap.codec.api.LdapApiService;
 
 
 /**
@@ -34,6 +35,21 @@ import org.apache.commons.pool.impl.Gene
  */
 public class LdapConnectionPool extends GenericObjectPool<LdapConnection>
 {
+
+    /**
+     * Instantiates a new LDAP connection pool.
+     *
+     * @param connectionConfig The connection configuration
+     * @param apiService The api service (codec)
+     * @param timeout The connection timeout in millis
+     */
+    public LdapConnectionPool( LdapConnectionConfig connectionConfig,
+        LdapApiService apiService, long timeout )
+    {
+        super( newPoolableConnectionFactory( connectionConfig, apiService, timeout ) );
+    }
+
+
     /**
      * Instantiates a new LDAP connection pool.
      *
@@ -58,6 +74,35 @@ public class LdapConnectionPool extends 
 
 
     /**
+     * Returns an LdapConnection from the pool that is not bound to an
+     * identity.  This type of connection is useful when you want to bind
+     * yourself for authentication/authorization purposes.
+     *
+     * @return An unbound LdapConnection from the pool
+     * @throws Exception If an error occurs while obtaining a connection 
+     * from the factory
+     */
+    public LdapConnection getUnboundConnection() throws Exception
+    {
+        LdapConnection connection = super.borrowObject();
+        connection.unBind();
+        return connection;
+    }
+
+
+    private static PoolableLdapConnectionFactory newPoolableConnectionFactory( 
+        LdapConnectionConfig connectionConfig, LdapApiService apiService, 
+        long timeout )
+    {
+        DefaultLdapConnectionFactory connectionFactory = 
+            new DefaultLdapConnectionFactory( connectionConfig );
+        connectionFactory.setLdapApiService( apiService );
+        connectionFactory.setTimeOut( timeout );
+        return new PoolableLdapConnectionFactory( connectionFactory );
+    }
+
+
+    /**
      * Places the given LdapConnection back in the pool.
      * 
      * @param connection the LdapConnection to be released

Modified: directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/PoolableLdapConnectionFactory.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/PoolableLdapConnectionFactory.java?rev=1590297&r1=1590296&r2=1590297&view=diff
==============================================================================
--- directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/PoolableLdapConnectionFactory.java
(original)
+++ directory/shared/trunk/ldap/client/api/src/main/java/org/apache/directory/ldap/client/api/PoolableLdapConnectionFactory.java
Sat Apr 26 20:56:03 2014
@@ -21,6 +21,8 @@
 package org.apache.directory.ldap.client.api;
 
 
+import java.io.IOException;
+
 import org.apache.commons.pool.PoolableObjectFactory;
 import org.apache.directory.api.ldap.model.constants.SchemaConstants;
 import org.apache.directory.api.ldap.model.exception.LdapException;
@@ -36,23 +38,30 @@ import org.slf4j.LoggerFactory;
  */
 public class PoolableLdapConnectionFactory implements PoolableObjectFactory<LdapConnection>
 {
-    /** configuration object for the connection */
-    private LdapConnectionConfig config;
-
-    /** the logger */
     private static final Logger LOG = LoggerFactory.getLogger( PoolableLdapConnectionFactory.class
);
 
+    private LdapConnectionFactory connectionFactory;
+
 
     /**
-     * 
-     * Creates a new instance of PoolableLdapConnectionFactory for the
-     * server running on localhost at the port 10389
+     * Creates a new instance of PoolableLdapConnectionFactory.
      *
      * @param config the configuration for creating LdapConnections
      */
     public PoolableLdapConnectionFactory( LdapConnectionConfig config )
     {
-        this.config = config;
+        this( new DefaultLdapConnectionFactory( config ) );
+    }
+
+
+    /**
+     * Creates a new instance of PoolableLdapConnectionFactory.
+     *
+     * @param connectionFactory the connection factory for creating LdapConnections
+     */
+    public PoolableLdapConnectionFactory( LdapConnectionFactory connectionFactory )
+    {
+        this.connectionFactory = connectionFactory;
     }
 
 
@@ -71,8 +80,21 @@ public class PoolableLdapConnectionFacto
     public void destroyObject( LdapConnection connection ) throws Exception
     {
         LOG.debug( "Destroying {}", connection );
-        connection.unBind();
-        connection.close();
+        try {
+            connection.unBind();
+        }
+        catch ( LdapException e ) {
+            LOG.error( "unable to unbind connection: {}", e.getMessage() );
+            LOG.debug( "unable to unbind connection:", e );
+        }
+
+        try {
+            connection.close();
+        }
+        catch ( IOException e ) {
+            LOG.error( "unable to close connection: {}", e.getMessage() );
+            LOG.debug( "unable to close connection:", e );
+        }
     }
 
 
@@ -82,25 +104,7 @@ public class PoolableLdapConnectionFacto
     public LdapConnection makeObject() throws Exception
     {
         LOG.debug( "Creating a LDAP connection" );
-
-        LdapNetworkConnection connection = new LdapNetworkConnection( config );
-
-        try
-        {
-            connection.bind( config.getName(), config.getCredentials() );
-        }
-        catch ( Exception e )
-        {
-            LOG.warn( "Cannot bind : {}", e.getMessage() );
-
-            // We weren't able to bind : close the connection
-            connection.close();
-
-            // And re-throw the exception
-            throw e;
-        }
-
-        return connection;
+        return connectionFactory.newLdapConnection();
     }
 
 
@@ -110,6 +114,11 @@ public class PoolableLdapConnectionFacto
     public void passivateObject( LdapConnection connection ) throws Exception
     {
         LOG.debug( "Passivating {}", connection );
+        
+        // in case connection configuration was modified, or rebound to a
+        // different identity, we reinitialize before returning to the pool.
+        connectionFactory.bindConnection( 
+            connectionFactory.configureConnection( connection ) );
     }
 
 



Mime
View raw message