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 */ 020 021package org.apache.directory.ldap.client.api; 022 023 024import org.apache.commons.pool.PoolableObjectFactory; 025import org.slf4j.Logger; 026import org.slf4j.LoggerFactory; 027 028 029/** 030 * A factory for creating LdapConnection objects managed by LdapConnectionPool. 031 * 032 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a> 033 */ 034public class PoolableLdapConnectionFactory implements PoolableObjectFactory 035{ 036 037 /** configuration object for the connection */ 038 private LdapConnectionConfig config; 039 040 /** the logger */ 041 private static final Logger LOG = LoggerFactory.getLogger( PoolableLdapConnectionFactory.class ); 042 043 044 /** 045 * 046 * Creates a new instance of PoolableLdapConnectionFactory for the 047 * server running on localhost at the port 10389 048 * 049 * @param config the configuration for creating LdapConnections 050 */ 051 public PoolableLdapConnectionFactory( LdapConnectionConfig config ) 052 { 053 this.config = config; 054 } 055 056 057 /** 058 * {@inheritDoc} 059 */ 060 public void activateObject( Object obj ) throws Exception 061 { 062 LOG.debug( "activating {}", obj ); 063 } 064 065 066 /** 067 * {@inheritDoc} 068 */ 069 public void destroyObject( Object obj ) throws Exception 070 { 071 LOG.debug( "destroying {}", obj ); 072 LdapConnection connection = ( LdapConnection ) obj; 073 connection.unBind(); 074 connection.close(); 075 } 076 077 078 /** 079 * {@inheritDoc} 080 */ 081 public Object makeObject() throws Exception 082 { 083 LOG.debug( "creating a LDAP connection" ); 084 085 LdapNetworkConnection connection = new LdapNetworkConnection( config ); 086 connection.bind( config.getName(), config.getCredentials() ); 087 return connection; 088 } 089 090 091 /** 092 * {@inheritDoc} 093 */ 094 public void passivateObject( Object obj ) throws Exception 095 { 096 LOG.debug( "passivating {}", obj ); 097 } 098 099 100 /** 101 * {@inheritDoc} 102 */ 103 public boolean validateObject( Object obj ) 104 { 105 LOG.debug( "validating {}", obj ); 106 107 LdapConnection connection = ( LdapConnection ) obj; 108 return connection.isConnected(); 109 } 110 111}