Search in sources :

Example 1 with ResourcePlugin

use of io.gravitee.plugin.resource.ResourcePlugin in project gravitee-management-rest-api by gravitee-io.

the class ResourceServiceImpl method convert.

private ResourceEntity convert(ResourcePlugin resourcePlugin, boolean withPlugin) {
    ResourceEntity entity = new ResourceEntity();
    entity.setId(resourcePlugin.id());
    entity.setDescription(resourcePlugin.manifest().description());
    entity.setName(resourcePlugin.manifest().name());
    entity.setVersion(resourcePlugin.manifest().version());
    if (withPlugin) {
        // Plugin information
        Plugin plugin = resourcePlugin;
        PluginEntity pluginEntity = new PluginEntity();
        pluginEntity.setPlugin(plugin.clazz());
        pluginEntity.setPath(plugin.path().toString());
        pluginEntity.setType(plugin.type().toString().toLowerCase());
        pluginEntity.setDependencies(plugin.dependencies());
        entity.setPlugin(pluginEntity);
    }
    return entity;
}
Also used : PluginEntity(io.gravitee.management.model.PluginEntity) ResourceEntity(io.gravitee.management.model.ResourceEntity) ResourcePlugin(io.gravitee.plugin.resource.ResourcePlugin) Plugin(io.gravitee.plugin.core.api.Plugin)

Example 2 with ResourcePlugin

use of io.gravitee.plugin.resource.ResourcePlugin 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);
                }
            }
        }
    });
}
Also used : ApplicationContextAware(org.springframework.context.ApplicationContextAware) Reactable(io.gravitee.gateway.reactor.Reactable) ResourceClassLoaderFactory(io.gravitee.plugin.resource.ResourceClassLoaderFactory) ResourceConfiguration(io.gravitee.resource.api.ResourceConfiguration) ReactorHandler(io.gravitee.gateway.reactor.handler.ReactorHandler) ResourcePlugin(io.gravitee.plugin.resource.ResourcePlugin) PluginClassLoader(io.gravitee.plugin.core.api.PluginClassLoader) ResourceConfigurationFactory(io.gravitee.gateway.resource.ResourceConfigurationFactory) Resource(io.gravitee.definition.model.plugins.resources.Resource) IOException(java.io.IOException) IOException(java.io.IOException) ResourcePluginManager(io.gravitee.plugin.resource.ResourcePluginManager)

Example 3 with ResourcePlugin

use of io.gravitee.plugin.resource.ResourcePlugin in project gravitee-management-rest-api by gravitee-io.

the class ResourceServiceImpl method findById.

@Override
public ResourceEntity findById(String resource) {
    LOGGER.debug("Find resource by ID: {}", resource);
    ResourcePlugin resourceDefinition = resourcePluginManager.get(resource);
    if (resourceDefinition == null) {
        throw new ResourceNotFoundException(resource);
    }
    return convert(resourceDefinition, true);
}
Also used : ResourcePlugin(io.gravitee.plugin.resource.ResourcePlugin) ResourceNotFoundException(io.gravitee.management.service.exceptions.ResourceNotFoundException)

Example 4 with ResourcePlugin

use of io.gravitee.plugin.resource.ResourcePlugin in project gravitee-management-rest-api by gravitee-io.

the class ResourceServiceImpl method findAll.

@Override
public Set<ResourceEntity> findAll() {
    try {
        LOGGER.debug("List all resources");
        final Collection<ResourcePlugin> resourceDefinitions = resourcePluginManager.findAll();
        return resourceDefinitions.stream().map(resourceDefinition -> convert(resourceDefinition, false)).collect(Collectors.toSet());
    } catch (Exception ex) {
        LOGGER.error("An error occurs while trying to list all resources", ex);
        throw new TechnicalManagementException("An error occurs while trying to list all resources", ex);
    }
}
Also used : Logger(org.slf4j.Logger) ResourceService(io.gravitee.management.service.ResourceService) PluginEntity(io.gravitee.management.model.PluginEntity) ResourceEntity(io.gravitee.management.model.ResourceEntity) ResourceNotFoundException(io.gravitee.management.service.exceptions.ResourceNotFoundException) ResourcePlugin(io.gravitee.plugin.resource.ResourcePlugin) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) Autowired(org.springframework.beans.factory.annotation.Autowired) Set(java.util.Set) ResourcePluginManager(io.gravitee.plugin.resource.ResourcePluginManager) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) Component(org.springframework.stereotype.Component) Plugin(io.gravitee.plugin.core.api.Plugin) ResourcePlugin(io.gravitee.plugin.resource.ResourcePlugin) ResourceNotFoundException(io.gravitee.management.service.exceptions.ResourceNotFoundException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException) IOException(java.io.IOException) TechnicalManagementException(io.gravitee.management.service.exceptions.TechnicalManagementException)

Aggregations

ResourcePlugin (io.gravitee.plugin.resource.ResourcePlugin)4 PluginEntity (io.gravitee.management.model.PluginEntity)2 ResourceEntity (io.gravitee.management.model.ResourceEntity)2 ResourceNotFoundException (io.gravitee.management.service.exceptions.ResourceNotFoundException)2 Plugin (io.gravitee.plugin.core.api.Plugin)2 ResourcePluginManager (io.gravitee.plugin.resource.ResourcePluginManager)2 IOException (java.io.IOException)2 Resource (io.gravitee.definition.model.plugins.resources.Resource)1 Reactable (io.gravitee.gateway.reactor.Reactable)1 ReactorHandler (io.gravitee.gateway.reactor.handler.ReactorHandler)1 ResourceConfigurationFactory (io.gravitee.gateway.resource.ResourceConfigurationFactory)1 ResourceService (io.gravitee.management.service.ResourceService)1 TechnicalManagementException (io.gravitee.management.service.exceptions.TechnicalManagementException)1 PluginClassLoader (io.gravitee.plugin.core.api.PluginClassLoader)1 ResourceClassLoaderFactory (io.gravitee.plugin.resource.ResourceClassLoaderFactory)1 ResourceConfiguration (io.gravitee.resource.api.ResourceConfiguration)1 Collection (java.util.Collection)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 Logger (org.slf4j.Logger)1