Example usage for javax.naming InitialContext InitialContext

List of usage examples for javax.naming InitialContext InitialContext

Introduction

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

Prototype

public InitialContext() throws NamingException 

Source Link

Document

Constructs an initial context.

Usage

From source file:com.katsu.eclipselink.JNDIEclipseLinkSessionCustomizer.java

/**
 * Get a dataSource connection and set it on the session with lookupType=STRING_LOOKUP
 *///from   w  ww .  ja v  a2s. com
@Override
public void customize(Session session) throws Exception {
    JNDIConnector connector = null;
    Context context = null;
    try {
        context = new InitialContext();
        if (null == context) {
            throw new Exception("JNDIEclipseLinkSessionCustomizer: Context is null");
        }
        connector = (JNDIConnector) session.getLogin().getConnector(); // possible CCE
        // Change from Composite to String_Lookup
        connector.setLookupType(JNDIConnector.STRING_LOOKUP);
    } catch (Exception e) {
        log.error(e);
    }
}

From source file:com.stratelia.silverpeas.silverStatisticsPeas.control.AbstractSpringDatasourceTest.java

private static void configureJNDIDatasource() throws Exception {
    springContext = new ClassPathXmlApplicationContext("/spring-silverpeas.xml",
            "/com/stratelia/silverpeas/silverStatisticsPeas/control/spring-h2-datasource.xml");
    datasource = springContext.getBean("dataSource", DataSource.class);
    InitialContext ic = new InitialContext();
    ic.bind("java:/datasources/silverpeas-jdbc", datasource);
}

From source file:org.tolven.web.servlet.PortletChartServlet.java

@Override
public void init(ServletConfig config) {
    try {/*  w  w  w.j  a va  2s . com*/
        InitialContext ctx = new InitialContext();
        if (snapshotBean == null) {
            snapshotBean = (SnapshotLocal) ctx.lookup("tolven/SnapshotBean/local");
        }
    } catch (NamingException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.wso2telco.dbUtil.DataBaseConnectUtils.java

private static void initializeConnectDatasource() throws NamingException {
    if (mConnectDatasource != null) {
        return;/* w ww .j a va  2  s.  co  m*/
    }

    String dataSourceName = null;
    try {
        Context ctx = new InitialContext();
        ConfigurationService configurationService = new ConfigurationServiceImpl();
        dataSourceName = configurationService.getDataHolder().getMobileConnectConfig().getDataSourceName();
        mConnectDatasource = (DataSource) ctx.lookup(dataSourceName);
    } catch (NamingException e) {
        throw new NamingException("Error while looking up the data source : " + dataSourceName);
    }
}

From source file:br.ufac.sion.converter.OrgaoExpedidorConverter.java

private OrgaoExpedidorFacadeLocal lookupOrgaoExpedidorFacadeLocal() {
    try {// w  w  w .ja  v  a2  s  .  c o m
        Context c = new InitialContext();
        return (OrgaoExpedidorFacadeLocal) c.lookup("java:global/sion-ear/sion-ejb-2.0/OrgaoExpedidorFacade");
    } catch (NamingException ne) {
        Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
        throw new RuntimeException(ne);
    }
}

From source file:org.wso2.carbon.metrics.jdbc.core.BaseReporterTest.java

@BeforeSuite
protected static void init() throws Exception {
    if (logger.isInfoEnabled()) {
        logger.info("Initializing the data source and populating data");
    }//from   ww  w .ja v  a2 s .  c o m
    // Setup datasource
    dataSource = JdbcConnectionPool.create("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1", "sa", "");
    template = new JdbcTemplate(dataSource);
    ResourceDatabasePopulator populator = new ResourceDatabasePopulator();
    populator.addScript(new ClassPathResource("dbscripts/h2.sql"));
    populator.populate(dataSource.getConnection());

    // 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("jdbc");
    ic.bind("jdbc/WSO2MetricsDB", dataSource);

    if (logger.isInfoEnabled()) {
        logger.info("Creating Metrics");
    }
    metrics = new Metrics(TestUtils.getConfigProvider("metrics.yaml"));
    metrics.activate();
    metricService = metrics.getMetricService();
    metricManagementService = metrics.getMetricManagementService();
}

From source file:de.griffel.confluence.plugins.plantuml.DatasourceHelper.java

/**
 * Determines data sources configured in Tomcat.
 *
 * @return List of data source names/*from   w  ww .j  av  a  2 s .c o  m*/
 */
public static List<String> listAvailableDataSources() {
    final List<String> results = new LinkedList<String>();

    // Workaround for classloader problems, see https://answers.atlassian.com/questions/6374/how-do-i-access-jndi-from-a-version-2-osgi-plugin
    final ClassLoader origCL = Thread.currentThread().getContextClassLoader();

    try {
        Thread.currentThread().setContextClassLoader(ComponentManager.class.getClassLoader());
        final InitialContext jndiContext = new InitialContext();
        final NamingEnumeration<Binding> bindings = jndiContext.listBindings(JDBC_CONTEXT);
        while (bindings != null && bindings.hasMore()) {
            results.add(bindings.next().getName());
        }
    } catch (NamingException e) {
        log.debug("NamingException listBindings", e);
        results.add(e.toString());
    } finally {
        Thread.currentThread().setContextClassLoader(origCL);
    }
    return results;
}

From source file:com.sapito.db.config.PersistenceConfig.java

@Bean
public DataSource dataSource() {
    try {//  w  w  w. ja va 2  s  . com
        InitialContext context = new InitialContext();
        return (DataSource) context.lookup("jdbc/sapito");
    } catch (NamingException ex) {
        System.err.println("Exception getting jdbc/sapito from jndi");
        ex.getExplanation();
        return null;
    }
}

From source file:com.silverpeas.admin.DomainTest.java

@BeforeClass
public static void setUpClass() throws Exception {
    SimpleMemoryContextFactory.setUpAsInitialContext();
    context = new ClassPathXmlApplicationContext(
            new String[] { "spring-admin-spacecomponents-embbed-datasource.xml", "spring-domains.xml" });
    dataSource = context.getBean("jpaDataSource", DataSource.class);
    InitialContext ic = new InitialContext();
    ic.rebind("jdbc/Silverpeas", dataSource);
    DBUtil.getInstanceForTest(dataSource.getConnection());
}

From source file:jp.co.golorp.emarf.sql.DataSources.java

/**
 * ?// w w  w  .ja v  a 2s . c o  m
 *
 * @return DataSource
 */
public static DataSource get() {

    if (ds != null) {
        return ds;
    }

    /*
     * JNDI??
     */

    String name = BUNDLE.getString(DATA_SOURCE_NAME);
    try {
        Context context = new InitialContext();
        ds = (DataSource) context.lookup(name);
        return ds;
    } catch (NamingException e) {
        LOG.warn(e.getMessage());
    }

    /*
     * DBCP??
     */

    Properties properties = new Properties();
    Enumeration<String> keys = BUNDLE.getKeys();
    while (keys.hasMoreElements()) {
        String key = keys.nextElement();
        String value = BUNDLE.getString(key);
        properties.put(key, value);
        // if (value.contains("mysql")) {
        // DataSources.isMySQL = true;
        // } else if (value.contains("oracle")) {
        // DataSources.isOracle = true;
        // }
    }

    try {
        ds = BasicDataSourceFactory.createDataSource(properties);
        return ds;
    } catch (Exception e) {
        throw new SystemError(e);
    }
}