Example usage for javax.naming.spi NamingManager getURLContext

List of usage examples for javax.naming.spi NamingManager getURLContext

Introduction

In this page you can find the example usage for javax.naming.spi NamingManager getURLContext.

Prototype

public static Context getURLContext(String scheme, Hashtable<?, ?> environment) throws NamingException 

Source Link

Document

Creates a context for the given URL scheme id.

Usage

From source file:org.wso2.carbon.appfactory.ext.jndi.ApplicationAwareCarbonInitialJNDIContext.java

/**
 * Create new sub context within tenant context and return
 *
 * @param name Context name//w  w  w  .j  av  a  2 s .  c  o m
 * @return Application sub context
 */
protected Context getInitialContext(String name) {

    /**
     * If environment is requesting a base context return the
     * base context.
     */

    if (isBaseContextRequested()) {
        return initialContext;
    }

    Context base = null;
    String scheme = null;
    if (name != null) {
        // If the name has components
        scheme = getScheme(name);
        if (scheme != null) {
            if (getContextCache().containsKey(scheme)) {
                base = getContextCache().get(scheme);
            } else {
                try {
                    Context urlContext = NamingManager.getURLContext(scheme, initialContext.getEnvironment());
                    if (urlContext != null) {
                        getContextCache().put(scheme, urlContext);
                        base = urlContext;
                    }
                } catch (NamingException ignored) {
                    // If we are unable to find the context, we use the default context.
                    if (log.isDebugEnabled()) {
                        log.debug("If we are unable to find the context, we use the default context.", ignored);
                    }
                }
            }
        }
    }
    if (base == null) {
        base = initialContext;
        scheme = null;
    }
    int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
    if (!isSubTenant(tenantId)) {
        return base;
    } else if (scheme != null) {
        if (allTenantUrlContextSchemes.contains(scheme)) {
            return base;
        } else if (superTenantOnlyUrlContextSchemes.contains(scheme)) {
            throw new SecurityException(
                    "Tenants are not allowed to use JNDI contexts " + "with scheme: " + scheme);
        }
    }
    String tenantContextName = Integer.toString(tenantId);
    String applicationContextName = Util.getCurrentApplicationContextName();
    Context subContext;
    try {
        subContext = (Context) base.lookup(tenantContextName);
        if (subContext != null) {
            return returnApplicationContext(subContext, applicationContextName, false, tenantId);
        }
    } catch (NamingException ignored) {
        // Depending on the JNDI Initial Context factory, the above operation may or may not
        // throw an exception. But, since we don't mind the exception, we can ignore it.
        if (log.isDebugEnabled()) {
            log.debug(ignored);
        }

    }
    try {
        log.debug("Creating Sub-Context: " + tenantContextName);
        subContext = base.createSubcontext(tenantContextName);
        subContext = returnApplicationContext(subContext, applicationContextName, true, tenantId);
        contextCleanupTask.register(tenantId, subContext);
        if (subContext == null) {
            throw new RuntimeException("Initial context was not created for tenant: " + tenantId);
        }
        return subContext;
    } catch (NamingException e) {
        throw new RuntimeException(
                "An error occurred while creating the initial context " + "for tenant: " + tenantId, e);
    }
}