KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > dao > annotation > PersistenceExceptionTranslationPostProcessor


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.annotation;
18
19 import java.lang.annotation.Annotation JavaDoc;
20
21 import org.springframework.aop.framework.Advised;
22 import org.springframework.aop.framework.ProxyFactory;
23 import org.springframework.aop.support.AopUtils;
24 import org.springframework.beans.BeansException;
25 import org.springframework.beans.factory.BeanClassLoaderAware;
26 import org.springframework.beans.factory.BeanFactory;
27 import org.springframework.beans.factory.BeanFactoryAware;
28 import org.springframework.beans.factory.ListableBeanFactory;
29 import org.springframework.beans.factory.config.BeanPostProcessor;
30 import org.springframework.core.Ordered;
31 import org.springframework.stereotype.Repository;
32 import org.springframework.util.Assert;
33 import org.springframework.util.ClassUtils;
34
35 /**
36  * Bean post-processor that automatically applies persistence exception
37  * translation to any bean that carries the
38  * {@link org.springframework.stereotype.Repository} annotation,
39  * adding a corresponding {@link PersistenceExceptionTranslationAdvisor}
40  * to the exposed proxy (either an existing AOP proxy or a newly generated
41  * proxy that implements all of the target's interfaces).
42  *
43  * <p>Translates native resource exceptions to Spring's
44  * {@link org.springframework.dao.DataAccessException} hierarchy.
45  * Autodetects beans that implement the
46  * {@link org.springframework.dao.support.PersistenceExceptionTranslator}
47  * interface, which are subsequently asked to translate candidate exceptions.
48  *
49  * <p>All of Spring's applicable resource factories implement the
50  * <code>PersistenceExceptionTranslator</code> interface out of the box.
51  * As a consequence, all that is usually needed to enable automatic exception
52  * translation is marking all affected beans (such as DAOs) with the
53  * <code>Repository</code> annotation, along with defining this post-processor
54  * as bean in the application context.
55  *
56  * @author Rod Johnson
57  * @author Juergen Hoeller
58  * @since 2.0
59  * @see PersistenceExceptionTranslationAdvisor
60  * @see org.springframework.stereotype.Repository
61  * @see org.springframework.dao.DataAccessException
62  * @see org.springframework.dao.support.PersistenceExceptionTranslator
63  */

64 public class PersistenceExceptionTranslationPostProcessor
65         implements BeanPostProcessor, BeanClassLoaderAware, BeanFactoryAware, Ordered {
66
67     private Class JavaDoc<? extends Annotation JavaDoc> repositoryAnnotationType = Repository.class;
68
69     private ClassLoader JavaDoc beanClassLoader = ClassUtils.getDefaultClassLoader();
70
71     private PersistenceExceptionTranslationAdvisor persistenceExceptionTranslationAdvisor;
72
73
74     /**
75      * Set the 'repository' annotation type.
76      * The default required annotation type is the {@link Repository} annotation.
77      * <p>This setter property exists so that developers can provide their own
78      * (non-Spring-specific) annotation type to indicate that a class has a
79      * repository role.
80      * @param repositoryAnnotationType the desired annotation type
81      */

82     public void setRepositoryAnnotationType(Class JavaDoc<? extends Annotation JavaDoc> repositoryAnnotationType) {
83         Assert.notNull(repositoryAnnotationType, "'requiredAnnotationType' must not be null");
84         this.repositoryAnnotationType = repositoryAnnotationType;
85     }
86
87     public void setBeanClassLoader(ClassLoader JavaDoc classLoader) {
88         this.beanClassLoader = classLoader;
89     }
90
91     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
92         if (!(beanFactory instanceof ListableBeanFactory)) {
93             throw new IllegalArgumentException JavaDoc(
94                     "Cannot use PersistenceExceptionTranslator autodetection without ListableBeanFactory");
95         }
96         this.persistenceExceptionTranslationAdvisor = new PersistenceExceptionTranslationAdvisor(
97                 (ListableBeanFactory) beanFactory, this.repositoryAnnotationType);
98     }
99
100     public int getOrder() {
101         // This should run after all other post-processors, so that it can just add
102
// an advisor to existing proxies rather than double-proxy.
103
return LOWEST_PRECEDENCE;
104     }
105
106
107     public Object JavaDoc postProcessBeforeInitialization(Object JavaDoc bean, String JavaDoc beanName) throws BeansException {
108         return bean;
109     }
110
111     public Object JavaDoc postProcessAfterInitialization(Object JavaDoc bean, String JavaDoc beanName) throws BeansException {
112         Class JavaDoc<?> targetClass =
113                 (bean instanceof Advised ? ((Advised) bean).getTargetSource().getTargetClass() : bean.getClass());
114         if (targetClass == null) {
115             // Can't do much here
116
return bean;
117         }
118         
119         if (AopUtils.canApply(this.persistenceExceptionTranslationAdvisor, targetClass)) {
120             if (bean instanceof Advised) {
121                 ((Advised) bean).addAdvisor(this.persistenceExceptionTranslationAdvisor);
122                 return bean;
123             }
124             else {
125                 ProxyFactory pf = new ProxyFactory(bean);
126                 pf.addAdvisor(this.persistenceExceptionTranslationAdvisor);
127                 return pf.getProxy(this.beanClassLoader);
128             }
129         }
130         else {
131             // This is not a repository.
132
return bean;
133         }
134     }
135
136 }
137
Popular Tags