@SuppressWarnings("unchecked") void jndiInJEE() throws NamingException { @SuppressWarnings("rawtypes") Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); InitialContext context = new InitialContext(env); DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/AccountDS"); NamingEnumeration<Binding> bindings = context.listBindings("java:comp/env/jdbc"); while (bindings.hasMore()) { Binding bd = (Binding) bindings.next(); System.out.println("Name = " + bd.getName() + ", Object = " + bd.getObject()); } context.bind("java:comp/env/jdbc/AccountDS", ds); // Directory search InitialDirContext dirContext = new InitialDirContext(); SearchControls control = new SearchControls(); ds = (DataSource) dirContext.search("java:comp/env/jdbc/AccountDS", "(ver=1.1)", control); // Or... BasicAttributes attrs = new BasicAttributes(); attrs.put(new BasicAttribute("ver", "1.1")); ds = (DataSource) dirContext.search("java:comp/env/jdbc/AccountDS", attrs); }
/** * 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); } }
@Test public void testDeploymentBinding() throws Exception { final InitialContext ctx = getRemoteContext(); BinderRemote binder = null; try { try { ctx.lookup("some/entry"); fail("expected exception"); } catch (NameNotFoundException e) { // expected } // test binding binder = (BinderRemote) ctx.lookup( ARCHIVE_NAME + "/" + Singleton.class.getSimpleName() + "!" + BinderRemote.class.getName()); assertNotNull(binder); binder.bind(); assertEquals("Test", ctx.lookup("some/entry")); NamingEnumeration<Binding> bindings = ctx.listBindings("some"); assertTrue(bindings.hasMore()); assertEquals("Test", bindings.next().getObject()); assertFalse(bindings.hasMore()); // test rebinding binder.rebind(); assertEquals("Test2", ctx.lookup("some/entry")); bindings = ctx.listBindings("some"); assertTrue(bindings.hasMore()); assertEquals("Test2", bindings.next().getObject()); assertFalse(bindings.hasMore()); // test unbinding binder.unbind(); try { ctx.lookup("some/entry"); fail("expected exception"); } catch (NameNotFoundException e) { // expected } // test rebinding when it doesn't already exist binder.rebind(); assertEquals("Test2", ctx.lookup("some/entry")); bindings = ctx.listBindings("some"); assertTrue(bindings.hasMore()); assertEquals("Test2", bindings.next().getObject()); assertFalse(bindings.hasMore()); } finally { // clean up in case any JNDI bindings were left around try { if (binder != null) binder.unbind(); } catch (Exception e) { // expected } ctx.close(); } }