Search in sources :

Example 16 with NameClassPair

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

the class NamingContextTestCase method checkListResults.

private void checkListResults(NamingEnumeration<? extends NameClassPair> results) throws NamingException {
    final Set<String> expected = new HashSet<String>(Arrays.asList("test", "testTwo", "testThree", "testContext"));
    while (results.hasMore()) {
        NameClassPair result = results.next();
        final String resultName = result.getName();
        if ("test".equals(resultName) || "testTwo".equals(resultName) || "testThree".equals(resultName)) {
            assertEquals(Object.class.getName(), result.getClassName());
        } else if ("testContext".equals(resultName)) {
            assertEquals(Context.class.getName(), result.getClassName());
        } else {
            fail("Unknown result name: " + resultName);
        }
        expected.remove(resultName);
    }
    assertTrue("Not all expected results were returned", expected.isEmpty());
}
Also used : NameClassPair(javax.naming.NameClassPair) HashSet(java.util.HashSet)

Example 17 with NameClassPair

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

the class NamingContextTestCase method testListWithContinuation.

@Test
@SuppressWarnings("unchecked")
public void testListWithContinuation() throws Exception {
    bindListWithContinuations();
    NamingEnumeration<NameClassPair> results = namingContext.list(new CompositeName("comp"));
    checkListWithContinuationsResults(results);
    // the same with security permissions
    results = (NamingEnumeration<NameClassPair>) testActionPermission(JndiPermission.ACTION_LIST, Arrays.asList(new JndiPermission("test", "list")), namingContext, "comp");
    checkListWithContinuationsResults(results);
}
Also used : NameClassPair(javax.naming.NameClassPair) CompositeName(javax.naming.CompositeName) JndiPermission(org.wildfly.naming.java.permission.JndiPermission) Test(org.junit.Test)

Example 18 with NameClassPair

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

the class ServiceBasedNamingStore method list.

public List<NameClassPair> list(final Name name) throws NamingException {
    final ServiceName lookupName = buildServiceName(name);
    final ServiceName floor = boundServices.floor(lookupName);
    boolean isContextBinding = false;
    if (floor != null && floor.isParentOf(lookupName)) {
        // Parent might be a reference or a link
        Object obj = lookup(name.toString(), floor, true);
        if (obj instanceof NamingContext) {
            isContextBinding = true;
        } else if (obj != null) {
            throw new RequireResolveException(convert(floor));
        }
    }
    final List<ServiceName> children = listChildren(lookupName, isContextBinding);
    final String[] lookupParts = lookupName.toArray();
    final Set<String> childContexts = new HashSet<String>();
    final List<NameClassPair> results = new ArrayList<NameClassPair>();
    for (ServiceName child : children) {
        final String[] childParts = child.toArray();
        if (childParts.length > lookupParts.length + 1) {
            childContexts.add(childParts[lookupParts.length]);
        } else {
            final Object binding = lookup(name.toString(), child, false);
            if (binding != null) {
                final String bindingType;
                if (binding instanceof ContextListManagedReferenceFactory) {
                    bindingType = ContextListManagedReferenceFactory.class.cast(binding).getInstanceClassName();
                } else {
                    if (binding instanceof ManagedReferenceFactory) {
                        bindingType = ContextListManagedReferenceFactory.DEFAULT_INSTANCE_CLASS_NAME;
                    } else {
                        bindingType = binding.getClass().getName();
                    }
                }
                results.add(new NameClassPair(childParts[childParts.length - 1], bindingType));
            }
        }
    }
    for (String contextName : childContexts) {
        results.add(new NameClassPair(contextName, Context.class.getName()));
    }
    return results;
}
Also used : ArrayList(java.util.ArrayList) ServiceName(org.jboss.msc.service.ServiceName) NameClassPair(javax.naming.NameClassPair) HashSet(java.util.HashSet)

Example 19 with NameClassPair

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

the class RemoteNamingEjbTestCase method testIt.

@Test
public void testIt() throws Exception {
    final InitialContext ctx = getRemoteContext();
    final ClassLoader current = Thread.currentThread().getContextClassLoader();
    try {
        Thread.currentThread().setContextClassLoader(Remote.class.getClassLoader());
        Remote remote = (Remote) ctx.lookup(ARCHIVE_NAME + "/" + Bean.class.getSimpleName() + "!" + Remote.class.getName());
        assertNotNull(remote);
        assertEquals("Echo: test", remote.echo("test"));
        remote = (Remote) ctx.lookup(ARCHIVE_NAME + "/" + Singleton.class.getSimpleName() + "!" + BinderRemote.class.getName());
        assertNotNull(remote);
        assertEquals("Echo: test", remote.echo("test"));
        remote = (Remote) ctx.lookup(ARCHIVE_NAME + "/" + StatefulBean.class.getSimpleName() + "!" + Remote.class.getName());
        assertNotNull(remote);
        assertEquals("Echo: test", remote.echo("test"));
        final Set<String> expected = new HashSet<String>();
        expected.add(Bean.class.getSimpleName() + "!" + Remote.class.getName());
        expected.add(Singleton.class.getSimpleName() + "!" + BinderRemote.class.getName());
        expected.add(StatefulBean.class.getSimpleName() + "!" + Remote.class.getName());
        NamingEnumeration<NameClassPair> e = ctx.list("test");
        while (e.hasMore()) {
            NameClassPair binding = e.next();
            if (!expected.remove(binding.getName())) {
                Assert.fail("unknown binding " + binding.getName());
            }
        }
        if (!expected.isEmpty()) {
            Assert.fail("bindings not found " + expected);
        }
    } finally {
        ctx.close();
        Thread.currentThread().setContextClassLoader(current);
    }
}
Also used : NameClassPair(javax.naming.NameClassPair) InitialContext(javax.naming.InitialContext) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 20 with NameClassPair

use of javax.naming.NameClassPair in project tomee by apache.

the class JndiServlet method addBindings.

private void addBindings(String path, Map<String, Object> bindings, Context context) {
    try {
        for (NameClassPair pair : Collections.list(context.list(""))) {
            String name = pair.getName();
            String className = pair.getClassName();
            if ("org.apache.naming.resources.FileDirContext$FileResource".equals(className)) {
                bindings.put(path + name, "<file>");
            } else {
                try {
                    Object value = context.lookup(name);
                    if (value instanceof Context) {
                        Context nextedContext = (Context) value;
                        bindings.put(path + name, "");
                        addBindings(path + name + "/", bindings, nextedContext);
                    } else {
                        bindings.put(path + name, value);
                    }
                } catch (NamingException e) {
                    // lookup failed
                    bindings.put(path + name, "ERROR: " + e.getMessage());
                }
            }
        }
    } catch (NamingException e) {
        bindings.put(path, "ERROR: list bindings threw an exception: " + e.getMessage());
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NameClassPair(javax.naming.NameClassPair) NamingException(javax.naming.NamingException)

Aggregations

NameClassPair (javax.naming.NameClassPair)59 NamingException (javax.naming.NamingException)27 DirContext (javax.naming.directory.DirContext)19 Test (org.junit.Test)17 HashSet (java.util.HashSet)13 Context (javax.naming.Context)11 InitialContext (javax.naming.InitialContext)10 Hashtable (java.util.Hashtable)9 NamingEnumeration (javax.naming.NamingEnumeration)8 InitialDirContext (javax.naming.directory.InitialDirContext)8 IOException (java.io.IOException)7 FileNotFoundException (java.io.FileNotFoundException)5 HashMap (java.util.HashMap)5 Resource (org.apache.naming.resources.Resource)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 OutputStreamWriter (java.io.OutputStreamWriter)4 Vector (java.util.Vector)4 CompositeName (javax.naming.CompositeName)4 File (java.io.File)3