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:Main.java

public static void main(String[] argv) throws Exception {
    String url = "iiop://localhost/";
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
    env.put(Context.PROVIDER_URL, url);

    Context ctx = new InitialContext(env);

    // Create a subcontext.
    Context childCtx = ctx.createSubcontext("child");

    // Destroy the subcontext.
    ctx.destroySubcontext("child");
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String url = "iiop://localhost/";
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
    env.put(Context.PROVIDER_URL, url);

    Context ctx = new InitialContext(env);
    // Create a subcontext.
    Context childCtx = ctx.createSubcontext("child");

    // Destroy the subcontext.
    ctx.destroySubcontext("child");

    Context obj = (Context) childCtx.lookup("grandChild");
    String fullname = obj.getNameInNamespace();
}

From source file:de.zib.gndms.infra.GridConfig.java

/**
 * @see #findSharedContext(javax.naming.Context, String)
 *//*from   w  w w  . j  a  v a  2 s  .  co  m*/
@NotNull
public static Context findSharedContext(@NotNull Context startContext, @NotNull Name name)
        throws NamingException {
    Context resultContext;

    try {
        resultContext = startContext.createSubcontext(name);
    } catch (NameAlreadyBoundException e) {
        resultContext = (Context) startContext.lookup(name);
    }
    return resultContext;
}

From source file:de.zib.gndms.infra.GridConfig.java

/**
 * Returns the {@code Subcontext} of {@code startContext}, which is bounded to {@code name}.
 * If it does not exist, a new subcontext is created and bounded to {@code name}.
 *
 * @param startContext a Context, which is supposed to have a certain subContext
 * @param name the name of a subcontext of {@code startContext}
 * @return a chosen subcontext of {@code startContext}
 * @throws NamingException if a naming exception occurs, while executing {@code createSubcontext()} or {@code lookup()}
 *///from   ww  w .j  a  va 2s.co  m
@NotNull
public static Context findSharedContext(@NotNull Context startContext, @NotNull String name)
        throws NamingException {
    Context resultContext;

    try {
        resultContext = startContext.createSubcontext(name);
    } catch (NameAlreadyBoundException e) {
        resultContext = (Context) startContext.lookup(name);
    }
    return resultContext;
}

From source file:ca.nrc.cadc.db.DBUtil.java

/**
 * Create a JNDI DataSOurce resource with the specified name and environment context.
 * //from  www .  j  av a2  s . c  o m
 * @param dataSourceName
 * @param envContextName
 * @param config
 * @throws NamingException 
 */
public static void createJNDIDataSource(String dataSourceName, String envContextName, ConnectionConfig config)
        throws NamingException {
    log.debug("createDataSource: " + dataSourceName + " START");
    StandaloneContextFactory.initJNDI();

    Context initContext = new InitialContext();
    Context envContext = (Context) initContext.lookup(envContextName);
    if (envContext == null) {
        envContext = initContext.createSubcontext(envContextName);
    }
    log.debug("env: " + envContext);

    DataSource ds = getDataSource(config);
    envContext.bind(dataSourceName, ds);
    log.debug("createDataSource: " + dataSourceName + " DONE");
}

From source file:com.silverpeas.components.model.AbstractSpringJndiDaoTest.java

/**
 * Workaround to be able to use Sun's JNDI file system provider on Unix
 *
 * @param ic : the JNDI initial context// w w w . java 2  s  .c  o  m
 * @param jndiName : the binding name
 * @param ref : the reference to be bound
 * @throws NamingException
 */
protected static void rebind(InitialContext ic, String jndiName, Object ref) throws NamingException {
    Context currentContext = ic;
    StringTokenizer tokenizer = new StringTokenizer(jndiName, "/", false);
    while (tokenizer.hasMoreTokens()) {
        String name = tokenizer.nextToken();
        if (tokenizer.hasMoreTokens()) {
            try {
                currentContext = (Context) currentContext.lookup(name);
            } catch (javax.naming.NameNotFoundException nnfex) {
                currentContext = currentContext.createSubcontext(name);
            }
        } else {
            currentContext.rebind(name, ref);
        }
    }
}

From source file:com.funambol.server.db.DataSourceContextHelper.java

/**
 * Create all intermediate subcontexts.//ww  w .  ja  va 2s.  co m
 */
private static void createSubcontexts(javax.naming.Context ctx, String name) throws NamingException {
    javax.naming.Context currentContext = ctx;
    StringTokenizer tokenizer = new StringTokenizer(name, "/");
    while (tokenizer.hasMoreTokens()) {
        String token = tokenizer.nextToken();
        if ((!token.equals("")) && (tokenizer.hasMoreTokens())) {
            try {
                currentContext = currentContext.createSubcontext(token);
            } catch (NamingException e) {
                // Silent catch. Probably an object is already bound in
                // the context.
                currentContext = (javax.naming.Context) currentContext.lookup(token);
            }
        }
    }
}

From source file:JNDIUtil.java

/**
 * Create a context path recursively.//from   w ww  .java 2 s . c  om
 */
public static Context createContext(Context c, String path) throws NamingException {
    Context crtContext = c;
    for (StringTokenizer st = new StringTokenizer(path, "/"); st.hasMoreTokens();) {
        String tok = st.nextToken();

        try {
            Object o = crtContext.lookup(tok);
            if (!(o instanceof Context)) {
                throw new NamingException("Path " + path + " overwrites and already bound object");
            }
            crtContext = (Context) o;
            continue;
        } catch (NameNotFoundException e) {
            // OK
        }
        crtContext = crtContext.createSubcontext(tok);
    }
    return crtContext;
}

From source file:Util.java

/**
 * Create a subcontext including any intermediate contexts.
 * /*from   w  w w.j a  va  2  s  . c o m*/
 * @param ctx
 *          the parent JNDI Context under which value will be bound
 * @param name
 *          the name relative to ctx of the subcontext.
 * @return The new or existing JNDI subcontext
 * @throws NamingException
 *           on any JNDI failure
 */
public static Context createSubcontext(Context ctx, Name name) throws NamingException {
    Context subctx = ctx;
    for (int pos = 0; pos < name.size(); pos++) {
        String ctxName = name.get(pos);
        try {
            subctx = (Context) ctx.lookup(ctxName);
        } catch (NameNotFoundException e) {
            subctx = ctx.createSubcontext(ctxName);
        }
        // The current subctx will be the ctx for the next name component
        ctx = subctx;
    }
    return subctx;
}

From source file:be.fedict.eid.dss.sp.StartupServletContextListener.java

public static void bindComponent(String jndiName, Object component) throws NamingException {

    LOG.debug("bind component: " + jndiName);
    InitialContext initialContext = new InitialContext();
    String[] names = jndiName.split("/");
    Context context = initialContext;
    for (int idx = 0; idx < names.length - 1; idx++) {
        String name = names[idx];
        LOG.debug("name: " + name);
        NamingEnumeration<NameClassPair> listContent = context.list("");
        boolean subContextPresent = false;
        while (listContent.hasMore()) {
            NameClassPair nameClassPair = listContent.next();
            if (!name.equals(nameClassPair.getName())) {
                continue;
            }//from   www . j  a  va 2s.com
            subContextPresent = true;
        }
        if (!subContextPresent) {
            context = context.createSubcontext(name);
        } else {
            context = (Context) context.lookup(name);
        }
    }
    String name = names[names.length - 1];
    context.rebind(name, component);
}