Example usage for org.springframework.mock.jndi SimpleNamingContext SimpleNamingContext

List of usage examples for org.springframework.mock.jndi SimpleNamingContext SimpleNamingContext

Introduction

In this page you can find the example usage for org.springframework.mock.jndi SimpleNamingContext SimpleNamingContext.

Prototype

public SimpleNamingContext(String root, Hashtable<String, Object> boundObjects,
        @Nullable Hashtable<String, Object> env) 

Source Link

Document

Create a new naming context with the given naming root, the given name/object map, and the JNDI environment entries.

Usage

From source file:org.springframework.mock.jndi.SimpleNamingContext.java

/**
 * Look up the object with the given name.
 * <p>Note: Not intended for direct use by applications.
 * Will be used by any standard InitialContext JNDI lookups.
 * @throws javax.naming.NameNotFoundException if the object could not be found
 */// w  ww  .  java 2 s  .  com
@Override
public Object lookup(String lookupName) throws NameNotFoundException {
    String name = this.root + lookupName;
    if (logger.isDebugEnabled()) {
        logger.debug("Static JNDI lookup: [" + name + "]");
    }
    if ("".equals(name)) {
        return new SimpleNamingContext(this.root, this.boundObjects, this.environment);
    }
    Object found = this.boundObjects.get(name);
    if (found == null) {
        if (!name.endsWith("/")) {
            name = name + "/";
        }
        for (String boundName : this.boundObjects.keySet()) {
            if (boundName.startsWith(name)) {
                return new SimpleNamingContext(name, this.boundObjects, this.environment);
            }
        }
        throw new NameNotFoundException(
                "Name [" + this.root + lookupName + "] not bound; " + this.boundObjects.size() + " bindings: ["
                        + StringUtils.collectionToDelimitedString(this.boundObjects.keySet(), ",") + "]");
    }
    return found;
}

From source file:org.springframework.mock.jndi.SimpleNamingContext.java

@Override
public Context createSubcontext(String name) {
    String subcontextName = this.root + name;
    if (!subcontextName.endsWith("/")) {
        subcontextName += "/";
    }/*from ww w  . j av  a  2  s .c  om*/
    Context subcontext = new SimpleNamingContext(subcontextName, this.boundObjects, this.environment);
    bind(name, subcontext);
    return subcontext;
}

From source file:org.springframework.mock.jndi.SimpleNamingContextBuilder.java

/**
 * Simple InitialContextFactoryBuilder implementation,
 * creating a new SimpleNamingContext instance.
 * @see SimpleNamingContext/*from  w  w  w.j a v  a  2s . c o m*/
 */
@Override
public InitialContextFactory createInitialContextFactory(@Nullable Hashtable<?, ?> environment) {
    if (activated == null && environment != null) {
        Object icf = environment.get(Context.INITIAL_CONTEXT_FACTORY);
        if (icf != null) {
            Class<?> icfClass;
            if (icf instanceof Class) {
                icfClass = (Class<?>) icf;
            } else if (icf instanceof String) {
                icfClass = ClassUtils.resolveClassName((String) icf, getClass().getClassLoader());
            } else {
                throw new IllegalArgumentException("Invalid value type for environment key ["
                        + Context.INITIAL_CONTEXT_FACTORY + "]: " + icf.getClass().getName());
            }
            if (!InitialContextFactory.class.isAssignableFrom(icfClass)) {
                throw new IllegalArgumentException("Specified class does not implement ["
                        + InitialContextFactory.class.getName() + "]: " + icf);
            }
            try {
                return (InitialContextFactory) ReflectionUtils.accessibleConstructor(icfClass).newInstance();
            } catch (Throwable ex) {
                throw new IllegalStateException("Unable to instantiate specified InitialContextFactory: " + icf,
                        ex);
            }
        }
    }

    // Default case...
    return new InitialContextFactory() {
        @Override
        @SuppressWarnings("unchecked")
        public Context getInitialContext(Hashtable<?, ?> environment) {
            return new SimpleNamingContext("", boundObjects, (Hashtable<String, Object>) environment);
        }
    };
}