Example usage for javax.naming Context getNameParser

List of usage examples for javax.naming Context getNameParser

Introduction

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

Prototype

public NameParser getNameParser(String name) throws NamingException;

Source Link

Document

Retrieves the parser associated with the named 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);
    NameParser parser = ctx.getNameParser("");
    Name dn = parser.parse("cn=John, ou=People, o=JNDITutorial");

    dn.remove(1);//w  w w  . ja v a2 s  .  c o  m
    dn.add(0, "c=us");
    dn.add("cn=fs");
}

From source file:Util.java

/**
 * Create a subcontext including any intermediate contexts.
 * // w  w w  .  j  a  v a  2s .co 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 javax.naming.NamingException
 *           on any JNDI failure
 */
public static Context createSubcontext(Context ctx, String name) throws NamingException {
    Name n = ctx.getNameParser("").parse(name);
    return createSubcontext(ctx, n);
}

From source file:Util.java

/**
 * Unbinds a name from ctx, and removes parents if they are empty
 * //from w  w w  .  j a  v a 2s.co  m
 * @param ctx
 *          the parent JNDI Context under which the name will be unbound
 * @param name
 *          The name to unbind
 * @throws NamingException
 *           for any error
 */
public static void unbind(Context ctx, String name) throws NamingException {
    unbind(ctx, ctx.getNameParser("").parse(name));
}

From source file:Util.java

/**
 * Bind val to name in ctx, and make sure that all intermediate contexts exist
 * //  w w w  . ja  v  a2s .c  om
 * @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 bind(Context ctx, String name, Object value) throws NamingException {
    Name n = ctx.getNameParser("").parse(name);
    bind(ctx, n, value);
}

From source file:Util.java

/**
 * Rebind val to name in ctx, and make sure that all intermediate contexts
 * exist/* www. j  a  va2 s  . c  om*/
 * 
 * @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, String name, Object value) throws NamingException {
    Name n = ctx.getNameParser("").parse(name);
    rebind(ctx, n, value);
}

From source file:Util.java

/**
 * Create a link//  w ww.  j  av  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);
}

From source file:NonSerializableFactory.java

/** A convenience method that simplifies the process of rebinding a
 non-serializable object into a JNDI context.
        /*from ww w .  j  a  va  2s .com*/
@param ctx the JNDI context to rebind to.
@param key the key to use in both the NonSerializableFactory map and JNDI. It
must be a valid name for use in ctx.bind().
@param target the non-Serializable object to bind.
@param createSubcontexts a flag indicating if subcontexts of name should
 be created if they do not already exist.
@throws NamingException thrown on failure to rebind key into ctx.
*/
public static synchronized void rebind(Context ctx, String key, Object target, boolean createSubcontexts)
        throws NamingException {
    Name name = ctx.getNameParser("").parse(key);
    if (createSubcontexts == true && name.size() > 1) {
        int size = name.size() - 1;
        Util.createSubcontext(ctx, name.getPrefix(size));
    }
    rebind(ctx, key, target);
}

From source file:jdao.JDAO.java

public static Context unbind(Context ctx, String nameStr) throws NamingException {
    log("unbinding " + nameStr);

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

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

    Context subCtx = ctx;//from   www  .j  a  v  a 2 s.c o m

    for (int i = 0; i < name.size() - 1; i++) {
        try {
            subCtx = (Context) subCtx.lookup(name.get(i));
        } catch (NameNotFoundException e) {
            log("Subcontext " + name.get(i) + " undefined", e);
            return null;
        }
    }

    subCtx.unbind(name.get(name.size() - 1));
    log("unbound object " + nameStr);
    return subCtx;
}

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;//from   w  ww . j ava  2s. c om

    //last component of the name will be the name to bind
    for (int i = 0; i < name.size() - 1; i++) {
        try {
            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:org.compass.core.jndi.NamingHelper.java

/**
 * Bind val to name in ctx, and make sure that all intermediate contexts
 * exist./*from w w  w  .ja va 2s  . c  o  m*/
 * 
 * @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);
    }
}