/**
 * 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.resource;

import java.util.Hashtable;

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

import org.objectweb.jonas.common.JNDIUtils;
import org.objectweb.jonas_rar.deployment.api.ConnectorDesc;
import org.objectweb.jonas_rar.deployment.api.JonasConnectorDesc;

/**
 *
 * @author Eric Hardesty
 * Contributor(s):
 *
 */
public class ResourceObjectJNDIHandler implements ObjectFactory {

    /**
     * Create an object using the information provided
     * @param refObj the possibly null object containing reference information to create the  object.
     * @param name the name of this object relative to nameCtx, or null if no name is specified.
     * @param nameCtx the context relative to which the name parameter is specified, or null if name
     *                is relative to the default initial context.
     * @param env the possibly null environment that is used in creating the object.
     * @return Object created or null if unable to create
     * @throws Exception if an exception happened while trying to create the object
     */
    public Object getObjectInstance(Object refObj, Name name,
                                    Context nameCtx, Hashtable env) throws Exception {

        Reference ref = (Reference) refObj;

        if (ref == null) {
            System.out.println("No reference found");
            return null;
        }
        String clname = ref.getClassName();
        String raname = (String) ref.get(ResourceServiceImpl.JNDI_NAME).getContent();

        Object obj = null;
        try {
            obj = Rar.getResourceObject(raname);
        } catch (Throwable ex) {
            ex.printStackTrace();
        }
        if (obj != null) {
            return obj;
        }

        // Need to load the factory in the client environment
        String rarObjectName = (String) ref.get(ResourceServiceImpl.RAR_OBJNAME).getContent();
        String factoryType = (String) ref.get(ResourceServiceImpl.FACTORY_TYPE).getContent();
        String factoryOff = (String) ref.get(ResourceServiceImpl.FACTORY_OFFSET).getContent();
        int factoryOffset = Integer.parseInt(factoryOff);

        RefAddr refAddr = null;
        ConnectorDesc conn = null;
        refAddr = ref.get(ResourceServiceImpl.RA_XML);
        if (refAddr != null) {
            conn = (ConnectorDesc) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent());
        }

        JonasConnectorDesc jConn = null;
        refAddr = ref.get(ResourceServiceImpl.JONAS_RA_XML);
        if (refAddr != null) {
            jConn = (JonasConnectorDesc) JNDIUtils.getObjectFromBytes((byte[]) refAddr.getContent());
        }

        // This will either get the other server's implemenation or a client version
        Rar ra = new Rar();
        obj = ra.createFactory(raname, rarObjectName, factoryOffset, factoryType,
                               conn, jConn);

        return obj;
    }
}
