tv.arte.resteventapi.core.querying.convertion.RestEventApiCustomConvertersLoader.java Source code

Java tutorial

Introduction

Here is the source code for tv.arte.resteventapi.core.querying.convertion.RestEventApiCustomConvertersLoader.java

Source

package tv.arte.resteventapi.core.querying.convertion;

/*
 * #%L
 * RestEventAPI
 * %%
 * Copyright (C) 2014 ARTE G.E.I.E
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of The MIT License (MIT) as published by the Open Source 
 * Initiative.
 * 
 * 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 
 * MIT License (MIT) for more details.
 * 
 * You should have received a copy of The MIT License (MIT) 
 * along with this program.  If not, see <http://opensource.org/licenses/MIT>
 * #L%
 */

import java.util.Map;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.GenericConverter;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.stereotype.Component;

import tv.arte.resteventapi.core.RestEventApiRuntimeException;

/**
 * Loads in the conversion service all beans annotated with {@link RestEventApiConverter} and having
 * the appropriate type
 * 
 * @author Simeon Petev
 * @since 0.1
 */
@Component
public class RestEventApiCustomConvertersLoader implements ApplicationListener<ContextRefreshedEvent> {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ConversionService conversionService;

    /**
     * Initializes the RestEventApi custom converters at the end of the Spring context loading
     */
    @SuppressWarnings("rawtypes")
    public void onApplicationEvent(ContextRefreshedEvent event) {

        if (conversionService instanceof GenericConversionService) {
            GenericConversionService genericConversionService = (GenericConversionService) conversionService;
            Map<String, Object> converters = event.getApplicationContext()
                    .getBeansWithAnnotation(RestEventApiConverter.class);

            for (Entry<String, Object> beanEntry : converters.entrySet()) {
                Object beanEntryValue = beanEntry.getValue();

                if (beanEntryValue instanceof Converter) {
                    genericConversionService.addConverter((Converter) beanEntryValue);
                    if (logger.isDebugEnabled())
                        logger.debug("Converter bean with name " + beanEntry.getKey() + " has been added");
                } else if (beanEntryValue instanceof GenericConverter) {
                    genericConversionService.addConverter((GenericConverter) beanEntryValue);
                    if (logger.isDebugEnabled())
                        logger.debug("Converter bean with name " + beanEntry.getKey() + " has been added");
                } else {
                    logger.error(
                            "Cannot consider the bean with name " + beanEntry.getKey() + " as a valid converter.");
                }
            }
        } else {
            String errorMessage = "RestEventApi custom convertes failed to be initialized. The convertion service is not of the expected type.";
            logger.error(errorMessage);
            throw new RestEventApiRuntimeException(errorMessage);
        }

    }
}