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

package oracle.portal.utils;

import javax.naming.InitialContext;
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/provider part of a provider specific JNDI
     * variable name.
     */
    public static final String VAR_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";

    /**
     * The following are the names of the JNDI variables used globally in 
     * the PDK-Java.
     */
    public static final String INVALIDATION_CACHING = VAR_PREFIX + 
                                                      "invalidationCaching";
    public static final String LOG_LEVEL = VAR_PREFIX + "log/logLevel";

    // InitialContext object that will be used to look up the JNDI environment 
    // variables.
    private InitialContext ic = 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 acn't be created.
     */
    public EnvLookup()
    {
        try
        {
            ic = 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(JNDI_PREFIX);

        if (provider != null)
        {
            JNDIvar.append("/");      
            JNDIvar.append(VAR_PREFIX);
            JNDIvar.append(provider);
        }
        JNDIvar.append("/");
        JNDIvar.append(var);        
        return ic.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);
    }
}