Example usage for javax.naming InitialContext doLookup

List of usage examples for javax.naming InitialContext doLookup

Introduction

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

Prototype

@SuppressWarnings("unchecked")
public static <T> T doLookup(String name) throws NamingException 

Source Link

Document

A static method to retrieve the named object.

Usage

From source file:org.wso2.carbon.apimgt.hybrid.gateway.usage.publisher.dao.UploadedUsageFileInfoDAOTest.java

private static void initializeDatabase(String configFilePath) {
    InputStream in;//  w  w  w.j a v a2 s  . com
    try {
        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);
        }
    } catch (XMLStreamException | IOException | NamingException e) {
        log.error(e);
    }
}

From source file:org.wso2.carbon.apimgt.impl.dao.test.APIMgtDAOTest.java

private void initializeDatabase(String configFilePath) {

    InputStream in = null;/* ww w.j  a  v  a 2s. co m*/
    try {
        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);
        }
    } catch (XMLStreamException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (NamingException e) {
        e.printStackTrace();
    }
}

From source file:org.wso2.carbon.apimgt.impl.dao.test.CertificateMgtDaoTest.java

private static void initializeDatabase(String configFilePath)
        throws IOException, XMLStreamException, NamingException {

    InputStream in;//w  ww . j  a  v a  2s  .c o m
    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);
    }
}

From source file:org.wso2.carbon.business.messaging.core.utils.MessageBrokerDBUtil.java

/**
 * Lookup JNDI data source for context store and set
 *
 * @param dbConfig configuration holding DB configurations
 *//*from w w  w .jav  a 2s. c o m*/
private void setContextStoreDataSource(MBDatabaseConfig dbConfig) {
    try {
        //if message store and context store both exposed by same jndi name
        if (dbConfig.isContextStoreAvaliable()) {
            contextStoreDataSource = InitialContext.doLookup(dbConfig.getContextStoreJndiName());
            isContextStoreDataSourceSet = true;
        } else {
            isContextStoreDataSourceSet = false;
        }

    } catch (NamingException e) {

        log.info("Cannot lookup data source named " + dbConfig.getContextStoreJndiName()
                + ". It is optional to use this");

    }
}

From source file:org.wso2.carbon.cassandra.mgt.util.CassandraManagementUtils.java

public static Cluster lookupCluster(String jndiName, final Hashtable<Object, Object> jndiProperties) {
    try {/*from   w w  w.  j av a  2s.c o  m*/
        if (jndiProperties == null || jndiProperties.isEmpty()) {
            return (Cluster) InitialContext.doLookup(jndiName);
        }
        final InitialContext context = new InitialContext(jndiProperties);
        return (Cluster) context.doLookup(jndiName);
    } catch (Exception e) {
        throw new RuntimeException("Error in looking up cluster instance: " + e.getMessage(), e);
    }
}

From source file:org.wso2.carbon.certificate.mgt.core.dao.CertificateManagementDAOUtil.java

public static DataSource lookupDataSource(String dataSourceName,
        final Hashtable<Object, Object> jndiProperties) {
    try {/*  w w  w . j a v  a2  s.co m*/
        if (jndiProperties == null || jndiProperties.isEmpty()) {
            return (DataSource) InitialContext.doLookup(dataSourceName);
        }
        final InitialContext context = new InitialContext(jndiProperties);
        return (DataSource) context.doLookup(dataSourceName);
    } catch (Exception e) {
        throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
    }
}

From source file:org.wso2.carbon.dataservices.core.DBDeployer.java

private void doExtractTransactionManager() {
    if (cachedTransactionManager != null) {
        return;//from w w  w  .  j a v a  2  s.  c  om
    }
    try {
        Object txObj = InitialContext.doLookup(DBConstants.STANDARD_USER_TRANSACTION_JNDI_NAME);
        if (txObj instanceof TransactionManager) {
            cachedTransactionManager = (TransactionManager) txObj;
        }
    } catch (Exception e) {
        if (log.isDebugEnabled()) {
            log.debug("Cannot find transaction manager at: " + DBConstants.STANDARD_USER_TRANSACTION_JNDI_NAME,
                    e);
        }
        /* ignore, move onto next step */
    }
    if (cachedTransactionManager == null) {
        try {
            cachedTransactionManager = InitialContext
                    .doLookup(DBConstants.STANDARD_TRANSACTION_MANAGER_JNDI_NAME);
        } catch (Exception e) {
            if (log.isDebugEnabled()) {
                log.debug("Cannot find transaction manager at: "
                        + DBConstants.STANDARD_TRANSACTION_MANAGER_JNDI_NAME, e);
            }
            /* we'll do the lookup later, maybe user provided a custom JNDI name */
        }
    }
}

From source file:org.wso2.carbon.dataservices.core.DBUtils.java

/**
 * Get the container managed transaction manager; if a JNDI name is given,
 * that name is looked for a TransactionManager object, if not, the standard JNDI
 * names are checked./*from  ww w.  j a v a2s  . com*/
 *
 * @param txManagerJNDIName The user given JNDI name of the TransactionManager
 * @return The TransactionManager object
 * @throws DataServiceFault
 */
public static TransactionManager getContainerTransactionManager(String txManagerJNDIName)
        throws DataServiceFault {
    TransactionManager txManager = null;
    if (txManagerJNDIName != null) {
        try {
            txManager = InitialContext.doLookup(txManagerJNDIName);
        } catch (Exception e) {
            throw new DataServiceFault(e,
                    "Cannot find TransactionManager with the given JNDI name '" + txManagerJNDIName + "'");
        }
    }
    /* get the transaction manager from the well known JNDI names from the cache */
    txManager = DBDeployer.getCachedTransactionManager();
    return txManager;
}

From source file:org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil.java

public static DataSource lookupDataSource(String dataSourceName,
        final Hashtable<Object, Object> jndiProperties) {
    try {/*from   w w  w  . j  a v a 2 s .  c o m*/
        if (jndiProperties == null || jndiProperties.isEmpty()) {
            return (DataSource) InitialContext.doLookup(dataSourceName);
        }
        final InitialContext context = new InitialContext(jndiProperties);
        return (DataSource) context.lookup(dataSourceName);
    } catch (Exception e) {
        throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
    }
}

From source file:org.wso2.carbon.device.mgt.core.dao.util.GroupManagementDAOUtil.java

/**
 * Lookup datasource using name and jndi properties
 *
 * @param dataSourceName Name of datasource to lookup
 * @param jndiProperties Hash table of JNDI Properties
 * @return datasource looked// w  ww  .  j a  va 2s.c  o m
 */
public static DataSource lookupDataSource(String dataSourceName,
        final Hashtable<Object, Object> jndiProperties) {
    try {
        if (jndiProperties == null || jndiProperties.isEmpty()) {
            return (DataSource) InitialContext.doLookup(dataSourceName);
        }
        final InitialContext context = new InitialContext(jndiProperties);
        return (DataSource) context.lookup(dataSourceName);
    } catch (Exception e) {
        throw new RuntimeException("Error in looking up data source: " + e.getMessage(), e);
    }
}