001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.dao.annotation;
018
019import java.lang.annotation.Annotation;
020
021import org.aopalliance.aop.Advice;
022
023import org.springframework.aop.Pointcut;
024import org.springframework.aop.support.AbstractPointcutAdvisor;
025import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;
026import org.springframework.beans.factory.ListableBeanFactory;
027import org.springframework.dao.support.PersistenceExceptionTranslationInterceptor;
028import org.springframework.dao.support.PersistenceExceptionTranslator;
029
030/**
031 * Spring AOP exception translation aspect for use at Repository or DAO layer level.
032 * Translates native persistence exceptions into Spring's DataAccessException hierarchy,
033 * based on a given PersistenceExceptionTranslator.
034 *
035 * @author Rod Johnson
036 * @author Juergen Hoeller
037 * @since 2.0
038 * @see org.springframework.dao.DataAccessException
039 * @see org.springframework.dao.support.PersistenceExceptionTranslator
040 */
041@SuppressWarnings("serial")
042public class PersistenceExceptionTranslationAdvisor extends AbstractPointcutAdvisor {
043
044        private final PersistenceExceptionTranslationInterceptor advice;
045
046        private final AnnotationMatchingPointcut pointcut;
047
048
049        /**
050         * Create a new PersistenceExceptionTranslationAdvisor.
051         * @param persistenceExceptionTranslator the PersistenceExceptionTranslator to use
052         * @param repositoryAnnotationType the annotation type to check for
053         */
054        public PersistenceExceptionTranslationAdvisor(
055                        PersistenceExceptionTranslator persistenceExceptionTranslator,
056                        Class<? extends Annotation> repositoryAnnotationType) {
057
058                this.advice = new PersistenceExceptionTranslationInterceptor(persistenceExceptionTranslator);
059                this.pointcut = new AnnotationMatchingPointcut(repositoryAnnotationType, true);
060        }
061
062        /**
063         * Create a new PersistenceExceptionTranslationAdvisor.
064         * @param beanFactory the ListableBeanFactory to obtaining all
065         * PersistenceExceptionTranslators from
066         * @param repositoryAnnotationType the annotation type to check for
067         */
068        PersistenceExceptionTranslationAdvisor(
069                        ListableBeanFactory beanFactory, Class<? extends Annotation> repositoryAnnotationType) {
070
071                this.advice = new PersistenceExceptionTranslationInterceptor(beanFactory);
072                this.pointcut = new AnnotationMatchingPointcut(repositoryAnnotationType, true);
073        }
074
075
076        @Override
077        public Advice getAdvice() {
078                return this.advice;
079        }
080
081        @Override
082        public Pointcut getPointcut() {
083                return this.pointcut;
084        }
085
086}