001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  You may obtain a copy of the License at
009 *
010 *     https://www.apache.org/licenses/LICENSE-2.0
011 *
012 *   Unless required by applicable law or agreed to in writing,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020package org.apache.directory.ldap.client.api;
021
022
023import java.io.IOException;
024
025import org.apache.directory.api.i18n.I18n;
026import org.apache.directory.api.ldap.codec.api.LdapApiService;
027import org.apache.directory.api.ldap.model.exception.LdapException;
028import org.slf4j.Logger;
029import org.slf4j.LoggerFactory;
030
031
032/**
033 * The default implementation of LdapConnectionFactory. Allows for the 
034 * setting of timeout and {@link LdapApiService} as well as the standard 
035 * {@link LdapConnectionConfig}.
036 * 
037 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
038 */
039public class DefaultLdapConnectionFactory implements LdapConnectionFactory
040{
041    private static final Logger LOG = LoggerFactory.getLogger( DefaultLdapConnectionFactory.class );
042
043    private LdapApiService apiService;
044    private LdapConnectionConfig connectionConfig;
045    private long timeout;
046
047
048    /**
049     * Creates a new instance of DefaultLdapConnectionFactory.
050     *
051     * @param config The connection config.
052     */
053    public DefaultLdapConnectionFactory( LdapConnectionConfig config )
054    {
055        this.connectionConfig = config;
056        this.timeout = config.getTimeout();
057    }
058
059
060    /**
061     * {@inheritDoc}
062     */
063    @Override
064    public LdapConnection bindConnection( LdapConnection connection ) throws LdapException
065    {
066        try
067        {
068            connection.bind( connectionConfig.getName(), connectionConfig.getCredentials() );
069        }
070        catch ( LdapException e )
071        {
072            LOG.error( I18n.err( I18n.ERR_04102_UNABLE_TO_BIND_CONNECTION, e.getMessage() ) );
073            
074            if ( LOG.isDebugEnabled() )
075            {
076                LOG.debug( I18n.msg( I18n.MSG_04158_UNABLE_TO_BIND, e.getMessage() ) );
077            }
078
079            try
080            {
081                connection.close();
082            }
083            catch ( IOException ioe )
084            {
085                LOG.error( I18n.err( I18n.ERR_04103_UNABLE_TO_CLOSE_FAILED_CONNECTION, e.getMessage() ), ioe );
086
087                if ( LOG.isDebugEnabled() )
088                {
089                    LOG.debug( I18n.msg( I18n.MSG_04159_UNABLE_TO_CLOSE_CONNECTION, e.getMessage() ) );
090                }
091            }
092
093            throw e;
094        }
095
096        return connection;
097    }
098
099
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}