/*
 * UserCreationConfig.java,v 1.0 5 juil. 2004
 * Copyright (c) 2004 Esup Portail (www.esup-portail.org)
 * Classes: UserCreationConfig.java
 * Original Author: tbellemb
 */

package org.esupportail.portal.utils.injac.userCreation.config;

import java.io.IOException;
import java.net.URL;
//import javax.servlet.jsp.JspException;
import org.apache.commons.digester.*;
import org.esupportail.portal.utils.injac.userCreation.exception.ConfigException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.apache.log4j.*;

/**
 * @author : tbellemb
 * date : 5 juil. 2004
 */

public class Config {
	
	private String ldapUrl; 			// LDAP URL
	private String ldapPort;            // LDAP port
	private String ldapBaseDn;          // LDAP baseDn
	private String ldapRequestFilter;   // LDAP request filter
	private String ldapUserUidAttribut; // The attribut of users UIDs in the LDAP directory
	private String ldapMaxResults;		// Max number of results returned by the LDAP directory
	
	private String webdavServerUrl;		 // WebDav server URL
	private String webdavServerLogin;	 // WebDav server login
	private String webdavServerPassword; // WebDav server password
	private String webdavServerTrustedPassword; //WebDav server trusted password
	private String webdavServerUsersRole;	    //Role assigned to users
		
	private String userDirectoryRootDirectory; // directory where users are stored on the WebDav server

	private String roleDirectoryRootDirectory; // directory where roles are stored on the WebDav server
	
	private static Config singleton = null;
	
	private Logger logger = Logger.getLogger("UserCreationConfig");
	
	private Config() throws ConfigException{
		
		//  Getting the configuration file
		Class requestingClass = this.getClass();
		URL resourceURL = requestingClass.getResource("/properties/config.xml");
		if (resourceURL == null){
			throw new ConfigException("Config","init","Configuration file not found");
		}
		
		//  Creating a Digester
		Digester dig = new Digester();
		dig.push(this);
		
		// Registering the entries of the configuration file
		dig.addCallMethod("root/ldapDirectory","configLdap",6);
		dig.addCallParam("root/ldapDirectory",0,"ldapUrl");
		dig.addCallParam("root/ldapDirectory",1,"port");
		dig.addCallParam("root/ldapDirectory",2,"baseDn");
		dig.addCallParam("root/ldapDirectory",3,"filter");
		dig.addCallParam("root/ldapDirectory",4,"userUidAttribut");
		dig.addCallParam("root/ldapDirectory",5,"maxResults");
		
		dig.addCallMethod("root/webdavServer","configWebdavServer",5);
		dig.addCallParam("root/webdavServer",0,"url");
		dig.addCallParam("root/webdavServer",1,"login");
		dig.addCallParam("root/webdavServer",2,"password");
		dig.addCallParam("root/webdavServer",3,"trustedPassword");
		dig.addCallParam("root/webdavServer",4,"usersRole");
		
		dig.addCallMethod("root/usersDirectory","configUserDirectory",1);
		dig.addCallParam("root/usersDirectory",0,"rootDirectory");
		
		dig.addCallMethod("root/rolesDirectory","configRoleDirectory",1);
		dig.addCallParam("root/rolesDirectory",0,"rootDirectory");
		
		try{
			//  Reading the configuration file
			dig.parse(new InputSource(resourceURL.toExternalForm()));
		}
		catch(SAXException saxException){
			logger.info("UserCreationConfig::HomedirCreationConfig::"+saxException);
			throw new ConfigException();}
		catch(IOException ioException){
			logger.info("UserCreationConfig::HomedirCreationConfig::"+ioException);
			throw new ConfigException();}
		
	}// Config
	
	/**
	 *
	 * @param serverName Parameter of file Config.xml
	 * @param port Parameter of file Config.xml
	 *
	 */
	public void configLdap(String ldapUrl, String ldapPort, String ldapBaseDn, String ldapRequestFilter, String ldapUserUidAttribut, String ldapMaxResults){
		this.ldapUrl = ldapUrl;
		this.ldapPort = ldapPort;
		this.ldapBaseDn = ldapBaseDn;
		this.ldapRequestFilter = ldapRequestFilter;
		this.ldapUserUidAttribut = ldapUserUidAttribut;
		this.ldapMaxResults = ldapMaxResults;
	}// configLdap
	
	public void configWebdavServer(String webdavServerUrl, String webdavServerLogin, String webdavServerPassword, String webdavServerTrustedPassword, String webdavServerUsersRole){
		this.webdavServerUrl = webdavServerUrl;
		this.webdavServerLogin = webdavServerLogin;
		this.webdavServerPassword = webdavServerPassword;
		this.webdavServerTrustedPassword = webdavServerTrustedPassword;
		this.webdavServerUsersRole = webdavServerUsersRole;
	}// configWebdavServer
	
	public void configUserDirectory(String userDirectoryRootDirectory){
		this.userDirectoryRootDirectory = userDirectoryRootDirectory;
	}// configUserDirectory
	
	public void configRoleDirectory(String roleDirectoryRootDirectory){
		this.roleDirectoryRootDirectory = roleDirectoryRootDirectory;
	}// configUserDirectory
	
	public static Config getInstance() throws ConfigException{
		if(singleton == null) {
			singleton = new Config();
		}
		return singleton;
	}// getInstance
	

	/**
	 * @return Returns the singleton.
	 */
	public static Config getSingleton() {
		return singleton;
	}
	/**
	 * @return Returns the ldapBaseDn.
	 */
	public String getLdapBaseDn() {
		return ldapBaseDn;
	}
	/**
	 * @return Returns the ldapMaxResults.
	 */
	public String getLdapMaxResults() {
		return ldapMaxResults;
	}
	/**
	 * @return Returns the ldapPort.
	 */
	public String getLdapPort() {
		return ldapPort;
	}
	/**
	 * @return Returns the ldapRequestFilter.
	 */
	public String getLdapRequestFilter() {
		return ldapRequestFilter;
	}
	/**
	 * @return Returns the ldapUrl.
	 */
	public String getLdapUrl() {
		return ldapUrl;
	}
	/**
	 * @return Returns the ldapUserUidAttribut.
	 */
	public String getLdapUserUidAttribut() {
		return ldapUserUidAttribut;
	}
	/**
	 * @return Returns the logger.
	 */
	public Logger getLogger() {
		return logger;
	}
	/**
	 * @return Returns the roleDirectoryRootDirectory.
	 */
	public String getRoleDirectoryRootDirectory() {
		return roleDirectoryRootDirectory;
	}
	/**
	 * @return Returns the userDirectoryRootDirectory.
	 */
	public String getUserDirectoryRootDirectory() {
		return userDirectoryRootDirectory;
	}
	/**
	 * @return Returns the webdavServerLogin.
	 */
	public String getWebdavServerLogin() {
		return webdavServerLogin;
	}
	/**
	 * @return Returns the webdavServerPassword.
	 */
	public String getWebdavServerPassword() {
		return webdavServerPassword;
	}
	/**
	 * @return Returns the webdavServerTrustedPassword.
	 */
	public String getWebdavServerTrustedPassword() {
		return webdavServerTrustedPassword;
	}
	/**
	 * @return Returns the webdavServerUrl.
	 */
	public String getWebdavServerUrl() {
		return webdavServerUrl;
	}
	/**
	 * @return Returns the webdavServerUsersRole.
	 */
	public String getWebdavServerUsersRole() {
		return webdavServerUsersRole;
	}
}
