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 java.lang.reflect.Constructor;
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.exception.LdapException;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32
33
34
35
36
37
38 public abstract class AbstractPoolableLdapConnectionFactory implements PoolableObjectFactory<LdapConnection>
39 {
40
41 private static final Logger LOG = LoggerFactory.getLogger( AbstractPoolableLdapConnectionFactory.class );
42
43
44 protected LdapConnectionFactory connectionFactory;
45
46
47 protected LdapConnectionValidator validator = new LookupLdapConnectionValidator();
48
49
50
51
52
53
54 @Override
55 public void activateObject( LdapConnection connection ) throws LdapException
56 {
57 LOG.debug( "Activating {}", connection );
58 if ( !connection.isConnected() || !connection.isAuthenticated() )
59 {
60 LOG.debug( "rebind due to connection dropped on {}", connection );
61 connectionFactory.bindConnection( connection );
62 }
63 }
64
65
66
67
68
69
70
71
72 @Override
73 public void destroyObject( LdapConnection connection ) throws LdapException
74 {
75 LOG.debug( "Destroying {}", connection );
76
77 try
78 {
79
80
81 connection.unBind();
82 }
83 catch ( LdapException e )
84 {
85 LOG.error( "unable to unbind connection: {}", e.getMessage() );
86 LOG.debug( "unable to unbind connection:", e );
87 }
88 }
89
90
91
92
93
94
95
96 public LdapApiService getLdapApiService()
97 {
98 return connectionFactory.getLdapApiService();
99 }
100
101
102
103
104
105
106
107
108
109 @Override
110 public LdapConnection makeObject() throws LdapException
111 {
112 LOG.debug( "Creating a LDAP connection" );
113 return connectionFactory.newLdapConnection();
114 }
115
116
117 protected static LdapConnectionFactory newLdapConnectionFactory(
118 LdapConnectionConfig config,
119 Class<? extends LdapConnectionFactory> connectionFactoryClass )
120 {
121 try
122 {
123 Constructor<? extends LdapConnectionFactory> constructor =
124 connectionFactoryClass.getConstructor( LdapConnectionConfig.class );
125 return constructor.newInstance( config );
126 }
127 catch ( Exception e )
128 {
129 throw new IllegalArgumentException( "unable to create LdapConnectionFactory" + e.getMessage(), e );
130 }
131 }
132
133
134
135
136
137
138
139
140
141
142 @Override
143 public void passivateObject( LdapConnection connection ) throws LdapException
144 {
145 LOG.debug( "Passivating {}", connection );
146 }
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163 public void setValidator( LdapConnectionValidator validator )
164 {
165 this.validator = validator;
166 }
167
168
169
170
171
172
173
174 @Override
175 public boolean validateObject( LdapConnection connection )
176 {
177 LOG.debug( "Validating {}", connection );
178 return validator.validate( connection );
179 }
180 }