/*
 * Copyright 2004-2007 IDCA. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
 * following conditions are met:
 * 
 *    1. Redistributions of source code must retain the above copyright notice, this list of conditions and
 *        the following disclaimer.
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions
 *        and the following disclaimer in the documentation and/or other materials provided with the
 *        distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY IDCA AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL IDCA OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 * 
 * The views and conclusions contained in the software and documentation are those of the authors and
 * should not be interpreted as representing official policies, either expressed or implied, of IDCA.
 */

package com.idcanet.vasc.backends.ldap;

import java.security.Security;

import com.novell.ldap.LDAPConnection;
import com.novell.ldap.LDAPJSSESecureSocketFactory;
import com.novell.ldap.LDAPSocketFactory;

/**
 * 
 * @author Willem Cazander
 * @version 1.0 Sep 4, 2008
 */
public class SimpleLdapConnectionProvider implements LdapConnectionProvider {

	
	private String ldapHost = "localhost";
	private int ldapPort = LDAPConnection.DEFAULT_PORT;
	private int ldapVersion  = LDAPConnection.LDAP_V3;
	private String bindUser = null;
	private String bindPass = null;

	
	/**
	 * @see com.idcanet.vasc.backends.ldap.LdapConnectionProvider#getLdapConnection()
	 */
	public LDAPConnection getLdapConnection() {
		try {
			
			// if ssl;
			//Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
			//System.setProperty("javax.net.ssl.trustStore", "/tmp/somewhere/ldap.root.crt");
			//LDAPSocketFactory ssf = new LDAPJSSESecureSocketFactory();
			// Set the socket factory as the default for all future connections
			//LDAPConnection.setSocketFactory(ssf);

			LDAPConnection lc = new LDAPConnection();
			lc.connect( ldapHost, ldapPort );
			if (bindUser!=null && bindPass!=null) { 
				lc.bind( ldapVersion, bindUser, bindPass.getBytes("UTF8") );
			}
			return lc;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}


	/**
	 * @return the ldapHost
	 */
	public String getLdapHost() {
		return ldapHost;
	}


	/**
	 * @param ldapHost the ldapHost to set
	 */
	public void setLdapHost(String ldapHost) {
		this.ldapHost = ldapHost;
	}


	/**
	 * @return the ldapPort
	 */
	public int getLdapPort() {
		return ldapPort;
	}


	/**
	 * @param ldapPort the ldapPort to set
	 */
	public void setLdapPort(int ldapPort) {
		this.ldapPort = ldapPort;
	}


	/**
	 * @return the ldapVersion
	 */
	public int getLdapVersion() {
		return ldapVersion;
	}


	/**
	 * @param ldapVersion the ldapVersion to set
	 */
	public void setLdapVersion(int ldapVersion) {
		this.ldapVersion = ldapVersion;
	}


	/**
	 * @return the bindUser
	 */
	public String getBindUser() {
		return bindUser;
	}


	/**
	 * @param bindUser the bindUser to set
	 */
	public void setBindUser(String bindUser) {
		this.bindUser = bindUser;
	}


	/**
	 * @return the bindPass
	 */
	public String getBindPass() {
		return bindPass;
	}


	/**
	 * @param bindPass the bindPass to set
	 */
	public void setBindPass(String bindPass) {
		this.bindPass = bindPass;
	}	
}