package testfactorymethod;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

/**
 * A simple data source for getting database connections.
 */
public class SimpleDataSource {

    private static String url;
    private static String username;
    private static String password;
    private static DaoFactory obj;

    /**
      Initializes the data source.
      @param fileName the name of the property file that
      contains the database driver, url, username and password
     * @throws IllegalAccessException 
     * @throws InstantiationException 
     */
    public static void init(String fileName)
    throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        Properties props = new Properties();
        FileInputStream in = new FileInputStream(fileName);
        props.load(in);
        
        String driver = props.getProperty("jdbc.driver");
        url = props.getProperty("jdbc.url");
        username = props.getProperty("jdbc.username");
        password = props.getProperty("jdbc.password");
        Class fabrica = Class.forName(props.getProperty("fabrica"));
        obj = (DaoFactory) fabrica.newInstance();  
        Class.forName(driver);
    }
    
    /**
      Gets a connection to the database.
      @return the database connection
     */
    
    public static DaoFactory getFabrica(){
    	return obj;
    }
}
