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 * http://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.ldap.codec.api.LdapApiService; 026import org.apache.directory.api.ldap.model.exception.LdapException; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029 030 031/** 032 * The default implementation of LdapConnectionFactory. Allows for the 033 * setting of timeout and {@link LdapApiService} as well as the standard 034 * {@link LdapConnectionConfig}. 035 * 036 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 037 */ 038public class DefaultLdapConnectionFactory implements LdapConnectionFactory 039{ 040 private static Logger LOG = LoggerFactory.getLogger( DefaultLdapConnectionFactory.class ); 041 042 private LdapApiService apiService; 043 private LdapConnectionConfig connectionConfig; 044 private long timeout; 045 046 047 /** 048 * Creates a new instance of DefaultLdapConnectionFactory. 049 * 050 * @param config The connection config. 051 */ 052 public DefaultLdapConnectionFactory( LdapConnectionConfig config ) 053 { 054 this.connectionConfig = config; 055 this.timeout = config.getDefaultTimeout(); 056 } 057 058 059 @Override 060 public LdapConnection bindConnection( LdapConnection connection ) throws LdapException 061 { 062 try 063 { 064 connection.bind( connectionConfig.getName(), connectionConfig.getCredentials() ); 065 } 066 catch ( LdapException e ) 067 { 068 LOG.error( "unable to bind connection: {}", e.getMessage() ); 069 LOG.debug( "unable to bind connection:", e ); 070 try 071 { 072 connection.close(); 073 } 074 catch ( IOException ioe ) 075 { 076 LOG.error( "unable to close failed bind connection: {}", e.getMessage() ); 077 LOG.debug( "unable to close failed bind connection:", e ); 078 } 079 throw e; 080 } 081 return connection; 082 } 083 084 085 @Override 086 public LdapConnection configureConnection( LdapConnection connection ) 087 { 088 connection.setTimeOut( timeout ); 089 connection.setBinaryAttributeDetector( connectionConfig.getBinaryAttributeDetector() ); 090 return connection; 091 } 092 093 094 @Override 095 public LdapApiService getLdapApiService() 096 { 097 return apiService; 098 } 099 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}