Example usage for javax.naming.spi NamingManager getObjectInstance

List of usage examples for javax.naming.spi NamingManager getObjectInstance

Introduction

In this page you can find the example usage for javax.naming.spi NamingManager getObjectInstance.

Prototype

public static Object getObjectInstance(Object refInfo, Name name, Context nameCtx, Hashtable<?, ?> environment)
        throws Exception 

Source Link

Document

Creates an instance of an object for the specified object and environment.

Usage

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

/**
 * Retrieves the named object.//from www  . j ava2 s.c o m
 * 
 * @param name the name of the object to look up
 * @param resolveLinks If true, the links will be resolved
 * @return the object bound to name
 * @exception NamingException if a naming exception is encountered
 */
protected Object lookup(Name name, boolean resolveLinks) throws NamingException {

    // Removing empty parts
    while ((!name.isEmpty()) && (name.get(0).length() == 0))
        name = name.getSuffix(1);
    if (name.isEmpty()) {
        // If name is empty, a newly allocated naming context is returned
        return new NamingContext(env, this.name, bindings);
    }

    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 the size of the name is greater that 1, then we go through a
        // number of subcontexts.
        if (entry.type != NamingEntry.CONTEXT) {
            throw new NamingException(sm.getString("namingContext.contextExpected"));
        }
        return ((Context) entry.value).lookup(name.getSuffix(1));
    } else {
        if ((resolveLinks) && (entry.type == NamingEntry.LINK_REF)) {
            String link = ((LinkRef) entry.value).getLinkName();
            if (link.startsWith(".")) {
                // Link relative to this context
                return lookup(link.substring(1));
            } else {
                return (new InitialContext(env)).lookup(link);
            }
        } else if (entry.type == NamingEntry.REFERENCE) {
            try {
                Object obj = NamingManager.getObjectInstance(entry.value, name, this, env);
                if (obj != null) {
                    entry.value = obj;
                    entry.type = NamingEntry.ENTRY;
                }
                return obj;
            } catch (NamingException e) {
                throw e;
            } catch (Exception e) {
                log.warn(sm.getString("namingContext.failResolvingReference"), e);
                throw new NamingException(e.getMessage());
            }
        } else {
            return entry.value;
        }
    }

}

From source file:org.eclipse.ecr.runtime.api.DataSourceHelper.java

/**
 * Look up a datasource given a partial name.
 * <p>//w  w w  .  j av  a  2s . com
 * For a datasource {@code "jdbc/foo"}, then it's sufficient to pass {@code
 * "foo"} to this method.
 *
 * @param partialName the partial name
 * @return the datasource
 * @throws NamingException
 */
public static DataSource getDataSource(String partialName) throws NamingException {
    String jndiName = getDataSourceJNDIName(partialName);
    InitialContext context = new InitialContext();
    Object resolved = context.lookup(jndiName);
    if (resolved instanceof Reference) {
        try {
            resolved = NamingManager.getObjectInstance(resolved, new CompositeName(jndiName), context, null);
        } catch (Exception e) {
            throw new Error("Cannot get access to " + jndiName, e);
        }
    }
    return (DataSource) resolved;
}

From source file:org.nuxeo.runtime.datasource.DataSourceHelper.java

public static <T> T getDataSource(String partialName, Class<T> clazz) throws NamingException {
    String jndiName = getDataSourceJNDIName(partialName);
    InitialContext context = new InitialContext();
    Object resolved = context.lookup(jndiName);
    if (resolved instanceof Reference) {
        try {//ww  w .  ja v a 2 s  .  co m
            resolved = NamingManager.getObjectInstance(resolved, new CompositeName(jndiName), context, null);
        } catch (Exception e) {
            throw new RuntimeException("Cannot get access to " + jndiName, e);
        }
    }
    return clazz.cast(resolved);
}

From source file:org.nuxeo.runtime.jtajca.NuxeoContainer.java

public static <T> T lookup(Context context, String name, Class<T> type) throws NamingException {
    Object resolved;// ww  w. j  av a  2 s.co m
    try {
        resolved = context.lookup(detectJNDIPrefix(context).concat(name));
    } catch (NamingException cause) {
        if (parentContext == null) {
            throw cause;
        }
        return type.cast(parentContext.lookup(detectJNDIPrefix(parentContext).concat(name)));
    }
    if (resolved instanceof Reference) {
        try {
            resolved = NamingManager.getObjectInstance(resolved, new CompositeName(name), rootContext, null);
        } catch (NamingException e) {
            throw e;
        } catch (Exception e) { // stupid JNDI API throws Exception
            throw ExceptionUtils.runtimeException(e);
        }
    }
    return type.cast(resolved);
}