Search in sources :

Example 1 with AnnotationBasedPluginContextConfigurer

use of io.gravitee.plugin.core.internal.AnnotationBasedPluginContextConfigurer in project gravitee-gateway by gravitee-io.

the class RepositoryPluginHandler method handle.

@Override
public void handle(Plugin plugin) {
    try {
        ClassLoader classloader = pluginClassLoaderFactory.getOrCreateClassLoader(plugin, this.getClass().getClassLoader());
        final Class<?> repositoryClass = classloader.loadClass(plugin.clazz());
        LOGGER.info("Register a new repository plugin: {} [{}]", plugin.id(), plugin.clazz());
        Assert.isAssignable(Repository.class, repositoryClass);
        Repository repository = createInstance((Class<Repository>) repositoryClass);
        for (Scope scope : repository.scopes()) {
            if (!repositories.containsKey(scope)) {
                String requiredRepositoryType = repositoryTypeByScope.get(scope);
                // Load only repository plugin for a given scope (provided in the configuration)
                if (repository.type().equalsIgnoreCase(requiredRepositoryType)) {
                    LOGGER.info("Repository [{}] loaded by {}", scope, repository.type());
                    // Not yet loaded, let's mount the repository in application context
                    try {
                        ApplicationContext repoApplicationContext = pluginContextFactory.create(new AnnotationBasedPluginContextConfigurer(plugin) {

                            @Override
                            public Set<Class<?>> configurations() {
                                return Collections.singleton(repository.configuration(scope));
                            }
                        });
                        registerRepositoryDefinitions(repository, repoApplicationContext);
                        repositories.put(scope, repository);
                    } catch (Exception iae) {
                        LOGGER.error("Unexpected error while creating context for repository instance", iae);
                        pluginContextFactory.remove(plugin);
                    }
                } else {
                    LOGGER.debug("Scoped repository [{}] must be loaded by {}. Skipping registration", scope, requiredRepositoryType);
                }
            } else {
                LOGGER.warn("Repository scope {} already loaded by {}", scope, repositories.get(scope));
            }
        }
    } catch (Exception iae) {
        LOGGER.error("Unexpected error while create repository instance", iae);
    }
}
Also used : AnnotationBasedPluginContextConfigurer(io.gravitee.plugin.core.internal.AnnotationBasedPluginContextConfigurer) Repository(io.gravitee.repository.Repository) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Set(java.util.Set) Scope(io.gravitee.repository.Scope)

Example 2 with AnnotationBasedPluginContextConfigurer

use of io.gravitee.plugin.core.internal.AnnotationBasedPluginContextConfigurer 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)

Example 3 with AnnotationBasedPluginContextConfigurer

use of io.gravitee.plugin.core.internal.AnnotationBasedPluginContextConfigurer in project gravitee-management-rest-api by gravitee-io.

the class RepositoryPluginHandler method handle.

@Override
public void handle(Plugin plugin) {
    try {
        ClassLoader classloader = pluginClassLoaderFactory.getOrCreateClassLoader(plugin, this.getClass().getClassLoader());
        final Class<?> repositoryClass = classloader.loadClass(plugin.clazz());
        LOGGER.info("Register a new repository: {} [{}]", plugin.id(), plugin.clazz());
        Assert.isAssignable(Repository.class, repositoryClass);
        Repository repository = createInstance((Class<Repository>) repositoryClass);
        Collection<Scope> scopes = scopeByRepositoryType.getOrDefault(repository.type(), Collections.EMPTY_LIST);
        for (Scope scope : scopes) {
            if (!repositories.containsKey(scope)) {
                // Not yet loaded, let's mount the repository in application context
                try {
                    ApplicationContext applicationContext = pluginContextFactory.create(new AnnotationBasedPluginContextConfigurer(plugin) {

                        @Override
                        public Set<Class<?>> configurations() {
                            return Collections.singleton(repository.configuration(scope));
                        }
                    });
                    registerRepositoryDefinitions(repository, applicationContext);
                    repositories.put(scope, repository);
                } catch (Exception iae) {
                    LOGGER.error("Unexpected error while creating context for repository instance", iae);
                    pluginContextFactory.remove(plugin);
                }
            } else {
                LOGGER.warn("Repository scope {} already loaded by {}", scope, repositories.get(scope));
            }
        }
    } catch (Exception iae) {
        LOGGER.error("Unexpected error while create repository instance", iae);
    }
}
Also used : AnnotationBasedPluginContextConfigurer(io.gravitee.plugin.core.internal.AnnotationBasedPluginContextConfigurer) Repository(io.gravitee.repository.Repository) ApplicationContext(org.springframework.context.ApplicationContext) ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Scope(io.gravitee.repository.Scope) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Aggregations

AnnotationBasedPluginContextConfigurer (io.gravitee.plugin.core.internal.AnnotationBasedPluginContextConfigurer)3 ApplicationContext (org.springframework.context.ApplicationContext)3 Repository (io.gravitee.repository.Repository)2 Scope (io.gravitee.repository.Scope)2 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)2 Set (java.util.Set)1 InitializingBean (org.springframework.beans.factory.InitializingBean)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 Import (org.springframework.context.annotation.Import)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