Search in sources :

Example 1 with Repository

use of io.gravitee.repository.Repository 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 Repository

use of io.gravitee.repository.Repository 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)2 Repository (io.gravitee.repository.Repository)2 Scope (io.gravitee.repository.Scope)2 ApplicationContext (org.springframework.context.ApplicationContext)2 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)2 Set (java.util.Set)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1