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