org.anyframe.iam.admin.common.web.JsonErrorAdvisingBeanPostProcessor.java Source code

Java tutorial

Introduction

Here is the source code for org.anyframe.iam.admin.common.web.JsonErrorAdvisingBeanPostProcessor.java

Source

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

package org.anyframe.iam.admin.common.web;

import java.lang.reflect.Method;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.ComposablePointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.RootClassFilter;
import org.springframework.aop.support.annotation.AnnotationMethodMatcher;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * Bean Post Processor to handle JsonError annotation
 * @author byounghoon.woo
 * 
 */
public class JsonErrorAdvisingBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware {

    private final Log log = LogFactory.getLog(this.getClass());

    private ApplicationContext context;

    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        log.debug("postProcessAfterInitialization with : " + beanName);
        return bean;
    }

    @SuppressWarnings("unchecked")
    public Object postProcessBeforeInitialization(final Object bean, String beanName) throws BeansException {

        //  ? bean ? 
        log.debug("postProcessBeforeInitialization with : " + beanName);

        Class clazz = bean.getClass();

        // JsonError Annotation ?  MethodMatcher
        AnnotationMethodMatcher annotationMethodMatcher = new AnnotationMethodMatcher(JsonError.class);

        // advice(JsonIAMException   JsonExceptionTransfer) + Pointcut 
        // ? Advisor
        DefaultPointcutAdvisor advisor = (DefaultPointcutAdvisor) context.getBean("jsonErrorAdvisor");

        //  bean Class ?     JsonError Annotation ?   
        // ?
        for (Method method : clazz.getDeclaredMethods()) {
            // JsonError Annotation ?   
            if (annotationMethodMatcher.matches(method, clazz)) {
                // maching method  
                log.debug("maching method : " + clazz.getSimpleName() + "." + method.getName());

                // jsonErrorAdvisor    Pointcut ?  -   JsonError
                // Annotation ?  Bean ?  Pointcut  
                if (advisor.getPointcut() == Pointcut.TRUE) {
                    advisor.setPointcut(
                            new ComposablePointcut(new RootClassFilter(clazz), annotationMethodMatcher));
                    //  ? Pointcut ?  ??  JsonError Annotation ?
                    //  Bean ?  Pointcut ? union  ?.
                } else {
                    ComposablePointcut pointcut = (ComposablePointcut) advisor.getPointcut();
                    pointcut.union(new ComposablePointcut(new RootClassFilter(clazz), annotationMethodMatcher));
                    advisor.setPointcut(pointcut);
                }
            }
        }

        return bean;
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }

}