LDAP-267: Implemented support for DataSource and Hibernate integration with namespace configuration.

This commit is contained in:
Mattias Hellborg Arthursson
2013-10-09 08:14:10 +02:00
parent e43233fd86
commit 41fc6306ae
8 changed files with 955 additions and 1 deletions

View File

@@ -22,6 +22,8 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.ldap.transaction.compensating.manager.ContextSourceAndDataSourceTransactionManager;
import org.springframework.ldap.transaction.compensating.manager.ContextSourceAndHibernateTransactionManager;
import org.springframework.ldap.transaction.compensating.manager.ContextSourceTransactionManager;
import org.springframework.ldap.transaction.compensating.support.DefaultTempEntryRenamingStrategy;
import org.springframework.ldap.transaction.compensating.support.DifferentSubtreeTempEntryRenamingStrategy;
@@ -58,7 +60,18 @@ public class TransactionManagerParser implements BeanDefinitionParser {
ATT_DATA_SOURCE_REF, ATT_SESSION_FACTORY_REF));
}
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(ContextSourceTransactionManager.class);
BeanDefinitionBuilder builder;
if(StringUtils.hasText(dataSourceRef)) {
builder = BeanDefinitionBuilder.rootBeanDefinition(ContextSourceAndDataSourceTransactionManager.class);
builder.addPropertyReference("dataSource", dataSourceRef);
} else if(StringUtils.hasText(sessionFactoryRef)) {
builder = BeanDefinitionBuilder.rootBeanDefinition(ContextSourceAndHibernateTransactionManager.class);
builder.addPropertyReference("sessionFactory", sessionFactoryRef);
} else {
// Standard transaction manager
builder = BeanDefinitionBuilder.rootBeanDefinition(ContextSourceTransactionManager.class);
}
builder.addPropertyReference("contextSource", contextSourceRef);
Element defaultStrategyChild = DomUtils.getChildElementByTagName(element, Elements.DEFAULT_RENAMING_STRATEGY);

View File

@@ -29,6 +29,7 @@ import org.springframework.ldap.pool.factory.PoolingContextSource;
import org.springframework.ldap.pool.validation.DefaultDirContextValidator;
import org.springframework.ldap.support.LdapUtils;
import org.springframework.ldap.transaction.compensating.TempEntryRenamingStrategy;
import org.springframework.ldap.transaction.compensating.manager.ContextSourceAndDataSourceTransactionManager;
import org.springframework.ldap.transaction.compensating.manager.ContextSourceTransactionManager;
import org.springframework.ldap.transaction.compensating.manager.TransactionAwareContextSourceProxy;
import org.springframework.ldap.transaction.compensating.support.DefaultTempEntryRenamingStrategy;
@@ -177,6 +178,14 @@ public class LdapTemplateNamespaceHandlerTest {
assertEquals("_temp", getInternalState(renamingStrategy, "tempSuffix"));
}
@Test
public void verifyParseTransactionWithDataSource() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-transactional-datasource.xml");
PlatformTransactionManager transactionManager = ctx.getBean(PlatformTransactionManager.class);
assertTrue(transactionManager instanceof ContextSourceAndDataSourceTransactionManager);
}
@Test
public void verifyParseTransactionsWithDefaultStrategyAndSuffix() {
ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-transactional-defaults-with-suffix.xml");

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.ldap.config;
import org.springframework.beans.factory.config.AbstractFactoryBean;
import static org.mockito.Mockito.mock;
/**
* @author Mattias Hellborg Arthursson
*/
public class MockFactoryBean extends AbstractFactoryBean<Object> {
private final Class<?> clazz;
public MockFactoryBean(Class<?> clazz) {
this.clazz = clazz;
}
@Override
public Class<?> getObjectType() {
return clazz;
}
@Override
protected Object createInstance() throws Exception {
return mock(clazz);
}
}

View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2005-2013 the original author or authors.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ldap="http://www.springframework.org/schema/ldap"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/ldap http://www.springframework.org/schema/ldap/spring-ldap.xsd">
<ldap:context-source password="apassword" url="ldap://localhost:389" username="uid=admin"/>
<ldap:ldap-template />
<ldap:transaction-manager data-source-ref="dataSource">
<ldap:default-renaming-strategy />
</ldap:transaction-manager>
<bean id="dataSource" class="org.springframework.ldap.config.MockFactoryBean">
<constructor-arg value="javax.sql.DataSource" />
</bean>
</beans>