Search in sources :

Example 1 with Import

use of org.springframework.context.annotation.Import in project gravitee-gateway by gravitee-io.

the class SpringPolicyContextProviderFactory method create.

public PolicyContextProvider create(PolicyContext policyContext) {
    Import importAnnotation = policyContext.getClass().getAnnotation(Import.class);
    List<Class<?>> importClasses = Arrays.asList(importAnnotation.value());
    LOGGER.info("Initializing a Spring context provider from @Import annotation: {}", String.join(",", importClasses.stream().map(Class::getName).collect(Collectors.toSet())));
    AnnotationConfigApplicationContext policyApplicationContext = new AnnotationConfigApplicationContext();
    policyApplicationContext.setClassLoader(policyContext.getClass().getClassLoader());
    importClasses.forEach(policyApplicationContext::register);
    // TODO: set the parent application context ?
    // pluginContext.setEnvironment(applicationContextParent.getEnvironment());
    // pluginContext.setParent(applicationContextParent);
    policyApplicationContext.refresh();
    return new SpringPolicyContextProvider(policyApplicationContext);
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Import(org.springframework.context.annotation.Import)

Example 2 with Import

use of org.springframework.context.annotation.Import in project spring-cloud-stream by spring-cloud.

the class TestChannelBinderConfiguration method getCompleteConfiguration.

/**
 * Utility operation to return an array of configuration classes
 * defined in {@link EnableBinding} annotation.
 * Typically used for tests that do not rely on creating an SCSt boot
 * application annotated with {@link EnableBinding}, yet require
 * full {@link Binder} configuration.
 */
public static Class<?>[] getCompleteConfiguration(Class<?>... additionalConfigurationClasses) {
    List<Class<?>> configClasses = new ArrayList<>();
    configClasses.add(TestChannelBinderConfiguration.class);
    Import annotation = AnnotationUtils.getAnnotation(EnableBinding.class, Import.class);
    Map<String, Object> annotationAttributes = AnnotationUtils.getAnnotationAttributes(annotation);
    configClasses.addAll(Arrays.asList((Class<?>[]) annotationAttributes.get("value")));
    if (additionalConfigurationClasses != null) {
        configClasses.addAll(Arrays.asList(additionalConfigurationClasses));
    }
    return configClasses.toArray(new Class<?>[] {});
}
Also used : Import(org.springframework.context.annotation.Import) ArrayList(java.util.ArrayList)

Example 3 with Import

use of org.springframework.context.annotation.Import in project gravitee-management-rest-api by gravitee-io.

the class IdentityProviderManagerImpl method create.

private <T> T create(Plugin plugin, Class<T> identityClass, Map<String, Object> properties) {
    if (identityClass == null) {
        return null;
    }
    try {
        T identityObj = createInstance(identityClass);
        final Import annImport = identityClass.getAnnotation(Import.class);
        Set<Class<?>> configurations = (annImport != null) ? new HashSet<>(Arrays.asList(annImport.value())) : Collections.emptySet();
        ApplicationContext idpApplicationContext = pluginContextFactory.create(new AnnotationBasedPluginContextConfigurer(plugin) {

            @Override
            public Set<Class<?>> configurations() {
                return configurations;
            }

            @Override
            public ConfigurableEnvironment environment() {
                return new StandardEnvironment() {

                    @Override
                    protected void customizePropertySources(MutablePropertySources propertySources) {
                        propertySources.addFirst(new MapPropertySource(plugin.id(), properties));
                        super.customizePropertySources(propertySources);
                    }
                };
            }
        });
        idpApplicationContext.getAutowireCapableBeanFactory().autowireBean(identityObj);
        if (identityObj instanceof InitializingBean) {
            ((InitializingBean) identityObj).afterPropertiesSet();
        }
        return identityObj;
    } catch (Exception ex) {
        LOGGER.error("An unexpected error occurs while loading identity provider", ex);
        return null;
    }
}
Also used : Import(org.springframework.context.annotation.Import) AnnotationBasedPluginContextConfigurer(io.gravitee.plugin.core.internal.AnnotationBasedPluginContextConfigurer) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurableEnvironment(org.springframework.core.env.ConfigurableEnvironment) MapPropertySource(org.springframework.core.env.MapPropertySource) MutablePropertySources(org.springframework.core.env.MutablePropertySources) InitializingBean(org.springframework.beans.factory.InitializingBean) StandardEnvironment(org.springframework.core.env.StandardEnvironment)

Aggregations

Import (org.springframework.context.annotation.Import)3 AnnotationBasedPluginContextConfigurer (io.gravitee.plugin.core.internal.AnnotationBasedPluginContextConfigurer)1 ArrayList (java.util.ArrayList)1 InitializingBean (org.springframework.beans.factory.InitializingBean)1 ApplicationContext (org.springframework.context.ApplicationContext)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 ConfigurableEnvironment (org.springframework.core.env.ConfigurableEnvironment)1 MapPropertySource (org.springframework.core.env.MapPropertySource)1 MutablePropertySources (org.springframework.core.env.MutablePropertySources)1 StandardEnvironment (org.springframework.core.env.StandardEnvironment)1