Example usage for javax.naming Name get

List of usage examples for javax.naming Name get

Introduction

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

Prototype

public String get(int posn);

Source Link

Document

Retrieves a component of this name.

Usage

From source file:Util.java

/**
 * Create a link/*  w w  w .  j  a  va  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

/**
 * Bind val to name in ctx, and make sure that all intermediate contexts exist
 * /*  ww w.ja v a  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/*from   w  w  w  .  ja  v  a 2 s  .com*/
 * 
 * @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 subcontext including any intermediate contexts.
 * /*from 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 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: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 w w w.ja va 2 s.com

    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  w w .j a va 2s  . co  m

    //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.apache.naming.modules.fs.FileDirContext.java

/**
 * Retrieves the named object. The result is a File relative to the docBase
 * or a FileDirContext for directories./*from w  ww .j a va2 s.c o  m*/
 * 
 * @param name the name of the object to look up
 * @return the object bound to name
 * @exception NamingException if a naming exception is encountered
 */
public Object lookup(Name nameObj, boolean resolveLinkx) throws NamingException {
    if (log.isDebugEnabled())
        log.debug("lookup " + nameObj);

    System.out.println("XXX " + nameObj.get(0));
    if ("fs:".equals(nameObj.get(0).toString()))
        nameObj = nameObj.getSuffix(1);

    String name = nameObj.toString(); // we need to convert anyway, for File constructor

    Object result = null;
    File file = file(name);

    if (file == null)
        throw new NamingException(sm.getString("resources.notFound", name));

    if (file.isDirectory()) {
        FileDirContext tempContext = new FileDirContext(env);
        tempContext.setDocBase(file.getPath());
        result = tempContext;
    } else {
        // TODO: based on the name, return various 'styles' of
        // content
        // TODO: use lazy streams, cacheable
        result = file; //new FileResource(file);
    }

    return result;
}

From source file:org.apache.naming.modules.fs.FileDirContext.java

/**
 * Unbinds the named object. Removes the terminal atomic name in name 
 * from the target context--that named by all but the terminal atomic 
 * part of name./* w w  w.j a v  a  2  s.  com*/
 * <p>
 * This method is idempotent. It succeeds even if the terminal atomic 
 * name is not bound in the target context, but throws 
 * NameNotFoundException if any of the intermediate contexts do not exist. 
 * 
 * @param name the name to bind; may not be empty
 * @exception NameNotFoundException if an intermediate context does not 
 * exist
 * @exception NamingException if a naming exception is encountered
 */
public void unbind(Name nameObj) throws NamingException {
    if ("fs:".equals(nameObj.get(0).toString()))
        nameObj = nameObj.getSuffix(1);
    String name = nameObj.toString();
    if (log.isDebugEnabled())
        log.debug("unbind " + name);
    File file = file(name);

    if (file == null)
        throw new NamingException(sm.getString("resources.notFound", name));

    if (!file.delete())
        throw new NamingException(sm.getString("resources.unbindFailed", name));

}

From source file:org.apache.naming.NamingContext.java

/**
 * Unbinds the named object. Removes the terminal atomic name in name 
 * from the target context--that named by all but the terminal atomic 
 * part of name.//from w w  w. j a  v a 2  s  .com
 * <p>
 * This method is idempotent. It succeeds even if the terminal atomic 
 * name is not bound in the target context, but throws 
 * NameNotFoundException if any of the intermediate contexts do not exist. 
 * 
 * @param name the name to bind; may not be empty
 * @exception NameNotFoundException if an intermediate context does not 
 * exist
 * @exception NamingException if a naming exception is encountered
 */
public void unbind(Name name) throws NamingException {
    checkWritable();

    while ((!name.isEmpty()) && (name.get(0).length() == 0))
        name = name.getSuffix(1);
    if (name.isEmpty())
        throw new NamingException(sm.getString("namingContext.invalidName"));

    NamingEntry entry = (NamingEntry) bindings.get(name.get(0));

    if (entry == null) {
        throw new NameNotFoundException(sm.getString("namingContext.nameNotBound", name.get(0)));
    }

    if (name.size() > 1) {
        if (entry.type == NamingEntry.CONTEXT) {
            ((Context) entry.value).unbind(name.getSuffix(1));
        } else {
            throw new NamingException(sm.getString("namingContext.contextExpected"));
        }
    } else {
        bindings.remove(name.get(0));
    }

}

From source file:org.apache.naming.NamingContext.java

/**
 * Enumerates the names bound in the named context, along with the class 
 * names of objects bound to them. The contents of any subcontexts are 
 * not included.//from   w  ww.ja va2  s.c o m
 * <p>
 * If a binding is added to or removed from this context, its effect on 
 * an enumeration previously returned is undefined.
 * 
 * @param name the name of the context to list
 * @return an enumeration of the names and class names of the bindings in 
 * this context. Each element of the enumeration is of type NameClassPair.
 * @exception NamingException if a naming exception is encountered
 */
public NamingEnumeration list(Name name) throws NamingException {
    // Removing empty parts
    while ((!name.isEmpty()) && (name.get(0).length() == 0))
        name = name.getSuffix(1);
    if (name.isEmpty()) {
        return new NamingContextEnumeration(bindings.elements());
    }

    NamingEntry entry = (NamingEntry) bindings.get(name.get(0));

    if (entry == null) {
        throw new NameNotFoundException(sm.getString("namingContext.nameNotBound", name.get(0)));
    }

    if (entry.type != NamingEntry.CONTEXT) {
        throw new NamingException(sm.getString("namingContext.contextExpected"));
    }
    return ((Context) entry.value).list(name.getSuffix(1));
}