br.com.d4n.ui4entity.mvc.SpringContextConfigurer.java Source code

Java tutorial

Introduction

Here is the source code for br.com.d4n.ui4entity.mvc.SpringContextConfigurer.java

Source

package br.com.d4n.ui4entity.mvc;

/*
 * #%L
 * ui4entity
 * %%
 * Copyright (C) 2012 - 2015 DevFor Ninjas
 * %%
 * This program 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.
 * 
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 * #L%
 */

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor;

import br.com.d4n.ui4entity.core.DefaultParserEngine;
import br.com.d4n.ui4entity.core.ParserEngine;
import br.com.d4n.ui4entity.core.SpringBeanFactoryDelegate;

public class SpringContextConfigurer implements BeanFactoryPostProcessor {

    Logger logger = LoggerFactory.getLogger(SpringContextConfigurer.class);

    private Class<? extends HandlerMethodReturnValueHandler> handlerReturnValueType = RequestResponseBodyMethodProcessor.class;

    private ParserEngine parserEngine = new DefaultParserEngine();

    public SpringContextConfigurer() {
    }

    public SpringContextConfigurer(Class<? extends HandlerMethodReturnValueHandler> handlerReturnValueType) {
        this.handlerReturnValueType = handlerReturnValueType;
    }

    public SpringContextConfigurer(ParserEngine engine) {
        this.parserEngine = engine;
    }

    public SpringContextConfigurer(Class<? extends HandlerMethodReturnValueHandler> handlerReturnValueType,
            ParserEngine engine) {
        this.parserEngine = engine;
        this.handlerReturnValueType = handlerReturnValueType;
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

        // TODO: add log

        parserEngine.setValidationBeanFactory(new SpringBeanFactoryDelegate(beanFactory));

        RequestMappingHandlerAdapter handlerAdapter = beanFactory.getBean(RequestMappingHandlerAdapter.class);

        // List<HandlerMethodReturnValueHandler> handlers = new
        // LinkedList<HandlerMethodReturnValueHandler>(handlerAdapter.getReturnValueHandlers().getHandlers());
        List<HandlerMethodReturnValueHandler> handlers = handlerAdapter.getReturnValueHandlers();

        decorateHandlers(handlers);
        handlerAdapter.setReturnValueHandlers(handlers);

        // handlerAdapter.afterPropertiesSet();
    }

    private void decorateHandlers(List<HandlerMethodReturnValueHandler> handlers) {
        for (HandlerMethodReturnValueHandler handler : handlers) {

            if (handlerReturnValueType.equals(handler.getClass())) {
                HandlerMethodReturnValueHandler decorator = new ValueOptionReturnValueHandler(
                        new ControllerReturnValueHandler(handler, parserEngine));
                int index = handlers.indexOf(handler);
                handlers.set(index, decorator);

                logger.info("@TODO: Metadata Injecting decorator wired up");
            }
        }
    }

    public void setHandlerReturnValueType(Class<? extends HandlerMethodReturnValueHandler> handlerReturnValueType) {
        this.handlerReturnValueType = handlerReturnValueType;
    }

    public void setParserEngine(ParserEngine parserEngine) {
        this.parserEngine = parserEngine;
    }

}