Search in sources :

Example 1 with PluginClassLoader

use of io.gravitee.plugin.core.api.PluginClassLoader in project gravitee-gateway by gravitee-io.

the class DefaultPolicyManager method initialize.

private void initialize() {
    PolicyPluginManager ppm = applicationContext.getBean(PolicyPluginManager.class);
    PolicyClassLoaderFactory pclf = applicationContext.getBean(PolicyClassLoaderFactory.class);
    ReactorHandler rh = applicationContext.getBean(ReactorHandler.class);
    ResourceLifecycleManager rm = applicationContext.getBean(ResourceLifecycleManager.class);
    Reactable reactable = rh.reactable();
    Set<Policy> requiredPlugins = reactable.dependencies(Policy.class);
    requiredPlugins.forEach(policy -> {
        final PolicyPlugin policyPlugin = ppm.get(policy.getName());
        if (policyPlugin == null) {
            logger.error("Policy [{}] can not be found in policy registry", policy.getName());
            throw new IllegalStateException("Policy [" + policy.getName() + "] can not be found in policy registry");
        }
        PluginClassLoader policyClassLoader = null;
        // Load dependant resources to enhance policy classloader
        Collection<? extends Resource> resources = rm.getResources();
        if (!resources.isEmpty()) {
            ClassLoader[] resourceClassLoaders = rm.getResources().stream().map(new Function<Resource, ClassLoader>() {

                @Override
                public ClassLoader apply(Resource resource) {
                    return resource.getClass().getClassLoader();
                }
            }).toArray(ClassLoader[]::new);
            DelegatingClassLoader parentClassLoader = new DelegatingClassLoader(rh.classloader(), resourceClassLoaders);
            policyClassLoader = pclf.getOrCreateClassLoader(policyPlugin, parentClassLoader);
        } else {
            policyClassLoader = pclf.getOrCreateClassLoader(policyPlugin, rh.classloader());
        }
        logger.debug("Loading policy {} for {}", policy.getName(), rh);
        PolicyMetadataBuilder builder = new PolicyMetadataBuilder();
        builder.setId(policyPlugin.id());
        try {
            // Prepare metadata
            Class<?> policyClass = ClassUtils.forName(policyPlugin.policy().getName(), policyClassLoader);
            builder.setPolicy(policyClass).setMethods(new PolicyMethodResolver().resolve(policyClass));
            if (policyPlugin.configuration() != null) {
                builder.setConfiguration((Class<? extends PolicyConfiguration>) ClassUtils.forName(policyPlugin.configuration().getName(), policyClassLoader));
            }
            // Prepare context if defined
            if (policyPlugin.context() != null) {
                Class<? extends PolicyContext> policyContextClass = (Class<? extends PolicyContext>) ClassUtils.forName(policyPlugin.context().getName(), policyClassLoader);
                // Create policy context instance and initialize context provider (if used)
                PolicyContext context = new PolicyContextFactory(reactable).create(policyContextClass);
                builder.setContext(context);
            }
            RegisteredPolicy registeredPolicy = new RegisteredPolicy();
            registeredPolicy.classLoader = policyClassLoader;
            registeredPolicy.metadata = builder.build();
            policies.put(policy.getName(), registeredPolicy);
        } catch (Exception ex) {
            logger.error("Unable to load policy metadata", ex);
            if (policyClassLoader != null) {
                try {
                    policyClassLoader.close();
                } catch (IOException ioe) {
                    logger.error("Unable to close classloader for policy", ioe);
                }
            }
        }
    });
}
Also used : Policy(io.gravitee.definition.model.Policy) Reactable(io.gravitee.gateway.reactor.Reactable) PolicyClassLoaderFactory(io.gravitee.plugin.policy.PolicyClassLoaderFactory) Function(java.util.function.Function) ReactorHandler(io.gravitee.gateway.reactor.handler.ReactorHandler) PolicyMethodResolver(io.gravitee.plugin.policy.internal.PolicyMethodResolver) PolicyContext(io.gravitee.policy.api.PolicyContext) PluginClassLoader(io.gravitee.plugin.core.api.PluginClassLoader) PluginClassLoader(io.gravitee.plugin.core.api.PluginClassLoader) PolicyPlugin(io.gravitee.plugin.policy.PolicyPlugin) Resource(io.gravitee.resource.api.Resource) IOException(java.io.IOException) IOException(java.io.IOException) ResourceLifecycleManager(io.gravitee.gateway.resource.ResourceLifecycleManager) PolicyPluginManager(io.gravitee.plugin.policy.PolicyPluginManager)

Example 2 with PluginClassLoader

use of io.gravitee.plugin.core.api.PluginClassLoader 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 PluginClassLoader

use of io.gravitee.plugin.core.api.PluginClassLoader in project gravitee-gateway by gravitee-io.

the class DefaultPolicyManager method doStop.

@Override
protected void doStop() throws Exception {
    // Deactivate policy context
    policies.values().stream().filter(registeredPolicy -> registeredPolicy.metadata.context() != null).forEach(registeredPolicy -> {
        try {
            logger.info("De-activating context for {} [{}]", registeredPolicy.metadata.id(), registeredPolicy.metadata.context().getClass().getName());
            registeredPolicy.metadata.context().onDeactivation();
        } catch (Exception ex) {
            logger.error("Unable to deactivate policy context", ex);
        }
    });
    // Close policy classloaders
    policies.values().forEach(policy -> {
        ClassLoader policyClassLoader = policy.classLoader;
        if (policyClassLoader instanceof PluginClassLoader) {
            try {
                ((PluginClassLoader) policyClassLoader).close();
            } catch (IOException e) {
                logger.error("Unable to close policy classloader for policy {}", policy.metadata.id());
            }
        }
    });
    // Be sure to remove all references to policies
    policies.clear();
}
Also used : OnRequest(io.gravitee.policy.api.annotations.OnRequest) Resource(io.gravitee.resource.api.Resource) AbstractLifecycleComponent(io.gravitee.common.component.AbstractLifecycleComponent) PolicyClassLoaderFactory(io.gravitee.plugin.policy.PolicyClassLoaderFactory) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) Function(java.util.function.Function) Reactable(io.gravitee.gateway.reactor.Reactable) Map(java.util.Map) OnRequestContent(io.gravitee.policy.api.annotations.OnRequestContent) PluginClassLoader(io.gravitee.plugin.core.api.PluginClassLoader) PolicyPluginManager(io.gravitee.plugin.policy.PolicyPluginManager) OnResponseContent(io.gravitee.policy.api.annotations.OnResponseContent) OnResponse(io.gravitee.policy.api.annotations.OnResponse) Logger(org.slf4j.Logger) ClassUtils(org.springframework.util.ClassUtils) ReactorHandler(io.gravitee.gateway.reactor.handler.ReactorHandler) Collection(java.util.Collection) PolicyConfiguration(io.gravitee.policy.api.PolicyConfiguration) Set(java.util.Set) IOException(java.io.IOException) ApplicationContext(org.springframework.context.ApplicationContext) ResourceLifecycleManager(io.gravitee.gateway.resource.ResourceLifecycleManager) Policy(io.gravitee.definition.model.Policy) PolicyMethodResolver(io.gravitee.plugin.policy.internal.PolicyMethodResolver) PolicyPlugin(io.gravitee.plugin.policy.PolicyPlugin) PolicyContext(io.gravitee.policy.api.PolicyContext) io.gravitee.gateway.policy(io.gravitee.gateway.policy) PluginClassLoader(io.gravitee.plugin.core.api.PluginClassLoader) IOException(java.io.IOException) IOException(java.io.IOException) PluginClassLoader(io.gravitee.plugin.core.api.PluginClassLoader)

Example 4 with PluginClassLoader

use of io.gravitee.plugin.core.api.PluginClassLoader in project gravitee-gateway by gravitee-io.

the class ResourceManagerImpl method doStop.

@Override
protected void doStop() throws Exception {
    // Stop resources
    resources.entrySet().stream().forEach(resource -> {
        try {
            logger.info("Stop resource {} [{}]", resource.getKey(), resource.getValue().getClass());
            resource.getValue().stop();
        } catch (Exception ex) {
            logger.error("Unable to stop resource", ex);
        }
    });
    // Close resource classLoaders
    resources.values().forEach(resource -> {
        ClassLoader resourceClassLoader = resource.getClass().getClassLoader();
        if (resourceClassLoader instanceof PluginClassLoader) {
            try {
                ((PluginClassLoader) resourceClassLoader).close();
            } catch (IOException ioe) {
                logger.error("Unable to close classloader for resource {}", resource.getClass(), ioe);
            }
        }
    });
    // Be sure to remove all references to resources
    resources.clear();
}
Also used : PluginClassLoader(io.gravitee.plugin.core.api.PluginClassLoader) IOException(java.io.IOException) IOException(java.io.IOException) PluginClassLoader(io.gravitee.plugin.core.api.PluginClassLoader)

Aggregations

PluginClassLoader (io.gravitee.plugin.core.api.PluginClassLoader)4 IOException (java.io.IOException)4 Reactable (io.gravitee.gateway.reactor.Reactable)3 ReactorHandler (io.gravitee.gateway.reactor.handler.ReactorHandler)3 Policy (io.gravitee.definition.model.Policy)2 ResourceLifecycleManager (io.gravitee.gateway.resource.ResourceLifecycleManager)2 PolicyClassLoaderFactory (io.gravitee.plugin.policy.PolicyClassLoaderFactory)2 PolicyPlugin (io.gravitee.plugin.policy.PolicyPlugin)2 PolicyPluginManager (io.gravitee.plugin.policy.PolicyPluginManager)2 PolicyMethodResolver (io.gravitee.plugin.policy.internal.PolicyMethodResolver)2 PolicyContext (io.gravitee.policy.api.PolicyContext)2 Resource (io.gravitee.resource.api.Resource)2 Function (java.util.function.Function)2 AbstractLifecycleComponent (io.gravitee.common.component.AbstractLifecycleComponent)1 Resource (io.gravitee.definition.model.plugins.resources.Resource)1 io.gravitee.gateway.policy (io.gravitee.gateway.policy)1 ResourceConfigurationFactory (io.gravitee.gateway.resource.ResourceConfigurationFactory)1 ResourceClassLoaderFactory (io.gravitee.plugin.resource.ResourceClassLoaderFactory)1 ResourcePlugin (io.gravitee.plugin.resource.ResourcePlugin)1 ResourcePluginManager (io.gravitee.plugin.resource.ResourcePluginManager)1