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:jdao.JDAO.java

public static Context bind(Context ctx, String nameStr, Object obj) throws NamingException {
    log("binding " + nameStr);

    Name name = ctx.getNameParser("").parse(nameStr);

    //no name, nothing to do
    if (name.size() == 0)
        return null;

    Context subCtx = ctx;

    //last component of the name will be the name to bind
    for (int i = 0; i < name.size() - 1; i++) {
        try {/*from  ww  w . ja v  a  2  s  . co  m*/
            subCtx = (Context) subCtx.lookup(name.get(i));
            log("Subcontext " + name.get(i) + " already exists");
        } catch (NameNotFoundException e) {
            subCtx = subCtx.createSubcontext(name.get(i));
            log("Subcontext " + name.get(i) + " created");
        }
    }

    subCtx.rebind(name.get(name.size() - 1), obj);
    log("Bound object to " + name.get(name.size() - 1));
    return subCtx;
}

From source file:jdao.JDAO.java

public static Context retrieveContext(String jndi_path) {
    InitialContext jndiContext = null;
    Context env = null;
    try {/*  w  w w  .  java  2 s.  c om*/
        log("INFO: resolving " + jndi_path);

        env = jndiContext = new InitialContext();
        env = (Context) jndiContext.lookup(jndi_path);
    } catch (Exception xe) {
        try {
            Name jname = jndiContext.getNameParser(jndi_path).parse(jndi_path);
            Enumeration<String> en = jname.getAll();
            while (en.hasMoreElements()) {
                String name = en.nextElement();
                Context tmp = null;
                try {
                    tmp = (Context) env.lookup(name);
                    env = (Context) env.lookup(name);
                } catch (NameNotFoundException nnf) {
                    log("INFO: creating " + name);
                    env = env.createSubcontext(name);
                }
            }
        } catch (Exception xe2) {
            log("ERROR: resolving " + jndi_path, xe2);
        }
    }
    return env;
}

From source file:org.apache.synapse.commons.datasource.JNDIBasedDataSourceRepository.java

/**
 * Helper method to create context tree for a given path
 *
 * @param initialContext The root context
 * @param path           The path of the resource
 *//*w  ww.java2 s  .  c om*/
private static void populateContextTree(InitialContext initialContext, String path) {

    String[] paths = path.split("/");
    if (paths != null && paths.length > 1) {

        Context context = initialContext;
        for (String path1 : paths) {

            try {
                assert context != null;
                context = context.createSubcontext(path1);
                if (context == null) {
                    throw new SynapseCommonsException("sub context " + path1 + " could not" + " be created",
                            log);
                }

            } catch (NamingException e) {
                throw new SynapseCommonsException("Unable to create sub context : " + path1, e, log);
            }
        }
    }
}

From source file:org.apache.synapse.util.DataSourceRegistrar.java

/**
 * Helper method to create context tree for a given path
 *
 * @param initialContext The root context
 * @param path           The path of the resource
 *///w w w  . java 2s  . c  o  m
private static void populateContextTree(InitialContext initialContext, String path) {

    String[] paths = path.split("/");
    if (paths != null && paths.length > 1) {

        Context context = initialContext;
        for (int i = 0; i < paths.length; i++) {

            try {
                context = context.createSubcontext(paths[i]);
                if (context == null) {
                    handleException("sub context " + paths[i] + " could not be created");
                }

            } catch (NamingException e) {
                handleException("Unable to create sub context : " + paths[i], e);
            }
        }
    }
}

From source file:org.apache.torque.dsfactory.JndiDataSourceFactory.java

/**
 *
 * @param ctx/*w  ww .  j a  v  a 2  s.  co  m*/
 * @param path
 * @param ds
 * @throws Exception
 */
private void bindDStoJndi(Context ctx, String path, Object ds) throws Exception {
    debugCtx(ctx);

    // add subcontexts, if not added already
    int start = path.indexOf(':') + 1;
    if (start > 0) {
        path = path.substring(start);
    }
    StringTokenizer st = new StringTokenizer(path, "/");
    while (st.hasMoreTokens()) {
        String subctx = st.nextToken();
        if (st.hasMoreTokens()) {
            try {
                ctx.createSubcontext(subctx);
                log.debug("Added sub context: " + subctx);
            } catch (NameAlreadyBoundException nabe) {
                // ignore
                log.debug("Sub context " + subctx + " already exists");
            } catch (NamingException ne) {
                log.debug("Naming exception caught " + "when creating subcontext" + subctx, ne);
                // even though there is a specific exception
                // for this condition, some implementations
                // throw the more general one.
                /*
                 *                      if (ne.getMessage().indexOf("already bound") == -1 )
                 *                      {
                 *                      throw ne;
                 *                      }
                 */
                // ignore
            }
            ctx = (Context) ctx.lookup(subctx);
        } else {
            // not really a subctx, it is the ds name
            ctx.bind(subctx, ds);
        }
    }
}

From source file:org.apache.torque.JndiConfigurationTest.java

/**
 * creates and binds a BasicDataSource into jndi.
 * @throws Exception if DataSource creation or binding fails.
 *///ww w  . j a va2s .c  o m
protected void bindDataSource() throws Exception {
    BasicDataSource dataSource = getDataSource();
    Context context = getInitialContext();
    context.createSubcontext(JNDI_SUBCONTEXT);
    context.bind(JNDI_PATH, dataSource);
}

From source file:org.compass.core.jndi.NamingHelper.java

/**
 * Bind val to name in ctx, and make sure that all intermediate contexts
 * exist.// www .  ja v a  2 s  .c om
 * 
 * @param ctx
 *            the root context
 * @param name
 *            the name as a string
 * @param val
 *            the object to be bound
 * @throws javax.naming.NamingException
 */
public static void bind(Context ctx, String name, Object val) throws NamingException {
    try {
        ctx.rebind(name, val);
    } catch (Exception e) {
        Name n = ctx.getNameParser("").parse(name);
        while (n.size() > 1) {
            String ctxName = n.get(0);

            Context subctx = null;
            try {
                subctx = (Context) ctx.lookup(ctxName);
            } catch (NameNotFoundException nfe) {
                // don't do nothing
            }

            if (subctx != null) {
                ctx = subctx;
            } else {
                ctx = ctx.createSubcontext(ctxName);
            }
            n = n.getSuffix(1);
        }
        ctx.rebind(n, val);
    }
}

From source file:org.eclipse.ecr.runtime.jtajca.NuxeoContainer.java

protected static void addDeepBinding(Context dir, CompositeName comp, Object obj) throws NamingException {
    Name name = comp.getPrefix(1);
    if (comp.size() == 1) {
        addBinding(dir, name, obj);//from w w  w  . j  av a  2 s  .c  o  m
        return;
    }
    Context subdir;
    try {
        subdir = (Context) dir.lookup(name);
    } catch (NamingException e) {
        subdir = dir.createSubcontext(name);
    }
    addDeepBinding(subdir, (CompositeName) comp.getSuffix(1), obj);
}

From source file:org.eclipse.ecr.testlib.runner.RuntimeFeature.java

public static void rebind(Context ctx, String key, Object value) throws NamingException {
    Name name = ctx.getNameParser("").parse(key);
    int depth = name.size() - 1;
    for (int i = 0; i < depth; i++) {
        String segment = name.get(i);
        try {// w  ww. java2s.c o m
            ctx = (Context) ctx.lookup(segment);
        } catch (NameNotFoundException e) {
            ctx = ctx.createSubcontext(segment);
        }
    }
    ctx.rebind(name.get(depth), value);
}

From source file:org.grouter.common.jndi.JNDIUtils.java

public static void createInMemoryJndiProvider(List<BindingItem> bindings) {
    if (bindings == null) {
        throw new IllegalArgumentException("Can not bind null to jndi tree.");
    }//from   w w w . j a va2s  . c om
    try {
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                "org.apache.commons.naming.java.javaURLContextFactory");
        System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");

        Iterator<BindingItem> bindingItemIterator = bindings.iterator();
        while (bindingItemIterator.hasNext()) {
            BindingItem item = bindingItemIterator.next();
            InitialContext initialContext = new InitialContext();
            logger.debug("Creating component context : " + item.getJndipath()[0]);
            Context compContext = initialContext.createSubcontext(item.getJndipath()[0]);
            logger.debug("Creating environment context : " + item.getJndipath()[1]);
            Context envContext = compContext.createSubcontext(item.getJndipath()[1]);
            logger.debug("Bidning : " + item.getJndiName() + " to implementation : " + item.getImplemenation());
            envContext.bind(item.getJndiName(), item.getImplemenation());
            printJNDI(envContext, logger);
            //logger.debug(envContext.listBindings(initialContext.getNameInNamespace()));
        }

    } catch (NamingException e) {
        logger.error(e, e);
    }

}