Example usage for org.springframework.beans.factory.config ConfigurableListableBeanFactory getConversionService

List of usage examples for org.springframework.beans.factory.config ConfigurableListableBeanFactory getConversionService

Introduction

In this page you can find the example usage for org.springframework.beans.factory.config ConfigurableListableBeanFactory getConversionService.

Prototype

@Nullable
ConversionService getConversionService();

Source Link

Document

Return the associated ConversionService, if any.

Usage

From source file:org.eclipse.gemini.blueprint.extender.internal.blueprint.activator.BlueprintContainerProcessor.java

public void preProcessRefresh(final ConfigurableOsgiBundleApplicationContext context) {
    final BundleContext bundleContext = context.getBundleContext();
    // create the ModuleContext adapter
    final BlueprintContainer blueprintContainer = createBlueprintContainer(context);

    // 1. add event listeners
    // add service publisher
    context.addApplicationListener(new BlueprintContainerServicePublisher(blueprintContainer, bundleContext));
    // add waiting event broadcaster
    context.addApplicationListener(new BlueprintWaitingEventDispatcher(context.getBundleContext()));

    // 2. add environmental managers
    context.addBeanFactoryPostProcessor(new BeanFactoryPostProcessor() {

        private static final String BLUEPRINT_BUNDLE = "blueprintBundle";
        private static final String BLUEPRINT_BUNDLE_CONTEXT = "blueprintBundleContext";
        private static final String BLUEPRINT_CONTAINER = "blueprintContainer";
        private static final String BLUEPRINT_EXTENDER = "blueprintExtenderBundle";
        private static final String BLUEPRINT_CONVERTER = "blueprintConverter";

        public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
            // lazy logger evaluation
            Log logger = LogFactory.getLog(context.getClass());

            if (!(beanFactory instanceof BeanDefinitionRegistry)) {
                logger.warn("Environmental beans will be registered as singletons instead "
                        + "of usual bean definitions since beanFactory " + beanFactory
                        + " is not a BeanDefinitionRegistry");
            }/* ww w  . jav  a2 s.  c  o  m*/

            // add blueprint container bean
            addPredefinedBlueprintBean(beanFactory, BLUEPRINT_BUNDLE, bundleContext.getBundle(), logger);
            addPredefinedBlueprintBean(beanFactory, BLUEPRINT_BUNDLE_CONTEXT, bundleContext, logger);
            addPredefinedBlueprintBean(beanFactory, BLUEPRINT_CONTAINER, blueprintContainer, logger);
            // addPredefinedBlueprintBean(beanFactory, BLUEPRINT_EXTENDER, extenderBundle, logger);
            addPredefinedBlueprintBean(beanFactory, BLUEPRINT_CONVERTER,
                    new SpringBlueprintConverter(beanFactory), logger);

            // add Blueprint conversion service
            // String[] beans = beanFactory.getBeanNamesForType(BlueprintConverterConfigurer.class, false, false);
            // if (ObjectUtils.isEmpty(beans)) {
            // beanFactory.addPropertyEditorRegistrar(new BlueprintEditorRegistrar());
            // }
            beanFactory.setConversionService(
                    new SpringBlueprintConverterService(beanFactory.getConversionService(), beanFactory));
        }

        private void addPredefinedBlueprintBean(ConfigurableListableBeanFactory beanFactory, String beanName,
                Object value, Log logger) {
            if (!beanFactory.containsLocalBean(beanName)) {
                logger.debug("Registering pre-defined bean named " + beanName);
                if (beanFactory instanceof BeanDefinitionRegistry) {
                    BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;

                    GenericBeanDefinition def = new GenericBeanDefinition();
                    def.setBeanClass(ENV_FB_CLASS);
                    ConstructorArgumentValues cav = new ConstructorArgumentValues();
                    cav.addIndexedArgumentValue(0, value);
                    def.setConstructorArgumentValues(cav);
                    def.setLazyInit(false);
                    def.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
                    registry.registerBeanDefinition(beanName, def);

                } else {
                    beanFactory.registerSingleton(beanName, value);
                }

            } else {
                logger.warn("A bean named " + beanName
                        + " already exists; aborting registration of the predefined value...");
            }
        }
    });

    // 3. add cycle breaker
    context.addBeanFactoryPostProcessor(cycleBreaker);

    BlueprintEvent creatingEvent = new BlueprintEvent(BlueprintEvent.CREATING, context.getBundle(),
            extenderBundle);
    listenerManager.blueprintEvent(creatingEvent);
    dispatcher.beforeRefresh(creatingEvent);
}

From source file:org.grails.plugins.AbstractGrailsPluginManager.java

/**
 * Base implementation that simply goes through the list of plugins and calls doWithRuntimeConfiguration on each
 * @param springConfig The RuntimeSpringConfiguration instance
 */// w  w w .j a v  a2s  .  com
public void doRuntimeConfiguration(RuntimeSpringConfiguration springConfig) {
    ApplicationContext context = springConfig.getUnrefreshedApplicationContext();
    AutowireCapableBeanFactory autowireCapableBeanFactory = context.getAutowireCapableBeanFactory();
    if (autowireCapableBeanFactory instanceof ConfigurableListableBeanFactory) {
        ConfigurableListableBeanFactory beanFactory = (ConfigurableListableBeanFactory) autowireCapableBeanFactory;
        ConversionService existingConversionService = beanFactory.getConversionService();
        ConverterRegistry converterRegistry;
        if (existingConversionService == null) {
            GenericConversionService conversionService = new GenericConversionService();
            converterRegistry = conversionService;
            beanFactory.setConversionService(conversionService);
        } else {
            converterRegistry = (ConverterRegistry) existingConversionService;
        }

        converterRegistry.addConverter(
                new Converter<GrailsApplication, org.codehaus.groovy.grails.commons.GrailsApplication>() {
                    @Override
                    public org.codehaus.groovy.grails.commons.GrailsApplication convert(
                            GrailsApplication source) {
                        return new LegacyGrailsApplication(source);
                    }
                });
        converterRegistry.addConverter(new Converter<NavigableMap.NullSafeNavigator, Object>() {
            @Override
            public Object convert(NavigableMap.NullSafeNavigator source) {
                return null;
            }
        });
    }
    checkInitialised();
    for (GrailsPlugin plugin : pluginList) {
        if (plugin.supportsCurrentScopeAndEnvironment()
                && plugin.isEnabled(context.getEnvironment().getActiveProfiles())) {
            plugin.doWithRuntimeConfiguration(springConfig);
        }
    }
}