View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
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   * The default implementation of LdapConnectionFactory. Allows for the 
33   * setting of timeout and {@link LdapApiService} as well as the standard 
34   * {@link LdapConnectionConfig}.
35   * 
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public class DefaultLdapConnectionFactory implements LdapConnectionFactory
39  {
40      private static Logger LOG = LoggerFactory.getLogger( DefaultLdapConnectionFactory.class );
41  
42      private LdapApiService apiService;
43      private LdapConnectionConfig connectionConfig;
44      private long timeout;
45  
46  
47      /**
48       * Creates a new instance of DefaultLdapConnectionFactory.
49       *
50       * @param config The connection config.
51       */
52      public DefaultLdapConnectionFactory( LdapConnectionConfig config )
53      {
54          this.connectionConfig = config;
55          this.timeout = config.getDefaultTimeout();
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              try
71              {
72                  connection.close();
73              }
74              catch ( IOException ioe )
75              {
76                  LOG.error( "unable to close failed bind connection: {}", e.getMessage() );
77                  LOG.debug( "unable to close failed bind connection:", e );
78              }
79              throw e;
80          }
81          return connection;
82      }
83  
84  
85      @Override
86      public LdapConnection configureConnection( LdapConnection connection )
87      {
88          connection.setTimeOut( timeout );
89          connection.setBinaryAttributeDetector( connectionConfig.getBinaryAttributeDetector() );
90          return connection;
91      }
92  
93  
94      @Override
95      public LdapApiService getLdapApiService()
96      {
97          return apiService;
98      }
99  
100 
101     @Override
102     public LdapConnection newLdapConnection() throws LdapException
103     {
104         return bindConnection( newUnboundLdapConnection() );
105     }
106 
107 
108     @Override
109     @SuppressWarnings("resource")
110     public LdapConnection newUnboundLdapConnection()
111     {
112         return configureConnection( apiService == null
113             ? new LdapNetworkConnection( connectionConfig )
114             : new LdapNetworkConnection( connectionConfig, apiService ) );
115     }
116 
117 
118     /**
119      * Sets the LdapApiService (codec) to be used by the connections created
120      * by this factory.
121      *
122      * @param apiService The codec to used by connections created by this 
123      * factory
124      */
125     public void setLdapApiService( LdapApiService apiService )
126     {
127         this.apiService = apiService;
128     }
129 
130 
131     /**
132      * Sets the timeout that will be used by all connections created by this
133      * factory.
134      *
135      * @param timeout The timeout in millis.
136      * 
137      * @see LdapConnection#setTimeOut(long)
138      */
139     public void setTimeOut( long timeout )
140     {
141         this.timeout = timeout;
142     }
143 
144 }