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.io.IOException;
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.constants.SchemaConstants;
29 import org.apache.directory.api.ldap.model.exception.LdapException;
30 import org.apache.directory.api.ldap.model.name.Dn;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34
35
36
37
38
39
40 public class PoolableLdapConnectionFactory implements PoolableObjectFactory<LdapConnection>
41 {
42 private static final Logger LOG = LoggerFactory.getLogger( PoolableLdapConnectionFactory.class );
43
44 private LdapConnectionFactory connectionFactory;
45
46
47
48
49
50
51
52 public PoolableLdapConnectionFactory( LdapConnectionConfig config )
53 {
54 this( new DefaultLdapConnectionFactory( config ) );
55 }
56
57
58
59
60
61
62
63 public PoolableLdapConnectionFactory( LdapConnectionFactory connectionFactory )
64 {
65 this.connectionFactory = connectionFactory;
66 }
67
68
69
70
71
72 public void activateObject( LdapConnection connection )
73 {
74 LOG.debug( "Activating {}", connection );
75 }
76
77
78
79
80
81 public void destroyObject( LdapConnection connection )
82 {
83 LOG.debug( "Destroying {}", connection );
84 try {
85 connection.unBind();
86 }
87 catch ( LdapException e ) {
88 LOG.error( "unable to unbind connection: {}", e.getMessage() );
89 LOG.debug( "unable to unbind connection:", e );
90 }
91
92 try {
93 connection.close();
94 }
95 catch ( IOException e ) {
96 LOG.error( "unable to close connection: {}", e.getMessage() );
97 LOG.debug( "unable to close connection:", e );
98 }
99 }
100
101
102
103
104
105
106
107 public LdapApiService getLdapApiService()
108 {
109 return connectionFactory.getLdapApiService();
110 }
111
112
113
114
115
116
117 public LdapConnection makeObject() throws LdapException
118 {
119 LOG.debug( "Creating a LDAP connection" );
120 return connectionFactory.newLdapConnection();
121 }
122
123
124
125
126
127
128 public void passivateObject( LdapConnection connection ) throws LdapException
129 {
130 LOG.debug( "Passivating {}", connection );
131
132
133
134 connectionFactory.bindConnection(
135 connectionFactory.configureConnection( connection ) );
136 }
137
138
139
140
141
142 public boolean validateObject( LdapConnection connection )
143 {
144 LOG.debug( "Validating {}", connection );
145
146 if ( connection.isConnected() )
147 {
148 try
149 {
150 return connection.lookup( Dn.ROOT_DSE, SchemaConstants.NO_ATTRIBUTE ) != null;
151 }
152 catch ( LdapException le )
153 {
154 return false;
155 }
156 }
157 else
158 {
159 return false;
160 }
161 }
162 }