Example usage for javax.naming Name isEmpty

List of usage examples for javax.naming Name isEmpty

Introduction

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

Prototype

public boolean isEmpty();

Source Link

Document

Determines whether this name is empty.

Usage

From source file:com.ktds.ldap.populator.LdapTestUtils.java

private static void loadLdif(DirContext context, Name rootNode, Resource ldifFile) {
    try {//from  w w w .  j a va  2s .  c o m
        LdapName baseDn = (LdapName) context.getEnvironment()
                .get(DefaultDirObjectFactory.JNDI_ENV_BASE_PATH_KEY);

        LdifParser parser = new LdifParser(ldifFile);
        parser.open();
        while (parser.hasMoreRecords()) {
            LdapAttributes record = parser.getRecord();

            LdapName dn = record.getName();

            if (baseDn != null) {
                dn = LdapUtils.removeFirst(dn, baseDn);
            }

            if (!rootNode.isEmpty()) {
                dn = LdapUtils.prepend(dn, rootNode);
            }
            context.bind(dn, null, record);
        }
    } catch (Exception e) {
        throw new UncategorizedLdapException("Failed to populate LDIF", e);
    }
}

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 .  j  a  va  2  s  . c  o 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

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

    ensureContextNotClosed();/*  w w  w.  j  a  v  a  2 s.  com*/

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

    if (name.size() == 1) {
        if (objectTable.containsKey(name)) {
            objectTable.remove(name);
        }
        return;
    }

    final Context parentContext = getParentContext(name);

    parentContext.unbind(name.getSuffix(name.size() - 1));
}

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

@Override
public void rename(final Name oldName, final Name newName) throws NamingException {

    ensureContextNotClosed();/*from   w w w  . j  ava2  s.c  o m*/

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

    final Object oldValue = lookup(oldName);
    if (oldValue == null) {
        throw new NamingException(String.format("Cannot rename object: name not found (%s)", oldName));
    }

    if (lookup(newName) != null) {
        throw new NameAlreadyBoundException(
                String.format("Cannot rename object: name already bound (%s)", newName));
    }

    unbind(oldName);
    unbind(newName);
    bind(newName, oldValue);
}

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

@Override
public NamingEnumeration<NameClassPair> list(final Name name) throws NamingException {

    ensureContextNotClosed();//from   w  w  w  .j  av  a 2s  .  c  o  m

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

    final Name prefixName = name.getPrefix(1);
    if (objectTable.containsKey(prefixName)) {
        throw new NotContextException(String.format("%s cannot be listed", name));
    }

    if (subContexts.containsKey(prefixName)) {
        return subContexts.get(prefixName).list(name.getSuffix(1));
    }

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

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

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

    ensureContextNotClosed();//from www.j av a  2s . 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));
}

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

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

    ensureContextNotClosed();/*from  w  w  w. j av a  2  s .  c o  m*/

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

    // the parent context must exists
    getParentContext(name);
    // final Object targetContext = lookup(name.getPrefix(name.size() - 1));
    // if (targetContext == null || !(targetContext instanceof Context)) {
    // throw new NamingException(
    // String.format("Cannot bind object due context does not exist (%s)", name.toString()));
    // }
    unbind(name);
    bind(name, object);
}

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

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

    ensureContextNotClosed();/* w  ww  .j  a v  a2 s  . c  o  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 Object lookup(final Name name) throws NamingException {

    ensureContextNotClosed();//  w  ww  . ja  v  a  2  s.  com

    /*
     * Extract from Context Javadoc: If name is empty, returns a new instance of this context (which represents the
     * same naming context as this context, but its environment may be modified independently and it may be accessed
     * concurrently).
     */
    if (name.isEmpty()) {
        try {
            return this.clone();
        } catch (final CloneNotSupportedException e) {
            // this shouldn't happen, since we are Cloneable
            throw (NamingException) new OperationNotSupportedException(e.getMessage()).initCause(e);
        }
    }

    if (name.size() > 1) {
        if (subContexts.containsKey(name.getPrefix(1))) {
            return subContexts.get(name.getPrefix(1)).lookup(name.getSuffix(1));
        }
        throw new NamingException(
                String.format("Invalid subcontext '%s' in context '%s'", name.getPrefix(1).toString(),
                        StringUtils.isBlank(getNameInNamespace()) ? "/" : getNameInNamespace()));
    }

    Object result = null; // not found
    if (objectTable.containsKey(name)) {
        result = objectTable.get(name);
    } else if (subContexts.containsKey(name)) {
        result = subContexts.get(name);
    } else if (env.containsKey(name.toString())) {
        result = env.get(name.toString());
    }

    return result;
}

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 a2 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));
    }

}