/* Copyright (c) 2003 by Oracle Corporation.  All Rights Reserved */

package oracle.portal.utils;

import javax.naming.InitialContext;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import oracle.webdb.utils.SimpleStringBuffer;

public class EnvLookup 
{
    /**
     * JNDI_PREFIX is the java:comp/env part of a JNDI variable name.
     */
    public static final String JNDI_PREFIX = "java:comp/env";

    /**
     * VAR_PREFIX is the /oracle/portal/provider part of a provider specific JNDI
     * variable name.
     */
    public static final String VAR_PREFIX = JNDI_PREFIX + 
                                            "/oracle/portal/provider/";

    public static final String OLD_VAR_PREFIX = JNDI_PREFIX + "/oracle/portal/";

    /**
     * The following are names of the provider specific JNDI variables used in 
     * the PDK-Java.
     */
    public static final String PROVIDER_DEFINITION = "definition";
    public static final String AUTO_RELOAD = "autoReload";
    public static final String TEST_PAGE_FLAG = "showTestPage";
    public static final String HMAC_TIME_DIFFERENCE = "maxTimeDifference"; 
    public static final String HMAC_SHARED_KEY = "sharedKey";
    public static final String FILE_PREF_STORE_LOCATION = "rootDirectory";
    public static final String PROVIDER_LOADER_CLASS = "loaderClass";
    public static final String PROVIDER_SERVICE_CLASS = "serviceClass";

    /**
     * A path separator for use when constructing JNDI variable names.
     */
    public static final String SEPARATOR = "/";

    /**
     * The global keyword used in provider non-specific JNDI varibales to avoid 
     * name clashes.
     */
    public static final String GLOBAL = "global/";

    /**
     * The following are the names of the JNDI variables used globally in 
     * the PDK-Java.
     */
    public static final String LOG_LEVEL = "log/logLevel";
    public static final String LOAD_ALL_PROVIDERS = "loadAllProviders";
    public static final String MBEAN_SERVER_ID = "JMX/MBeanServerID";


    // InitialContext object that will be used to look up the JNDI environment 
    // variables.
    private InitialContext mIC = null;

    /**
     * Constructor.  Initializes an {@link javax.naming.InitialContext} object
     * to enable the lookup of JNDI Resources.
     * 
     * @throws  {@link javax.naming.NamingException} when the 
     *          {@link javax.naming.InitialContext} object can't be created.
     */
    public EnvLookup()
    {
        try
        {
            mIC = new InitialContext();
        }
        catch (NamingException ne)
        {
            
        }
    }

    /**
     * Looks up the value of a  provider specific JNDI variable.  Automatically 
     * adds the java:comp/env prefix needed to look up a JNDI variable in a J2EE 
     * container and builds up the rest of the variable including the provider 
     * name in the relevant place.
     * 
     * @param   var       The name of the JNDI variable to lookup.      
     * @param   provider  The name of the web provider that the JNDI variable is
     *                    related to.
     *                    
     * @return  A {@link java.lang.Object} representing the value of the JNDI
     *          variable being looked up.     
     *          
     * @throws  {@link javax.naming.NamingException} when the JNDI variable
     *          can't be accessed.
     */    
    public Object lookup(String var, String provider) throws NamingException
    {
        SimpleStringBuffer JNDIvar = new SimpleStringBuffer(VAR_PREFIX);
        if (provider != null)
        {
            JNDIvar.append(provider);
            JNDIvar.append(SEPARATOR);
        }
        else
        {
            JNDIvar.append(GLOBAL);
        }
        JNDIvar.append(var);  
        
        try
        {
            return mIC.lookup(JNDIvar.toString());
        }
        catch (NamingException ne)
        {
            // If the variable was not found using the new naming conventions 
            // check for it again using the old naming conventions to ensure 
            // backwards compatibility.
            JNDIvar = new SimpleStringBuffer(OLD_VAR_PREFIX);
            if (provider != null)
            {
                JNDIvar.append(provider);
                JNDIvar.append(SEPARATOR);
            }
            JNDIvar.append(var);
        }
        return mIC.lookup(JNDIvar.toString());
    }

    /**
     * Looks up the value of a  non-provider specific JNDI variable.  
     * Automatically adds the java:comp/env prefix needed to look up a JNDI 
     * variable in a J2EE container.
     * 
     * @param   var       The name of the JNDI variable to lookup.      
     *                    
     * @return  A {@link java.lang.Object} representing the value of the JNDI
     *          variable being looked up.     
     *          
     * @throws  {@link javax.naming.NamingException} when the JNDI variable
     *          can't be accessed.
     */
    public Object lookup(String var) throws NamingException
    {
        return lookup(var, null);
    }

        /**
     * Lists all of the JNDI variables for the specified provider.
     * 
     * @param   provider    The provider you would like a listing of variables 
     *                      for.
     * @return  A {@link javax.naming.NamingEnumeration) of all of the 
     *          JNDI variables for that provider.
     *          
     * @throws  {@link javax.naming.NamingException} when the list of JNDI 
     *          variables can't be accessed.
     */
    public NamingEnumeration listProviderVariables(String provider)
        throws NamingException
    {
        SimpleStringBuffer buff = new SimpleStringBuffer(VAR_PREFIX);
        if (provider != null && provider.length() > 0)
        {
            buff.append(provider);
        }     
        return mIC.listBindings(buff.toString());
    }

    /**
     * Lists all of the global JNDI variables in the web application.
     * 
     * @return  A {@link javax.naming.NamingEnumeration) of all of the 
     *          JNDI variables for the web application.
     *          
     * @throws  {@link javax.naming.NamingException} when the list of JNDI 
     *          variables can't be accessed.
     */
    public NamingEnumeration listGlobalVariables() throws NamingException
    {
        SimpleStringBuffer buff = new SimpleStringBuffer(VAR_PREFIX);
        buff.append(GLOBAL);
        return mIC.listBindings(buff.toString());
    }
}