de.systemoutprintln.util.logging.spring.LogPostProcessor.java Source code

Java tutorial

Introduction

Here is the source code for de.systemoutprintln.util.logging.spring.LogPostProcessor.java

Source

/*
 *  This file is part of Benedikt Ritter's utils library.
 *
 *  Benedikt Ritter's utils library is free software: you can redistribute it 
 *  and/or modify it under the terms of the GNU General Public License as published 
 *  by the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Benedikt Ritter's utils library is distributed in the hope that it will 
 *  be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Benedikt Ritter's utils library. 
 *  If not, see <http://www.gnu.org/licenses/>.
 */
package de.systemoutprintln.util.logging.spring;

import java.lang.reflect.Field;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.FieldCallback;

import de.systemoutprintln.util.logging.annotations.Log;

/**
 * BeanPostProcessor, that injects a slf4j Logger into a field annotated with
 * {@link Log}.
 * 
 * @author Benedikt Ritter
 * 
 */
public class LogPostProcessor implements BeanPostProcessor {

    // TODO: implement some mechanism to decide which Logging Framework to use.
    // Maybe take a Logger class as an argument on @Log. But this would require
    // to add all the logging Frameworks as a dependency.
    // Maybe it would be better to provide several BeanPostProcessors for different
    // Frameworks.
    // Likewise several MethodInterceptors could be implemented to provide
    // interception for all the logging frameworks

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.beans.factory.config.BeanPostProcessor#
     * postProcessBeforeInitialization(java.lang.Object, java.lang.String)
     */
    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.springframework.beans.factory.config.BeanPostProcessor#
     * postProcessAfterInitialization(java.lang.Object, java.lang.String)
     */
    @Override
    public Object postProcessAfterInitialization(final Object bean, String beanName) throws BeansException {
        ReflectionUtils.doWithFields(bean.getClass(), new FieldCallback() {

            @Override
            public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
                ReflectionUtils.makeAccessible(field);

                if (field.getAnnotation(Log.class) != null) {
                    Logger logger = LoggerFactory.getLogger(bean.getClass());
                    field.set(bean, logger);
                }
            }
        });

        return bean;
    }
}