1 16 17 package org.springframework.dao.support; 18 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import org.aopalliance.intercept.MethodInterceptor; 23 import org.aopalliance.intercept.MethodInvocation; 24 25 import org.springframework.beans.BeansException; 26 import org.springframework.beans.factory.BeanFactory; 27 import org.springframework.beans.factory.BeanFactoryAware; 28 import org.springframework.beans.factory.BeanFactoryUtils; 29 import org.springframework.beans.factory.InitializingBean; 30 import org.springframework.beans.factory.ListableBeanFactory; 31 import org.springframework.util.Assert; 32 33 47 public class PersistenceExceptionTranslationInterceptor 48 implements MethodInterceptor, BeanFactoryAware, InitializingBean { 49 50 private PersistenceExceptionTranslator persistenceExceptionTranslator; 51 52 53 58 public PersistenceExceptionTranslationInterceptor() { 59 } 60 61 66 public PersistenceExceptionTranslationInterceptor(PersistenceExceptionTranslator persistenceExceptionTranslator) { 67 setPersistenceExceptionTranslator(persistenceExceptionTranslator); 68 } 69 70 76 public PersistenceExceptionTranslationInterceptor(ListableBeanFactory beanFactory) { 77 this.persistenceExceptionTranslator = detectPersistenceExceptionTranslators(beanFactory); 78 } 79 80 81 87 public void setPersistenceExceptionTranslator(PersistenceExceptionTranslator pet) { 88 Assert.notNull(pet, "PersistenceExceptionTranslator must not be null"); 89 this.persistenceExceptionTranslator = pet; 90 } 91 92 public void setBeanFactory(BeanFactory beanFactory) throws BeansException { 93 if (this.persistenceExceptionTranslator == null) { 94 if (!(beanFactory instanceof ListableBeanFactory)) { 96 throw new IllegalArgumentException ( 97 "Cannot use PersistenceExceptionTranslator autodetection without ListableBeanFactory"); 98 } 99 this.persistenceExceptionTranslator = 100 detectPersistenceExceptionTranslators((ListableBeanFactory) beanFactory); 101 } 102 } 103 104 public void afterPropertiesSet() { 105 if (this.persistenceExceptionTranslator == null) { 106 throw new IllegalArgumentException ("Property 'persistenceExceptionTranslator' is required"); 107 } 108 } 109 110 111 119 protected PersistenceExceptionTranslator detectPersistenceExceptionTranslators(ListableBeanFactory beanFactory) { 120 Map pets = BeanFactoryUtils.beansOfTypeIncludingAncestors( 122 beanFactory, PersistenceExceptionTranslator.class, false, false); 123 if (pets.isEmpty()) { 124 throw new IllegalStateException ( 125 "No persistence exception translators found in bean factory. Cannot perform exception translation."); 126 } 127 ChainedPersistenceExceptionTranslator cpet = new ChainedPersistenceExceptionTranslator(); 128 for (Iterator it = pets.values().iterator(); it.hasNext();) { 129 cpet.addDelegate((PersistenceExceptionTranslator) it.next()); 130 } 131 return cpet; 132 } 133 134 135 public Object invoke(MethodInvocation mi) throws Throwable { 136 try { 137 return mi.proceed(); 138 } 139 catch (RuntimeException ex) { 140 Class [] declaredExceptions = mi.getMethod().getExceptionTypes(); 142 for (int i = 0; i < declaredExceptions.length; i++) { 143 if (declaredExceptions[i].isInstance(ex)) { 144 throw ex; 145 } 146 } 147 throw DataAccessUtils.translateIfNecessary(ex, this.persistenceExceptionTranslator); 148 } 149 } 150 151 } 152 | Popular Tags |