prospring3.ch5.beanpostprocessor.DeassociatePointcutAdvisor.java Source code

Java tutorial

Introduction

Here is the source code for prospring3.ch5.beanpostprocessor.DeassociatePointcutAdvisor.java

Source

package prospring3.ch5.beanpostprocessor;

import org.aopalliance.aop.Advice;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.AbstractPointcutAdvisor;
import org.springframework.aop.support.ComposablePointcut;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;

import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * ***********************************************************************
 * <p/>
 * Copyright:
 * 2012 1&1 Internet AG, Germany, http://www.1und1.de
 * <p/>
 * License:
 * commercial
 * <p/>
 * Authors:
 * Antonel (Tony) Pazargic (antonel.pazargic@1and1.ro)
 * <p/>
 * Date: 5/14/12
 * Time: 10:17 AM
 * <p/>
 * ***********************************************************************
 */
public class DeassociatePointcutAdvisor extends AbstractPointcutAdvisor {

    private Advice advice;

    private Pointcut pointcut;

    public DeassociatePointcutAdvisor() {
        this.advice = buildAdvice();
        Set<Class<? extends Annotation>> annotations = new LinkedHashSet<Class<? extends Annotation>>();
        annotations.add(Deassociate.class);
        this.pointcut = buildPointcut(annotations);
    }

    public void setAnnotationType(Class<? extends Annotation> annotationType) {
        Set<Class<? extends Annotation>> annotationTypes = new HashSet<Class<? extends Annotation>>();
        annotationTypes.add(annotationType);
        this.pointcut = buildPointcut(annotationTypes);
    }

    private Pointcut buildPointcut(Set<Class<? extends Annotation>> deassociateAnnotationTypes) {
        ComposablePointcut result = null;
        for (Class<? extends Annotation> deassociateAnnotationType : deassociateAnnotationTypes) {
            Pointcut mpc = new AnnotationMatchingPointcut(null, deassociateAnnotationType);
            if (result == null) {
                result = new ComposablePointcut(mpc);
            } else {
                result.union(mpc);
            }
        }
        return result;
    }

    private Advice buildAdvice() {
        return new DeassociateInterceptor();
    }

    public Advice getAdvice() {
        return this.advice;
    }

    public Pointcut getPointcut() {
        return pointcut;
    }
}