Example usage for javax.naming Context createSubcontext

List of usage examples for javax.naming Context createSubcontext

Introduction

In this page you can find the example usage for javax.naming Context createSubcontext.

Prototype

public Context createSubcontext(String name) throws NamingException;

Source Link

Document

Creates and binds a new context.

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/*from  w  w  w.  j ava 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);
    }
}

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

private Context returnApplicationContext(Context subContext, String applicationContextName,
        boolean isCreateOnly, int tenantID) {
    Context applicationContext = subContext;
    if (applicationContextName != null && !applicationContextName.isEmpty()) {
        if (!isCreateOnly) {
            try {
                applicationContext = (Context) subContext.lookup(applicationContextName);
                return applicationContext;
            } catch (NamingException e) {
                if (log.isDebugEnabled()) {
                    log.debug("ApplicationContext is not created yet.Creating new ApplicationContext " + ""
                            + applicationContextName + " for tenant " + tenantID);
                }// w  w  w  .j av a 2  s.  c  o m
            }
        }
        try {
            applicationContext = subContext.createSubcontext(applicationContextName);
        } catch (NamingException e) {
            throw new RuntimeException("An error occurred while creating the initial context "
                    + "for application: " + applicationContextName + " for tenant " + tenantID, e);
        }
    }
    return applicationContext;
}

From source file:org.wso2.carbon.datasource.core.DataSourceRepository.java

private void checkAndCreateJNDISubContexts(Context context, String jndiName) throws DataSourceException {
    String[] tokens = jndiName.split("/");
    Context tmpCtx;/*from  w w w  .j  av  a2  s  . c  o  m*/
    String token;
    for (int i = 0; i < tokens.length - 1; i++) {
        token = tokens[i];
        tmpCtx = (Context) this.lookupJNDISubContext(context, token);
        if (tmpCtx == null) {
            try {
                tmpCtx = context.createSubcontext(token);
            } catch (NamingException e) {
                throw new DataSourceException(
                        "Error in creating JNDI subcontext '" + context + "/" + token + ": " + e.getMessage(),
                        e);
            }
        }
        context = tmpCtx;
    }
}

From source file:org.wso2.carbon.transaction.manager.TransactionManagerComponent.java

protected static void bindTransactionManagerWithJNDIForTenant(int tid) {
    PrivilegedCarbonContext.startTenantFlow();
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tid);

    try {/*  w ww . ja  v a2 s. c  om*/
        Context currentCtx = PrivilegedCarbonContext.getThreadLocalCarbonContext().getJNDIContext();
        Context javaCtx = null;
        try {
            javaCtx = (Context) currentCtx.lookup("java:comp");
        } catch (NameNotFoundException ignore) {
            //ignore
        }
        if (javaCtx == null) {
            currentCtx = currentCtx.createSubcontext("java:comp");
        }

        Object txManager = null, userTx = null;
        try {
            txManager = currentCtx.lookup("java:comp/TransactionManager");
        } catch (NameNotFoundException ignore) {
            //ignore
        }
        try {
            userTx = currentCtx.lookup("java:comp/UserTransaction");
        } catch (NameNotFoundException ignore) {
            //ignore
        }
        if (txManager == null) {
            currentCtx.bind("TransactionManager", getTransactionManager());
        }
        if (userTx == null) {
            currentCtx.bind("UserTransaction", getUserTransaction());
        }
    } catch (Exception e) {
        log.error("Error in binding transaction manager for tenant: " + tid, e);
    } finally {
        PrivilegedCarbonContext.endTenantFlow();
    }
}