Java Code Examples for org.springframework.jndi.JndiObjectFactoryBean

The following examples show how to use org.springframework.jndi.JndiObjectFactoryBean. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private DataSource getJndiDataSource(final String jndiName) throws NamingException {
    JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
    bean.setResourceRef(true);
    bean.setJndiName(jndiName);
    bean.setProxyInterface(DataSource.class);
    bean.afterPropertiesSet();
    return (DataSource) bean.getObject();
}
 
Example 2
@Nullable
static AbstractBeanDefinition findServerForSpecialEnvironment() {
	if (weblogicPresent) {
		RootBeanDefinition bd = new RootBeanDefinition(JndiObjectFactoryBean.class);
		bd.getPropertyValues().add("jndiName", "java:comp/env/jmx/runtime");
		return bd;
	}
	else if (webspherePresent) {
		return new RootBeanDefinition(WebSphereMBeanServerFactoryBean.class);
	}
	else {
		return null;
	}
}
 
Example 3
Source Project: spring-analysis-note   Source File: JeeNamespaceHandlerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testSimpleDefinition() throws Exception {
	BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("simple");
	assertEquals(JndiObjectFactoryBean.class.getName(), beanDefinition.getBeanClassName());
	assertPropertyValue(beanDefinition, "jndiName", "jdbc/MyDataSource");
	assertPropertyValue(beanDefinition, "resourceRef", "true");
}
 
Example 4
Source Project: spring-analysis-note   Source File: JeeNamespaceHandlerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testComplexDefinition() throws Exception {
	BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("complex");
	assertEquals(JndiObjectFactoryBean.class.getName(), beanDefinition.getBeanClassName());
	assertPropertyValue(beanDefinition, "jndiName", "jdbc/MyDataSource");
	assertPropertyValue(beanDefinition, "resourceRef", "true");
	assertPropertyValue(beanDefinition, "cache", "true");
	assertPropertyValue(beanDefinition, "lookupOnStartup", "true");
	assertPropertyValue(beanDefinition, "exposeAccessContext", "true");
	assertPropertyValue(beanDefinition, "expectedType", "com.myapp.DefaultFoo");
	assertPropertyValue(beanDefinition, "proxyInterface", "com.myapp.Foo");
	assertPropertyValue(beanDefinition, "defaultObject", "myValue");
}
 
Example 5
Source Project: Project   Source File: DataSourceConfig.java    License: Apache License 2.0 5 votes vote down vote up
/**
* <p>描述:jndi数据源;生产环境</p>
* @return
* @author LZC
* @date   2019-02-14 17:38
 */
@Bean
@Profile("prod")
public DataSource jndiDataSource() {
	JndiObjectFactoryBean jndiObjectFactoryBean = new JndiObjectFactoryBean();
	jndiObjectFactoryBean.setJndiName("jdbc/myDS");
	jndiObjectFactoryBean.setResourceRef(true);
	jndiObjectFactoryBean.setProxyInterface(DataSource.class);
	return (DataSource) jndiObjectFactoryBean.getObject();
}
 
Example 6
@Nullable
static AbstractBeanDefinition findServerForSpecialEnvironment() {
	if (weblogicPresent) {
		RootBeanDefinition bd = new RootBeanDefinition(JndiObjectFactoryBean.class);
		bd.getPropertyValues().add("jndiName", "java:comp/env/jmx/runtime");
		return bd;
	}
	else if (webspherePresent) {
		return new RootBeanDefinition(WebSphereMBeanServerFactoryBean.class);
	}
	else {
		return null;
	}
}
 
Example 7
Source Project: java-technology-stack   Source File: JeeNamespaceHandlerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testSimpleDefinition() throws Exception {
	BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("simple");
	assertEquals(JndiObjectFactoryBean.class.getName(), beanDefinition.getBeanClassName());
	assertPropertyValue(beanDefinition, "jndiName", "jdbc/MyDataSource");
	assertPropertyValue(beanDefinition, "resourceRef", "true");
}
 
Example 8
Source Project: java-technology-stack   Source File: JeeNamespaceHandlerTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testComplexDefinition() throws Exception {
	BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("complex");
	assertEquals(JndiObjectFactoryBean.class.getName(), beanDefinition.getBeanClassName());
	assertPropertyValue(beanDefinition, "jndiName", "jdbc/MyDataSource");
	assertPropertyValue(beanDefinition, "resourceRef", "true");
	assertPropertyValue(beanDefinition, "cache", "true");
	assertPropertyValue(beanDefinition, "lookupOnStartup", "true");
	assertPropertyValue(beanDefinition, "exposeAccessContext", "true");
	assertPropertyValue(beanDefinition, "expectedType", "com.myapp.DefaultFoo");
	assertPropertyValue(beanDefinition, "proxyInterface", "com.myapp.Foo");
	assertPropertyValue(beanDefinition, "defaultObject", "myValue");
}
 
Example 9
static AbstractBeanDefinition findServerForSpecialEnvironment() {
	if (weblogicPresent) {
		RootBeanDefinition bd = new RootBeanDefinition(JndiObjectFactoryBean.class);
		bd.getPropertyValues().add("jndiName", "java:comp/env/jmx/runtime");
		return bd;
	}
	else if (webspherePresent) {
		return new RootBeanDefinition(WebSphereMBeanServerFactoryBean.class);
	}
	else {
		return null;
	}
}
 
Example 10
static AbstractBeanDefinition findServerForSpecialEnvironment() {
	if (weblogicPresent) {
		RootBeanDefinition bd = new RootBeanDefinition(JndiObjectFactoryBean.class);
		bd.getPropertyValues().add("jndiName", "java:comp/env/jmx/runtime");
		return bd;
	}
	else if (webspherePresent) {
		return new RootBeanDefinition(WebSphereMBeanServerFactoryBean.class);
	}
	else {
		return null;
	}
}
 
Example 11
@Test
public void testSimpleDefinition() throws Exception {
	BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("simple");
	assertEquals(JndiObjectFactoryBean.class.getName(), beanDefinition.getBeanClassName());
	assertPropertyValue(beanDefinition, "jndiName", "jdbc/MyDataSource");
	assertPropertyValue(beanDefinition, "resourceRef", "true");
}
 
Example 12
@Test
public void testComplexDefinition() throws Exception {
	BeanDefinition beanDefinition = this.beanFactory.getMergedBeanDefinition("complex");
	assertEquals(JndiObjectFactoryBean.class.getName(), beanDefinition.getBeanClassName());
	assertPropertyValue(beanDefinition, "jndiName", "jdbc/MyDataSource");
	assertPropertyValue(beanDefinition, "resourceRef", "true");
	assertPropertyValue(beanDefinition, "cache", "true");
	assertPropertyValue(beanDefinition, "lookupOnStartup", "true");
	assertPropertyValue(beanDefinition, "exposeAccessContext", "true");
	assertPropertyValue(beanDefinition, "expectedType", "com.myapp.DefaultFoo");
	assertPropertyValue(beanDefinition, "proxyInterface", "com.myapp.Foo");
	assertPropertyValue(beanDefinition, "defaultObject", "myValue");
}
 
Example 13
Source Project: shardingsphere   Source File: DataSourceMapSetter.java    License: Apache License 2.0 5 votes vote down vote up
private static DataSource getJNDIDataSource(final String jndiName) throws NamingException {
    JndiObjectFactoryBean bean = new JndiObjectFactoryBean();
    bean.setResourceRef(true);
    bean.setJndiName(jndiName);
    bean.setProxyInterface(DataSource.class);
    bean.afterPropertiesSet();
    return (DataSource) bean.getObject();
}
 
Example 14
Source Project: Thunder   Source File: MQJndiHierachy.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void initialize() throws Exception {
    super.initialize();

    String jndiInitialContextFactoryClass = mqPropertyEntity.getMQEntity().getJndiInitialContextFactoryClass();
    String url = mqPropertyEntity.getString(ThunderConstant.MQ_URL_ATTRIBUTE_NAME);
    String userName = mqPropertyEntity.getString(ThunderConstant.MQ_USER_NAME_ATTRIBUTE_NAME);
    String password = mqPropertyEntity.getString(ThunderConstant.MQ_PASSWORD_ATTRIBUTE_NAME);
    String jndiName = mqPropertyEntity.getString(ThunderConstant.MQ_JNDI_NAME_ATTRIBUTE_NAME);

    Properties environment = new Properties();
    environment.put("java.naming.factory.initial", jndiInitialContextFactoryClass);
    environment.put("java.naming.provider.url", url);
    environment.put("java.naming.security.principal", userName);
    environment.put("java.naming.security.credentials", password);

    JndiTemplate jndiTemplate = new JndiTemplate();
    jndiTemplate.setEnvironment(environment);

    JndiObjectFactoryBean targetConnectionFactory = new JndiObjectFactoryBean();
    targetConnectionFactory.setJndiTemplate(jndiTemplate);
    targetConnectionFactory.setJndiName(jndiName);
    targetConnectionFactory.setLookupOnStartup(true);
    targetConnectionFactory.afterPropertiesSet();

    setTargetConnectionFactory((ConnectionFactory) targetConnectionFactory.getObject());

    afterPropertiesSet();
}
 
Example 15
Source Project: wildfly-camel   Source File: SpringCamelContextBootstrap.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Gets JNDI names for all configured instances of {@link JndiObjectFactoryBean}
 *
 * Note: If this method is invoked before ApplicationContext.refresh() then any bean property
 * values containing property placeholders will not be resolved.
 *
 * @return the unmodifiable list of JNDI binding names
 */
public List<String> getJndiNames() {
    List<String> bindings = new ArrayList<>();
    for(String beanName : applicationContext.getBeanDefinitionNames()) {
        BeanDefinition definition = applicationContext.getBeanDefinition(beanName);
        String beanClassName = definition.getBeanClassName();

        if (beanClassName != null && beanClassName.equals(JndiObjectFactoryBean.class.getName())) {
            MutablePropertyValues propertyValues = definition.getPropertyValues();
            Object jndiPropertyValue = propertyValues.get("jndiName");

            if (jndiPropertyValue == null) {
                LOGGER.debug("Skipping JNDI binding dependency for bean: {}", beanName);
                continue;
            }

            String jndiName = null;
            if (jndiPropertyValue instanceof String) {
                jndiName = (String) jndiPropertyValue;
            } else if (jndiPropertyValue instanceof TypedStringValue) {
                jndiName = ((TypedStringValue) jndiPropertyValue).getValue();
            } else {
                LOGGER.debug("Ignoring unknown JndiObjectFactoryBean property value type {}", jndiPropertyValue.getClass().getSimpleName());
            }

            if (jndiName != null) {
                bindings.add(jndiName);
            }
        }
    }
    return Collections.unmodifiableList(bindings);
}
 
Example 16
Source Project: quartz-glass   Source File: SpringConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public DataSource dataSource() throws Exception {
    if (configuration().isInMemory()) return null;

    JndiObjectFactoryBean factoryBean = new JndiObjectFactoryBean();
    factoryBean.setJndiName("java:comp/env/jdbc/glassDb");
    factoryBean.afterPropertiesSet();

    return (DataSource) factoryBean.getObject();
}
 
Example 17
@Bean
public JndiObjectFactoryBean dataSource() {
    JndiObjectFactoryBean jpfb = new JndiObjectFactoryBean();
    jpfb.setJndiName("jdbc/cerberus"+ System.getProperty(Property.ENVIRONMENT));
    jpfb.setResourceRef(true);
    return jpfb;
}
 
Example 18
@Override
protected Class<?> getBeanClass(Element element) {
	return JndiObjectFactoryBean.class;
}
 
Example 19
@Override
protected Class<?> getBeanClass(Element element) {
	return JndiObjectFactoryBean.class;
}
 
Example 20
@Override
protected Class<?> getBeanClass(Element element) {
	return JndiObjectFactoryBean.class;
}
 
Example 21
@Override
protected Class<?> getBeanClass(Element element) {
	return JndiObjectFactoryBean.class;
}
 
Example 22
Source Project: syncope   Source File: DomainConfFactory.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void register(final Domain domain) {
    HikariConfig hikariConfig = new HikariConfig();
    hikariConfig.setDriverClassName(domain.getJdbcDriver());
    hikariConfig.setJdbcUrl(domain.getJdbcURL());
    hikariConfig.setUsername(domain.getDbUsername());
    hikariConfig.setPassword(domain.getDbPassword());
    hikariConfig.setSchema(domain.getDbSchema());
    hikariConfig.setTransactionIsolation(domain.getTransactionIsolation().name());
    hikariConfig.setMaximumPoolSize(domain.getPoolMaxActive());
    hikariConfig.setMinimumIdle(domain.getPoolMinIdle());

    // domainDataSource
    registerBeanDefinition(
            domain.getKey() + "DataSource",
            BeanDefinitionBuilder.rootBeanDefinition(JndiObjectFactoryBean.class).
                    addPropertyValue("jndiName", "java:comp/env/jdbc/syncope" + domain.getKey() + "DataSource").
                    addPropertyValue("defaultObject", new HikariDataSource(hikariConfig)).
                    getBeanDefinition());
    DataSource initedDataSource = ApplicationContextProvider.getBeanFactory().
            getBean(domain.getKey() + "DataSource", DataSource.class);

    // domainResourceDatabasePopulator
    ResourceDatabasePopulator databasePopulator = new ResourceDatabasePopulator();
    databasePopulator.setContinueOnError(true);
    databasePopulator.setIgnoreFailedDrops(true);
    databasePopulator.setSqlScriptEncoding(StandardCharsets.UTF_8.name());
    databasePopulator.addScript(new ClassPathResource("/audit/" + domain.getAuditSql()));
    registerSingleton(domain.getKey().toLowerCase() + "ResourceDatabasePopulator", databasePopulator);

    // domainDataSourceInitializer
    DataSourceInitializer dataSourceInitializer = new DataSourceInitializer();
    dataSourceInitializer.setDataSource(initedDataSource);
    dataSourceInitializer.setEnabled(true);
    dataSourceInitializer.setDatabasePopulator(databasePopulator);
    registerSingleton(domain.getKey().toLowerCase() + "DataSourceInitializer", dataSourceInitializer);

    // domainEntityManagerFactory
    OpenJpaVendorAdapter vendorAdapter = new OpenJpaVendorAdapter();
    vendorAdapter.setShowSql(false);
    vendorAdapter.setGenerateDdl(true);
    vendorAdapter.setDatabasePlatform(domain.getDatabasePlatform());

    BeanDefinitionBuilder emf = BeanDefinitionBuilder.rootBeanDefinition(DomainEntityManagerFactoryBean.class).
            addPropertyValue("mappingResources", domain.getOrm()).
            addPropertyValue("persistenceUnitName", domain.getKey()).
            addPropertyReference("dataSource", domain.getKey() + "DataSource").
            addPropertyValue("jpaVendorAdapter", vendorAdapter).
            addPropertyReference("commonEntityManagerFactoryConf", "commonEMFConf");
    if (env.containsProperty("openjpaMetaDataFactory")) {
        emf.addPropertyValue("jpaPropertyMap", Map.of(
                "openjpa.MetaDataFactory",
                Objects.requireNonNull(env.getProperty("openjpaMetaDataFactory")).
                        replace("##orm##", domain.getOrm())));
    }
    registerBeanDefinition(domain.getKey() + "EntityManagerFactory", emf.getBeanDefinition());
    ApplicationContextProvider.getBeanFactory().getBean(domain.getKey() + "EntityManagerFactory");

    // domainTransactionManager
    AbstractBeanDefinition domainTransactionManager =
            BeanDefinitionBuilder.rootBeanDefinition(JpaTransactionManager.class).
                    addPropertyReference("entityManagerFactory", domain.getKey() + "EntityManagerFactory").
                    getBeanDefinition();
    domainTransactionManager.addQualifier(new AutowireCandidateQualifier(Qualifier.class, domain.getKey()));
    registerBeanDefinition(domain.getKey() + "TransactionManager", domainTransactionManager);

    // domainContentXML
    registerBeanDefinition(domain.getKey() + "ContentXML",
            BeanDefinitionBuilder.rootBeanDefinition(ByteArrayInputStream.class).
                    addConstructorArgValue(domain.getContent().getBytes()).
                    getBeanDefinition());

    // domainKeymasterConfParamsJSON
    registerBeanDefinition(domain.getKey() + "KeymasterConfParamsJSON",
            BeanDefinitionBuilder.rootBeanDefinition(ByteArrayInputStream.class).
                    addConstructorArgValue(domain.getKeymasterConfParams().getBytes()).
                    getBeanDefinition());
}