Example usage for org.springframework.jndi JndiObjectFactoryBean setResourceRef

List of usage examples for org.springframework.jndi JndiObjectFactoryBean setResourceRef

Introduction

In this page you can find the example usage for org.springframework.jndi JndiObjectFactoryBean setResourceRef.

Prototype

public void setResourceRef(boolean resourceRef) 

Source Link

Document

Set whether the lookup occurs in a Java EE container, i.e.

Usage

From source file:com.mycompany.projetsportmanager.spring.configuration.DefaultProfileConfiguration.java

/**
 * Builds a JNDI datasource.//from   w  ww. j  a v  a  2s  .c om
 * @return the datasource.
 */
@Bean(destroyMethod = "")
public DataSource dataSource() {
    JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
    jndiObjectFactoryBean.setJndiName("jdbc/ProjetSportManager");
    jndiObjectFactoryBean.setResourceRef(true);
    jndiObjectFactoryBean.setExpectedType(DataSource.class);
    try {
        jndiObjectFactoryBean.afterPropertiesSet();
    } catch (NamingException e) {
        throw new RuntimeException(e);
    }
    return (DataSource) jndiObjectFactoryBean.getObject();
}

From source file:info.jtrac.mail.MailSender.java

private void initMailSenderFromJndi(String mailSessionJndiName) {
    logger.info("attempting to initialize mail sender from jndi name = '" + mailSessionJndiName + "'");
    JndiObjectFactoryBean factoryBean = new JndiObjectFactoryBean();
    factoryBean.setJndiName(mailSessionJndiName);
    // "java:comp/env/" will be prefixed if the JNDI name doesn't already have it
    factoryBean.setResourceRef(true);
    try {/* w  w  w .j av a 2  s.c  om*/
        // this step actually does the JNDI lookup
        factoryBean.afterPropertiesSet();
    } catch (Exception e) {
        logger.warn("failed to locate mail session : " + e);
        return;
    }
    Session session = (Session) factoryBean.getObject();
    sender = new JavaMailSenderImpl();
    sender.setSession(session);
    logger.info("email sender initialized from jndi name = '" + mailSessionJndiName + "'");
}

From source file:MailSender.java

private void initMailSenderFromJndi(String mailSessionJndiName) {
    logger.info("attempting to initialize mail sender from jndi name = '" + mailSessionJndiName + "'");
    JndiObjectFactoryBean factoryBean = new JndiObjectFactoryBean();
    factoryBean.setJndiName(mailSessionJndiName);
    // "java:comp/env/" will be prefixed if the JNDI name doesn't already
    // have it//from w  w w . ja  v a  2  s. co m

    // major: the above behavoiur is already documented
    // in the class JndiObjectFactoryBean. Remove it.

    factoryBean.setResourceRef(true);
    try {
        // this step actually does the JNDI lookup
        factoryBean.afterPropertiesSet();
    } catch (Exception e) {
        logger.warn("failed to locate mail session : " + e);
        return;
    }
    Session session = (Session) factoryBean.getObject();
    sender = new JavaMailSenderImpl();
    sender.setSession(session);
    logger.info("email sender initialized from jndi name = '" + mailSessionJndiName + "'");
}

From source file:gr.abiss.calipso.config.DataSourceFactoryBean.java

public Object getObject() throws Exception {
    if (StringUtils.hasText(dataSourceJndiName)) {
        logger.info("JNDI datasource requested, looking up datasource from JNDI name: '" + dataSourceJndiName
                + "'");
        JndiObjectFactoryBean factoryBean = new JndiObjectFactoryBean();
        factoryBean.setJndiName(dataSourceJndiName);
        // "java:comp/env/" will be prefixed if the JNDI name doesn't already have it
        factoryBean.setResourceRef(true);
        // this step actually does the JNDI lookup
        try {//  w  w w.  j av  a 2 s . c o m
            factoryBean.afterPropertiesSet();
        } catch (Exception e) {
            logger.error("datasource init from JNDI failed : " + e);
            logger.error("aborting application startup");
            throw new RuntimeException(e);
        }
        dataSource = (DataSource) factoryBean.getObject();
    } else if (url.startsWith("jdbc:hsqldb:file")) {
        logger.info("embedded HSQLDB mode detected, switching on spring single connection data source");
        SingleConnectionDataSource ds = new SingleConnectionDataSource();
        ds.setUrl(url);
        ds.setDriverClassName(driverClassName);
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setSuppressClose(true);
        dataSource = ds;
    } else {
        logger.info(
                "Not using embedded HSQLDB or JNDI datasource, switching on Apache DBCP data source connection pooling");
        BasicDataSource ds = new BasicDataSource();
        ds.setUrl(url);
        ds.setDriverClassName(driverClassName);
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setValidationQuery(validationQuery);
        ds.setTestOnBorrow(false);
        ds.setTestWhileIdle(true);
        ds.setTimeBetweenEvictionRunsMillis(600000);
        dataSource = ds;
    }
    return dataSource;
}

From source file:gr.abiss.calipso.mail.MailSender.java

private void initMailSenderFromJndi(String mailSessionJndiName) {
    //logger.info("attempting to initialize mail sender from jndi name = '" + mailSessionJndiName + "'");
    JndiObjectFactoryBean factoryBean = new JndiObjectFactoryBean();
    factoryBean.setJndiName(mailSessionJndiName);
    // "java:comp/env/" will be prefixed if the JNDI name doesn't already have it
    factoryBean.setResourceRef(true);
    try {//from w  w w . j  av a2s .  c o  m
        // this step actually does the JNDI lookup
        factoryBean.afterPropertiesSet();
    } catch (Exception e) {
        logger.warn("failed to locate mail session : " + e);
        return;
    }
    Session session = (Session) factoryBean.getObject();
    sender = new JavaMailSenderImpl();
    sender.setSession(session);
    logger.info("email sender initialized from jndi name = '" + mailSessionJndiName + "'");
}

From source file:info.jtrac.config.DataSourceFactoryBean.java

/**
 * This method returns the dataSource object used for the DB access.
 *
 * @throws Exception/*from   ww  w  .ja v a  2s.  c o m*/
 * @see org.springframework.beans.factory.FactoryBean#getObject()
 */
@Override
public DataSource getObject() throws Exception {
    if (StringUtils.hasText(dataSourceJndiName)) {
        logger.info("JNDI datasource requested, looking up datasource from JNDI name: '" + dataSourceJndiName
                + "'");
        JndiObjectFactoryBean factoryBean = new JndiObjectFactoryBean();
        factoryBean.setJndiName(dataSourceJndiName);

        // "java:comp/env/" will be prefixed if the JNDI name doesn't already have it
        factoryBean.setResourceRef(true);

        // This step actually does the JNDI lookup
        try {
            factoryBean.afterPropertiesSet();
        } catch (Exception e) {
            logger.error("datasource init from JNDI failed : " + e);
            logger.error("aborting application startup");
            throw new RuntimeException(e);
        } // end try..catch

        dataSource = (DataSource) factoryBean.getObject();
    } else if (url.startsWith("jdbc:hsqldb:file")) {
        logger.info("embedded HSQLDB mode detected, switching on spring single connection data source");
        SingleConnectionDataSource ds = new SingleConnectionDataSource();
        ds.setUrl(url);
        ds.setDriverClassName(driverClassName);
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setSuppressClose(true);
        dataSource = ds;
    } else {
        logger.info(
                "Not using embedded HSQLDB or JNDI datasource, switching on Apache DBCP data source connection pooling");
        BasicDataSource ds = new BasicDataSource();
        ds.setUrl(url);
        ds.setDriverClassName(driverClassName);
        ds.setUsername(username);
        ds.setPassword(password);
        ds.setValidationQuery(validationQuery);
        ds.setTestOnBorrow(false);
        ds.setTestWhileIdle(true);
        ds.setTimeBetweenEvictionRunsMillis(600000);
        dataSource = ds;
    } // end if..else

    return dataSource;
}