use of io.gravitee.plugin.resource.ResourcePluginManager in project gravitee-gateway by gravitee-io.
the class ResourceManagerImpl method initialize.
private void initialize() {
ResourcePluginManager rpm = applicationContext.getBean(ResourcePluginManager.class);
ResourceClassLoaderFactory rclf = applicationContext.getBean(ResourceClassLoaderFactory.class);
ResourceConfigurationFactory rcf = applicationContext.getBean(ResourceConfigurationFactory.class);
ReactorHandler rh = applicationContext.getBean(ReactorHandler.class);
Reactable reactable = rh.reactable();
Set<Resource> resourceDeps = reactable.dependencies(Resource.class);
resourceDeps.forEach(resource -> {
final ResourcePlugin resourcePlugin = rpm.get(resource.getType());
if (resourcePlugin == null) {
logger.error("Resource [{}] can not be found in plugin registry", resource.getType());
throw new IllegalStateException("Resource [" + resource.getType() + "] can not be found in plugin registry");
}
PluginClassLoader resourceClassLoader = rclf.getOrCreateClassLoader(resourcePlugin, rh.classloader());
logger.debug("Loading resource {} for {}", resource.getName(), rh);
try {
Class<? extends io.gravitee.resource.api.Resource> resourceClass = (Class<? extends io.gravitee.resource.api.Resource>) ClassUtils.forName(resourcePlugin.resource().getName(), resourceClassLoader);
Map<Class<?>, Object> injectables = new HashMap<>();
if (resourcePlugin.configuration() != null) {
Class<? extends ResourceConfiguration> resourceConfigurationClass = (Class<? extends ResourceConfiguration>) ClassUtils.forName(resourcePlugin.configuration().getName(), resourceClassLoader);
injectables.put(resourceConfigurationClass, rcf.create(resourceConfigurationClass, resource.getConfiguration()));
}
io.gravitee.resource.api.Resource resourceInstance = new ResourceFactory().create(resourceClass, injectables);
if (resourceInstance instanceof ApplicationContextAware) {
((ApplicationContextAware) resourceInstance).setApplicationContext(applicationContext);
}
resources.put(resource.getName(), resourceInstance);
} catch (Exception ex) {
logger.error("Unable to create resource", ex);
if (resourceClassLoader != null) {
try {
resourceClassLoader.close();
} catch (IOException ioe) {
logger.error("Unable to close classloader for resource", ioe);
}
}
}
});
}
Aggregations