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);
}
}
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;
}
}
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);
}
}
Aggregations