/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2004 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): Guillaume Sauthier
 * --------------------------------------------------------------------------
 * $Id$
 * --------------------------------------------------------------------------
 */
package org.objectweb.jonas.jaxr.factory;

import java.util.Hashtable;
import java.util.Properties;

import javax.naming.Context;
import javax.naming.Name;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.spi.ObjectFactory;
import javax.xml.registry.ConnectionFactory;

import org.objectweb.jonas.common.JNDIUtils;
import org.objectweb.jonas.common.Log;
import org.objectweb.jonas.common.PropDump;

import org.objectweb.util.monolog.api.BasicLevel;
import org.objectweb.util.monolog.api.Logger;


/**
 * JAXR ConnectionFactory factory
 * @author Guillaume Sauthier
 */
public class JAXRFactory implements ObjectFactory {

    /**
     * logger
     */
    private static Logger logger;

    /**
     * Classname returned
     */
    public static final String FACTORY_TYPE = "javax.xml.registry.ConnectionFactory";

    /**
     * Properties stored inside Reference
     */
    public static final String PROPS_NAME = "jaxr.properties";

    /**
     * JAXR Factory property name
     */
    private static final String JAXR_FACTORY_CLASSNAME = "jaxr.factory.classname";

    /**
     * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
     */
    public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception {
        //Get the logger
        if (logger == null) {
            logger = Log.getLogger(Log.JONAS_JAXR_PREFIX);
        }

        //Get the reference
        if (!(obj instanceof Reference)) {
            return null;
        }
        Reference ref = (Reference) obj;

        //Get the class name
        String clname = ref.getClassName();

        //Check the class name
        if (!ref.getClassName().equals(FACTORY_TYPE)) {
            logger.log(BasicLevel.ERROR, "Cannot create object : required type is '" + FACTORY_TYPE + "', but found type is '" + clname + "'.");
            return null;
        }

        // Load properties
        Properties jaxrProps = new Properties();
        RefAddr refAddr = null;

        refAddr =  ref.get(PROPS_NAME);
        if (refAddr != null) {
            jaxrProps = (Properties) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent());
            if (logger.isLoggable(BasicLevel.DEBUG)) {
                PropDump.print("These are the properties used to obtain a new jaxr object", jaxrProps, logger, BasicLevel.DEBUG);
            }
        }

        // set implementation if specified
        String clsName = jaxrProps.getProperty(JAXR_FACTORY_CLASSNAME);
        if (clsName != null) {
            System.setProperty("javax.xml.registry.ConnectionFactoryClass", clsName);
        }
        // else fallback to default implementation
        ConnectionFactory cf = ConnectionFactory.newInstance();

        // setup properties
        cf.setProperties(jaxrProps);

        return cf;
    }

}
