package edu.vt.marian.common;

import java.io.*;
import java.net.*;
import java.util.*;


/**
    class name:  Configuration
    class description: this class contains the configuration information about the 
            whole system, including the directory names of other objects.
    uses the services of class(es): 
    designer(s): Jianxin Zhao (jxzhao@csgrad.cs.vt.edu)
    implementator(s): Ning Chai (nchai@csgrad.cs.vt.edu)
    finished time: Nov 23, 1998
    known bugs: 
    JDK version: 1.1.5
    side effects:
*/


public class Configuration
{
	// Bindings for names.
    private Dictionary cdt;
	
    /** this is just used for debugging
    */
    Debug debug;
	
    /**
        method description: this constructor will create a configuration object 
                from the specified directory/file
        uses the services of class(es):
        input parameter(s): 
                file_name -- this directory contains all the configuration information
                        of the system
                debug -- used for debugging
        output parameter(s): none
        return value: none
        synchronization: none
    */
    public Configuration(String config_file_name, Debug debug)
    {
        this.debug = debug;

        cdt = new Hashtable();

        if ( (config_file_name == null) || config_file_name.equals("") )
        {
			debug.dumpTrace("configuration.[constructor]: config file name is null or empty.");
            return;
        }

        try
        {
            BufferedReader in_config_file = null;

            String line = null;
            StringTokenizer token_line = null;      //hold one line of marian.cfg file
            String stoken;

            String pclass_name;     //First parameter of class_dir_table's method add
            String pdir_name;       //Second parameter of class_dir_table's method add

            in_config_file = new BufferedReader(new FileReader(config_file_name));

            line = in_config_file.readLine();
            while (line != null)
            {
                token_line = new StringTokenizer(line," ",false);
                if (token_line.countTokens() > 1) //in  case an empty line is encountered
                {
                    //ignore empty lines and lines with only one token
                    stoken = token_line.nextToken();
                    if (! stoken.startsWith("#")) //ignore comments(begin with "#")
                    {
                        pclass_name = stoken;
                        pdir_name = token_line.nextToken();

						cdt.put(pclass_name, pdir_name);
                    }
                }
                line = in_config_file.readLine();
            }
            in_config_file.close();
        } 
        catch (IOException e) 
        {
			debug.dumpTrace("Configuration.[constructor]: FileInputStream problem for config file: "
							+ e.getMessage());
            return; 
        }

        return; //Success
    }


    /**
            method description: this method will return the directory/file name of the 
                    specified object in the system
            uses the services of class(es): 
            input parameter(s): 
                    name --- the name of the object
            output parameter(s): none
            return value: 
                    a directory name as a string -- the directory should contain all the
                            configuration information about the object
                    null -- there is no directory corresponds to the specified object
            synchronization: none
    */      
    public String get_binding(String name)
    {
		String value = (String) cdt.get(name);
        return( value );
    }
	
    public String get_dir(String name)
    {
		String value = (String) cdt.get(name);
        if (! value.endsWith(File.separator))
            value = value.concat(File.separator);
        return( value );
    }
	
	public int get_port()
	{
		String value = (String) cdt.get("Port");
        return( Integer.parseInt(value) );
	}
}

