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);
}
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();
}
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);
}
}
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");
}
}
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;
}
Aggregations