Example usage for javax.naming Context rebind

List of usage examples for javax.naming Context rebind

Introduction

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

Prototype

public void rebind(String name, Object obj) throws NamingException;

Source Link

Document

Binds a name to an object, overwriting any existing binding.

Usage

From source file:Bind.java

License:asdf

public static void main(String[] args) throws Exception {
    Hashtable env = new Hashtable();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
    env.put(Context.PROVIDER_URL, "file:/tmp");

    Context initCtx = new InitialContext(env);
    initCtx.rebind("Susan", new Car("Toyota", "Camry"));
    Car c = (Car) initCtx.lookup("Susan");
    System.out.println(c);//  w w w. ja  v  a 2  s  .c  o m

}

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);
    // Add a binding.
    ctx.bind("Name", null);

    // Replace a binding.
    ctx.rebind("Name", null);

    // Remove a binding.
    ctx.unbind("Name");

    // Rename a binding.
    ctx.rename("Name", "NewSample");
}

From source file:Rebind.java

public static void main(String[] args) {

    // Set up the environment for creating the initial context
    Hashtable<String, Object> env = new Hashtable<String, Object>(11);
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389/o=JNDITutorial");

    try {/*ww  w  . j  a v a2 s  .c  o  m*/
        // Create the initial context
        Context ctx = new InitialContext(env);

        // Create the object to be bound
        Fruit fruit = new Fruit("lemon");

        // Perform the bind
        ctx.rebind("cn=Favorite Fruit", fruit);

        // Check that it is bound
        Object obj = ctx.lookup("cn=Favorite Fruit");
        System.out.println(obj);

        // Close the context when we're done
        ctx.close();
    } catch (NamingException e) {
        System.out.println("Operation failed: " + e);
    }
}

From source file:JndiDataSource.java

public static void bindDataSource(Context ctx, String dsn) throws SQLException, NamingException {

    OracleDataSource ods = new OracleDataSource();

    ods.setUser("yourName");
    ods.setPassword("mypwd");
    ods.setDriverType("thin");
    ods.setDatabaseName("ORCL");
    ods.setServerName("localhost");
    ods.setPortNumber(1521);//  ww  w . j  a va  2  s  . c o  m

    ctx.rebind(dsn, ods);

}

From source file:JNDIUtil.java

/**
 * Context.rebind() requires that all intermediate contexts and the target context (that named by
 * all but terminal atomic component of the name) must already exist, otherwise
 * NameNotFoundException is thrown. This method behaves similar to Context.rebind(), but creates
 * intermediate contexts, if necessary./* ww  w.java2 s.  c  om*/
 */
public static void rebind(Context c, String jndiName, Object o) throws NamingException {
    Context context = c;
    String name = jndiName;

    int idx = jndiName.lastIndexOf('/');
    if (idx != -1) {
        context = createContext(c, jndiName.substring(0, idx));
        name = jndiName.substring(idx + 1);
    }
    boolean failed = false;
    try {
        context.rebind(name, o);
    } catch (Exception ignored) {
        failed = true;
    }
    if (failed) {
        context.bind(name, o);
    }
}

From source file:Util.java

/**
 * Rebind val to name in ctx, and make sure that all intermediate contexts
 * exist//www. ja  v a2  s .c  o  m
 * 
 * @param ctx
 *          the parent JNDI Context under which value will be bound
 * @param name
 *          the name relative to ctx where value will be bound
 * @param value
 *          the value to bind.
 * @throws NamingException
 *           for any error
 */
public static void rebind(Context ctx, Name name, Object value) throws NamingException {
    int size = name.size();
    String atom = name.get(size - 1);
    Context parentCtx = createSubcontext(ctx, name.getPrefix(size - 1));
    parentCtx.rebind(atom, value);
}

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  .  ja  va2  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.dattack.naming.loader.NamingLoader.java

private static void execBind(final Context context, final String key, final Object value)
        throws NamingException {

    Object obj = context.lookup(key);

    if (obj instanceof Context) {
        LOGGER.debug("Destroying context with name '{}'", key);
        context.destroySubcontext(key);/*w  w  w  .  j  a v a 2 s  .co m*/
        obj = null;
    }

    if (obj == null) {
        LOGGER.debug("Executing bind method for '{}'", key);
        context.bind(key, value);
    } else {
        LOGGER.debug("Executing rebind method for '{}'", key);
        context.rebind(key, value);
    }
}

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  ww  w. j ava 2  s.  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);
}

From source file:Util.java

/**
 * Create a link/*w  w w.  jav  a 2  s.  co  m*/
 * 
 * @param ctx
 *          the context
 * @param fromName
 *          the from name
 * @param toName
 *          the to name
 * @throws NamingException
 *           for any error
 */
public static void createLinkRef(Context ctx, String fromName, String toName) throws NamingException {
    LinkRef link = new LinkRef(toName);
    Context fromCtx = ctx;
    Name name = ctx.getNameParser("").parse(fromName);
    String atom = name.get(name.size() - 1);
    for (int n = 0; n < name.size() - 1; n++) {
        String comp = name.get(n);
        try {
            fromCtx = (Context) fromCtx.lookup(comp);
        } catch (NameNotFoundException e) {
            fromCtx = fromCtx.createSubcontext(comp);
        }
    }

    System.out.println("atom: " + atom);
    System.out.println("link: " + link);

    fromCtx.rebind(atom, link);

    System.out.println("Bound link " + fromName + " to " + toName);
}