/*
File Name		:	GBMDBPool.java
Purpose			:	GBM Database Pool class implementing Singleton pattern
					for maintaining Single instance of the same in the application.
					Extends Resin's Database Connection Pooling component.
Author			:	Prakash T.L.
Creation Date	:	18-Feb-2004
Modification Log:
Modified By		:
Modified Date	:
*/

package com.gbm.common;

import java.io.*;
import java.util.*;
import com.caucho.sql.DBPool;
import com.gbm.common.GBMGeneralComponent;
import com.gbm.common.security.GBMKeyDecryption;
import java.sql.Connection;
import java.sql.SQLException;
import oracle.jdbc.pool.OracleConnectionPoolDataSource;

import javax.naming.NamingException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import java.sql.DriverManager;

public class GBMDBPool {

	private static GBMDBPool gbmDBpool = null;

		private GBMDBPool(){
	}

	private static DataSource ds;
	private	static String sPassword = null;
	private	static String sUserId="";


public static java.sql.Connection getConnection(){

	Connection dbConnection = null; 
	oracle.jdbc.driver.OracleConnection conn = null;

	try{

		if(ds==null)
		{
			System.out.println("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
			String DATA_SOURCE;
			GBMGeneralComponent gbmgc =  null;
			GBMKeyDecryption gbmkd = null;
			FileInputStream fin = null;
			Properties propConnect = null;
			ArrayList vErrorObjects = new ArrayList();

			gbmgc = new GBMGeneralComponent();
			gbmkd = new GBMKeyDecryption();

			gbmkd.doDecrypt(vErrorObjects);
			sPassword = gbmkd.getDecryptedString();

			//Get the Application Path
			String sAppPath = gbmgc.getApplicationPath();

			//Create a FileInputStream Object for the properties file and load it into a Property Object
			fin = new FileInputStream(sAppPath+"/properties/connection.properties");
			propConnect = new Properties();
			propConnect.load(fin);
			sUserId=propConnect.getProperty("UserName");

			try{
				Context ctx = new InitialContext();
				ds = (DataSource)ctx.lookup("java:comp/env/jdbc/test");
				//ds.setPassword(sPassword);
				//ds.setUser(sUserId);
				//System.out.println("Inside the try block. For the first time" + ds.toString());
				//System.out.println("No. of Maximum Connections" + ds.getMaxConnections());
				//System.out.println("ds.setPassword" + ds.getPassword());
				//System.out.println("ds.setUser" + ds.getUser());
				ctx.close();

			}catch (NamingException e){

				System.out.println("Class Not Found exception during getConnection"+ e );
			} catch (Exception ex){
				ex.printStackTrace();
			}
			
			finally{
				gbmgc = null;
				gbmkd = null;
				propConnect = null;
				try{
					if (fin != null){
						fin.close();
						fin = null;
					}
				}
				catch(Exception e){
					System.out.println(e);
				}
			}

		} //end of if ds=null

	//System.out.println("No. of Active Connections bef" + ds.getActiveConnections());
	dbConnection = ds.getConnection(sUserId,sPassword);
	//dbConnection = ds.getConnection();
	//System.out.println("No. of Active Connections after" + ds.getActiveConnections());
	
	}catch(Exception e){
		System.out.println(e);
		return null;
	}
	finally{

	}
	return  dbConnection;
}



/*	public static GBMDBPool getDBPoolInstance(ArrayList vErrorObjects){

		if(gbmDBpool == null){

			GBMGeneralComponent gbmgc =  null;
			GBMKeyDecryption gbmkd = null;
			FileInputStream fin = null;
			Properties propConnect = null;

        	try{
				gbmgc = new GBMGeneralComponent();
				gbmkd = new GBMKeyDecryption();
				String sPassword = null;

				gbmkd.doDecrypt(vErrorObjects);
				sPassword = gbmkd.getDecryptedString();

				//Get the Application Path
				String sAppPath = gbmgc.getApplicationPath();

				//Create a FileInputStream Object for the properties file and load it into a Property Object
				fin = new FileInputStream(sAppPath+"/properties/connection.properties");
				propConnect = new Properties();
				propConnect.load(fin);

				//Establish Connection using the DSNName and UserName given in the properties file

				gbmDBpool = new GBMDBPool();
				//System.out.println("New Instance of GBMDBPool : " + gbmDBpool.toString());

				gbmDBpool.setURL(propConnect.getProperty("DSNName"));
				gbmDBpool.setUser(propConnect.getProperty("UserName"));
				gbmDBpool.setPassword(sPassword);
				gbmDBpool.setMaxConnections(Integer.parseInt(propConnect.getProperty("MaxConnections")));
				gbmDBpool.setDriverName("oracle.jdbc.driver.OracleDriver");
				return gbmDBpool;

			}catch(Exception e){
				//System.out.println(e);
				return null;
			}finally{
				gbmgc = null;
				gbmkd = null;
				propConnect = null;
				try{
					if (fin != null){
						fin.close();
						fin = null;
					}
				}catch(Exception e){
					//System.out.println(e);
				}
			}

		}else{

			return gbmDBpool;
		}

	}*/
}
