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   *     https://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 org.apache.commons.pool2.PooledObjectFactory;
25  import org.apache.commons.pool2.impl.GenericObjectPool;
26  import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
27  import org.apache.directory.api.i18n.I18n;
28  import org.apache.directory.api.ldap.codec.api.LdapApiService;
29  import org.apache.directory.api.ldap.model.exception.LdapException;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  
34  /**
35   * A pool implementation for LdapConnection objects.
36   * 
37   * This class is just a wrapper around the commons GenericObjectPool, and has
38   * a more meaningful name to represent the pool type.
39   * 
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   */
42  public class LdapConnectionPool extends GenericObjectPool<LdapConnection>
43  {
44      private static final Logger LOG = LoggerFactory.getLogger( LdapConnectionPool.class );
45  
46      private PooledObjectFactory<LdapConnection> factory;
47  
48  
49      /**
50       * Instantiates a new LDAP connection pool.
51       *
52       * @param connectionConfig The connection configuration
53       * @param apiService The api service (codec)
54       * @param timeout The connection timeout in millis
55       */
56      public LdapConnectionPool( LdapConnectionConfig connectionConfig,
57          LdapApiService apiService, long timeout )
58      {
59          this( connectionConfig, apiService, timeout, null );
60      }
61  
62  
63      /**
64       * Instantiates a new LDAP connection pool.
65       *
66       * @param connectionConfig The connection configuration
67       * @param apiService The api service (codec)
68       * @param timeout The connection timeout in millis
69       * @param poolConfig The pool configuration
70       */
71      public LdapConnectionPool( LdapConnectionConfig connectionConfig,
72          LdapApiService apiService, long timeout, GenericObjectPoolConfig poolConfig )
73      {
74          this( newPoolableConnectionFactory( connectionConfig, apiService, timeout ), poolConfig );
75      }
76  
77  
78      /**
79       * Instantiates a new LDAP connection pool.
80       *
81       * @param factory The LDAP connection factory
82       */
83      public LdapConnectionPool( PooledObjectFactory<LdapConnection> factory )
84      {
85          this( factory, null );
86      }
87  
88  
89      /**
90       * Instantiates a new LDAP connection pool.
91       *
92       * @param factory The LDAP connection factory
93       * @param poolConfig The pool configuration
94       */
95      public LdapConnectionPool( PooledObjectFactory<LdapConnection> factory, GenericObjectPoolConfig poolConfig )
96      {
97          super( factory, poolConfig == null ? new GenericObjectPoolConfig() : poolConfig );
98          this.factory = factory;
99      }
100 
101 
102     /**
103      * Returns the LdapApiService instance used by this connection pool.
104      *
105      * @return The LdapApiService instance used by this connection pool.
106      */
107     public LdapApiService getLdapApiService()
108     {
109         return ( ( AbstractPoolableLdapConnectionFactory ) factory ).getLdapApiService();
110     }
111 
112 
113     /**
114      * Gives a LdapConnection fetched from the pool.
115      *
116      * @return an LdapConnection object from pool
117      * @throws LdapException if an error occurs while obtaining a connection from the factory
118      */
119     public LdapConnection getConnection() throws LdapException
120     {
121         LdapConnection connection;
122 
123         try
124         {
125             connection = super.borrowObject();
126             
127             if ( LOG.isTraceEnabled() )
128             {
129                 LOG.trace( I18n.msg( I18n.MSG_04163_BORROWED_CONNECTION, connection ) );
130             }
131         }
132         catch ( LdapException | RuntimeException e )
133         {
134             throw e;
135         }
136         catch ( Exception e )
137         {
138             // wrap in runtime, but this should NEVER happen per published 
139             // contract as it only throws what the makeObject throws and our 
140             // PoolableLdapConnectionFactory only throws LdapException
141             LOG.error( I18n.err( I18n.ERR_04107_UNEXPECTED_THROWN_EXCEPTION, e.getMessage() ), e );
142             throw new RuntimeException( e );
143         }
144 
145         return connection;
146     }
147 
148 
149     private static ValidatingPoolableLdapConnectionFactory newPoolableConnectionFactory(
150         LdapConnectionConfig connectionConfig, LdapApiService apiService,
151         long timeout )
152     {
153         DefaultLdapConnectionFactory connectionFactory =
154             new DefaultLdapConnectionFactory( connectionConfig );
155         connectionFactory.setLdapApiService( apiService );
156         connectionFactory.setTimeOut( timeout );
157         return new ValidatingPoolableLdapConnectionFactory( connectionFactory );
158     }
159 
160 
161     /**
162      * Places the given LdapConnection back in the pool.
163      * 
164      * @param connection the LdapConnection to be released
165      * @throws LdapException if an error occurs while releasing the connection
166      */
167     public void releaseConnection( LdapConnection connection ) throws LdapException
168     {
169         try
170         {
171             super.returnObject( connection );
172 
173             if ( LOG.isTraceEnabled() )
174             {
175                 LOG.trace( I18n.msg( I18n.MSG_04164_RETURNED_CONNECTION, connection ) );
176             }
177         }
178         catch ( Exception e )
179         {
180             // wrap in runtime, but this should NEVER happen as it only throws 
181             // what the passivateObject throws and our 
182             // PoolableLdapConnectionFactory only throws LdapException
183             LOG.error( I18n.err( I18n.ERR_04107_UNEXPECTED_THROWN_EXCEPTION, e.getMessage() ), e );
184             throw new RuntimeException( e );
185         }
186     }
187 }