Search in sources :

Example 41 with InitialContext

use of javax.naming.InitialContext in project quickstarts by jboss-switchyard.

the class CamelJpaBindingTest method beforeClass.

@BeforeClass
public static void beforeClass() throws Exception {
    namingMixIn = new NamingMixIn();
    namingMixIn.initialize();
    connection = DriverManager.getConnection("jdbc:h2:mem:events-jpa-quickstart", "sa", "sa");
    JdbcDataSource ds = new JdbcDataSource();
    ds.setURL("jdbc:h2:mem:events-jpa-quickstart");
    ds.setUser("sa");
    ds.setPassword("sa");
    InitialContext initialContext = new InitialContext();
    bind(initialContext, System.getProperty("jpa.persistence.datasource.name"), ds);
}
Also used : JdbcDataSource(org.h2.jdbcx.JdbcDataSource) NamingMixIn(org.switchyard.component.test.mixins.naming.NamingMixIn) InitialContext(javax.naming.InitialContext) BeforeClass(org.junit.BeforeClass)

Example 42 with InitialContext

use of javax.naming.InitialContext in project javaee7-samples by javaee-samples.

the class UserTransactionTest method should_work_with_jndi.

@Test
public void should_work_with_jndi() throws SystemException, NotSupportedException, HeuristicRollbackException, HeuristicMixedException, RollbackException, NamingException {
    Context context = new InitialContext();
    UserTransaction ut = (UserTransaction) context.lookup("java:comp/UserTransaction");
    ut.begin();
    ut.commit();
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) InitialContext(javax.naming.InitialContext) Test(org.junit.Test)

Example 43 with InitialContext

use of javax.naming.InitialContext in project javaee7-samples by javaee-samples.

the class EmployeeBean method postConstruct.

@PostConstruct
public void postConstruct() {
    try {
        Context context = new InitialContext();
        em = (EntityManager) context.lookup("java:comp/env/persistence/myJNDI");
    } catch (NamingException ex) {
        Logger.getLogger(EmployeeBean.class.getName()).log(Level.SEVERE, null, ex);
    }
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) PersistenceContext(javax.persistence.PersistenceContext) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext) PostConstruct(javax.annotation.PostConstruct)

Example 44 with InitialContext

use of javax.naming.InitialContext in project head by mifos.

the class ApplicationInitializer method initJNDIforPentaho.

private void initJNDIforPentaho(ApplicationContext applicationContext) {
    try {
        InitialContext ic = new InitialContext();
        //check if ds is already bound
        boolean dataSourceBound = true;
        boolean dataSourceBoundDw = true;
        try {
            DataSource datasource = (DataSource) ic.lookup("jdbc/SourceDB");
            if (datasource != null) {
                dataSourceBound = true;
            }
        } catch (Exception ex) {
            dataSourceBound = false;
        }
        try {
            DataSource datasourcedw = (DataSource) ic.lookup("jdbc/DestinationDB");
            if (datasourcedw != null) {
                dataSourceBoundDw = true;
            }
        } catch (Exception ex) {
            dataSourceBoundDw = false;
        }
        if (!dataSourceBound) {
            Object dataSource = applicationContext.getBean("dataSource");
            try {
                ic.createSubcontext("jdbc");
            } catch (NameAlreadyBoundException ex) {
                logger.info("Subcontext jdbc was already bound");
            }
            ic.bind("jdbc/SourceDB", dataSource);
            logger.info("Bound datasource to jdbc/SourceDB");
        } else {
            logger.info("jdbc/SourceDB is already bound");
        }
        if (!dataSourceBoundDw) {
            Object dataSourcedw = applicationContext.getBean("dataSourcePentahoDW");
            try {
                ic.createSubcontext("jdbc");
            } catch (NameAlreadyBoundException ex) {
                logger.info("Subcontext jdbc was already bound");
            }
            ic.bind("jdbc/DestinationDB", dataSourcedw);
            logger.info("Bound datasource to jdbc/DestinationDB");
        } else {
            logger.info("jdbc/DestinationDB is already bound");
        }
    } catch (Exception ex) {
        logger.error("Unable to bind dataSource to JNDI");
    }
}
Also used : NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) InitialContext(javax.naming.InitialContext) SystemException(org.mifos.framework.exceptions.SystemException) TaskSystemException(org.mifos.framework.components.batchjobs.exceptions.TaskSystemException) XMLReaderException(org.mifos.framework.exceptions.XMLReaderException) HibernateProcessException(org.mifos.framework.exceptions.HibernateProcessException) PersistenceException(org.mifos.framework.exceptions.PersistenceException) ConfigurationException(org.mifos.config.exceptions.ConfigurationException) AppNotConfiguredException(org.mifos.framework.exceptions.AppNotConfiguredException) AccountException(org.mifos.accounts.exceptions.AccountException) FinancialException(org.mifos.accounts.financial.exceptions.FinancialException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) IOException(java.io.IOException) HibernateStartUpException(org.mifos.framework.exceptions.HibernateStartUpException) ApplicationException(org.mifos.framework.exceptions.ApplicationException) DataSource(javax.sql.DataSource)

Example 45 with InitialContext

use of javax.naming.InitialContext in project hudson-2.x by hudson.

the class AbstractModelObject method getConfiguredHudsonProperty.

/**
     * Checks jndi,environment, hudson environment and system properties for specified key.
     * Property is checked in direct order:
     * <ol>
     * <li>JNDI ({@link InitialContext#lookup(String)})</li>
     * <li>Hudson environment ({@link EnvVars#masterEnvVars})</li>
     * <li>System properties ({@link System#getProperty(String)})</li>
     * </ol>
     * @param key - the name of the configured property.
     * @return the string value of the configured property, or null if there is no property with that key.
     */
protected String getConfiguredHudsonProperty(String key) {
    if (StringUtils.isNotBlank(key)) {
        String resultValue;
        try {
            InitialContext iniCtxt = new InitialContext();
            Context env = (Context) iniCtxt.lookup("java:comp/env");
            resultValue = StringUtils.trimToNull((String) env.lookup(key));
            if (null != resultValue) {
                return resultValue;
            }
            // look at one more place. See http://issues.hudson-ci.org/browse/HUDSON-1314
            resultValue = StringUtils.trimToNull((String) iniCtxt.lookup(key));
            if (null != resultValue) {
                return resultValue;
            }
        } catch (NamingException e) {
        // ignore
        }
        // look at the env var next
        resultValue = StringUtils.trimToNull(EnvVars.masterEnvVars.get(key));
        if (null != resultValue) {
            return resultValue;
        }
        // finally check the system property
        resultValue = StringUtils.trimToNull(System.getProperty(key));
        if (null != resultValue) {
            return resultValue;
        }
    }
    return null;
}
Also used : InitialContext(javax.naming.InitialContext) Context(javax.naming.Context) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Aggregations

InitialContext (javax.naming.InitialContext)1068 Test (org.junit.Test)325 EJBException (javax.ejb.EJBException)228 Properties (java.util.Properties)213 Context (javax.naming.Context)194 RemoteException (java.rmi.RemoteException)173 TestFailureException (org.apache.openejb.test.TestFailureException)172 NamingException (javax.naming.NamingException)168 AssertionFailedError (junit.framework.AssertionFailedError)168 JMSException (javax.jms.JMSException)167 RemoveException (javax.ejb.RemoveException)66 CreateException (javax.ejb.CreateException)57 DataSource (javax.sql.DataSource)55 Hashtable (java.util.Hashtable)54 Assembler (org.apache.openejb.assembler.classic.Assembler)47 EjbJar (org.apache.openejb.jee.EjbJar)41 NameNotFoundException (javax.naming.NameNotFoundException)40 ConfigurationFactory (org.apache.openejb.config.ConfigurationFactory)38 Connection (java.sql.Connection)37 StatelessBean (org.apache.openejb.jee.StatelessBean)30