Browse Source

DATACMNS-318 - Refined persistence exception translation infrastructure.

Extracted the activation of persistence exception translation into a separate RepositoryProxyPostProcessor. Adapted TransactionalRepositoryFactoryBeanSupport to also register the newly introduced PersistenceExceptionTranslationRepositoryProxyPostProcessor.
pull/28/head
Oliver Gierke 13 years ago
parent
commit
4303fdf399
  1. 55
      src/main/java/org/springframework/data/repository/core/support/PersistenceExceptionTranslationRepositoryProxyPostProcessor.java
  2. 19
      src/main/java/org/springframework/data/repository/core/support/TransactionalRepositoryFactoryBeanSupport.java
  3. 24
      src/main/java/org/springframework/data/repository/core/support/TransactionalRepositoryProxyPostProcessor.java
  4. 73
      src/test/java/org/springframework/data/repository/core/support/PersistenceExceptionTranslationRepositoryProxyPostProcessorUnitTests.java
  5. 15
      src/test/java/org/springframework/data/repository/core/support/TransactionRepositoryProxyPostProcessorUnitTests.java

55
src/main/java/org/springframework/data/repository/core/support/PersistenceExceptionTranslationRepositoryProxyPostProcessor.java

@ -0,0 +1,55 @@ @@ -0,0 +1,55 @@
/*
* Copyright 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.data.repository.core.support;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.dao.support.PersistenceExceptionTranslationInterceptor;
import org.springframework.util.Assert;
/**
* {@link RepositoryProxyPostProcessor} to register a {@link PersistenceExceptionTranslationInterceptor} on the
* repository proxy.
*
* @author Oliver Gierke
*/
public class PersistenceExceptionTranslationRepositoryProxyPostProcessor implements RepositoryProxyPostProcessor {
private final PersistenceExceptionTranslationInterceptor interceptor;
/**
* Creates a new {@link PersistenceExceptionTranslationRepositoryProxyPostProcessor} using the given
* {@link ListableBeanFactory}.
*
* @param beanFactory must not be {@literal null}.
*/
public PersistenceExceptionTranslationRepositoryProxyPostProcessor(ListableBeanFactory beanFactory) {
Assert.notNull(beanFactory, "BeanFactory must not be null!");
this.interceptor = new PersistenceExceptionTranslationInterceptor();
this.interceptor.setBeanFactory(beanFactory);
this.interceptor.afterPropertiesSet();
}
/*
* (non-Javadoc)
* @see org.springframework.data.repository.core.support.RepositoryProxyPostProcessor#postProcess(org.springframework.aop.framework.ProxyFactory)
*/
public void postProcess(ProxyFactory factory) {
factory.addAdvice(interceptor);
}
}

19
src/main/java/org/springframework/data/repository/core/support/TransactionalRepositoryFactoryBeanSupport.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2008-2010 the original author or authors.
* Copyright 2008-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.
@ -37,6 +37,7 @@ public abstract class TransactionalRepositoryFactoryBeanSupport<T extends Reposi @@ -37,6 +37,7 @@ public abstract class TransactionalRepositoryFactoryBeanSupport<T extends Reposi
private String transactionManagerName = TxUtils.DEFAULT_TRANSACTION_MANAGER;
private RepositoryProxyPostProcessor txPostProcessor;
private RepositoryProxyPostProcessor exceptionPostProcessor;
/**
* Setter to configure which transaction manager to be used. We have to use the bean name explicitly as otherwise the
@ -46,7 +47,6 @@ public abstract class TransactionalRepositoryFactoryBeanSupport<T extends Reposi @@ -46,7 +47,6 @@ public abstract class TransactionalRepositoryFactoryBeanSupport<T extends Reposi
* @param transactionManager
*/
public void setTransactionManager(String transactionManager) {
this.transactionManagerName = transactionManager == null ? TxUtils.DEFAULT_TRANSACTION_MANAGER : transactionManager;
}
@ -60,6 +60,7 @@ public abstract class TransactionalRepositoryFactoryBeanSupport<T extends Reposi @@ -60,6 +60,7 @@ public abstract class TransactionalRepositoryFactoryBeanSupport<T extends Reposi
protected final RepositoryFactorySupport createRepositoryFactory() {
RepositoryFactorySupport factory = doCreateRepositoryFactory();
factory.addRepositoryProxyPostProcessor(exceptionPostProcessor);
factory.addRepositoryProxyPostProcessor(txPostProcessor);
return factory;
}
@ -72,17 +73,15 @@ public abstract class TransactionalRepositoryFactoryBeanSupport<T extends Reposi @@ -72,17 +73,15 @@ public abstract class TransactionalRepositoryFactoryBeanSupport<T extends Reposi
protected abstract RepositoryFactorySupport doCreateRepositoryFactory();
/*
* (non-Javadoc)
*
* @see
* org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org
* .springframework.beans.factory.BeanFactory)
*/
* (non-Javadoc)
* @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org.springframework.beans.factory.BeanFactory)
*/
public void setBeanFactory(BeanFactory beanFactory) {
Assert.isInstanceOf(ListableBeanFactory.class, beanFactory);
this.txPostProcessor = new TransactionalRepositoryProxyPostProcessor((ListableBeanFactory) beanFactory,
transactionManagerName);
ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory;
this.txPostProcessor = new TransactionalRepositoryProxyPostProcessor(listableBeanFactory, transactionManagerName);
this.exceptionPostProcessor = new PersistenceExceptionTranslationRepositoryProxyPostProcessor(listableBeanFactory);
}
}

24
src/main/java/org/springframework/data/repository/core/support/TransactionalRepositoryProxyPostProcessor.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2008-2010 the original author or authors.
* Copyright 2008-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.
@ -53,20 +53,19 @@ import org.springframework.util.ObjectUtils; @@ -53,20 +53,19 @@ import org.springframework.util.ObjectUtils;
class TransactionalRepositoryProxyPostProcessor implements RepositoryProxyPostProcessor {
private final TransactionInterceptor transactionInterceptor;
private final PersistenceExceptionTranslationInterceptor petInterceptor;
/**
* Creates a new {@link TransactionalRepositoryProxyPostProcessor}.
* Creates a new {@link TransactionalRepositoryProxyPostProcessor} using the given {@link ListableBeanFactory} and
* transaction manager bean name.
*
* @param beanFactory must not be {@literal null}.
* @param transactionManagerName must not be {@literal null} or empty.
*/
public TransactionalRepositoryProxyPostProcessor(ListableBeanFactory beanFactory, String transactionManagerName) {
Assert.notNull(beanFactory);
Assert.notNull(transactionManagerName);
this.petInterceptor = new PersistenceExceptionTranslationInterceptor();
this.petInterceptor.setBeanFactory(beanFactory);
this.petInterceptor.afterPropertiesSet();
this.transactionInterceptor = new TransactionInterceptor(null, new CustomAnnotationTransactionAttributeSource());
this.transactionInterceptor.setTransactionManagerBeanName(transactionManagerName);
this.transactionInterceptor.setBeanFactory(beanFactory);
@ -74,15 +73,10 @@ class TransactionalRepositoryProxyPostProcessor implements RepositoryProxyPostPr @@ -74,15 +73,10 @@ class TransactionalRepositoryProxyPostProcessor implements RepositoryProxyPostPr
}
/*
* (non-Javadoc)
*
* @see
* org.springframework.data.repository.support.RepositoryProxyPostProcessor
* #postProcess(org.springframework.aop.framework.ProxyFactory)
*/
* (non-Javadoc)
* @see org.springframework.data.repository.core.support.RepositoryProxyPostProcessor#postProcess(org.springframework.aop.framework.ProxyFactory)
*/
public void postProcess(ProxyFactory factory) {
factory.addAdvice(petInterceptor);
factory.addAdvice(transactionInterceptor);
}

73
src/test/java/org/springframework/data/repository/core/support/PersistenceExceptionTranslationRepositoryProxyPostProcessorUnitTests.java

@ -0,0 +1,73 @@ @@ -0,0 +1,73 @@
/*
* Copyright 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.data.repository.core.support;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.util.HashMap;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.dao.support.PersistenceExceptionTranslationInterceptor;
import org.springframework.dao.support.PersistenceExceptionTranslator;
/**
* Unit test for {@link TransactionalRepositoryProxyPostProcessor}.
*
* @author Oliver Gierke
* @since 1.6
*/
@RunWith(MockitoJUnitRunner.class)
public class PersistenceExceptionTranslationRepositoryProxyPostProcessorUnitTests {
@Mock
ListableBeanFactory beanFactory;
@Mock
ProxyFactory proxyFactory;
@Before
public void setUp() {
Map<String, PersistenceExceptionTranslator> beans = new HashMap<String, PersistenceExceptionTranslator>();
beans.put("foo", mock(PersistenceExceptionTranslator.class));
when(beanFactory.getBeansOfType(eq(PersistenceExceptionTranslator.class), anyBoolean(), anyBoolean())).thenReturn(
beans);
}
@Test(expected = IllegalArgumentException.class)
public void rejectsNullBeanFactory() throws Exception {
new PersistenceExceptionTranslationRepositoryProxyPostProcessor(null);
}
@Test
public void setsUpBasicInstance() throws Exception {
RepositoryProxyPostProcessor postProcessor = new PersistenceExceptionTranslationRepositoryProxyPostProcessor(
beanFactory);
postProcessor.postProcess(proxyFactory);
verify(proxyFactory).addAdvice(isA(PersistenceExceptionTranslationInterceptor.class));
}
}

15
src/test/java/org/springframework/data/repository/core/support/TransactionRepositoryProxyPostProcessorUnitTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2008-2010 the original author or authors.
* Copyright 2008-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
@ -15,9 +15,7 @@ @@ -15,9 +15,7 @@
*/
package org.springframework.data.repository.core.support;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isA;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.util.HashMap;
@ -30,10 +28,7 @@ import org.mockito.Mock; @@ -30,10 +28,7 @@ import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.dao.support.PersistenceExceptionTranslationInterceptor;
import org.springframework.dao.support.PersistenceExceptionTranslator;
import org.springframework.data.repository.core.support.RepositoryProxyPostProcessor;
import org.springframework.data.repository.core.support.TransactionalRepositoryProxyPostProcessor;
import org.springframework.transaction.interceptor.TransactionInterceptor;
/**
@ -44,8 +39,6 @@ import org.springframework.transaction.interceptor.TransactionInterceptor; @@ -44,8 +39,6 @@ import org.springframework.transaction.interceptor.TransactionInterceptor;
@RunWith(MockitoJUnitRunner.class)
public class TransactionRepositoryProxyPostProcessorUnitTests {
TransactionalRepositoryProxyPostProcessor processor;
@Mock
ListableBeanFactory beanFactory;
@Mock
@ -62,13 +55,11 @@ public class TransactionRepositoryProxyPostProcessorUnitTests { @@ -62,13 +55,11 @@ public class TransactionRepositoryProxyPostProcessorUnitTests {
@Test(expected = IllegalArgumentException.class)
public void rejectsNullBeanFactory() throws Exception {
new TransactionalRepositoryProxyPostProcessor(null, "transactionManager");
}
@Test(expected = IllegalArgumentException.class)
public void rejectsNullTxManagerName() throws Exception {
new TransactionalRepositoryProxyPostProcessor(beanFactory, null);
}
@ -76,10 +67,8 @@ public class TransactionRepositoryProxyPostProcessorUnitTests { @@ -76,10 +67,8 @@ public class TransactionRepositoryProxyPostProcessorUnitTests {
public void setsUpBasicInstance() throws Exception {
RepositoryProxyPostProcessor postProcessor = new TransactionalRepositoryProxyPostProcessor(beanFactory, "txManager");
postProcessor.postProcess(proxyFactory);
verify(proxyFactory).addAdvice(isA(PersistenceExceptionTranslationInterceptor.class));
verify(proxyFactory).addAdvice(isA(TransactionInterceptor.class));
}
}

Loading…
Cancel
Save