Skip to content

Commit

Permalink
Merge pull request #72 from groldan/wps_plugin_require_no_wps_install…
Browse files Browse the repository at this point in the history
…ed_in_geoserver

Make it possible to have the gs-acl-plugin-wps module on the classpath if the WPS extension is not installed
  • Loading branch information
groldan authored Aug 12, 2024
2 parents 6dedd58 + 502efa0 commit a734372
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@

import org.geoserver.acl.plugin.autoconfigure.accessmanager.ConditionalOnAclEnabled;
import org.geoserver.acl.plugin.config.wps.AclWpsIntegrationConfiguration;
import org.geoserver.wps.resource.WPSResourceManager;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Import;

@AutoConfiguration
@ConditionalOnAclEnabled
@ConditionalOnBean(WPSResourceManager.class)
@ConditionalOnBean(name = "wpsResourceManager")
@Import({AclWpsIntegrationConfiguration.class})
public class AclWpsAutoConfiguration {}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@
import static org.mockito.Mockito.mock;

import org.geoserver.acl.plugin.accessmanager.wps.ChainStatusHolder;
import org.geoserver.acl.plugin.config.wps.WPSResourceManagerClassCondition;
import org.geoserver.acl.plugin.wps.DefaultExecutionIdRetriever;
import org.geoserver.acl.plugin.wps.WPSProcessListener;
import org.geoserver.wps.resource.WPSResourceManager;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;

/** {@link AclWpsAutoConfiguration} tests */
Expand All @@ -24,7 +26,10 @@ class AclWpsAutoConfigurationTest {

@Test
void testEnabledWhenAllConditionsMatch() {
runner.withBean(WPSResourceManager.class, () -> mock(WPSResourceManager.class))
runner.withBean(
"wpsResourceManager",
WPSResourceManager.class,
() -> mock(WPSResourceManager.class))
.run(
context -> {
assertThat(context)
Expand All @@ -37,7 +42,10 @@ void testEnabledWhenAllConditionsMatch() {

@Test
void testConditionalOnAclEnabled() {
runner.withBean(WPSResourceManager.class, () -> mock(WPSResourceManager.class))
runner.withBean(
"wpsResourceManager",
WPSResourceManager.class,
() -> mock(WPSResourceManager.class))
.withPropertyValues("geoserver.acl.enabled=false")
.run(
context -> {
Expand All @@ -48,7 +56,10 @@ void testConditionalOnAclEnabled() {
.doesNotHaveBean(WPSProcessListener.class);
});
runner.withPropertyValues("geoserver.acl.enabled=true")
.withBean(WPSResourceManager.class, () -> mock(WPSResourceManager.class))
.withBean(
"wpsResourceManager",
WPSResourceManager.class,
() -> mock(WPSResourceManager.class))
.run(
context -> {
assertThat(context)
Expand All @@ -70,4 +81,29 @@ void testConditionalOnWPSResourceManager() {
.doesNotHaveBean(WPSProcessListener.class);
});
}

@Test
void testWPSResourceManagerClassCondition() {

FilteredClassLoader classLoader = new FilteredClassLoader(WPSResourceManager.class);
WPSResourceManagerClassCondition.classLoader(classLoader);
try {
runner
// bypass the @ConditionalOnBean(name="wpsResourceManager") in the
// Autoconfiguration to hit the plain spring condition in
// AclWpsIntegrationConfiguration
.withBean("wpsResourceManager", Object.class, () -> new Object())
.withClassLoader(classLoader)
.run(
context -> {
assertThat(context)
.hasNotFailed()
.doesNotHaveBean(ChainStatusHolder.class)
.doesNotHaveBean(DefaultExecutionIdRetriever.class)
.doesNotHaveBean(WPSProcessListener.class);
});
} finally {
WPSResourceManagerClassCondition.classLoader(null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@
import org.geoserver.acl.plugin.wps.WPSProcessListener;
import org.geoserver.wps.resource.WPSResourceManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

@Configuration(proxyBeanMethods = false)
// spring with no spring boot equivalent to @ConditionalOnClass(WPSResourceManager.class)
@Conditional(value = WPSResourceManagerClassCondition.class)
public class AclWpsIntegrationConfiguration {

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* (c) 2024 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geoserver.acl.plugin.config.wps;

import com.google.common.annotations.VisibleForTesting;

import org.springframework.context.annotation.ConditionContext;
import org.springframework.context.annotation.ConfigurationCondition;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
* Spring without spring boot equivalent to {@code @ConditionalOnClass(WPSResourceManager.class)},
* so the {@code gs-acl-plugin-wps} module can be on the vanilla GeoServer classpath when the
* geoserver WPS extension is not installed.
*/
public class WPSResourceManagerClassCondition implements ConfigurationCondition {

private static ClassLoader classLoader;

@VisibleForTesting
public static void classLoader(ClassLoader cl) {
classLoader = cl;
}

@Override
public ConfigurationPhase getConfigurationPhase() {
return ConfigurationPhase.PARSE_CONFIGURATION;
}

@Override
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
try {
if (null == classLoader) classLoader = getClass().getClassLoader();
classLoader.loadClass("org.geoserver.wps.resource.WPSResourceManager");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}

0 comments on commit a734372

Please sign in to comment.