Search in sources :

Example 11 with Binding

use of javax.naming.Binding in project jetty.project by eclipse.

the class NamingContext method dump.

/* ------------------------------------------------------------ */
public void dump(Appendable out, String indent) throws IOException {
    out.append(this.getClass().getSimpleName()).append("@").append(Long.toHexString(this.hashCode())).append("\n");
    int size = _bindings.size();
    int i = 0;
    for (Map.Entry<String, Binding> entry : ((Map<String, Binding>) _bindings).entrySet()) {
        boolean last = ++i == size;
        out.append(indent).append(" +- ").append(entry.getKey()).append(": ");
        Binding binding = entry.getValue();
        Object value = binding.getObject();
        if ("comp".equals(entry.getKey()) && value instanceof Reference && "org.eclipse.jetty.jndi.ContextFactory".equals(((Reference) value).getFactoryClassName())) {
            ContextFactory.dump(out, indent + (last ? "    " : " |  "));
        } else if (value instanceof Dumpable) {
            ((Dumpable) value).dump(out, indent + (last ? "    " : " |  "));
        } else {
            out.append(value.getClass().getSimpleName()).append("=");
            out.append(String.valueOf(value).replace('\n', '|').replace('\r', '|'));
            out.append("\n");
        }
    }
}
Also used : Binding(javax.naming.Binding) Reference(javax.naming.Reference) HashMap(java.util.HashMap) Map(java.util.Map) Dumpable(org.eclipse.jetty.util.component.Dumpable)

Example 12 with Binding

use of javax.naming.Binding in project jetty.project by eclipse.

the class TestNamingEntries method after.

/**
     * after each test we should scrape out any lingering bindings to prevent cross test pollution
     * as observed when running java 7
     *
     * @throws Exception on test failure
     */
@After
public void after() throws Exception {
    InitialContext icontext = new InitialContext();
    NamingEnumeration<Binding> bindings = icontext.listBindings("");
    List<String> names = new ArrayList<String>();
    while (bindings.hasMore()) {
        Binding bd = (Binding) bindings.next();
        names.add(bd.getName());
    }
    for (String name : names) {
        icontext.unbind(name);
    }
}
Also used : Binding(javax.naming.Binding) ArrayList(java.util.ArrayList) InitialContext(javax.naming.InitialContext) After(org.junit.After)

Example 13 with Binding

use of javax.naming.Binding in project spring-framework by spring-projects.

the class SimpleNamingContextTests method testNamingContextBuilder.

@Test
public void testNamingContextBuilder() throws NamingException {
    SimpleNamingContextBuilder builder = new SimpleNamingContextBuilder();
    InitialContextFactory factory = builder.createInitialContextFactory(null);
    DataSource ds = new StubDataSource();
    builder.bind("java:comp/env/jdbc/myds", ds);
    Object obj = new Object();
    builder.bind("myobject", obj);
    Context context1 = factory.getInitialContext(null);
    assertTrue("Correct DataSource registered", context1.lookup("java:comp/env/jdbc/myds") == ds);
    assertTrue("Correct Object registered", context1.lookup("myobject") == obj);
    Hashtable<String, String> env2 = new Hashtable<>();
    env2.put("key1", "value1");
    Context context2 = factory.getInitialContext(env2);
    assertTrue("Correct DataSource registered", context2.lookup("java:comp/env/jdbc/myds") == ds);
    assertTrue("Correct Object registered", context2.lookup("myobject") == obj);
    assertTrue("Correct environment", context2.getEnvironment() != env2);
    assertTrue("Correct key1", "value1".equals(context2.getEnvironment().get("key1")));
    Integer i = new Integer(0);
    context1.rebind("myinteger", i);
    String s = "";
    context2.bind("mystring", s);
    Context context3 = (Context) context2.lookup("");
    context3.rename("java:comp/env/jdbc/myds", "jdbc/myds");
    context3.unbind("myobject");
    assertTrue("Correct environment", context3.getEnvironment() != context2.getEnvironment());
    context3.addToEnvironment("key2", "value2");
    assertTrue("key2 added", "value2".equals(context3.getEnvironment().get("key2")));
    context3.removeFromEnvironment("key1");
    assertTrue("key1 removed", context3.getEnvironment().get("key1") == null);
    assertTrue("Correct DataSource registered", context1.lookup("jdbc/myds") == ds);
    try {
        context1.lookup("myobject");
        fail("Should have thrown NameNotFoundException");
    } catch (NameNotFoundException ex) {
    // expected
    }
    assertTrue("Correct Integer registered", context1.lookup("myinteger") == i);
    assertTrue("Correct String registered", context1.lookup("mystring") == s);
    assertTrue("Correct DataSource registered", context2.lookup("jdbc/myds") == ds);
    try {
        context2.lookup("myobject");
        fail("Should have thrown NameNotFoundException");
    } catch (NameNotFoundException ex) {
    // expected
    }
    assertTrue("Correct Integer registered", context2.lookup("myinteger") == i);
    assertTrue("Correct String registered", context2.lookup("mystring") == s);
    assertTrue("Correct DataSource registered", context3.lookup("jdbc/myds") == ds);
    try {
        context3.lookup("myobject");
        fail("Should have thrown NameNotFoundException");
    } catch (NameNotFoundException ex) {
    // expected
    }
    assertTrue("Correct Integer registered", context3.lookup("myinteger") == i);
    assertTrue("Correct String registered", context3.lookup("mystring") == s);
    Map<String, Binding> bindingMap = new HashMap<>();
    NamingEnumeration<?> bindingEnum = context3.listBindings("");
    while (bindingEnum.hasMoreElements()) {
        Binding binding = (Binding) bindingEnum.nextElement();
        bindingMap.put(binding.getName(), binding);
    }
    assertTrue("Correct jdbc subcontext", bindingMap.get("jdbc").getObject() instanceof Context);
    assertTrue("Correct jdbc subcontext", SimpleNamingContext.class.getName().equals(bindingMap.get("jdbc").getClassName()));
    Context jdbcContext = (Context) context3.lookup("jdbc");
    jdbcContext.bind("mydsX", ds);
    Map<String, Binding> subBindingMap = new HashMap<>();
    NamingEnumeration<?> subBindingEnum = jdbcContext.listBindings("");
    while (subBindingEnum.hasMoreElements()) {
        Binding binding = (Binding) subBindingEnum.nextElement();
        subBindingMap.put(binding.getName(), binding);
    }
    assertTrue("Correct DataSource registered", ds.equals(subBindingMap.get("myds").getObject()));
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(subBindingMap.get("myds").getClassName()));
    assertTrue("Correct DataSource registered", ds.equals(subBindingMap.get("mydsX").getObject()));
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(subBindingMap.get("mydsX").getClassName()));
    assertTrue("Correct Integer registered", i.equals(bindingMap.get("myinteger").getObject()));
    assertTrue("Correct Integer registered", Integer.class.getName().equals(bindingMap.get("myinteger").getClassName()));
    assertTrue("Correct String registered", s.equals(bindingMap.get("mystring").getObject()));
    assertTrue("Correct String registered", String.class.getName().equals(bindingMap.get("mystring").getClassName()));
    context1.createSubcontext("jdbc").bind("sub/subds", ds);
    Map<String, String> pairMap = new HashMap<>();
    NamingEnumeration<?> pairEnum = context2.list("jdbc");
    while (pairEnum.hasMore()) {
        NameClassPair pair = (NameClassPair) pairEnum.next();
        pairMap.put(pair.getName(), pair.getClassName());
    }
    assertTrue("Correct sub subcontext", SimpleNamingContext.class.getName().equals(pairMap.get("sub")));
    Context subContext = (Context) context2.lookup("jdbc/sub");
    Map<String, String> subPairMap = new HashMap<>();
    NamingEnumeration<?> subPairEnum = subContext.list("");
    while (subPairEnum.hasMoreElements()) {
        NameClassPair pair = (NameClassPair) subPairEnum.next();
        subPairMap.put(pair.getName(), pair.getClassName());
    }
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(subPairMap.get("subds")));
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("myds")));
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("mydsX")));
    pairMap.clear();
    pairEnum = context1.list("jdbc/");
    while (pairEnum.hasMore()) {
        NameClassPair pair = (NameClassPair) pairEnum.next();
        pairMap.put(pair.getName(), pair.getClassName());
    }
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("myds")));
    assertTrue("Correct DataSource registered", StubDataSource.class.getName().equals(pairMap.get("mydsX")));
}
Also used : SimpleNamingContextBuilder(org.springframework.tests.mock.jndi.SimpleNamingContextBuilder) InitialContext(javax.naming.InitialContext) SimpleNamingContext(org.springframework.tests.mock.jndi.SimpleNamingContext) Context(javax.naming.Context) Binding(javax.naming.Binding) NameNotFoundException(javax.naming.NameNotFoundException) HashMap(java.util.HashMap) Hashtable(java.util.Hashtable) InitialContextFactory(javax.naming.spi.InitialContextFactory) DataSource(javax.sql.DataSource) NameClassPair(javax.naming.NameClassPair) Test(org.junit.Test)

Example 14 with Binding

use of javax.naming.Binding in project Payara by payara.

the class StandardContext method listCollectionPaths.

/**
 * List resource paths (recursively), and store all of them in the given
 * Set.
 */
private static void listCollectionPaths(Set<String> set, DirContext resources, String path) throws NamingException {
    Enumeration<Binding> childPaths = resources.listBindings(path);
    while (childPaths.hasMoreElements()) {
        Binding binding = childPaths.nextElement();
        String name = binding.getName();
        StringBuilder childPath = new StringBuilder(path);
        if (!"/".equals(path) && !path.endsWith("/"))
            childPath.append("/");
        childPath.append(name);
        Object object = binding.getObject();
        if (object instanceof DirContext && childPath.charAt(childPath.length() - 1) != '/') {
            childPath.append("/");
        }
        set.add(childPath.toString());
    }
}
Also used : Binding(javax.naming.Binding) WARDirContext(org.apache.naming.resources.WARDirContext) BaseDirContext(org.apache.naming.resources.BaseDirContext) ProxyDirContext(org.apache.naming.resources.ProxyDirContext) DirContext(javax.naming.directory.DirContext) WebDirContext(org.apache.naming.resources.WebDirContext) FileDirContext(org.apache.naming.resources.FileDirContext)

Example 15 with Binding

use of javax.naming.Binding in project Payara by payara.

the class ExtensionValidator method validateApplication.

// --------------------------------------------------------- Public Methods
/**
 * Runtime validation of a Web Applicaiton.
 *
 * This method uses JNDI to look up the resources located under a
 * <code>DirContext</code>. It locates Web Application MANIFEST.MF
 * file in the /META-INF/ directory of the application and all
 * MANIFEST.MF files in each JAR file located in the WEB-INF/lib
 * directory and creates an <code>ArrayList</code> of
 * <code>ManifestResorce<code> objects. These objects are then passed
 * to the validateManifestResources method for validation.
 *
 * @param dirContext The JNDI root of the Web Application
 * @param context The context from which the Logger and path to the
 *                application
 *
 * @return true if all required extensions satisfied
 */
public static synchronized boolean validateApplication(DirContext dirContext, StandardContext context) throws IOException {
    String appName = context.getPath();
    ArrayList<ManifestResource> appManifestResources = new ArrayList<ManifestResource>();
    ManifestResource appManifestResource = null;
    // therefore is not valid
    if (dirContext == null)
        return false;
    // Find the Manifest for the Web Applicaiton
    InputStream inputStream = null;
    try {
        NamingEnumeration wne = dirContext.listBindings("/META-INF/");
        Binding binding = (Binding) wne.nextElement();
        if (binding.getName().toUpperCase(Locale.ENGLISH).equals("MANIFEST.MF")) {
            Resource resource = (Resource) dirContext.lookup("/META-INF/" + binding.getName());
            inputStream = resource.streamContent();
            Manifest manifest = new Manifest(inputStream);
            inputStream.close();
            inputStream = null;
            // Can we do it like this?
            String resourceName = "Web Application Manifest";
            ManifestResource mre = new ManifestResource(resourceName, manifest, ManifestResource.WAR);
            appManifestResources.add(mre);
        }
    } catch (NamingException nex) {
    // Application does not contain a MANIFEST.MF file
    } catch (NoSuchElementException nse) {
    // Application does not contain a MANIFEST.MF file
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Throwable t) {
            // Ignore
            }
        }
    }
    // Locate the Manifests for all bundled JARs
    NamingEnumeration ne = null;
    try {
        ne = dirContext.listBindings("WEB-INF/lib/");
        while ((ne != null) && ne.hasMoreElements()) {
            Binding binding = (Binding) ne.nextElement();
            if (!binding.getName().toLowerCase(Locale.ENGLISH).endsWith(".jar")) {
                continue;
            }
            Object obj = dirContext.lookup("/WEB-INF/lib/" + binding.getName());
            if (!(obj instanceof Resource)) {
                // Probably a directory named xxx.jar - ignore it
                continue;
            }
            Resource resource = (Resource) obj;
            Manifest jmanifest = getManifest(resource.streamContent());
            if (jmanifest != null) {
                ManifestResource mre = new ManifestResource(binding.getName(), jmanifest, ManifestResource.APPLICATION);
                appManifestResources.add(mre);
            }
        }
    } catch (NamingException nex) {
    // Jump out of the check for this application because it
    // has no resources
    }
    return validateManifestResources(appName, appManifestResources);
}
Also used : Binding(javax.naming.Binding) FileInputStream(java.io.FileInputStream) JarInputStream(java.util.jar.JarInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Resource(org.apache.naming.resources.Resource) NamingEnumeration(javax.naming.NamingEnumeration) Manifest(java.util.jar.Manifest) NamingException(javax.naming.NamingException) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

Binding (javax.naming.Binding)69 NamingException (javax.naming.NamingException)36 Context (javax.naming.Context)31 InitialContext (javax.naming.InitialContext)29 NameNotFoundException (javax.naming.NameNotFoundException)22 Name (javax.naming.Name)19 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)19 NotContextException (javax.naming.NotContextException)18 OperationNotSupportedException (javax.naming.OperationNotSupportedException)18 Reference (javax.naming.Reference)17 CompoundName (javax.naming.CompoundName)16 IOException (java.io.IOException)11 ArrayList (java.util.ArrayList)10 NamingContext (org.eclipse.jetty.jndi.NamingContext)9 CompositeName (javax.naming.CompositeName)8 Test (org.junit.Test)8 HashMap (java.util.HashMap)7 NamingEnumeration (javax.naming.NamingEnumeration)5 ServletContext (javax.servlet.ServletContext)5 MalformedURLException (java.net.MalformedURLException)4