/**
 * JOnAS: Java(TM) Open Application Server
 * Copyright (C) 2008 Bull S.A.S.
 * Contact: jonas-team@ow2.org
 *
 * Application Versioning System
 * Copyright (C) 2008 France Telecom R&D
 *
 * 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 1any 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$
 * --------------------------------------------------------------------------
 */

// Use the org.ow2.carol.jndi.spi instead of an org.ow2.jonas.* package in
// order to make the transition between MultiOrbInitialContextFactory and
// VirtualJNDIContextFactory transparent for CAROL bundle inclusions.
package org.ow2.carol.jndi.spi;

import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingException;

/**
 * Context factory that uses {@link VirtualJNDILookup} for JNDI lookups.
 *
 * @author S. Ali Tokmen
 */
public class VirtualJNDIContextFactory extends MultiOrbInitialContextFactory {

    /**
     * If env has its {@link Context#PROVIDER_URL} defined, returns
     * {@link MultiOrbInitialContextFactory#getInitialContext(Hashtable)}(env).
     * Otherwise, it returns an {@link VirtualJNDILookup} instance.
     *
     * @param env The possibly null environment specifying information
     *        to be used in the creation of the initial context.
     *
     * @return A non-null initial context object that implements the Context
     *         interface.
     *
     * @exception NamingException If cannot create an initial context.
     */
    @Override
    public Context getInitialContext(final Hashtable<?, ?> env) throws NamingException {
        String providerURL = null;
        if (env != null) {
            providerURL = (String) env.get(Context.PROVIDER_URL);
        }

        if (providerURL == null) {
            return new VirtualJNDILookup(env);
        } else {
            return super.getInitialContext(env);
        }
    }

}
