Example usage for javax.naming InitialContext lookup

List of usage examples for javax.naming InitialContext lookup

Introduction

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

Prototype

public Object lookup(Name name) throws NamingException 

Source Link

Usage

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

/**
 * Returns DataSource for given data source name.
 *
 * @param name Name of data source as returned by listAvailableDataSources
 * @return DataSource if name exists, otherwise null
 */// w  w w .  ja v  a2 s .c  o  m
public static DataSource getDatasource(String name) {
    final ClassLoader origCL = Thread.currentThread().getContextClassLoader();

    try {
        Thread.currentThread().setContextClassLoader(ComponentManager.class.getClassLoader());
        final InitialContext jndiContext = new InitialContext();
        return (javax.sql.DataSource) jndiContext.lookup(JDBC_CONTEXT + "/" + name);
    } catch (NamingException e) {
        log.debug("NamingException lookup", e);
        return null;
    } finally {
        Thread.currentThread().setContextClassLoader(origCL);
    }
}

From source file:org.apache.jackrabbit.ocm.spring.RepositoryUtil.java

/**
 * Get a repository/*from  www  . j  a  va  2 s  .  c o m*/
 *
 * @param repositoryName The repository name
 * @return a JCR repository reference
 *
 * @throws RepositoryException when it is not possible to get the repository.
 *         Before calling this method, the repository has to be registered (@see RepositoryUtil#registerRepository(String, String, String)
 */
public static Repository getRepository(String repositoryName) throws RepositoryException {
    try {
        Hashtable env = new Hashtable();
        env.put(Context.INITIAL_CONTEXT_FACTORY,
                "org.apache.jackrabbit.core.jndi.provider.DummyInitialContextFactory");
        env.put(Context.PROVIDER_URL, "localhost");
        InitialContext ctx = new InitialContext(env);

        Repository repository = (Repository) ctx.lookup(repositoryName);
        return repository;
    } catch (Exception e) {
        throw new RepositoryException("Impossible to get the repository : " + repositoryName, e);
    }
}

From source file:fr.cnes.sitools.datasource.jdbc.business.SitoolsDataSourceFactory.java

/**
 * Retrieval of a DataSource as a JNDI resource
 * //from   w w  w.ja  va2  s .co m
 * Lookup the DataSource, which will be backed by a pool that the application server provides. DataSource instances
 * are also a good candidate for caching as an instance variable, as JNDI lookups can be expensive as well.
 * 
 * @param dsName
 *          JNDI resource name
 * @return DataSource
 */
public static DataSource getJNDIDataSource(String dsName) {
    SitoolsDataSource foundDataSource = dataSources.get(dsName);
    if (foundDataSource != null) {
        return foundDataSource;
    }

    DataSource ds = null;
    try {
        InitialContext ctx = new InitialContext();

        ds = (DataSource) ctx.lookup(dsName);
        JDBCDataSource jdbcDS = new JDBCDataSource();
        jdbcDS.setName(dsName);
        dataSources.put(dsName, new SitoolsDataSource(jdbcDS, ds, null));

    } catch (NamingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return ds;
}

From source file:fr.cnes.sitools.datasource.jdbc.business.SitoolsSQLDataSourceFactory.java

/**
 * Retrieval of a DataSource as a JNDI resource
 * //from  w ww . j a va 2  s . com
 * Lookup the DataSource, which will be backed by a pool that the application server provides. DataSource instances
 * are also a good candidate for caching as an instance variable, as JNDI lookups can be expensive as well.
 * 
 * @param dsName
 *          JNDI resource name
 * @return DataSource
 */
public static DataSource getJNDIDataSource(String dsName) {
    SitoolsSQLDataSource foundDataSource = dataSources.get(dsName);
    if (foundDataSource != null) {
        return foundDataSource;
    }

    DataSource ds = null;
    try {
        InitialContext ctx = new InitialContext();

        ds = (DataSource) ctx.lookup(dsName);
        JDBCDataSource jdbcDS = new JDBCDataSource();
        jdbcDS.setName(dsName);
        dataSources.put(dsName, new SitoolsSQLDataSource(jdbcDS, ds, null));

    } catch (NamingException e) {
        Context.getCurrentLogger().log(Level.INFO, null, e);

    }
    return ds;
}

From source file:test.TestHelper.java

/**
 * Helper method to retrieve XmlTransforming instance.
 *
 * @return instance of getXmlTransforming
 * @throws NamingException Thrown when the service was not found by the expected service name.
 */// w  ww  .j a  v  a 2s  .c  o m
public static XmlTransforming getXmlTransforming() throws NamingException {
    InitialContext context = new InitialContext();
    XmlTransforming xmlTransforming = (XmlTransforming) context.lookup(XmlTransforming.SERVICE_NAME);
    assertNotNull(xmlTransforming);
    return xmlTransforming;
}

From source file:org.nimbustools.ctxbroker.rest.BrokerResource.java

protected static ContextBrokerHomeImpl discoverHome() throws Exception {
    InitialContext ctx = null;
    try {/*from  ww w  .  j  av  a2s.  c  o m*/
        ctx = new InitialContext();
        final ContextBrokerHomeImpl home = (ContextBrokerHomeImpl) ctx
                .lookup(ContextBrokerServiceImpl.CONTEXTUALIZATION_HOME);
        if (home == null) {
            throw new Exception("null from JNDI for ContextBrokerHome (?)");
        }
        return home;
    } finally {
        if (ctx != null) {
            ctx.close();
        }
    }
}

From source file:org.nuxeo.ecm.platform.ec.notification.email.EmailHelper.java

/**
 * Gets the session from the JNDI./*from w w w .  j av  a 2s  .  co m*/
 */
private static Session getSession() {
    Session session = null;
    if (javaMailNotAvailable) {
        return null;
    }
    // First, try to get the session from JNDI, as would be done under J2EE.
    try {
        NotificationService service = (NotificationService) Framework.getRuntime()
                .getComponent(NotificationService.NAME);
        InitialContext ic = new InitialContext();
        session = (Session) ic.lookup(service.getMailSessionJndiName());
    } catch (NamingException ex) {
        log.warn("Unable to find Java mail API", ex);
        javaMailNotAvailable = true;
    }

    return session;
}

From source file:io.apiman.manager.api.jdbc.JdbcMetricsAccessor.java

/**
 * Lookup the datasource in JNDI.// w ww.j  av  a 2  s. c o  m
 * @param dsJndiLocation
 */
private static DataSource lookupDS(String dsJndiLocation) {
    DataSource ds;
    try {
        InitialContext ctx = new InitialContext();
        ds = (DataSource) ctx.lookup(dsJndiLocation);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    if (ds == null) {
        throw new RuntimeException("Datasource not found: " + dsJndiLocation); //$NON-NLS-1$
    }
    return ds;
}

From source file:test.TestHelper.java

/**
 * Helper method to retrieve ValidationTransforming instance.
 *
 * @return instance of get ValidationTransforming
 * @throws NamingException Thrown when the service was not found by the expected service name.
 *//* w w w.  j  ava2 s  .c o  m*/
public static ValidationTransforming getValidationTransforming() throws NamingException {
    InitialContext context = new InitialContext();
    ValidationTransforming vTransforming = (ValidationTransforming) context
            .lookup("ejb:validation_ear/validation/ValidationTransformingBean!"
                    + ValidationTransforming.class.getName());
    assertNotNull(vTransforming);
    return vTransforming;
}

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 www .  j a  va2  s.  c om*/
        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);
    }
}