Example usage for org.springframework.jndi JndiObjectFactoryBean getObject

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

Introduction

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

Prototype

@Override
@Nullable
public Object getObject() 

Source Link

Document

Return the singleton JNDI object.

Usage

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 {/*www  . j a v  a 2  s . co 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);// w  w w . j  ava  2 s . c  o m
    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:info.jtrac.config.DataSourceFactoryBean.java

/**
 * This method returns the dataSource object used for the DB access.
 *
 * @throws Exception//www  .j av 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;
}

From source file:org.artifactory.storage.db.spring.DbConfigFactory.java

private DataSource getDataSourceFromBeanOrJndi(StorageProperties storageProperties, String suffix) {
    DataSource result = null;//w w  w.  j  a  v a2 s.  co  m
    String connectionUrl = storageProperties.getConnectionUrl();
    if (StringUtils.startsWithIgnoreCase(connectionUrl, BEAN_PREFIX)) {
        result = beanFactory.getBean(connectionUrl.substring(BEAN_PREFIX.length()) + suffix, DataSource.class);
    } else if (StringUtils.startsWithIgnoreCase(connectionUrl, JNDI_PREFIX)) {
        String jndiName = connectionUrl.substring(JNDI_PREFIX.length());
        JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
        jndiObjectFactoryBean.setJndiName(jndiName + suffix);
        try {
            jndiObjectFactoryBean.afterPropertiesSet();
        } catch (NamingException e) {
            throw new RuntimeException(e);
        }
        result = (DataSource) jndiObjectFactoryBean.getObject();
    }
    return result;
}

From source file:org.jumpmind.symmetric.ClientSymmetricEngine.java

public static IDatabasePlatform createDatabasePlatform(ApplicationContext springContext,
        TypedProperties properties, DataSource dataSource, boolean waitOnAvailableDatabase) {
    if (dataSource == null) {
        String jndiName = properties.getProperty(ParameterConstants.DB_JNDI_NAME);
        if (StringUtils.isNotBlank(jndiName)) {
            try {
                log.info("Looking up datasource in jndi.  The jndi name is {}", jndiName);
                JndiObjectFactoryBean jndiFactory = new JndiObjectFactoryBean();
                jndiFactory.setJndiName(jndiName);
                jndiFactory.afterPropertiesSet();
                dataSource = (DataSource) jndiFactory.getObject();

                if (dataSource == null) {
                    throw new SymmetricException(
                            "Could not locate the configured datasource in jndi.  The jndi name is %s",
                            jndiName);/*from  w  w w .j a v  a  2s  . c  om*/
                }
            } catch (IllegalArgumentException e) {
                throw new SymmetricException(
                        "Could not locate the configured datasource in jndi.  The jndi name is %s", e,
                        jndiName);
            } catch (NamingException e) {
                throw new SymmetricException(
                        "Could not locate the configured datasource in jndi.  The jndi name is %s", e,
                        jndiName);
            }
        }

        String springBeanName = properties.getProperty(ParameterConstants.DB_SPRING_BEAN_NAME);
        if (isNotBlank(springBeanName) && springContext != null) {
            log.info("Using datasource from spring.  The spring bean name is {}", springBeanName);
            dataSource = (DataSource) springContext.getBean(springBeanName);
        }

        if (dataSource == null) {
            dataSource = BasicDataSourceFactory.create(properties,
                    SecurityServiceFactory.create(SecurityServiceType.CLIENT, properties));
        }
    }
    if (waitOnAvailableDatabase) {
        waitForAvailableDatabase(dataSource);
    }
    boolean delimitedIdentifierMode = properties.is(ParameterConstants.DB_DELIMITED_IDENTIFIER_MODE, true);
    return JdbcDatabasePlatformFactory.createNewPlatformInstance(dataSource,
            createSqlTemplateSettings(properties), delimitedIdentifierMode);
}

From source file:org.nema.medical.mint.server.ServerConfig.java

@Bean(destroyMethod = "close")
public SessionFactory sessionFactory() throws Exception {
    if (sessionFactory == null) {
        final AnnotationSessionFactoryBean annotationSessionFactoryBean = new AnnotationSessionFactoryBean();

        if (StringUtils.isBlank(getConfigString("hibernate.connection.datasource"))) {
            // Not using JNDI data source
            final BasicDataSource dataSource = new BasicDataSource();
            dataSource.setDriverClassName(getConfigString("hibernate.connection.driver_class"));
            String url = getConfigString("hibernate.connection.url");
            url = url.replace("$MINT_HOME", mintHome().getPath());
            dataSource.setUrl(url);/*from   www.  ja  va2 s. com*/

            dataSource.setUsername(getConfigString("hibernate.connection.username"));
            dataSource.setPassword(getConfigString("hibernate.connection.password"));
            annotationSessionFactoryBean.setDataSource(dataSource);
        } else {
            // Using a JNDI dataSource
            final JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
            jndiObjectFactoryBean.setExpectedType(DataSource.class);
            jndiObjectFactoryBean.setJndiName(getConfigString("hibernate.connection.datasource"));
            jndiObjectFactoryBean.afterPropertiesSet();
            annotationSessionFactoryBean.setDataSource((DataSource) jndiObjectFactoryBean.getObject());
        }

        final Properties hibernateProperties = new Properties();
        hibernateProperties.put("hibernate.connection.autocommit", Boolean.TRUE);

        final String dialect = getConfigString("hibernate.dialect");
        if (StringUtils.isNotBlank(dialect)) {
            hibernateProperties.put("hibernate.dialect", dialect);
        }

        final String hbm2dll = getConfigString("hibernate.hbm2ddl.auto");
        hibernateProperties.put("hibernate.hbm2ddl.auto", hbm2dll == null ? "verify" : hbm2dll);

        hibernateProperties.put("hibernate.show_sql",
                "true".equalsIgnoreCase(getConfigString("hibernate.show_sql")));

        hibernateProperties.put("hibernate.c3p0.max_statement", 50);
        hibernateProperties.put("hibernate.c3p0.maxPoolSize", 20);
        hibernateProperties.put("hibernate.c3p0.minPoolSize", 5);
        hibernateProperties.put("hibernate.c3p0.testConnectionOnCheckout", Boolean.FALSE);
        hibernateProperties.put("hibernate.c3p0.timeout", 600);
        annotationSessionFactoryBean.setHibernateProperties(hibernateProperties);

        annotationSessionFactoryBean.setPackagesToScan(getPackagesToScan());
        annotationSessionFactoryBean.afterPropertiesSet();

        sessionFactory = annotationSessionFactoryBean.getObject();
    }
    return sessionFactory;
}