/**
 * 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
 *
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */


package org.objectweb.jonas.dbm;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.Name;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;

import org.objectweb.jonas.common.Log;
import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Logger;

/**
 * @author Philippe Durieux
 * Contributor(s):
 * <p>
 * 03/05/23 Adriana Danes - Introduce connection pool size configuration
 */
public class DataSourceFactory implements ObjectFactory {

    static private Logger logger = null;

    public Object getObjectInstance(Object refObj, Name name, Context nameCtx, Hashtable env) throws Exception {

        Reference ref = (Reference) refObj;
        String clname = ref.getClassName();
        if (logger == null) {
            logger = Log.getLogger(Log.JONAS_DBM_PREFIX);
        }
        if (logger.isLoggable(BasicLevel.DEBUG)) {
            logger.log(BasicLevel.DEBUG, "");
        }

        // Old fashioned Connection Manager
        if (clname.equals("org.objectweb.jonas.dbm.ConnectionManager")) {
            String dsname = (String)ref.get("datasource.name").getContent();
            ConnectionManager ds = ConnectionManager.getConnectionManager(dsname);
            if (ds == null) {
                // The DataSource was not in this JOnAS Server: Create it.
                if (logger.isLoggable(BasicLevel.DEBUG)) {
                    logger.log(BasicLevel.DEBUG, "Creating a new Connection Manager for " + dsname);
                }
                try {
                    String jBase = System.getProperty("jonas.base");
                    if (jBase == null) {
                        //TODO : must be removed. A client can lookup a datasource without modifications
                        // pure client case
                        ds = new ConnectionManager(true);
                        ds.setDSName(dsname);
                        ds.setUrl((String)ref.get("datasource.url").getContent());
                        ds.setClassName((String)ref.get("datasource.classname").getContent());
                        ds.setUserName((String)ref.get("datasource.username").getContent());
                        ds.setPassword((String)ref.get("datasource.password").getContent());
                        return ds;
                    }
                    //build a new datasource for another server
                    ds = new ConnectionManager();
                    ds.setDSName(dsname);
                    ds.setUrl((String)ref.get("datasource.url").getContent());
                    ds.setClassName((String)ref.get("datasource.classname").getContent());
                    ds.setUserName((String)ref.get("datasource.username").getContent());
                    ds.setPassword((String)ref.get("datasource.password").getContent());
                    ds.setTransactionIsolation((String)ref.get("datasource.isolationlevel").getContent());
                    ds.setMapperName((String)ref.get("datasource.mapper").getContent());
                    ds.poolConfigure((String)ref.get("connchecklevel").getContent(),
                                     (String)ref.get("connmaxage").getContent(),
                                     (String)ref.get("maxopentime").getContent(),
                                     (String)ref.get("connteststmt").getContent(),
                                     (String)ref.get("pstmtmax").getContent(),
                                     (String)ref.get("minconpool").getContent(),
                                     (String)ref.get("maxconpool").getContent(),
                                     (String)ref.get("maxwaittime").getContent(),
                                     (String)ref.get("maxwaiters").getContent(),
                                     (String)ref.get("samplingperiod").getContent());
                } catch (Exception e) {
                    logger.log(BasicLevel.ERROR, "DataSourceFactory error", e);
                }
            } else {
                // The DataSource is already in this JOnAS Server: Just return it.
            }
            return ds;
        }

        logger.log(BasicLevel.ERROR, "no object found for " + clname);
        return null;
    }
}
