use of io.gravitee.gateway.reactor.handler.ReactorHandler 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);
}
}
}
});
}
use of io.gravitee.gateway.reactor.handler.ReactorHandler 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);
}
}
}
});
}
use of io.gravitee.gateway.reactor.handler.ReactorHandler in project gravitee-gateway by gravitee-io.
the class DefaultReactorHandlerRegistry method prepare.
private ReactorHandler prepare(Reactable reactable) {
logger.info("Preparing a new handler for {}", reactable);
ReactorHandler handler = create0(reactable);
if (handler != null) {
try {
handler.start();
} catch (Exception ex) {
logger.error("Unable to register handler", ex);
return null;
}
}
return handler;
}
use of io.gravitee.gateway.reactor.handler.ReactorHandler in project gravitee-gateway by gravitee-io.
the class DefaultReactorHandlerResolver method resolve.
@Override
public ReactorHandler resolve(Request request) {
StringBuilder path = new StringBuilder(request.path());
if (path.charAt(path.length() - 1) != '/') {
path.append('/');
}
String sPath = path.toString();
ReactorHandler handler = null;
for (ReactorHandler reactorHandler : handlerRegistry.getReactorHandlers()) {
if (sPath.startsWith(reactorHandler.contextPath())) {
handler = reactorHandler;
break;
}
}
if (handler != null) {
LOGGER.debug("Returning the first handler matching path {} : {}", sPath, handler);
return handler;
}
return null;
}
use of io.gravitee.gateway.reactor.handler.ReactorHandler in project gravitee-gateway by gravitee-io.
the class ApiContextHandlerFactoryTest method shouldNotCreateContext.
@Test
public void shouldNotCreateContext() {
when(api.isEnabled()).thenReturn(false);
ReactorHandler handler = apiContextHandlerFactory.create(api);
assertNull(handler);
}
Aggregations