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