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.jberet.support.io.JsonItemReaderWriterBase.java

/**
 * Initializes {@code JsonFactory} or its subtypes, e.g., {@code com.fasterxml.jackson.dataformat.xml.XmlFactory},
 * or {@code com.fasterxml.jackson.dataformat.csv.CsvFactory}. The factory should be initialized with a valid
 * {@code ObjectMapper}.//w  w  w .j av a  2s  .com
 *
 * Subclass may override this method to use different concrete types of {@code JsonFactory}.
 *
 * @throws Exception
 * @since 1.2.0.Alpha1
 */
protected void initJsonFactory() throws Exception {
    if (jsonFactoryLookup != null) {
        jsonFactory = InitialContext.doLookup(jsonFactoryLookup);
    } else {
        jsonFactory = new MappingJsonFactory();
    }
}

From source file:com.egt.core.util.EA.java

private static boolean lookup() {
    Bitacora.trace(EA.class, "lookup", jndi_ejb_persistence_pattern);
    String jndi = MessageFormat.format(jndi_ejb_persistence_pattern, EJBL.UsuarioFacade);
    try {/*from w w w. j a  va2  s  .  c  o  m*/
        InitialContext.doLookup(jndi);
        return true;
    } catch (Exception ex) {
        String string = ThrowableUtils.getString(ex);
        messages += string + " (lookup " + jndi_ejb_persistence_pattern + "); ";
        Bitacora.trace(string);
    }
    return false;
}

From source file:ips1ap101.lib.core.util.EA.java

private static boolean lookup() {
    Bitacora.trace(EA.class, "lookup", jndi_ejb_lookup_pattern);
    String bean = "UsuarioFacadeBean";
    String base = UsuarioFacadeBase.class.getName();
    String jndi = MessageFormat.format(jndi_ejb_lookup_pattern, bean, base);
    try {/*  w ww  .j a  v  a 2 s.  c  o m*/
        InitialContext.doLookup(jndi);
        return true;
    } catch (Exception ex) {
        String string = ThrowableUtils.getString(ex);
        messages += string + " (lookup " + jndi_ejb_lookup_pattern + "); ";
        Bitacora.trace(string);
    }
    return false;
}

From source file:com.enioka.jqm.tools.Helpers.java

/**
 * Send a mail message using a JNDI resource.<br>
 * As JNDI resource providers are inside the EXT class loader, this uses reflection. This method is basically a bonus on top of the
 * MailSessionFactory offered to payloads, making it accessible also to the engine.
 * //www.j a  va  2  s  .  c om
 * @param to
 * @param subject
 * @param body
 * @param mailSessionJndiAlias
 * @throws MessagingException
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
static void sendMessage(String to, String subject, String body, String mailSessionJndiAlias)
        throws MessagingException {
    jqmlogger.debug("sending mail to " + to + " - subject is " + subject);
    ClassLoader extLoader = getExtClassLoader();
    ClassLoader old = Thread.currentThread().getContextClassLoader();
    Object mailSession = null;

    try {
        mailSession = InitialContext.doLookup(mailSessionJndiAlias);
    } catch (NamingException e) {
        throw new MessagingException("could not find mail session description", e);
    }

    try {
        Thread.currentThread().setContextClassLoader(extLoader);
        Class transportZ = extLoader.loadClass("javax.mail.Transport");
        Class sessionZ = extLoader.loadClass("javax.mail.Session");
        Class mimeMessageZ = extLoader.loadClass("javax.mail.internet.MimeMessage");
        Class messageZ = extLoader.loadClass("javax.mail.Message");
        Class recipientTypeZ = extLoader.loadClass("javax.mail.Message$RecipientType");
        Object msg = mimeMessageZ.getConstructor(sessionZ).newInstance(mailSession);

        mimeMessageZ.getMethod("setRecipients", recipientTypeZ, String.class).invoke(msg,
                recipientTypeZ.getField("TO").get(null), to);
        mimeMessageZ.getMethod("setSubject", String.class).invoke(msg, subject);
        mimeMessageZ.getMethod("setText", String.class).invoke(msg, body);

        transportZ.getMethod("send", messageZ).invoke(null, msg);
        jqmlogger.trace("Mail was sent");
    } catch (Exception e) {
        throw new MessagingException("an exception occurred during mail sending", e);
    } finally {
        Thread.currentThread().setContextClassLoader(old);
    }
}

From source file:org.droolsjbpm.services.test.ci.SessionMGMTandCIBaseTest.java

protected UserTransaction setupEnvironment(Environment environment) {
    UserTransaction ut = null;/*  w w  w .  jav  a 2s  .c o m*/
    try {
        this.em.toString();
        environment.set(EnvironmentName.ENTITY_MANAGER_FACTORY, this.em.getEntityManagerFactory());
    } catch (ContextNotActiveException e) {
        try {
            ut = InitialContext.doLookup("java:comp/UserTransaction");
        } catch (Exception ex) {
            try {
                ut = InitialContext
                        .doLookup(System.getProperty("jbpm.ut.jndi.lookup", "java:jboss/UserTransaction"));

            } catch (Exception e1) {
                throw new RuntimeException("Cannot find UserTransaction", e1);
            }
        }
        try {
            ut.begin();
            environment.set(EnvironmentName.TRANSACTION, ut);
        } catch (Exception ex) {

        }
        environment.set(EnvironmentName.ENTITY_MANAGER_FACTORY, this.emf);
    }

    return ut;
}

From source file:org.jbpm.executor.impl.ExecutorImpl.java

protected ScheduledExecutorService getScheduledExecutorService() {
    ThreadFactory threadFactory = null;

    try {/*from  w  w  w  .j a v a 2s  .  c  o  m*/
        threadFactory = InitialContext.doLookup(threadFactoryLookup);
    } catch (Exception e) {
        threadFactory = Executors.defaultThreadFactory();
    }

    return new PrioritisedScheduledThreadPoolExecutor(threadPoolSize, threadFactory);
}

From source file:org.kie.server.services.impl.KieServerImpl.java

protected ContainerManager getContainerManager() {
    try {//from   w ww  .  j  a va 2 s . co m
        return InitialContext.doLookup("java:module/ContainerManagerEJB");
    } catch (Exception e) {
        logger.debug("Unable to find JEE version of ContainerManager suing default one");
        return new ContainerManager();
    }
}

From source file:org.wso2.andes.store.cassandra.cql.CQLConnection.java

@Override
public void initialize(ConfigurationProperties connectionProperties) throws AndesException {
    String jndiLookupName = "";
    try {//from w ww .ja v  a2s  .  c o  m

        jndiLookupName = connectionProperties.getProperty(CassandraConstants.PROP_JNDI_LOOKUP_NAME);

        String replicationFactor = connectionProperties.getProperty(CassandraConstants.PROP_REPLICATION_FACTOR);
        if (replicationFactor.isEmpty()) {
            replicationFactor = CassandraConstants.DEFAULT_REPLICATION_FACTOR;
        }
        String strategyClass = connectionProperties.getProperty(CassandraConstants.PROP_STRATEGY_CLASS);

        if (strategyClass.isEmpty()) {
            strategyClass = CassandraConstants.DEFAULT_STRATEGY_CLASS;
        }
        String readConsistencyLevel = connectionProperties
                .getProperty(CassandraConstants.PROP_READ_CONSISTENCY);
        if (readConsistencyLevel.isEmpty()) {
            readConsistencyLevel = CassandraConstants.DEFAULT_READ_CONSISTENCY;
        }

        String writeConsistencyLevel = connectionProperties
                .getProperty(CassandraConstants.PROP_WRITE_CONSISTENCY);
        if (writeConsistencyLevel.isEmpty()) {
            writeConsistencyLevel = CassandraConstants.DEFAULT_WRITE_CONSISTENCY;
        }

        String gcGraceSeconds = connectionProperties.getProperty(CassandraConstants.PROP_GC_GRACE_SECONDS);

        if (gcGraceSeconds.isEmpty()) {
            gcGraceSeconds = CassandraConstants.DEFAULT_GC_GRACE_SECONDS;
        }

        setGcGraceSeconds(Integer.parseInt(gcGraceSeconds));
        //todo remove this if elsee condtions after testing
        if (cluster == null) {
            cluster = InitialContext.doLookup(jndiLookupName);
        }

        //set consistency levels
        GenericCQLDAO.setReadConsistencyLevel(readConsistencyLevel);
        GenericCQLDAO.setWriteConsistencyLevel(writeConsistencyLevel);

        GenericCQLDAO.setCluster(cluster);
        createKeySpace(Integer.parseInt(replicationFactor), strategyClass);

        //start Cassandra connection live check
        isCassandraConnectionLive = true;
        checkCassandraConnection();

    } catch (NamingException e) {
        throw new AndesException("Couldn't look up jndi entry for " + "\"" + jndiLookupName + "\"", e);
    } catch (CassandraDataAccessException e) {
        throw new AndesException("Cannot Initialize Cassandra Connection", e);
    }

}

From source file:org.wso2.andes.store.rdbms.RDBMSConnection.java

@Override
public void initialize(ConfigurationProperties connectionProperties) throws AndesException {

    super.initialize(connectionProperties);

    Connection connection = null;
    String jndiLookupName = "";
    String dataSourceUserName = "";
    try {/*from  w w  w .ja v  a 2 s  .c  o m*/
        // try to get the lookup name. If error empty string will be returned
        jndiLookupName = connectionProperties.getProperty(RDBMSConstants.PROP_JNDI_LOOKUP_NAME);
        datasource = InitialContext.doLookup(jndiLookupName);

        if (datasource instanceof org.apache.tomcat.jdbc.pool.DataSource) {
            org.apache.tomcat.jdbc.pool.DataSource tcDataSource = (org.apache.tomcat.jdbc.pool.DataSource) datasource;
            if (StringUtils.isNotBlank(tcDataSource.getUsername())) {
                dataSourceUserName = tcDataSource.getUsername();
            }
        }

        connection = datasource.getConnection();
        logger.info("JDBC connection established with jndi config " + jndiLookupName);
    } catch (SQLException e) {
        throw new AndesException("Connecting to database failed with jndi lookup : " + jndiLookupName
                + ". data source username : " + dataSourceUserName + ". SQL Error message : " + e.getMessage(),
                e);
    } catch (NamingException e) {
        throw new AndesException("Couldn't look up jndi entry for " + "\"" + jndiLookupName + "\"", e);
    } finally {
        String task = "Initialising database";
        close(connection, task);
    }
}

From source file:org.wso2.carbon.analytics.datasource.hdfs.HDFSAnalyticsFileSystem.java

@Override
public void init(Map<String, String> properties) throws AnalyticsException {
    String dsName = properties.get(HDFSAnalyticsDataSourceConstants.DATASOURCE_NAME);
    if (dsName == null) {
        throw new AnalyticsException(
                "The property '" + HDFSAnalyticsDataSourceConstants.DATASOURCE_NAME + "' is required");
    }/*from   w  w w . ja v a 2 s  .  c  om*/
    try {
        this.fileSystem = (FileSystem) InitialContext.doLookup(dsName);
    } catch (NamingException e) {
        throw new AnalyticsException("Error in looking up data source: " + e.getMessage(), e);
    }
}