KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > dao > support > PersistenceExceptionTranslationInterceptor


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.dao.support;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
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 /**
34  * AOP Alliance MethodInterceptor that provides persistence exception translation
35  * based on a given PersistenceExceptionTranslator.
36  *
37  * <p>Delegates to the given {@link PersistenceExceptionTranslator} to translate
38  * a RuntimeException thrown into Spring's DataAccessException hierarchy
39  * (if appropriate). If the RuntimeException in question is declared on the
40  * target method, it is always propagated as-is (with no translation applied).
41  *
42  * @author Rod Johnson
43  * @author Juergen Hoeller
44  * @since 2.0
45  * @see PersistenceExceptionTranslator
46  */

47 public class PersistenceExceptionTranslationInterceptor
48         implements MethodInterceptor, BeanFactoryAware, InitializingBean {
49
50     private PersistenceExceptionTranslator persistenceExceptionTranslator;
51
52
53     /**
54      * Create a new PersistenceExceptionTranslationInterceptor.
55      * Needs to be configured with a PersistenceExceptionTranslator afterwards.
56      * @see #setPersistenceExceptionTranslator
57      */

58     public PersistenceExceptionTranslationInterceptor() {
59     }
60
61     /**
62      * Create a new PersistenceExceptionTranslationInterceptor
63      * for the given PersistenceExceptionTranslator.
64      * @param persistenceExceptionTranslator the PersistenceExceptionTranslator to use
65      */

66     public PersistenceExceptionTranslationInterceptor(PersistenceExceptionTranslator persistenceExceptionTranslator) {
67         setPersistenceExceptionTranslator(persistenceExceptionTranslator);
68     }
69
70     /**
71      * Create a new PersistenceExceptionTranslationInterceptor, autodetecting
72      * PersistenceExceptionTranslators in the given BeanFactory.
73      * @param beanFactory the ListableBeanFactory to obtaining all
74      * PersistenceExceptionTranslators from
75      */

76     public PersistenceExceptionTranslationInterceptor(ListableBeanFactory beanFactory) {
77         this.persistenceExceptionTranslator = detectPersistenceExceptionTranslators(beanFactory);
78     }
79
80
81     /**
82      * Specify the PersistenceExceptionTranslator to use.
83      * <p>Default is to autodetect all PersistenceExceptionTranslators
84      * in the containing BeanFactory, using them in a chain.
85      * @see #detectPersistenceExceptionTranslators
86      */

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             // No explicit exception translator specified - perform autodetection.
95
if (!(beanFactory instanceof ListableBeanFactory)) {
96                 throw new IllegalArgumentException JavaDoc(
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 JavaDoc("Property 'persistenceExceptionTranslator' is required");
107         }
108     }
109
110
111     /**
112      * Detect all PersistenceExceptionTranslators in the given BeanFactory.
113      * @param beanFactory the ListableBeanFactory to obtaining all
114      * PersistenceExceptionTranslators from
115      * @return a chained PersistenceExceptionTranslator, combining all
116      * PersistenceExceptionTranslators found in the factory
117      * @see ChainedPersistenceExceptionTranslator
118      */

119     protected PersistenceExceptionTranslator detectPersistenceExceptionTranslators(ListableBeanFactory beanFactory) {
120         // Find all translators, being careful not to activate FactoryBeans.
121
Map JavaDoc pets = BeanFactoryUtils.beansOfTypeIncludingAncestors(
122                 beanFactory, PersistenceExceptionTranslator.class, false, false);
123         if (pets.isEmpty()) {
124             throw new IllegalStateException JavaDoc(
125                     "No persistence exception translators found in bean factory. Cannot perform exception translation.");
126         }
127         ChainedPersistenceExceptionTranslator cpet = new ChainedPersistenceExceptionTranslator();
128         for (Iterator JavaDoc it = pets.values().iterator(); it.hasNext();) {
129             cpet.addDelegate((PersistenceExceptionTranslator) it.next());
130         }
131         return cpet;
132     }
133
134
135     public Object JavaDoc invoke(MethodInvocation mi) throws Throwable JavaDoc {
136         try {
137             return mi.proceed();
138         }
139         catch (RuntimeException JavaDoc ex) {
140             // Let it throw raw if the type of the exception is on the throws clause of the method.
141
Class JavaDoc[] 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