This is an automated email from the ASF dual-hosted git repository. mmoayyed pushed a commit to branch SYNCOPE-163-1 in repository https://gitbox.apache.org/repos/asf/syncope.git
commit 951eb110754195f64abf6065b8124d7fbe0f70a9 Author: Misagh Moayyed <[email protected]> AuthorDate: Thu Mar 26 11:39:13 2020 +0430 SYNCOPE-160: lazy-load config for SyncopeClient access --- fit/wa-reference/pom.xml | 2 ++ pom.xml | 5 ++++ wa/bootstrap/pom.xml | 16 +++-------- .../java/org/apache/syncope/wa/WARestClient.java | 32 ++++++++++++++++------ .../bootstrap/SyncopeWABootstrapConfiguration.java | 12 ++------ .../bootstrap/SyncopeWAPropertySourceLocator.java | 24 ++++++++++++++-- 6 files changed, 58 insertions(+), 33 deletions(-) diff --git a/fit/wa-reference/pom.xml b/fit/wa-reference/pom.xml index c0adcd2..d774a57 100644 --- a/fit/wa-reference/pom.xml +++ b/fit/wa-reference/pom.xml @@ -146,6 +146,7 @@ under the License. <context>syncope</context> </properties> </deployable> + <!-- <deployable> <location>${basedir}/../console-reference/target/syncope-fit-console-reference-${project.version}</location> <properties> @@ -158,6 +159,7 @@ under the License. <context>syncope-enduser</context> </properties> </deployable> + --> <deployable> <location>${project.build.directory}/${project.build.finalName}</location> <properties> diff --git a/pom.xml b/pom.xml index 8515449..088a25c 100644 --- a/pom.xml +++ b/pom.xml @@ -1462,6 +1462,11 @@ under the License. </dependency> <dependency> <groupId>org.apereo.cas</groupId> + <artifactId>cas-server-core-util-api</artifactId> + <version>${cas.version}</version> + </dependency> + <dependency> + <groupId>org.apereo.cas</groupId> <artifactId>cas-server-core-configuration</artifactId> <version>${cas.version}</version> </dependency> diff --git a/wa/bootstrap/pom.xml b/wa/bootstrap/pom.xml index d9e5203..9ea1e97 100644 --- a/wa/bootstrap/pom.xml +++ b/wa/bootstrap/pom.xml @@ -43,26 +43,18 @@ under the License. <artifactId>syncope-client-am-lib</artifactId> <version>${project.version}</version> </dependency> - <dependency> <groupId>org.apache.syncope.common.keymaster</groupId> - <artifactId>syncope-common-keymaster-client-zookeeper</artifactId> + <artifactId>syncope-common-keymaster-client-api</artifactId> <version>${project.version}</version> </dependency> <dependency> - <groupId>org.apache.syncope.core</groupId> - <artifactId>syncope-core-provisioning-api</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>org.apache.syncope.ext.self-keymaster</groupId> - <artifactId>syncope-ext-self-keymaster-client</artifactId> - <version>${project.version}</version> + <groupId>org.apereo.cas</groupId> + <artifactId>cas-server-core-configuration-api</artifactId> </dependency> - <dependency> <groupId>org.apereo.cas</groupId> - <artifactId>cas-server-core-configuration-api</artifactId> + <artifactId>cas-server-core-util-api</artifactId> </dependency> </dependencies> diff --git a/wa/bootstrap/src/main/java/org/apache/syncope/wa/WARestClient.java b/wa/bootstrap/src/main/java/org/apache/syncope/wa/WARestClient.java index c33a738..ca2d048 100644 --- a/wa/bootstrap/src/main/java/org/apache/syncope/wa/WARestClient.java +++ b/wa/bootstrap/src/main/java/org/apache/syncope/wa/WARestClient.java @@ -18,6 +18,8 @@ */ package org.apache.syncope.wa; +import org.apereo.cas.util.spring.ApplicationContextProvider; + import org.apache.syncope.client.lib.AnonymousAuthenticationHandler; import org.apache.syncope.client.lib.SyncopeClient; import org.apache.syncope.client.lib.SyncopeClientFactoryBean; @@ -26,13 +28,14 @@ import org.apache.syncope.common.keymaster.client.api.ServiceOps; import org.apache.syncope.common.keymaster.client.api.model.NetworkService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.context.ApplicationContext; + +import java.util.Collection; public class WARestClient { private static final Logger LOG = LoggerFactory.getLogger(WARestClient.class); - private final ServiceOps serviceOps; - private final String anonymousUser; private final String anonymousKey; @@ -42,12 +45,10 @@ public class WARestClient { private SyncopeClient client; public WARestClient( - final ServiceOps serviceOps, final String anonymousUser, final String anonymousKey, final boolean useGZIPCompression) { - this.serviceOps = serviceOps; this.anonymousUser = anonymousUser; this.anonymousKey = anonymousKey; this.useGZIPCompression = useGZIPCompression; @@ -70,14 +71,29 @@ public class WARestClient { return client; } - private NetworkService getCore() { - return serviceOps.get(NetworkService.Type.CORE); + private static NetworkService getCore() { + try { + final ApplicationContext context = ApplicationContextProvider.getApplicationContext(); + if (context == null) { + return null; + } + + Collection<ServiceOps> serviceOpsList = context.getBeansOfType(ServiceOps.class).values(); + if (serviceOpsList.isEmpty()) { + return null; + } + ServiceOps serviceOps = serviceOpsList.iterator().next(); + return serviceOps.get(NetworkService.Type.CORE); + } catch (KeymasterException e) { + LOG.trace(e.getMessage()); + } + return null; } - public boolean isReady() { + public static boolean isReady() { try { return getCore() != null; - } catch (KeymasterException e) { + } catch (Exception e) { LOG.trace(e.getMessage()); } return false; diff --git a/wa/bootstrap/src/main/java/org/apache/syncope/wa/bootstrap/SyncopeWABootstrapConfiguration.java b/wa/bootstrap/src/main/java/org/apache/syncope/wa/bootstrap/SyncopeWABootstrapConfiguration.java index e9fdfa9..2330c3f 100644 --- a/wa/bootstrap/src/main/java/org/apache/syncope/wa/bootstrap/SyncopeWABootstrapConfiguration.java +++ b/wa/bootstrap/src/main/java/org/apache/syncope/wa/bootstrap/SyncopeWABootstrapConfiguration.java @@ -18,25 +18,18 @@ */ package org.apache.syncope.wa.bootstrap; -import org.apache.syncope.common.keymaster.client.api.ServiceOps; -import org.apache.syncope.common.keymaster.client.self.SelfKeymasterClientContext; -import org.apache.syncope.common.keymaster.client.zookeper.ZookeeperKeymasterClientContext; import org.apache.syncope.wa.WARestClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; @Configuration(proxyBeanMethods = false) -@ImportAutoConfiguration(classes = {ZookeeperKeymasterClientContext.class, SelfKeymasterClientContext.class}) @PropertySource("classpath:wa.properties") @PropertySource(value = "file:${conf.directory}/wa.properties", ignoreResourceNotFound = true) public class SyncopeWABootstrapConfiguration { - - @Value("${anonymousUser}") private String anonymousUser; @@ -46,10 +39,9 @@ public class SyncopeWABootstrapConfiguration { @Value("${useGZIPCompression}") private boolean useGZIPCompression; - @Autowired @Bean - public WARestClient waRestClient(final ServiceOps serviceOps) { - return new WARestClient(serviceOps, anonymousUser, anonymousKey, useGZIPCompression); + public WARestClient waRestClient() { + return new WARestClient(anonymousUser, anonymousKey, useGZIPCompression); } @Autowired diff --git a/wa/bootstrap/src/main/java/org/apache/syncope/wa/bootstrap/SyncopeWAPropertySourceLocator.java b/wa/bootstrap/src/main/java/org/apache/syncope/wa/bootstrap/SyncopeWAPropertySourceLocator.java index c60284e..c55edbb 100644 --- a/wa/bootstrap/src/main/java/org/apache/syncope/wa/bootstrap/SyncopeWAPropertySourceLocator.java +++ b/wa/bootstrap/src/main/java/org/apache/syncope/wa/bootstrap/SyncopeWAPropertySourceLocator.java @@ -29,21 +29,39 @@ import org.springframework.core.env.MapPropertySource; import org.springframework.core.env.PropertySource; import java.util.HashMap; +import java.util.Map; @Order public class SyncopeWAPropertySourceLocator implements PropertySourceLocator { private static final Logger LOG = LoggerFactory.getLogger(SyncopeWABootstrapConfiguration.class); + private final WARestClient waRestClient; public SyncopeWAPropertySourceLocator(final WARestClient waRestClient) { - + this.waRestClient = waRestClient; } @Override public PropertySource<?> locate(final Environment environment) { try { - LOG.info("Bootstrapping WA configuration"); - return new MapPropertySource(getClass().getName(), new HashMap<>()); + Map<String, Object> properties = new HashMap<>(); + if (WARestClient.isReady()) { + LOG.info("Bootstrapping WA configuration"); + /* + String content = WebClient.create(URI.create("https://demo5926981.mockable.io/casproperties")). + accept(MediaType.APPLICATION_JSON_TYPE). + get(). + readEntity(String.class); + + properties.putAll(MAPPER.readValue(content, new TypeReference<Map<String, Object>>() { + })); + LOG.debug("Loaded properties {}", properties); + */ + return new MapPropertySource(getClass().getName(), properties); + } + + LOG.warn("Application context is not ready to bootstrap WA configuration"); + return null; } catch (Exception e) { throw new IllegalArgumentException("Unable to fetch settings", e); }
