Example usage for javax.naming Name getPrefix

List of usage examples for javax.naming Name getPrefix

Introduction

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

Prototype

public Name getPrefix(int posn);

Source Link

Document

Creates a name whose components consist of a prefix of the components of this name.

Usage

From source file:Util.java

/**
 * Bind val to name in ctx, and make sure that all intermediate contexts exist
 * /*from  w ww.j  ava  2s.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 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//w  ww .  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 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

/**
 * Unbinds a name from ctx, and removes parents if they are empty
 * //from  w w w  . j  a va2 s. 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, 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.
        //w w  w. j  ava 2  s.  c o m
@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.
 * //from  w w  w. j  a v a2  s.  c  om
 * @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

private Context getParentContext(final Name name) throws NamingException {

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

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;
        }/* w w w  .  ja  v a 2 s.  com*/
        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;
    }//  ww  w.j ava  2s. c  om

    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

@Override
public void bind(final Name name, final Object object) throws NamingException {

    ensureContextNotClosed();//from ww w  .  jav  a2  s  .  co m

    if (object == null) {
        return;
    }

    if (name.isEmpty()) {
        throw new InvalidNameException("Cannot bind to an empty name");
    }

    final Name prefix = name.getPrefix(1);
    if (subContexts.containsKey(prefix)) {
        subContexts.get(prefix).bind(name.getSuffix(1), object);
        return;
    }

    if (objectTable.containsKey(name) || subContexts.containsKey(name) || env.containsKey(name.toString())) {
        throw new NameAlreadyBoundException(
                String.format("Name %s already bound. Use rebind() to override", name.toString()));
    }

    if (object instanceof Context) {
        subContexts.put(name, (Context) object);
    } else {
        objectTable.put(name, object);
    }
}

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

@Override
public NamingEnumeration<Binding> listBindings(final Name name) throws NamingException {

    ensureContextNotClosed();/*from  w  ww  .  j  av  a2 s.c  o  m*/

    if (name == null || name.isEmpty()) {
        final Map<Name, Object> enumStore = new HashMap<Name, Object>();
        enumStore.putAll(objectTable);
        enumStore.putAll(subContexts);
        return new BindingNamingEnumeration(enumStore);
    }

    final Name subName = name.getPrefix(1);

    if (objectTable.containsKey(subName)) {
        throw new NotContextException(String.format("%s cannot be listed", name));
    }

    if (subContexts.containsKey(subName)) {
        return subContexts.get(subName).listBindings(name.getSuffix(1));
    }

    throw new NamingException(String.format("The named context '%s' can't be found", name));
}