Search in sources :

Example 11 with LinkRef

use of javax.naming.LinkRef in project activemq-artemis by apache.

the class TestContext method lookup.

@Override
public Object lookup(String name) throws NamingException {
    if (name.length() == 0) {
        return this;
    }
    Object result = treeBindings.get(name);
    if (result == null) {
        result = bindings.get(name);
    }
    if (result == null) {
        int pos = name.indexOf(':');
        if (pos > 0) {
            String scheme = name.substring(0, pos);
            Context ctx = NamingManager.getURLContext(scheme, environment);
            if (ctx == null) {
                throw new NamingException("scheme " + scheme + " not recognized");
            }
            return ctx.lookup(name);
        } else {
            // Split out the first name of the path
            // and look for it in the bindings map.
            CompositeName path = new CompositeName(name);
            if (path.size() == 0) {
                return this;
            } else {
                String first = path.get(0);
                Object obj = bindings.get(first);
                if (obj == null) {
                    throw new NameNotFoundException(name);
                } else if (obj instanceof Context && path.size() > 1) {
                    Context subContext = (Context) obj;
                    obj = subContext.lookup(path.getSuffix(1));
                }
                return obj;
            }
        }
    }
    if (result instanceof LinkRef) {
        LinkRef ref = (LinkRef) result;
        result = lookup(ref.getLinkName());
    }
    if (result instanceof Reference) {
        try {
            result = NamingManager.getObjectInstance(result, null, null, this.environment);
        } catch (NamingException e) {
            throw e;
        } catch (Exception e) {
            throw (NamingException) new NamingException("could not look up : " + name).initCause(e);
        }
    }
    if (result instanceof TestContext) {
        String prefix = getNameInNamespace();
        if (prefix.length() > 0) {
            prefix = prefix + SEPARATOR;
        }
        result = new TestContext((TestContext) result, environment, prefix + name);
    }
    return result;
}
Also used : Context(javax.naming.Context) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) CompositeName(javax.naming.CompositeName) NamingException(javax.naming.NamingException) NotContextException(javax.naming.NotContextException) NamingException(javax.naming.NamingException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) NameNotFoundException(javax.naming.NameNotFoundException) LinkRef(javax.naming.LinkRef)

Example 12 with LinkRef

use of javax.naming.LinkRef in project tomcat70 by apache.

the class NamingContext method bind.

/**
 * Binds a name to an object. All intermediate contexts and the target
 * context (that named by all but terminal atomic component of the name)
 * must already exist.
 *
 * @param name the name to bind; may not be empty
 * @param obj the object to bind; possibly null
 * @param rebind if true, then perform a rebind (ie, overwrite)
 * @exception NameAlreadyBoundException if name is already bound
 * @exception javax.naming.directory.InvalidAttributesException if object
 * did not supply all mandatory attributes
 * @exception NamingException if a naming exception is encountered
 */
protected void bind(Name name, Object obj, boolean rebind) throws NamingException {
    if (!checkWritable()) {
        return;
    }
    while ((!name.isEmpty()) && (name.get(0).length() == 0)) name = name.getSuffix(1);
    if (name.isEmpty())
        throw new NamingException(sm.getString("namingContext.invalidName"));
    NamingEntry entry = bindings.get(name.get(0));
    if (name.size() > 1) {
        if (entry == null) {
            throw new NameNotFoundException(sm.getString("namingContext.nameNotBound", name, name.get(0)));
        }
        if (entry.type == NamingEntry.CONTEXT) {
            if (rebind) {
                ((Context) entry.value).rebind(name.getSuffix(1), obj);
            } else {
                ((Context) entry.value).bind(name.getSuffix(1), obj);
            }
        } else {
            throw new NamingException(sm.getString("namingContext.contextExpected"));
        }
    } else {
        if ((!rebind) && (entry != null)) {
            throw new NameAlreadyBoundException(sm.getString("namingContext.alreadyBound", name.get(0)));
        } else {
            // Getting the type of the object and wrapping it within a new
            // NamingEntry
            Object toBind = NamingManager.getStateToBind(obj, name, this, env);
            if (toBind instanceof Context) {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.CONTEXT);
            } else if (toBind instanceof LinkRef) {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.LINK_REF);
            } else if (toBind instanceof Reference) {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE);
            } else if (toBind instanceof Referenceable) {
                toBind = ((Referenceable) toBind).getReference();
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.REFERENCE);
            } else {
                entry = new NamingEntry(name.get(0), toBind, NamingEntry.ENTRY);
            }
            bindings.put(name.get(0), entry);
        }
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) Referenceable(javax.naming.Referenceable) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) LinkRef(javax.naming.LinkRef)

Example 13 with LinkRef

use of javax.naming.LinkRef in project tomcat70 by apache.

the class NamingContext method lookup.

// ------------------------------------------------------ Protected Methods
/**
 * Retrieves the named object.
 *
 * @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 = bindings.get(name.get(0));
    if (entry == null) {
        throw new NameNotFoundException(sm.getString("namingContext.nameNotBound", name, name.get(0)));
    }
    if (name.size() > 1) {
        // 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 (entry.value instanceof ResourceRef) {
                    boolean singleton = Boolean.parseBoolean((String) ((ResourceRef) entry.value).get("singleton").getContent());
                    if (singleton) {
                        entry.type = NamingEntry.ENTRY;
                        entry.value = obj;
                    }
                }
                return obj;
            } catch (NamingException e) {
                throw e;
            } catch (Exception e) {
                String msg = sm.getString("namingContext.failResolvingReference");
                log.warn(msg, e);
                NamingException ne = new NamingException(msg);
                ne.initCause(e);
                throw ne;
            }
        } else {
            return entry.value;
        }
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NameNotFoundException(javax.naming.NameNotFoundException) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) NotContextException(javax.naming.NotContextException) NamingException(javax.naming.NamingException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) NameNotFoundException(javax.naming.NameNotFoundException) LinkRef(javax.naming.LinkRef)

Example 14 with LinkRef

use of javax.naming.LinkRef in project wildfly by wildfly.

the class NamingContext method resolveLink.

private Object resolveLink(Object result, boolean dereference) throws NamingException {
    final Object linkResult;
    try {
        final LinkRef linkRef = (LinkRef) result;
        final String referenceName = linkRef.getLinkName();
        if (referenceName.startsWith("./")) {
            linkResult = lookup(parseName(referenceName.substring(2)), dereference);
        } else {
            linkResult = new InitialContext().lookup(referenceName);
        }
    } catch (Throwable t) {
        throw NamingLogger.ROOT_LOGGER.cannotDeferenceObject(t);
    }
    return linkResult;
}
Also used : InitialContext(javax.naming.InitialContext) LinkRef(javax.naming.LinkRef)

Example 15 with LinkRef

use of javax.naming.LinkRef in project wildfly by wildfly.

the class BinderBean method bindAndLink.

public void bindAndLink(Object value) throws NamingException {
    InitialContext initialContext = new InitialContext();
    initialContext.bind(SRC_NAME, value);
    initialContext.bind(Binder.LINK_NAME, new LinkRef(SRC_NAME));
}
Also used : InitialContext(javax.naming.InitialContext) LinkRef(javax.naming.LinkRef)

Aggregations

LinkRef (javax.naming.LinkRef)32 Context (javax.naming.Context)23 NamingException (javax.naming.NamingException)18 InitialContext (javax.naming.InitialContext)17 NameNotFoundException (javax.naming.NameNotFoundException)17 Reference (javax.naming.Reference)15 NotContextException (javax.naming.NotContextException)11 OperationNotSupportedException (javax.naming.OperationNotSupportedException)11 CompositeName (javax.naming.CompositeName)10 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)8 Name (javax.naming.Name)7 Referenceable (javax.naming.Referenceable)4 Test (org.junit.Test)4 MalformedURLException (java.net.MalformedURLException)2 HashMap (java.util.HashMap)2 Binding (javax.naming.Binding)2 CannotProceedException (javax.naming.CannotProceedException)2 CompoundName (javax.naming.CompoundName)2 Provider (com.google.inject.Provider)1 IOException (java.io.IOException)1