Java Code Examples for javax.naming.InitialContext#bind()

The following examples show how to use javax.naming.InitialContext#bind() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ContextTest.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
protected void setUp() throws Exception {
  // InitialContextFactoryImpl impl = new InitialContextFactoryImpl();
  //	impl.setAsInitial();
  Hashtable table = new Hashtable();
  table
  .put(
      Context.INITIAL_CONTEXT_FACTORY,
  "com.gemstone.gemfire.internal.jndi.InitialContextFactoryImpl");
  //	table.put(Context.URL_PKG_PREFIXES,
  // "com.gemstone.gemfire.internal.jndi");
  initialCtx = new InitialContext(table);
  initialCtx.bind("java:gf/env/datasource/oracle", "a");
  gfCtx = (Context) initialCtx.lookup("java:gf");
  envCtx = (Context) gfCtx.lookup("env");
  datasourceCtx = (Context) envCtx.lookup("datasource");
}
 
Example 2
Source File: BeanLocatorTest.java    From eplmp with Eclipse Public License 1.0 6 votes vote down vote up
@BeforeClass
public static void setup() throws Exception {
    ctx = new InitialContext(new Hashtable<>(Collections.singletonMap(Context.INITIAL_CONTEXT_FACTORY,
            "org.osjava.sj.memory.MemoryContextFactory")));
    CADConverter converter1 = Mockito.mock(CADConverter.class);
    Mockito.when(converter1.canConvertToOBJ("format")).thenReturn(true);
    CADConverter converter2 = Mockito.mock(CADConverter.class);
    Mockito.when(converter2.canConvertToOBJ("format")).thenReturn(true);
    ctx.createSubcontext("java:global");
    ctx.createSubcontext("java:global/application");
    ctx.createSubcontext("java:global/application/module");
    ctx.bind("java:global/application/module/c1Bean!org.polarsys.eplmp.server.converters.CADConverter", converter1);
    ctx.bind("java:global/application/module/c1Bean", converter1);
    ctx.bind("java:global/application/module/c2Bean", converter2);
    ctx.bind("java:global/application/module/c2Bean!org.polarsys.eplmp.server.converters.CADConverter", converter2);
}
 
Example 3
Source File: CacheJndiTmFactorySelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override protected void beforeTestsStarted() throws Exception {
    super.beforeTestsStarted();

    initCtxFactoryBackup = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
    urlPkgPrefixesBackup = System.getProperty(Context.URL_PKG_PREFIXES);

    // Create initial context
    System.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.apache.naming.java.javaURLContextFactory");
    System.setProperty(Context.URL_PKG_PREFIXES, "org.apache.naming");

    InitialContext ic = new InitialContext();

    ic.createSubcontext("java:");
    ic.createSubcontext("java:/comp");
    ic.createSubcontext("java:/comp/env");
    ic.createSubcontext("java:/comp/env/tm");

    ic.bind(TM_JNDI_NAME, new TestTransactionManager());
    ic.bind(TM_JNDI_NAME2, new TestTransactionManager2());
    ic.bind(NOT_TM_JNDI_NAME, 1);
}
 
Example 4
Source File: APPCommunicationServiceBeanTest.java    From development with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() throws Exception {

    // container.enableInterfaceMocking(true);
    if (!NamingManager.hasInitialContextFactoryBuilder()) {
        NamingManager
                .setInitialContextFactoryBuilder(new TestNamingContextFactoryBuilder());
    }
    InitialContext initialContext = new InitialContext();
    Properties properties = new Properties();
    properties.put("mail.from", "[email protected]");
    mailMock = Session.getInstance(properties);
    initialContext.bind("mail/BSSMail", mailMock);
    configurationService = mock(APPConfigurationServiceBean.class);
    commService = spy(new APPCommunicationServiceBean());
    commService.configService = configurationService;
    doNothing().when(commService).transportMail(
            Matchers.any(MimeMessage.class));
}
 
Example 5
Source File: BasicAuthJDBCTest.java    From apiman with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setup() throws Exception {
    // Create a test datasource and bind it to JNDI
    BasicDataSource ds = createInMemoryDatasource();
    InitialContext ctx = TestUtil.initialContext();
    TestUtil.ensureCtx(ctx, "java:comp/env"); //$NON-NLS-1$
    TestUtil.ensureCtx(ctx, "java:comp/env/jdbc"); //$NON-NLS-1$
    ctx.bind("java:comp/env/jdbc/BasicAuthJDBCTest", ds); //$NON-NLS-1$
}
 
Example 6
Source File: LocalClientTest.java    From tomee with Apache License 2.0 5 votes vote down vote up
public void test() throws Exception {

        final Properties properties = new Properties();
        properties.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName());
        final InitialContext context = new InitialContext(properties);
        context.bind("inject", this);

        assertRefs();
    }
 
Example 7
Source File: RMIConnectorServer.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Bind a stub to a registry.
 * @param jndiUrl URL of the stub in the registry, extracted
 *        from the <code>JMXServiceURL</code>.
 * @param attributes A Hashtable containing environment parameters,
 *        built from the Map specified at this object creation.
 * @param rmiServer The object to bind in the registry
 * @param rebind true if the object must be rebound.
 **/
void bind(String jndiUrl, Hashtable<?, ?> attributes,
          RMIServer rmiServer, boolean rebind)
    throws NamingException, MalformedURLException {
    // if jndiURL is not null, we nust bind the stub to a
    // directory.
    InitialContext ctx =
        new InitialContext(attributes);

    if (rebind)
        ctx.rebind(jndiUrl, rmiServer);
    else
        ctx.bind(jndiUrl, rmiServer);
    ctx.close();
}
 
Example 8
Source File: RMIConnectorServer.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Bind a stub to a registry.
 * @param jndiUrl URL of the stub in the registry, extracted
 *        from the <code>JMXServiceURL</code>.
 * @param attributes A Hashtable containing environment parameters,
 *        built from the Map specified at this object creation.
 * @param rmiServer The object to bind in the registry
 * @param rebind true if the object must be rebound.
 **/
void bind(String jndiUrl, Hashtable<?, ?> attributes,
          RMIServer rmiServer, boolean rebind)
    throws NamingException, MalformedURLException {
    // if jndiURL is not null, we nust bind the stub to a
    // directory.
    InitialContext ctx =
        new InitialContext(attributes);

    if (rebind)
        ctx.rebind(jndiUrl, rmiServer);
    else
        ctx.bind(jndiUrl, rmiServer);
    ctx.close();
}
 
Example 9
Source File: RMIConnectorServer.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Bind a stub to a registry.
 * @param jndiUrl URL of the stub in the registry, extracted
 *        from the <code>JMXServiceURL</code>.
 * @param attributes A Hashtable containing environment parameters,
 *        built from the Map specified at this object creation.
 * @param rmiServer The object to bind in the registry
 * @param rebind true if the object must be rebound.
 **/
void bind(String jndiUrl, Hashtable<?, ?> attributes,
          RMIServer rmiServer, boolean rebind)
    throws NamingException, MalformedURLException {
    // if jndiURL is not null, we nust bind the stub to a
    // directory.
    InitialContext ctx =
        new InitialContext(attributes);

    if (rebind)
        ctx.rebind(jndiUrl, rmiServer);
    else
        ctx.bind(jndiUrl, rmiServer);
    ctx.close();
}
 
Example 10
Source File: RMIConnectorServer.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Bind a stub to a registry.
 * @param jndiUrl URL of the stub in the registry, extracted
 *        from the <code>JMXServiceURL</code>.
 * @param attributes A Hashtable containing environment parameters,
 *        built from the Map specified at this object creation.
 * @param rmiServer The object to bind in the registry
 * @param rebind true if the object must be rebound.
 **/
void bind(String jndiUrl, Hashtable<?, ?> attributes,
          RMIServer rmiServer, boolean rebind)
    throws NamingException, MalformedURLException {
    // if jndiURL is not null, we nust bind the stub to a
    // directory.
    InitialContext ctx =
        new InitialContext(attributes);

    if (rebind)
        ctx.rebind(jndiUrl, rmiServer);
    else
        ctx.bind(jndiUrl, rmiServer);
    ctx.close();
}
 
Example 11
Source File: CertificateMgtDaoTest.java    From carbon-apimgt with Apache License 2.0 5 votes vote down vote up
private static void initializeDatabase(String configFilePath) throws IOException, XMLStreamException, NamingException {

        InputStream in;
        in = FileUtils.openInputStream(new File(configFilePath));
        StAXOMBuilder builder = new StAXOMBuilder(in);
        String dataSource = builder.getDocumentElement().getFirstChildWithName(new QName("DataSourceName"))
                .getText();
        OMElement databaseElement = builder.getDocumentElement()
                .getFirstChildWithName(new QName("Database"));
        String databaseURL = databaseElement.getFirstChildWithName(new QName("URL")).getText();
        String databaseUser = databaseElement.getFirstChildWithName(new QName("Username")).getText();
        String databasePass = databaseElement.getFirstChildWithName(new QName("Password")).getText();
        String databaseDriver = databaseElement.getFirstChildWithName(new QName("Driver")).getText();

        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName(databaseDriver);
        basicDataSource.setUrl(databaseURL);
        basicDataSource.setUsername(databaseUser);
        basicDataSource.setPassword(databasePass);

        // Create initial context
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                "org.apache.naming.java.javaURLContextFactory");
        System.setProperty(Context.URL_PKG_PREFIXES,
                "org.apache.naming");
        try {
            InitialContext.doLookup("java:/comp/env/jdbc/WSO2AM_DB");
        } catch (NamingException e) {
            InitialContext ic = new InitialContext();
            ic.createSubcontext("java:");
            ic.createSubcontext("java:/comp");
            ic.createSubcontext("java:/comp/env");
            ic.createSubcontext("java:/comp/env/jdbc");

            ic.bind("java:/comp/env/jdbc/WSO2AM_DB", basicDataSource);
        }
    }
 
Example 12
Source File: LocalClientSubclassTest.java    From tomee with Apache License 2.0 5 votes vote down vote up
@Override
public void test() throws Exception {
    final Properties properties = new Properties();
    properties.setProperty(javax.naming.Context.INITIAL_CONTEXT_FACTORY, LocalInitialContextFactory.class.getName());
    final InitialContext context = new InitialContext(properties);
    context.bind("inject", this);

    assertRefs();

}
 
Example 13
Source File: RMIConnectorServer.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Bind a stub to a registry.
 * @param jndiUrl URL of the stub in the registry, extracted
 *        from the <code>JMXServiceURL</code>.
 * @param attributes A Hashtable containing environment parameters,
 *        built from the Map specified at this object creation.
 * @param rmiServer The object to bind in the registry
 * @param rebind true if the object must be rebound.
 **/
void bind(String jndiUrl, Hashtable<?, ?> attributes,
          RMIServer rmiServer, boolean rebind)
    throws NamingException, MalformedURLException {
    // if jndiURL is not null, we nust bind the stub to a
    // directory.
    InitialContext ctx =
        new InitialContext(attributes);

    if (rebind)
        ctx.rebind(jndiUrl, rmiServer);
    else
        ctx.bind(jndiUrl, rmiServer);
    ctx.close();
}
 
Example 14
Source File: RMIConnectorServer.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
/**
 * Bind a stub to a registry.
 * @param jndiUrl URL of the stub in the registry, extracted
 *        from the <code>JMXServiceURL</code>.
 * @param attributes A Hashtable containing environment parameters,
 *        built from the Map specified at this object creation.
 * @param rmiServer The object to bind in the registry
 * @param rebind true if the object must be rebound.
 **/
void bind(String jndiUrl, Hashtable<?, ?> attributes,
          RMIServer rmiServer, boolean rebind)
    throws NamingException, MalformedURLException {
    // if jndiURL is not null, we nust bind the stub to a
    // directory.
    InitialContext ctx =
        new InitialContext(attributes);

    if (rebind)
        ctx.rebind(jndiUrl, rmiServer);
    else
        ctx.bind(jndiUrl, rmiServer);
    ctx.close();
}
 
Example 15
Source File: Step4Test.java    From learning with Apache License 2.0 5 votes vote down vote up
private void bindDataSource(InitialContext context, String name, DataSource ds) throws Exception {
    try {
        context.bind(name, ds);
    } catch (NameAlreadyBoundException e) {
        e.getMessage(); // ignore
    }
}
 
Example 16
Source File: RMIConnectorServer.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Bind a stub to a registry.
 * @param jndiUrl URL of the stub in the registry, extracted
 *        from the <code>JMXServiceURL</code>.
 * @param attributes A Hashtable containing environment parameters,
 *        built from the Map specified at this object creation.
 * @param rmiServer The object to bind in the registry
 * @param rebind true if the object must be rebound.
 **/
void bind(String jndiUrl, Hashtable<?, ?> attributes,
          RMIServer rmiServer, boolean rebind)
    throws NamingException, MalformedURLException {
    // if jndiURL is not null, we nust bind the stub to a
    // directory.
    InitialContext ctx =
        new InitialContext(attributes);

    if (rebind)
        ctx.rebind(jndiUrl, rmiServer);
    else
        ctx.bind(jndiUrl, rmiServer);
    ctx.close();
}
 
Example 17
Source File: AuthorizationFilterTest.java    From development with Apache License 2.0 4 votes vote down vote up
@Override
protected void setup(TestContainer container) throws Exception {

    platformService = new APPlatformServiceMockup() {
        @Override
        public User authenticate(String controllerId,
                PasswordAuthentication authentication)
                throws AuthenticationException, ConfigurationException,
                APPlatformException {
            User user = new User();
            user.setLocale("en");
            if (exception) {
                throw new APPlatformException("failed");
            }
            return user;

        }

        @Override
        public boolean checkToken(String token, String signature) {

            return true;
        }

    };
    enableJndiMock();

    InitialContext context = new InitialContext();
    context.bind(APPlatformService.JNDI_NAME, platformService);

    chain = Mockito.mock(FilterChain.class);
    config = Mockito.mock(FilterConfig.class);
    req = Mockito.mock(HttpServletRequest.class);
    resp = Mockito.mock(HttpServletResponse.class);
    session = Mockito.mock(HttpSession.class);

    Mockito.when(resp.getWriter()).thenReturn(new PrintWriter(responseOut));
    Mockito.when(req.getSession()).thenReturn(session);
    Mockito.when(req.getLocale()).thenReturn(new Locale("en"));
    Mockito.when(config.getInitParameter("exclude-url-pattern")).thenReturn(
            "(.*/a4j/.*|.*/img/.*|.*/css/.*|.*/fonts/.*|.*/scripts/.*|.*/faq/.*|.*/org.richfaces.resources|.*/javax.faces.resource/.*|^/public/.*)");

    controllerAccess = Mockito.mock(ControllerAccess.class);
    Mockito.when(controllerAccess.getControllerId())
            .thenReturn("ess.common");

    filter = new AuthorizationFilter();
    filter.setControllerAccess(controllerAccess);
    filter.init(config);
}
 
Example 18
Source File: JPA2DatabasesTest.java    From piranha with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/**
 * Test database.
 *
 * @throws Exception when a serious error occurs.
 */
@Test
public void testJpaDatabase() throws Exception {
    System.getProperties().put("java.naming.factory.initial", "cloud.piranha.jndi.memory.DefaultInitialContextFactory");
    InitialContext initialContext = new InitialContext();
    JdbcDataSource dataSource = new JdbcDataSource();
    dataSource.setURL("jdbc:h2:./target/JPADatabaseTest2");
    initialContext.bind("java:/JPADatabaseTest2", dataSource);
    JdbcDataSource dataSource2 = new JdbcDataSource();
    dataSource2.setURL("jdbc:h2:./target/JPADatabaseTest3");
    initialContext.bind("java:/JPADatabaseTest3", dataSource2);
    DefaultTransactionManager transactionManager = new DefaultTransactionManager();
    initialContext.rebind("java:/TransactionManager", transactionManager);
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPADatabaseTest2");
    EntityManagerFactory emf2 = Persistence.createEntityManagerFactory("JPADatabaseTest3");
    transactionManager.begin();
    EntityManager em = emf.createEntityManager();
    JPATest jpaTest = new JPATest();
    jpaTest.setId(1);
    em.persist(jpaTest);
    em.flush();
    Transaction transaction = transactionManager.getTransaction();
    assertEquals(transaction.getStatus(), 0);
    EntityManager em2 = emf2.createEntityManager();
    JPATest jpaTest2 = new JPATest();
    jpaTest2.setId(2);
    em2.persist(jpaTest2);
    em2.flush();
    Transaction transaction2 = transactionManager.getTransaction();
    assertEquals(transaction2.getStatus(), 0);
    transactionManager.commit();
    
    assertEquals(transaction.getStatus(), 3);
    em = emf.createEntityManager();
    JPATest jpaTestb = em.find(JPATest.class, 1);
    assertNotNull(jpaTestb);
    assertEquals(jpaTest.getId(), jpaTestb.getId());
    assertEquals(transaction2.getStatus(), 3);
    em2 = emf2.createEntityManager();
    JPATest jpaTest2b = em2.find(JPATest.class, 2);
    assertNotNull(jpaTest2b);
    assertEquals(jpaTest2b.getId(), jpaTest2.getId());
    
    em.close();
    emf.close();
    em2.close();
    emf2.close();
}
 
Example 19
Source File: JdbcThinDataSourceSelfTest.java    From ignite with Apache License 2.0 4 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testJndi() throws Exception {
    IgniteJdbcThinDataSource ids = new IgniteJdbcThinDataSource();

    ids.setUrl("jdbc:ignite:thin://127.0.0.1");

    InitialContext ic = getInitialContext();

    ic.bind("ds/test", ids);

    IgniteJdbcThinDataSource ds = (IgniteJdbcThinDataSource)ic.lookup("ds/test");

    assertTrue("Cannot looking up DataSource from JNDI", ds != null);

    assertEquals(ids.getUrl(), ds.getUrl());
}
 
Example 20
Source File: APPAuthenticationServiceBeanIT.java    From development with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
protected void setup(TestContainer container) throws Exception {
    AESEncrypter.generateKey();

    container.addBean(configService = Mockito
            .mock(APPConfigurationServiceBean.class));
    container.addBean(Mockito.mock(ServiceInstanceDAO.class));

    container.addBean(Mockito.mock(APPConcurrencyServiceBean.class));
    container.addBean(
            Mockito.mock(ProductProvisioningServiceFactoryBean.class));
    container.addBean(Mockito.mock(APPCommunicationServiceBean.class));

    serviceMock = Mockito.mock(Service.class);
    besDAO = Mockito.mock(BesDAO.class);
    subcriptionService = Mockito.mock(SubscriptionService.class);
    identityService = Mockito.mock(EnhancedIdentityService.class);
    Mockito.doReturn(Arrays.asList(new VOUserDetails())).when(besDAO)
            .getBESTechnologyManagers(Matchers.any(ServiceInstance.class));

    Mockito.doReturn(identityService).when(besDAO).getBESWebService(
            Matchers.eq(IdentityService.class),
            Matchers.any(ServiceInstance.class));

    Mockito.doNothing().when(besDAO).setUserCredentialsInContext(
            Matchers.any(BindingProvider.class), Matchers.anyString(),
            Matchers.anyString(), Matchers.anyMap());

    Mockito.doReturn(subcriptionService).when(besDAO).getBESWebService(
            Matchers.eq(SubscriptionService.class),
            Matchers.any(ServiceInstance.class));

    Mockito.doNothing().when(subcriptionService).completeAsyncSubscription(
            Matchers.anyString(), Matchers.anyString(),
            Matchers.any(VOInstanceInfo.class));
    Mockito.doNothing().when(subcriptionService).abortAsyncSubscription(
            Matchers.anyString(), Matchers.anyString(),
            Matchers.anyListOf(VOLocalizedText.class));
    Mockito.doReturn(subcriptionService).when(serviceMock).getPort(
            Matchers.any(QName.class),
            Matchers.eq(SubscriptionService.class));

    Mockito.doReturn(serviceMock).when(besDAO).createWebService(
            Matchers.any(URL.class), Matchers.any(QName.class));

    Mockito.doReturn(identityService).when(serviceMock).getPort(
            Matchers.any(QName.class), Matchers.eq(IdentityService.class));

    container.addBean(besDAO);
    container.addBean(Mockito.mock(OperationDAO.class));

    container.addBean(Mockito.mock(ServiceInstanceDAO.class));
    container.addBean(Mockito.mock(OperationServiceBean.class));

    container.addBean(
            authService = Mockito.spy(new APPAuthenticationServiceBean()));
    container.addBean(Mockito.mock(OperationServiceBean.class));

    container.addBean(new APPlatformServiceBean());
    controller = Mockito.mock(APPlatformController.class);
    InitialContext context = new InitialContext();
    context.bind("bss/app/controller/ess.vmware", controller);
    container.addBean(controller);

    besDAO = container.get(BesDAO.class);

    platformService = container.get(APPlatformService.class);

    em = container.getPersistenceUnit("oscm-app");

    defaultAuth = new PasswordAuthentication("user", "password");
}