Example usage for javax.naming Name size

List of usage examples for javax.naming Name size

Introduction

In this page you can find the example usage for javax.naming Name size.

Prototype

public int size();

Source Link

Document

Returns the number of components in this name.

Usage

From source file:Util.java

/**
 * Bind val to name in ctx, and make sure that all intermediate contexts exist
 * /*  w w w.  jav  a 2s.  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, 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.bind(atom, value);
}

From source file:Util.java

/**
 * Rebind val to name in ctx, and make sure that all intermediate contexts
 * exist//from  w ww  . 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 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:Util.java

/**
 * Create a link//from w ww.jav  a 2  s.c om
 * 
 * @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:Util.java

/**
 * Create a subcontext including any intermediate contexts.
 * //from ww  w  .jav  a  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:Util.java

/**
 * Unbinds a name from ctx, and removes parents if they are empty
 * //from w  ww .  j  a  v a 2 s  . c  o 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, Name name) throws NamingException {
    ctx.unbind(name); // unbind the end node in the name
    int sz = name.size();
    // walk the tree backwards, stopping at the domain
    while (--sz > 0) {
        Name pname = name.getPrefix(sz);
        try {
            ctx.destroySubcontext(pname);
        } catch (NamingException e) {
            System.out.println("Unable to remove context " + pname + e);
            break;
        }
    }
}

From source file:NonSerializableFactory.java

/** A convenience method that simplifies the process of rebinding a
 non-serializable object into a JNDI context.
        /*from   w w  w  . ja  va2 s .  c om*/
@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:NonSerializableFactory.java

/** 
 * A convenience method that simplifies the process of rebinding a 
 * non-serializable object into a JNDI context. This version binds the
 * target object into the default IntitialContext using name path.
 * //  w  ww .jav a  2 s.  c o  m
 * @param name the name to use as JNDI path name. The key into the 
 *   NonSerializableFactory map is obtained from the toString() value of name. 
 *   The name parameter cannot be a 0 length name.
 * @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.
 * @author Matt Carter
 **/
public static synchronized void rebind(Name name, Object target, boolean createSubcontexts)
        throws NamingException {
    String key = name.toString();
    InitialContext ctx = new InitialContext();
    if (createSubcontexts == true && name.size() > 1) {
        int size = name.size() - 1;
        Util.createSubcontext(ctx, name.getPrefix(size));
    }
    rebind(ctx, key, target);
}

From source file:com.dattack.naming.AbstractContext.java

@Override
public void destroySubcontext(final Name name) throws NamingException {

    if (name.size() > 1) {
        if (subContexts.containsKey(name.getPrefix(1))) {
            final Context subContext = subContexts.get(name.getPrefix(1));
            subContext.destroySubcontext(name.getSuffix(1));
            return;
        }//from w  w w  .  j a v a 2  s. co m
        throw new NameNotFoundException();
    }

    if (objectTable.containsKey(name) || !subContexts.containsKey(name)) {
        throw new NameNotFoundException(String.format("Context not found: %s", name));
    }

    final Context subContext = subContexts.get(name);
    final NamingEnumeration<NameClassPair> names = subContext.list("");
    if (names.hasMore()) {
        throw new ContextNotEmptyException();
    }

    subContexts.get(name).close();
    subContexts.remove(name);
}

From source file:com.dattack.naming.AbstractContext.java

@Override
public NameParser getNameParser(final Name name) throws NamingException {

    if (name == null || name.isEmpty() || (name.size() == 1 && name.toString().equals(getNameInNamespace()))) {
        return nameParser;
    }/*from w ww  .  ja  va 2  s . co m*/

    final Name subName = name.getPrefix(1);
    if (subContexts.containsKey(subName)) {
        return subContexts.get(subName).getNameParser(name.getSuffix(1));
    }

    throw new NotContextException();
}

From source file:com.dattack.naming.AbstractContext.java

private Context getParentContext(final Name name) throws NamingException {

    final Object context = lookup(name.getPrefix(name.size() - 1));
    if (context instanceof Context) {
        return (Context) context;
    }/*  w ww  . j a v  a2s  . c  om*/
    throw new NamingException(String.format("Cannot unbind object. Target context does not exist (%s)",
            name.getPrefix(name.size() - 1)));
}