Example usage for javax.naming InitialContext bind

List of usage examples for javax.naming InitialContext bind

Introduction

In this page you can find the example usage for javax.naming InitialContext bind.

Prototype

public void bind(Name name, Object obj) throws NamingException 

Source Link

Usage

From source file:com.dattack.naming.StandaloneJndiTest.java

@Ignore("Validate the response against the JNDI specification")
@Test//from   w  w  w  .j av a 2s . c o m
public void testBindInvalidContext() throws NamingException {
    exception.expect(NamingException.class);
    final InitialContext context = new InitialContext();
    final String name = getCompositeName(INVALID_CONTEXT, "testBind");
    final Object obj = new Integer(10);
    context.bind(name, obj);
    fail(String.format("This test must fail because the name '%s' not exists (object: %s)", INVALID_CONTEXT,
            ObjectUtils.toString(obj)));
}

From source file:com.dattack.naming.StandaloneJndiTest.java

@Test
public void testBind() {
    try {/*from   w w  w. java 2  s  .c  o m*/
        final InitialContext context = new InitialContext();
        final String name = getCompositeName("jdbc", "testBind");
        final Object obj = new Integer(10);
        context.bind(name, obj);
        assertEquals(obj, context.lookup(name));
    } catch (final NamingException e) {
        fail(e.getMessage());
    }
}

From source file:org.exoplatform.services.naming.InitialContextTest.java

public void testGetContext() throws Exception {
    assertNotNull(System.getProperty(Context.INITIAL_CONTEXT_FACTORY));
    InitialContext ctx = new InitialContext();
    assertNotNull(ctx);/*from   w  w  w.j av a 2 s .  co m*/
    ctx.bind("test", "test");
    assertEquals("test", ctx.lookup("test"));
    try {
        ctx.bind("test", "test2");
        fail("A NameAlreadyBoundException is expected here");
    } catch (NameAlreadyBoundException e) {
        // expected exception
    }
    assertEquals("test", ctx.lookup("test"));
    ctx.rebind("test", "test2");
    assertEquals("test2", ctx.lookup("test"));

    initializer.getInitialContext().bind("test", "test3");
    assertEquals("test3", ctx.lookup("test"));
    ctx.rebind("test", "test4");
    assertEquals("test3", ctx.lookup("test"));
    initializer.getInitialContext().rebind("test", "test5");
    assertEquals("test5", ctx.lookup("test"));
    initializer.getInitialContext().unbind("test");
    try {
        initializer.getInitialContext().lookup("test");
        fail("A NameNotFoundException is expected here");
    } catch (NameNotFoundException e) {
        // expected exception
    }
    assertEquals("test4", ctx.lookup("test"));
    ctx.unbind("test");
    try {
        ctx.lookup("test");
        fail("A NameNotFoundException is expected here");
    } catch (NameNotFoundException e) {
        // expected exception
    }
    try {
        initializer.getInitialContext().unbind("test2");
        fail("A NameNotFoundException is expected here");
    } catch (NameNotFoundException e) {
        // expected exception
    }
    initializer.getInitialContext().bind("foo", "foo");
    assertEquals("foo", ctx.lookup("foo"));
    initializer.getInitialContext().bind("foo2", "foo2");
    assertEquals("foo2", ctx.lookup("foo2"));
    try {
        initializer.getInitialContext().rename("foo", "foo2");
        fail("A NameAlreadyBoundException is expected here");
    } catch (NameAlreadyBoundException e) {
        // expected exception
    }
    assertEquals("foo", ctx.lookup("foo"));
    assertEquals("foo2", ctx.lookup("foo2"));
    try {
        initializer.getInitialContext().rename("foo3", "foo4");
        fail("A NameNotFoundException is expected here");
    } catch (NameNotFoundException e) {
        // expected exception
    }
    initializer.getInitialContext().rename("foo", "foo3");
    assertEquals("foo", ctx.lookup("foo3"));
    assertEquals("foo2", ctx.lookup("foo2"));
    try {
        initializer.getInitialContext().lookup("foo");
        fail("A NameNotFoundException is expected here");
    } catch (NameNotFoundException e) {
        // expected exception
    }

    // check same instance
    initializer.getInitialContext().bind("bla", "bla");
    Object obj1 = initializer.getInitialContext().lookup("bla");
    Object obj2 = initializer.getInitialContext().lookup("bla");
    assertTrue(obj1 == obj2);
}

From source file:org.milyn.db.JndiDataSourceTest.java

@Override
protected void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    MockContextFactory.setAsInitial();/*from  w  w w.j  a  v a2  s . co m*/

    source = new StringSource("<root><a /><b /></root>");

    InitialContext context = new InitialContext();
    context.bind("java:/mockDS", dataSource);
    context.bind("java:/mockTransaction", transaction);

    when(dataSource.getConnection()).thenReturn(connection);

}

From source file:org.mule.transport.ejb.SimpleEjbContextFactory.java

public Object create(Map<?, ?> properties) throws Exception {
    Hashtable<String, Object> env = new Hashtable<String, Object>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, MuleInitialContextFactory.class.getName());

    InitialContext context = new InitialContext(env);
    for (Map.Entry<?, ?> entry : properties.entrySet()) {
        Object key = entry.getKey();
        if (key instanceof String) {
            Object value = entry.getValue();
            logger.debug("Binding " + key + " to " + value);
            context.bind((String) key, value);
        }/*from www.  j ava2s .  com*/
    }

    return context;
}

From source file:org.dhatim.db.JndiDataSourceTest.java

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    MockContextFactory.setAsInitial();/*from www  .j a  v a2s  .c  o  m*/

    source = new StringSource("<root><a /><b /></root>");

    InitialContext context = new InitialContext();
    context.bind("java:/mockDS", dataSource);
    context.bind("java:/mockTransaction", transaction);

    when(dataSource.getConnection()).thenReturn(connection);

}

From source file:org.efaps.bpm.BPM.java

/**
 * Initialize BPM.// w  w w  .j a  v  a  2 s . c  o  m
 *
 * @throws EFapsException on error
 */
public static void initialize() throws EFapsException {
    final SystemConfiguration config = EFapsSystemConfiguration.get();
    final boolean active = config != null ? config.getAttributeValueAsBoolean(KernelSettings.ACTIVATE_BPM)
            : false;
    if (active) {

        if (BPM.PMANAGER != null) {
            BPM.PMANAGER.close();
            BPM.PMANAGER = null;
        }
        if (BPM.SMANAGER != null) {
            BPM.SMANAGER.close();
            BPM.SMANAGER = null;
        }

        UserTransaction userTrans = null;
        InitialContext context = null;
        try {
            context = new InitialContext();
            userTrans = TransactionHelper.findUserTransaction();
            Object object = null;
            try {
                object = context.lookup(JtaTransactionManager.DEFAULT_USER_TRANSACTION_NAME);
            } catch (final NamingException ex) {
                BPM.LOG.info("Checked for JtaTransactionManager");
            }
            if (object == null) {
                context.bind(JtaTransactionManager.DEFAULT_USER_TRANSACTION_NAME, userTrans);
                context.bind(JtaTransactionManager.FALLBACK_TRANSACTION_MANAGER_NAMES[0],
                        TransactionHelper.findTransactionManager());
            }
        } catch (final NamingException ex) {
            BPM.LOG.error("Could not initialise JNDI InitialContext", ex);
        }

        // register our own KnowledgeBuilderFactoryService
        ServiceRegistryImpl.getInstance().addDefault(KnowledgeBuilderFactoryService.class,
                KnowledgeBuilderFactoryServiceImpl.class.getName());

        final RegisterableItemsFactoryImpl itemsFactory = new RegisterableItemsFactoryImpl();
        itemsFactory.addWorkItemHandler("Manual Task", ManualTaskItemHandler.class);
        itemsFactory.addProcessListener(WorkingMemoryLogListener.class);

        final Map<String, String> properties = new HashMap<String, String>();
        properties.put(AvailableSettings.DIALECT, Context.getDbType().getHibernateDialect());
        properties.put(AvailableSettings.SHOW_SQL, String.valueOf(BPM.LOG.isDebugEnabled()));
        properties.put(AvailableSettings.FORMAT_SQL, "true");
        properties.put(AvailableSettings.RELEASE_CONNECTIONS, "after_transaction");
        properties.put(AvailableSettings.CONNECTION_PROVIDER, ConnectionProvider.class.getName());
        properties.put(org.hibernate.jpa.AvailableSettings.NAMING_STRATEGY, NamingStrategy.class.getName());

        final EntityManagerFactory emf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa",
                properties);

        final RuntimeEnvironmentBuilder builder = RuntimeEnvironmentBuilder.getDefault()
                .classLoader(EFapsClassLoader.getInstance()).userGroupCallback(new UserGroupCallbackImpl())
                .entityManagerFactory(emf).registerableItemsFactory(itemsFactory).persistence(true)
                .addEnvironmentEntry("TRANSACTION_LOCK_ENABLED", "false");

        BPM.add2EnvironmentBuilder(builder);

        final RuntimeEnvironment environment = builder.get();
        final ManagerFactoryImpl factory = new ManagerFactoryImpl();

        BPM.PMANAGER = factory.newPerProcessInstanceRuntimeManager(environment);
        BPM.SMANAGER = factory.newSingletonRuntimeManager(environment);
    }
}

From source file:io.apiman.gateway.engine.policies.BasicAuthenticationPolicyTest.java

/**
 * Ensure that the given name is bound to a context.
 * @param ctx/* w ww. ja v a  2 s.co m*/
 * @param name
 * @throws NamingException 
 */
private void ensureCtx(InitialContext ctx, String name) throws NamingException {
    try {
        ctx.bind(name, new InitialContext());
    } catch (NameAlreadyBoundException e) {
        // this is ok
    }
}

From source file:io.apiman.manager.test.server.ManagerApiTestServer.java

/**
 * Stuff to do before the server is started.
 *///w ww .  ja  v  a 2 s.c  o m
protected void preStart() {
    System.setProperty("hibernate.hbm2ddl.auto", "create-drop"); //$NON-NLS-1$ //$NON-NLS-2$

    try {
        InitialContext ctx = new InitialContext();
        ensureCtx(ctx, "java:/comp/env"); //$NON-NLS-1$
        ensureCtx(ctx, "java:/comp/env/jdbc"); //$NON-NLS-1$
        ds = createInMemoryDatasource();
        ctx.bind("java:/comp/env/jdbc/ApiManagerDS", ds); //$NON-NLS-1$
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.overlord.gadgets.server.devsvr.GadgetDevServer.java

/**
 * @see org.overlord.commons.dev.server.DevServer#preConfig()
 *///from  w  ww.j  av  a2s.  c o  m
@Override
protected void preConfig() {
    System.setProperty(Bootstrap.HIBERNATE_HBM2DDL_AUTO, "create-drop");
    // Configure shindig data services authentication
    System.setProperty(AuthenticationConstants.CONFIG_AUTHENTICATION_PROVIDER,
            BasicAuthenticationProvider.class.getName());
    System.setProperty(AuthenticationConstants.CONFIG_BASIC_AUTH_USER, "rest-client");
    System.setProperty(AuthenticationConstants.CONFIG_BASIC_AUTH_PASS, "rest-client");
    System.setProperty(AuthenticationConstants.CONFIG_AUTHENTICATION_ENDPOINTS, "/overlord-rtgov/");

    System.setProperty(AuthenticationConstants.CONFIG_CONNECTION_TIMEOUT, "10000");
    System.setProperty(AuthenticationConstants.CONFIG_READ_TIMEOUT, "60000");

    // Configure REST proxy authentication
    System.setProperty("gadget-server.rest-proxy.service-overview.authentication-provider",
            RestProxyBasicAuthProvider.class.getName());
    System.setProperty("gadget-server.rest-proxy.service-overview.authentication.basic.username",
            "rest-client");
    System.setProperty("gadget-server.rest-proxy.service-overview.authentication.basic.password",
            "rest-client");

    // Add JNDI resources
    try {
        InitialContext ctx = new InitialContext();
        ctx.bind("java:jboss", new InitialContext());
        ctx.bind("java:jboss/datasources", new InitialContext());
        ds = createInMemoryDatasource();
        ctx.bind("java:jboss/datasources/GadgetServer", ds);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}