/**
 * @author Raj Prakash
 */
package com.tandbergtv.workflow.webservice.filesubsystem;

import java.util.Map;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.io.*;

//singleton class
public class FTPConfigurationManager
{
	private static final String FTP_CONF_FILE = "com/tandbergtv/workflow/webservice/filesubsystem/ftpconfiguration.txt";
	
	private static FTPConfigurationManager _instance;
	
	private Map<String,String> hostUserNameMap = new HashMap<String,String>();
	private Map<String,String> hostPasswordMap = new HashMap<String,String>();
	
	private FTPConfigurationManager() throws IOException
	{
		loadConfiguration();
	}
	
	public static FTPConfigurationManager getInstance() throws IOException
	{
		if(_instance == null)
			_instance = new FTPConfigurationManager();
		return _instance;
	}
	
	private void loadConfiguration() throws IOException
	{
		//read config file and fill in the map objects
		BufferedReader br = new BufferedReader(new InputStreamReader (this.getClass().getClassLoader().getResourceAsStream(FTP_CONF_FILE)));
		String line;
		while((line=br.readLine()) != null)
		{
			line = line.trim();
			if(line.equals("") || line.startsWith("#"))
				continue;
			StringTokenizer st = new StringTokenizer(line, "\t");
			String host="", userName="", password="";
			if (st.hasMoreTokens()) 
				host = st.nextToken();
			if (st.hasMoreTokens()) 
				userName = st.nextToken();
			if (st.hasMoreTokens()) 
				password = st.nextToken();
			hostUserNameMap.put(host, userName);
			hostPasswordMap.put(host, password);			
		}
	}
	
	public String getUserName(String host)
	{
		return hostUserNameMap.get(host);
	}
	
	public String getPassword(String host)
	{
		return hostPasswordMap.get(host);
	}

}
