Example usage for javax.naming Context lookup

List of usage examples for javax.naming Context lookup

Introduction

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

Prototype

public Object lookup(String name) throws NamingException;

Source Link

Document

Retrieves the named object.

Usage

From source file:org.bibsonomy.rest.RestProperties.java

private static String getByJndi(final String name, final Context ctx) {
    if (ctx == null) {
        return null;
    }/*w ww  . ja  v a2s  .c  o m*/
    try {
        return (String) ctx.lookup(name);
    } catch (final NamingException ex) {
        if (log.isDebugEnabled()) {
            log.debug("cannot retrieve java:/comp/env/" + name);
        }
        return null;
    }
}

From source file:org.apache.stratos.adc.mgt.utils.StratosDBUtils.java

/**
 * Initializes the data source//from w  w  w .  java  2  s.c o  m
 * 
 * @throws RuntimeException
 *             if an error occurs while loading DB configuration
 */
public static void initialize() throws Exception {
    if (dataSource != null) {
        return;
    }

    synchronized (StratosDBUtils.class) {
        if (dataSource == null) {

            String datasourceName = System.getProperty(CartridgeConstants.DB_DATASOURCE);

            if (datasourceName != null && datasourceName.trim().length() > 0) {
                if (log.isInfoEnabled()) {
                    log.info("Initializing data source: " + datasourceName);
                }
                try {
                    Context ctx = new InitialContext();
                    dataSource = (DataSource) ctx.lookup(datasourceName);
                    if (dataSource != null && log.isInfoEnabled()) {
                        log.info("Found data source: " + datasourceName + ", "
                                + dataSource.getClass().getName());
                    }
                } catch (NamingException e) {
                    throw new RuntimeException("Error while looking up the data source: " + datasourceName, e);
                }
            } else {
                // FIXME Should we use system properties to get database
                // details?
                String dbUrl = System.getProperty(CartridgeConstants.DB_URL);
                String driver = System.getProperty(CartridgeConstants.DB_DRIVER);
                String username = System.getProperty(CartridgeConstants.DB_USERNAME);
                String password = System.getProperty(CartridgeConstants.DB_PASSWORD);

                if (dbUrl == null || driver == null || username == null || password == null) {
                    String msg = "Required DB configuration parameters are not specified.";
                    log.warn(msg);
                    throw new RuntimeException(msg);
                }

                if (log.isInfoEnabled()) {
                    log.info("Initializing data source for JDBC URL: " + dbUrl);
                }

                PoolProperties p = new PoolProperties();
                p.setUrl(dbUrl);
                p.setDriverClassName(driver);
                p.setUsername(username);
                p.setPassword(password);
                p.setJmxEnabled(true);
                p.setTestWhileIdle(false);
                p.setTestOnBorrow(true);
                p.setValidationQuery("SELECT 1");
                p.setTestOnReturn(false);
                p.setValidationInterval(30000);
                p.setTimeBetweenEvictionRunsMillis(30000);
                p.setMaxActive(100);
                p.setInitialSize(10);
                p.setMaxWait(10000);
                p.setRemoveAbandonedTimeout(60);
                p.setMinEvictableIdleTimeMillis(30000);
                p.setMinIdle(10);
                p.setLogAbandoned(true);
                p.setRemoveAbandoned(true);
                p.setDefaultAutoCommit(false);
                p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"
                        + "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
                DataSource tomcatDatasource = new DataSource();
                tomcatDatasource.setPoolProperties(p);

                dataSource = tomcatDatasource;
            }

        }
    }
}

From source file:br.gov.jfrj.siga.persistencia.oracle.JDBCUtilOracle.java

public static Connection getConnectionPool(final String dataSource) {

    Context initContext = null;
    Context envContext = null;//from ww w .j  a v  a  2 s.c o  m
    Connection conn = null;

    try {
        JDBCUtilOracle.log.debug("Criando variavel de contexto");
        initContext = new InitialContext();

        envContext = (Context) initContext.lookup("java:/comp/env");
        JDBCUtilOracle.log.debug("Criando datasource ");
        final DataSource ds = (DataSource) envContext.lookup(dataSource);
        conn = ds.getConnection();

    } catch (final NamingException e) {
        JDBCUtilOracle.log.error(Messages.getString("Oracle.LookUpErro"));
        System.err.print(Messages.getString("Oracle.LookUpErro") + e.getMessage());

    } catch (final SQLException ex) {
        System.err.print(Messages.getString("Oracle.SQLErro") + ex.getMessage());
    }
    return conn;
}

From source file:gov.nih.nci.logging.api.util.HibernateUtil.java

private static DataSource getDataSource(String jndiName) throws Exception {
    Context ctx = new InitialContext();
    if (ctx == null) {
        throw new Exception("No Context available");
    }/*  w ww . j av a  2  s  .  c om*/
    return (DataSource) ctx.lookup(jndiName);
}

From source file:ca.nrc.cadc.db.DBUtil.java

/**
 * Find a JNDI DataSource in the specified context.
 * /*w w w  .  j  a  v a  2  s  .  c om*/
 * @param dataSource
 * @param envContextName
 * @return
 * @throws NamingException 
 */
public static DataSource findJNDIDataSource(String dataSource, String envContextName) throws NamingException {
    log.debug("getDataSource: " + dataSource);
    Context initContext = new InitialContext();
    Context envContext = (Context) initContext.lookup(envContextName);
    DataSource ds = (DataSource) envContext.lookup(dataSource);

    return ds;
}

From source file:dsd.dao.DAOProvider.java

/**
 * //w ww  .ja  v a2  s. c om
 * @return
 */
public static DataSource getDataSource() {
    try {
        if (dataSource == null) {
            Context ctx = new InitialContext();
            dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/RTBMconnection");
        }
    } catch (NamingException e) {
        e.printStackTrace();
    }
    return dataSource;
}

From source file:eu.planets_project.tb.gui.UserBean.java

/**
 * Create a user manager:// w  w w. j a  v  a  2 s  . c om
 * @return
 */
public static UserManager getUserManager() {
    try {
        Context jndiContext = getInitialContext();
        UserManager um = (UserManager) PortableRemoteObject
                .narrow(jndiContext.lookup("planets-project.eu/UserManager/remote"), UserManager.class);
        return um;
    } catch (NamingException e) {
        log.error("Failure in getting PortableRemoteObject: " + e.toString());
        return null;
    }
}

From source file:ca.nrc.cadc.db.DBUtil.java

/**
 * Create a JNDI DataSOurce resource with the specified name and environment context.
 * //w  ww  .j av a 2 s. c o m
 * @param dataSourceName
 * @param envContextName
 * @param config
 * @throws NamingException 
 */
public static void createJNDIDataSource(String dataSourceName, String envContextName, ConnectionConfig config)
        throws NamingException {
    log.debug("createDataSource: " + dataSourceName + " START");
    StandaloneContextFactory.initJNDI();

    Context initContext = new InitialContext();
    Context envContext = (Context) initContext.lookup(envContextName);
    if (envContext == null) {
        envContext = initContext.createSubcontext(envContextName);
    }
    log.debug("env: " + envContext);

    DataSource ds = getDataSource(config);
    envContext.bind(dataSourceName, ds);
    log.debug("createDataSource: " + dataSourceName + " DONE");
}

From source file:eu.planets_project.tb.impl.persistency.TestbedServiceTemplatePersistencyImpl.java

/**
 * A Factory method to build a reference to this interface.
 * @return/*w  ww.  j  a  v a  2 s  .  c o m*/
 */
public static TestbedServiceTemplatePersistencyRemote getInstance() {
    Log log = LogFactory.getLog(TestbedServiceTemplatePersistencyImpl.class);
    try {
        Context jndiContext = new javax.naming.InitialContext();
        TestbedServiceTemplatePersistencyRemote dao_r = (TestbedServiceTemplatePersistencyRemote) PortableRemoteObject
                .narrow(jndiContext.lookup("testbed/TestbedServiceTemplatePersistencyImpl/remote"),
                        TestbedServiceTemplatePersistencyRemote.class);
        return dao_r;
    } catch (NamingException e) {
        log.error("Failure in getting PortableRemoteObject: " + e.toString());
        return null;
    }
}

From source file:org.jboss.processFlow.knowledgeService.QuartzSchedulerService.java

public static void start() throws Exception {
    Context jndiContext;
    jndiContext = new InitialContext();
    kSessionProxy = (IBaseKnowledgeSession) jndiContext.lookup(IBaseKnowledgeSession.BASE_JNDI);
    scheduler = StdSchedulerFactory.getDefaultScheduler();
    scheduler.start();//ww  w.j a v a 2s. com
}