org.springframework.jndi.JndiObjectFactoryBean Java Examples

The following examples show how to use org.springframework.jndi.JndiObjectFactoryBean. 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
Source File: ShardingSpringBootConfiguration.java    From seata-samples with Apache License 2.0 6 votes vote down vote up
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
Source File: JeeNamespaceHandlerTests.java    From java-technology-stack with 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 #3
Source File: MQJndiHierachy.java    From Thunder with 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 #4
Source File: DataSourceMapSetter.java    From shardingsphere with 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 #5
Source File: JeeNamespaceHandlerTests.java    From spring4-understanding with Apache License 2.0 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 #6
Source File: JeeNamespaceHandlerTests.java    From spring4-understanding with Apache License 2.0 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 #7
Source File: SpringCamelContextBootstrap.java    From wildfly-camel with 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 #8
Source File: MBeanServerBeanDefinitionParser.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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 #9
Source File: MBeanServerBeanDefinitionParser.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #10
Source File: MBeanServerBeanDefinitionParser.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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
Source File: JeeNamespaceHandlerTests.java    From java-technology-stack with 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 #12
Source File: SpringConfig.java    From quartz-glass with 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 #13
Source File: MBeanServerBeanDefinitionParser.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #14
Source File: DataSourceConfig.java    From Project with 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 #15
Source File: JeeNamespaceHandlerTests.java    From spring-analysis-note with 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 #16
Source File: JeeNamespaceHandlerTests.java    From spring-analysis-note with 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 #17
Source File: CerberusConfiguration.java    From cerberus-source with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public JndiObjectFactoryBean dataSource() {
    JndiObjectFactoryBean jpfb = new JndiObjectFactoryBean();
    jpfb.setJndiName("jdbc/cerberus"+ System.getProperty(Property.ENVIRONMENT));
    jpfb.setResourceRef(true);
    return jpfb;
}
 
Example #18
Source File: JndiLookupBeanDefinitionParser.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected Class<?> getBeanClass(Element element) {
	return JndiObjectFactoryBean.class;
}
 
Example #19
Source File: DomainConfFactory.java    From syncope with 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());
}
 
Example #20
Source File: JndiLookupBeanDefinitionParser.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
protected Class<?> getBeanClass(Element element) {
	return JndiObjectFactoryBean.class;
}
 
Example #21
Source File: JndiLookupBeanDefinitionParser.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
protected Class<?> getBeanClass(Element element) {
	return JndiObjectFactoryBean.class;
}
 
Example #22
Source File: JndiLookupBeanDefinitionParser.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
protected Class<?> getBeanClass(Element element) {
	return JndiObjectFactoryBean.class;
}