1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.ldap.client.api;
21
22
23 import java.io.IOException;
24
25 import org.apache.directory.api.i18n.I18n;
26 import org.apache.directory.api.ldap.codec.api.LdapApiService;
27 import org.apache.directory.api.ldap.model.exception.LdapException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31
32
33
34
35
36
37
38
39 public class DefaultLdapConnectionFactory implements LdapConnectionFactory
40 {
41 private static final Logger LOG = LoggerFactory.getLogger( DefaultLdapConnectionFactory.class );
42
43 private LdapApiService apiService;
44 private LdapConnectionConfig connectionConfig;
45 private long timeout;
46
47
48
49
50
51
52
53 public DefaultLdapConnectionFactory( LdapConnectionConfig config )
54 {
55 this.connectionConfig = config;
56 this.timeout = config.getTimeout();
57 }
58
59
60
61
62
63 @Override
64 public LdapConnection bindConnection( LdapConnection connection ) throws LdapException
65 {
66 try
67 {
68 connection.bind( connectionConfig.getName(), connectionConfig.getCredentials() );
69 }
70 catch ( LdapException e )
71 {
72 LOG.error( I18n.err( I18n.ERR_04102_UNABLE_TO_BIND_CONNECTION, e.getMessage() ) );
73
74 if ( LOG.isDebugEnabled() )
75 {
76 LOG.debug( I18n.msg( I18n.MSG_04158_UNABLE_TO_BIND, e.getMessage() ) );
77 }
78
79 try
80 {
81 connection.close();
82 }
83 catch ( IOException ioe )
84 {
85 LOG.error( I18n.err( I18n.ERR_04103_UNABLE_TO_CLOSE_FAILED_CONNECTION, e.getMessage() ), ioe );
86
87 if ( LOG.isDebugEnabled() )
88 {
89 LOG.debug( I18n.msg( I18n.MSG_04159_UNABLE_TO_CLOSE_CONNECTION, e.getMessage() ) );
90 }
91 }
92
93 throw e;
94 }
95
96 return connection;
97 }
98
99
100
101
102
103 @Override
104 public LdapConnection configureConnection( LdapConnection connection )
105 {
106 connection.setTimeOut( timeout );
107 connection.setBinaryAttributeDetector( connectionConfig.getBinaryAttributeDetector() );
108 return connection;
109 }
110
111
112
113
114
115 @Override
116 public LdapApiService getLdapApiService()
117 {
118 return apiService;
119 }
120
121
122
123
124
125 @Override
126 public LdapConnection newLdapConnection() throws LdapException
127 {
128 return bindConnection( newUnboundLdapConnection() );
129 }
130
131
132
133
134
135 @Override
136 public LdapConnection newUnboundLdapConnection()
137 {
138 if ( apiService == null )
139 {
140 return configureConnection( new LdapNetworkConnection( connectionConfig ) );
141 }
142 else
143 {
144 return configureConnection( new LdapNetworkConnection( connectionConfig, apiService ) );
145 }
146 }
147
148
149
150
151
152
153
154
155
156 public void setLdapApiService( LdapApiService apiService )
157 {
158 this.apiService = apiService;
159 }
160
161
162
163
164
165
166
167
168
169
170 public void setTimeOut( long timeout )
171 {
172 this.timeout = timeout;
173 }
174 }