/*
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 1999 Bull S.A.
 * Contact: jonas-team@objectweb.org
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
 * USA
 *
 * Initial developer(s): ____________________________________.
 * Contributor(s): ______________________________________.
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */


package org.objectweb.jonas.dbm;

import java.util.Set;
import java.util.HashSet;
import org.enhydra.jdbc.pool.StandardXAPoolDataSource;
import javax.naming.Reference;
import javax.naming.Referenceable;
import javax.naming.NamingException;
import javax.naming.StringRefAddr;

public class JonasXAPoolDataSource extends StandardXAPoolDataSource implements Referenceable, JonasXAPoolDataSourceMBean {

    /**
     * Management param : url and class and name of this connection
     */
    private String url = null;
    private String className = null;
    private String dsName = null;
    private String dsDescription = null;
    public String XAFactory;

    public Reference getReference() throws NamingException {
	// Jonas has its own DataSourceFactory
	log("JonasXAPoolDataSource:getReference return a reference of the object");
	Reference ref = new Reference (this.getClass().getName(), "org.objectweb.jonas.dbm.DataSourceFactory", null);
	ref.add(new StringRefAddr("checkLevelObject", Integer.toString(getCheckLevelObject())));
	ref.add(new StringRefAddr("lifeTime", Long.toString(getLifeTime())));
	ref.add(new StringRefAddr("jdbcTestStmt", getJdbcTestStmt()));
	ref.add(new StringRefAddr("maxSize", Integer.toString(getMaxSize())));
	ref.add(new StringRefAddr("minSize", Integer.toString(getMinSize())));
	ref.add(new StringRefAddr("dataSourceName", getDataSourceName()));
	ref.add(new StringRefAddr("XAFactory", getXAFactory()));
	// The others properties are set in the factory itself
        return ref;
    }
    
    /**
     * MBean method:
     * @return Integer number of open connection in this ds
     */
    public Integer getCurrentNumberOfJDBCConnectionOpen() {
	return new Integer(pool.getCount());
    }

    /**
     * MBean method:
     * @return User of this Connection (from org.enhydra.jdbc.core.CoreDataSource)
     */
    public String getUserName() {
	return getUser();
    }

    /**
     * Management method: set connection url
     * @param url of this connection
     */
    public void setUrl(String url) {
	this.url = url; 
    }


    /**
     * MBean method:
     * @return url of this connection
     */
    public String getUrl() {
       return url;
    }

    /**
     * Management method: set connection className
     * @param className of this connection
     */
    public void setClassName(String className) {
	this.className = className; 
    }

    /**
     * MBean method:
     * @return class of this datasource
     */
    public String getClassName() {
	return className;
    }

    /**
     * MBean method:
     * @param datasource name
     */
    public void setDSName(String dsName) {
	this.dsName = dsName;
    }

    /**
     * MBean method:
     * @return datasource name
     */
    public String getDSName() {
	return dsName;
    }

    /**
     * MBean method:
     * @return the data source factory
     * none or JonasStandardXADataSource
     */
    public String getDataSourceFactory() {
	return "org.objectweb.jonas.dbm.JonasStandardXADataSource";
    }

    /**
     * MBean method:
     * @return the DataSourceXAName
     */
    public String getDataSourceXADataName() {
	return getDataSourceName();
    }    

    /**
     * MBean method:
     * @return connection level
     */
    public Integer getJDBCConnCheckLevel() {
	return new Integer(getCheckLevelObject());
    }

    /**
     * MBean method:
     * @param connection level
     */
    public void setJDBCConnCheckLevel(Integer level) {
	setCheckLevelObject(level.intValue());
    }


    /**
     * MBean method:
     * @return connection max age
     */
    public Integer getJDBCConnMaxAge(){
	return new Integer(""+getLifeTime());
    }
    /**
     * MBean method:
     * @param connection max age
     */
    public void setJDBCConnMaxAge(Integer age) {
	setLifeTime(age.intValue());
    }

    /**
     * MBean method:
     * @return test statement 
     */
    public String getJDBCTestStatement(){
	return getJdbcTestStmt();
    }
    /**
     * MBean method:
     * @param test statement 
     */
    public void setJDBCTestStatement(String test) {
	setJdbcTestStmt(test);
    }

    /**
     * MBean method:
     * @return connection pool min
     */
    public Integer getJDBCMinConnPool(){
	return new Integer(getMinSize());
    }
   /**
     * MBean method: 
     * @param min for connection pool
     */
    public void setJDBCMinConnPool(Integer min){
	try {
	    setMinSize(min.intValue());
	} catch (Exception e) {}
    }
    /**
     * MBean method:
     * @return connection pool max
     */
    public Integer getJDBCMaxConnPool(){
	return new Integer(getMaxSize());
    }
   /**
     * MBean method:
     * @param max for connection pool 
     */
    public void setJDBCMaxConnPool(Integer max){
	try {
	    setMaxSize(max.intValue());
	} catch (Exception e) {}
    }
    /**
     * MBean method:
     * @return Integer sleep Time out
     */
    public Integer getJDBCSleepTimeOut(){
	return new Integer(""+getSleepTime());
    }
   /**
     * MBean method: do nothing 
     * @param Integer sleep Time out
     */
    public void setJDBCSleepTimeOut(Integer timeOut){
	setSleepTime(timeOut.intValue());
    }

    /**
     * MBean method:
     * @return Integer lock pool
     */
    public Integer getJDBCDeadLockPool(){
	return new Integer("" +getDeadLockMaxWait());
    }
   /**
     * MBean method: 
     * @param Integer lock pool
     */
    public void setJDBCDeadLockPool(Integer size){
	setDeadLockMaxWait(size.intValue());
    }
    /**
     * MBean method:
     * @return Integer wait pool
     */
    public Integer getJDBCLoopWaitPool(){
	return new Integer(""+getDeadLockRetryWait());
    }
   /**
     * MBean method:
     * @param Integer wait pool
     */
    public void setJDBCLoopWaitPool(Integer size){
	setDeadLockRetryWait(size.intValue());
    }
    
    /**
     * return the type of this data source
     */
    public String getDataSourceType() {
	String result = null;
	if (getClassName().equals("oracle.jdbc.driver.OracleDriver")) result = "Oracle";
	if (getClassName().equals("interbase.interclient.Driver")) result = "InterBase";
	if (getClassName().equals("org.enhydra.instantdb.jdbc.idbDriver")) result = "Instant DB";
	if (getClassName().equals("postgresql.Driver")) result = "Postgres";     // For V. 6.x
	if (getClassName().equals("org.postgresql.Driver")) result = "Postgres"; // For V. 7.x
	return result;
    }

    /**
     * MBean Method:
     * @return the desrciption of this datasource
     */
    public String getDataSourceDescription() {
	return dsDescription;
    }

    /**
     * Management Method:
     * @param the desrciption of this datasource
     */
    public void setDataSourceDescription(String dsDesc) {
	dsDescription = dsDesc;
    }

    public void setXAFactory(String xafactory) {
	XAFactory = xafactory;
    } // method setXAFactory

    public String getXAFactory() {
	return XAFactory;
    } // method getXAFactory


}
