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.ldap.codec.api.LdapApiService;
26 import org.apache.directory.api.ldap.model.exception.LdapException;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30
31
32
33
34
35
36
37
38 public class DefaultLdapConnectionFactory implements LdapConnectionFactory
39 {
40 private static final Logger LOG = LoggerFactory.getLogger( DefaultLdapConnectionFactory.class );
41
42 private LdapApiService apiService;
43 private LdapConnectionConfig connectionConfig;
44 private long timeout;
45
46
47
48
49
50
51
52 public DefaultLdapConnectionFactory( LdapConnectionConfig config )
53 {
54 this.connectionConfig = config;
55 this.timeout = config.getTimeout();
56 }
57
58
59 @Override
60 public LdapConnection bindConnection( LdapConnection connection ) throws LdapException
61 {
62 try
63 {
64 connection.bind( connectionConfig.getName(), connectionConfig.getCredentials() );
65 }
66 catch ( LdapException e )
67 {
68 LOG.error( "unable to bind connection: {}", e.getMessage() );
69 LOG.debug( "unable to bind connection:", e );
70
71 try
72 {
73 connection.close();
74 }
75 catch ( IOException ioe )
76 {
77 LOG.error( "unable to close failed bind connection: {}", e.getMessage() );
78 LOG.debug( "unable to close failed bind connection:", e );
79 }
80
81 throw e;
82 }
83
84 return connection;
85 }
86
87
88 @Override
89 public LdapConnection configureConnection( LdapConnection connection )
90 {
91 connection.setTimeOut( timeout );
92 connection.setBinaryAttributeDetector( connectionConfig.getBinaryAttributeDetector() );
93 return connection;
94 }
95
96
97 @Override
98 public LdapApiService getLdapApiService()
99 {
100 return apiService;
101 }
102
103
104 @Override
105 public LdapConnection newLdapConnection() throws LdapException
106 {
107 return bindConnection( newUnboundLdapConnection() );
108 }
109
110
111 @Override
112 public LdapConnection newUnboundLdapConnection()
113 {
114 if ( apiService == null )
115 {
116 return configureConnection( new LdapNetworkConnection( connectionConfig ) );
117 }
118 else
119 {
120 return configureConnection( new LdapNetworkConnection( connectionConfig, apiService ) );
121 }
122 }
123
124
125
126
127
128
129
130
131
132 public void setLdapApiService( LdapApiService apiService )
133 {
134 this.apiService = apiService;
135 }
136
137
138
139
140
141
142
143
144
145
146 public void setTimeOut( long timeout )
147 {
148 this.timeout = timeout;
149 }
150 }