View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  
21  package org.apache.directory.ldap.client.api;
22  
23  
24  import java.io.IOException;
25  
26  import org.apache.commons.pool.PoolableObjectFactory;
27  import org.apache.directory.api.ldap.codec.api.LdapApiService;
28  import org.apache.directory.api.ldap.model.constants.SchemaConstants;
29  import org.apache.directory.api.ldap.model.exception.LdapException;
30  import org.apache.directory.api.ldap.model.name.Dn;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  
35  /**
36   * A factory for creating LdapConnection objects managed by LdapConnectionPool.
37   * 
38   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
39   */
40  public class PoolableLdapConnectionFactory implements PoolableObjectFactory<LdapConnection>
41  {
42      private static final Logger LOG = LoggerFactory.getLogger( PoolableLdapConnectionFactory.class );
43  
44      private LdapConnectionFactory connectionFactory;
45  
46  
47      /**
48       * Creates a new instance of PoolableLdapConnectionFactory.
49       *
50       * @param config the configuration for creating LdapConnections
51       */
52      public PoolableLdapConnectionFactory( LdapConnectionConfig config )
53      {
54          this( new DefaultLdapConnectionFactory( config ) );
55      }
56  
57  
58      /**
59       * Creates a new instance of PoolableLdapConnectionFactory.
60       *
61       * @param connectionFactory the connection factory for creating LdapConnections
62       */
63      public PoolableLdapConnectionFactory( LdapConnectionFactory connectionFactory )
64      {
65          this.connectionFactory = connectionFactory;
66      }
67  
68  
69      /**
70       * {@inheritDoc}
71       */
72      public void activateObject( LdapConnection connection )
73      {
74          LOG.debug( "Activating {}", connection );
75      }
76  
77  
78      /**
79       * {@inheritDoc}
80       */
81      public void destroyObject( LdapConnection connection ) 
82      {
83          LOG.debug( "Destroying {}", connection );
84          try {
85              connection.unBind();
86          }
87          catch ( LdapException e ) {
88              LOG.error( "unable to unbind connection: {}", e.getMessage() );
89              LOG.debug( "unable to unbind connection:", e );
90          }
91  
92          try {
93              connection.close();
94          }
95          catch ( IOException e ) {
96              LOG.error( "unable to close connection: {}", e.getMessage() );
97              LOG.debug( "unable to close connection:", e );
98          }
99      }
100 
101 
102     /**
103      * Returns the LdapApiService instance used by this factory.
104      *
105      * @return The LdapApiService instance used by this factory
106      */
107     public LdapApiService getLdapApiService()
108     {
109         return connectionFactory.getLdapApiService();
110     }
111 
112 
113     /**
114      * {@inheritDoc}
115      * @throws LdapException If unable to connect.
116      */
117     public LdapConnection makeObject() throws LdapException
118     {
119         LOG.debug( "Creating a LDAP connection" );
120         return connectionFactory.newLdapConnection();
121     }
122 
123 
124     /**
125      * {@inheritDoc}
126      * @throws LdapException If unable to reconfigure and rebind.
127      */
128     public void passivateObject( LdapConnection connection ) throws LdapException
129     {
130         LOG.debug( "Passivating {}", connection );
131         
132         // in case connection configuration was modified, or rebound to a
133         // different identity, we reinitialize before returning to the pool.
134         connectionFactory.bindConnection( 
135             connectionFactory.configureConnection( connection ) );
136     }
137 
138 
139     /**
140      * {@inheritDoc}
141      */
142     public boolean validateObject( LdapConnection connection )
143     {
144         LOG.debug( "Validating {}", connection );
145 
146         if ( connection.isConnected() )
147         {
148             try
149             {
150                 return connection.lookup( Dn.ROOT_DSE, SchemaConstants.NO_ATTRIBUTE ) != null;
151             }
152             catch ( LdapException le )
153             {
154                 return false;
155             }
156         }
157         else
158         {
159             return false;
160         }
161     }
162 }