/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */

package org.jfox.jndi;

import java.net.URL;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.Reference;

/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */


public class InitialContextFactoryImpl implements javax.naming.spi.InitialContextFactory, javax.naming.spi.ObjectFactory {

    public Context getInitialContext(Hashtable env) throws NamingException {
        if(env == null) {
            env = JNDIProperties.getDefaultEnvironment();
        }
        else {
            env = JNDIProperties.mergeEnvironment(env);
        }
        return new ContextImpl("/", env);
    }

    // will perform after urlContextFactory failed and if set Context.OBJECT_FACTORY
    public Object getObjectInstance(Object obj, Name name, Context ctx, Hashtable environment) throws Exception {
//		Context context = getInitialContext(environment);
        Reference ref = (Reference) obj;
        String url_string = (String) ref.get("URL").getContent();
        URL url = new URL(url_string);
        return url.getContent();
    }

    public static void main(String[] args) {

    }
}
