Search in sources :

Example 26 with LinkRef

use of javax.naming.LinkRef in project geronimo-xbean by apache.

the class AbstractContext method lookup.

public Object lookup(Name name) throws NamingException {
    if (name == null)
        throw new NullPointerException("name is null");
    Object value = lookup(null, name);
    // if we got a link back we need to resolve it
    if (value instanceof LinkRef) {
        LinkRef linkRef = (LinkRef) value;
        value = lookup(linkRef.getLinkName());
    }
    return value;
}
Also used : LinkRef(javax.naming.LinkRef)

Example 27 with LinkRef

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

the class NamingContext method lookup.

/**
 * 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 = null;
                if (!GRAAL) {
                    obj = NamingManager.getObjectInstance(entry.value, name, this, env);
                } else {
                    // NamingManager.getObjectInstance would simply return the reference here
                    // Use the configured object factory to resolve it directly if possible
                    // Note: This may need manual constructor reflection configuration
                    Reference reference = (Reference) entry.value;
                    Class<?> factoryClass = getClass().getClassLoader().loadClass(reference.getFactoryClassName());
                    ObjectFactory factory = (ObjectFactory) factoryClass.getDeclaredConstructor().newInstance();
                    obj = factory.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;
                    }
                }
                if (obj == null) {
                    throw new NamingException(sm.getString("namingContext.failResolvingReference"));
                }
                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) Reference(javax.naming.Reference) InitialContext(javax.naming.InitialContext) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) NotContextException(javax.naming.NotContextException) NamingException(javax.naming.NamingException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) NameNotFoundException(javax.naming.NameNotFoundException) ObjectFactory(javax.naming.spi.ObjectFactory) NamingException(javax.naming.NamingException) LinkRef(javax.naming.LinkRef)

Example 28 with LinkRef

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

the class NamingContext method lookupLink.

/**
 * {@inheritDoc}
 */
public Object lookupLink(Name name) throws NamingException {
    check(name, JndiPermission.ACTION_LOOKUP);
    if (name.isEmpty()) {
        return lookup(name);
    }
    try {
        final Name absoluteName = getAbsoluteName(name);
        Object link = namingStore.lookup(absoluteName);
        if (!(link instanceof LinkRef) && link instanceof Reference) {
            link = getObjectInstance(link, name, null);
        }
        return link;
    } catch (Exception e) {
        throw namingException(NamingLogger.ROOT_LOGGER.cannotLookupLink(), e, name);
    }
}
Also used : Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) NamingUtils.notAContextException(org.jboss.as.naming.util.NamingUtils.notAContextException) CannotProceedException(javax.naming.CannotProceedException) NamingUtils.namingException(org.jboss.as.naming.util.NamingUtils.namingException) CompositeName(javax.naming.CompositeName) Name(javax.naming.Name) LinkRef(javax.naming.LinkRef)

Example 29 with LinkRef

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

the class NamingContext method lookup.

public Object lookup(final Name name, boolean dereference) throws NamingException {
    check(name, JndiPermission.ACTION_LOOKUP);
    if (isEmpty(name)) {
        return new NamingContext(prefix, namingStore, environment);
    }
    final Name absoluteName = getAbsoluteName(name);
    Object result;
    try {
        result = namingStore.lookup(absoluteName, dereference);
    } catch (CannotProceedException cpe) {
        final Context continuationContext = NamingManager.getContinuationContext(cpe);
        if (continuationContext instanceof NamingContext) {
            result = ((NamingContext) continuationContext).lookup(cpe.getRemainingName(), dereference);
        } else {
            result = continuationContext.lookup(cpe.getRemainingName());
        }
    }
    if (result instanceof ResolveResult) {
        final ResolveResult resolveResult = (ResolveResult) result;
        final Object resolvedObject = resolveResult.getResolvedObj();
        Object context;
        if (resolvedObject instanceof Context) {
            context = resolvedObject;
        } else if (resolvedObject instanceof LinkRef) {
            context = resolveLink(resolvedObject, dereference);
        } else {
            context = getObjectInstance(resolvedObject, absoluteName, environment);
        }
        if (!(context instanceof Context)) {
            throw notAContextException(absoluteName.getPrefix(absoluteName.size() - resolveResult.getRemainingName().size()));
        }
        final Context namingContext = (Context) context;
        if (namingContext instanceof NamingContext) {
            return ((NamingContext) namingContext).lookup(resolveResult.getRemainingName(), dereference);
        } else {
            return namingContext.lookup(resolveResult.getRemainingName());
        }
    } else if (result instanceof LinkRef) {
        result = resolveLink(result, dereference);
    } else if (result instanceof Reference) {
        result = getObjectInstance(result, absoluteName, environment);
        if (result instanceof LinkRef) {
            result = resolveLink(result, dereference);
        }
    }
    return result;
}
Also used : Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) EventContext(javax.naming.event.EventContext) CannotProceedException(javax.naming.CannotProceedException) Reference(javax.naming.Reference) ResolveResult(javax.naming.spi.ResolveResult) CompositeName(javax.naming.CompositeName) Name(javax.naming.Name) LinkRef(javax.naming.LinkRef)

Example 30 with LinkRef

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

the class NamingContextTestCase method testLookupLink.

@Test
public void testLookupLink() throws Exception {
    final Name name = new CompositeName("test");
    namingStore.bind(name, "testValue", String.class);
    final Name linkName = new CompositeName("link");
    namingStore.bind(linkName, new LinkRef("./test"));
    Object result = namingContext.lookup(linkName);
    assertEquals("testValue", result);
    // the same with security permissions
    result = testActionPermission(JndiPermission.ACTION_LOOKUP, Arrays.asList(new JndiPermission("test", "lookup")), namingContext, "link");
    assertEquals("testValue", result);
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, InitialContextFactory.class.getName());
    namingStore.rebind(linkName, new LinkRef(name));
    result = namingContext.lookup(linkName);
    assertEquals("testValue", result);
    // the same with security permissions
    result = testActionPermission(JndiPermission.ACTION_LOOKUP, Arrays.asList(new JndiPermission("test", "lookup")), namingContext, "link");
    assertEquals("testValue", result);
}
Also used : CompositeName(javax.naming.CompositeName) JndiPermission(org.wildfly.naming.java.permission.JndiPermission) CompositeName(javax.naming.CompositeName) Name(javax.naming.Name) LinkRef(javax.naming.LinkRef) Test(org.junit.Test)

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