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   *     https://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.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   * The default implementation of LdapConnectionFactory. Allows for the 
34   * setting of timeout and {@link LdapApiService} as well as the standard 
35   * {@link LdapConnectionConfig}.
36   * 
37   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
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       * Creates a new instance of DefaultLdapConnectionFactory.
50       *
51       * @param config The connection config.
52       */
53      public DefaultLdapConnectionFactory( LdapConnectionConfig config )
54      {
55          this.connectionConfig = config;
56          this.timeout = config.getTimeout();
57      }
58  
59  
60      /**
61       * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
114      */
115     @Override
116     public LdapApiService getLdapApiService()
117     {
118         return apiService;
119     }
120 
121 
122     /**
123      * {@inheritDoc}
124      */
125     @Override
126     public LdapConnection newLdapConnection() throws LdapException
127     {
128         return bindConnection( newUnboundLdapConnection() );
129     }
130 
131 
132     /**
133      * {@inheritDoc}
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      * Sets the LdapApiService (codec) to be used by the connections created
151      * by this factory.
152      *
153      * @param apiService The codec to used by connections created by this 
154      * factory
155      */
156     public void setLdapApiService( LdapApiService apiService )
157     {
158         this.apiService = apiService;
159     }
160 
161 
162     /**
163      * Sets the timeout that will be used by all connections created by this
164      * factory.
165      *
166      * @param timeout The timeout in millis.
167      * 
168      * @see LdapConnection#setTimeOut(long)
169      */
170     public void setTimeOut( long timeout )
171     {
172         this.timeout = timeout;
173     }
174 }