1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.directory.ldap.client.api;
22
23
24 import org.apache.commons.pool.PoolableObjectFactory;
25 import org.apache.commons.pool.impl.GenericObjectPool;
26 import org.apache.directory.api.ldap.codec.api.LdapApiService;
27 import org.apache.directory.api.ldap.model.exception.LdapException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31
32
33
34
35
36
37
38
39
40 public class LdapConnectionPool extends GenericObjectPool<LdapConnection>
41 {
42 private static Logger LOG = LoggerFactory.getLogger( LdapConnectionPool.class );
43
44 private PoolableObjectFactory<LdapConnection> factory;
45
46
47
48
49
50
51
52
53
54 public LdapConnectionPool( LdapConnectionConfig connectionConfig,
55 LdapApiService apiService, long timeout )
56 {
57 this( connectionConfig, apiService, timeout, null );
58 }
59
60
61
62
63
64
65
66
67
68
69 public LdapConnectionPool( LdapConnectionConfig connectionConfig,
70 LdapApiService apiService, long timeout, Config poolConfig )
71 {
72 this( newPoolableConnectionFactory( connectionConfig, apiService, timeout ), poolConfig );
73 }
74
75
76
77
78
79
80
81 public LdapConnectionPool( PoolableObjectFactory<LdapConnection> factory )
82 {
83 this( factory, null );
84 }
85
86
87
88
89
90
91
92
93 public LdapConnectionPool( PoolableObjectFactory<LdapConnection> factory, Config poolConfig )
94 {
95 super( factory, poolConfig == null ? new Config() : poolConfig );
96 this.factory = factory;
97 }
98
99
100
101
102
103
104
105 public LdapApiService getLdapApiService()
106 {
107 return ( ( AbstractPoolableLdapConnectionFactory ) factory ).getLdapApiService();
108 }
109
110
111
112
113
114
115
116
117 public LdapConnection getConnection() throws LdapException
118 {
119 LdapConnection connection;
120
121 try
122 {
123 connection = super.borrowObject();
124 LOG.trace( "borrowed connection {}", connection );
125 }
126 catch ( LdapException e )
127 {
128 throw ( e );
129 }
130 catch ( RuntimeException e )
131 {
132 throw ( e );
133 }
134 catch ( Exception e )
135 {
136
137
138
139 LOG.error( "An unexpected exception was thrown: ", e );
140 throw new RuntimeException( e );
141 }
142
143 return connection;
144 }
145
146
147 private static ValidatingPoolableLdapConnectionFactory newPoolableConnectionFactory(
148 LdapConnectionConfig connectionConfig, LdapApiService apiService,
149 long timeout )
150 {
151 DefaultLdapConnectionFactory connectionFactory =
152 new DefaultLdapConnectionFactory( connectionConfig );
153 connectionFactory.setLdapApiService( apiService );
154 connectionFactory.setTimeOut( timeout );
155 return new ValidatingPoolableLdapConnectionFactory( connectionFactory );
156 }
157
158
159
160
161
162
163
164
165 public void releaseConnection( LdapConnection connection ) throws LdapException
166 {
167 try
168 {
169 super.returnObject( connection );
170 LOG.trace( "returned connection {}", connection );
171 }
172 catch ( LdapException e )
173 {
174 throw ( e );
175 }
176 catch ( RuntimeException e )
177 {
178 throw ( e );
179 }
180 catch ( Exception e )
181 {
182
183
184
185 LOG.error( "An unexpected exception was thrown: ", e );
186 throw new RuntimeException( e );
187 }
188 }
189 }