/*
 * The contents of this file are subject to the JOnAS Public License Version 
 * 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License on the JOnAS web site.
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.
 * See the License for the specific terms governing rights and limitations under 
 * the License.
 *
 * The Original Code is JOnAS application server code released July 1999.
 *
 * The Initial Developer of the Original Code is Bull S.A.
 * The Original Code and portions created by Bull S.A. are
 *    Copyright (C) 1999,2000 Bull S.A. All Rights Reserved.
 *
 * Contributor(s): ______________________________________.
 *
 * --------------------------------------------------------------------------
 * $Id: DataSourceFactory.java,v 1.1 2000/03/06 14:37:28 durieuph Exp $
 * --------------------------------------------------------------------------
 */


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.Trace;

//Start:Jun Inamori
import org.objectweb.jonas.deployment.api.*;
//End:Jun Inamori

public class DataSourceFactory implements ObjectFactory {

    private static final int trace = Trace.DB_8;

    public Object getObjectInstance(Object refObj, Name name, Context nameCtx, Hashtable env) throws Exception {

	//Start:Jun Inamori
	Object obj=env.get("BeanDesc");
	if(obj==null){
	    Trace.errln("DataSourceFactory.getObjectInstance(): No BeanDesc found");
	    return null;
	}
	BeanDesc desc=(BeanDesc)obj;
	//End:Jun Inamori

	Reference ref = (Reference) refObj;

	if (ref.getClassName().equals("org.objectweb.jonas.dbm.ConnectionManager")) {
	    String dsname = (String)ref.get("datasource.name").getContent();
	    ConnectionManager ds = ConnectionManager.getConnectionManager(dsname);
	    //Start:Jun Inamori
	    if (ds != null) {
		if(desc instanceof EntityCmpDesc){
		    return ds;
		}
		ResourceRefDesc[] r_descs=desc.getResourceRefDesc();
		ResourceRefDesc r_desc=null;
		for(int i=0; i<r_descs.length; i++){
		    String jndi_name=r_descs[i].getJndiName();
		    if(jndi_name.equals(dsname)){
			r_desc=r_descs[i];
			break;
		    }
		}
		if(r_desc==null){
		    Trace.errln("DataSourceFactory.getObjectInstance(): No ResourceRefDesc found");
		    return null;
		}
		if(r_desc.getAuthentication()==ResourceRefDesc.APPLICATION_AUTH){
		    ds.setUserName(null);
		    ds.setPassword(null);
		    return ds;
		}
		else{
		    ds.setUserName((String)ref.get("datasource.username").getContent());
		    ds.setPassword((String)ref.get("datasource.password").getContent());
		    return ds;
		}
	    } else {
		Trace.errln("DataSourceFactory.getObjectInstance(): No object found");
		return null;
	    }
	} else {
	    Trace.errln("DataSourceFactory.getObjectInstance(): No object found");
	    return null;
	}
	//End:Jun Inamori
    }

}
