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.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
36
37
38
39
40
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
51
52
53
54
55
56 public LdapConnectionPool( LdapConnectionConfig connectionConfig,
57 LdapApiService apiService, long timeout )
58 {
59 this( connectionConfig, apiService, timeout, null );
60 }
61
62
63
64
65
66
67
68
69
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
80
81
82
83 public LdapConnectionPool( PooledObjectFactory<LdapConnection> factory )
84 {
85 this( factory, null );
86 }
87
88
89
90
91
92
93
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
104
105
106
107 public LdapApiService getLdapApiService()
108 {
109 return ( ( AbstractPoolableLdapConnectionFactory ) factory ).getLdapApiService();
110 }
111
112
113
114
115
116
117
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
139
140
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
163
164
165
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
181
182
183 LOG.error( I18n.err( I18n.ERR_04107_UNEXPECTED_THROWN_EXCEPTION, e.getMessage() ), e );
184 throw new RuntimeException( e );
185 }
186 }
187 }