Skip to content

Commit

Permalink
More robust handling of domain add when zookeeper is keymaster
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgrosso committed Sep 7, 2023
1 parent 852b1a0 commit 0cb5081
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
Expand Down Expand Up @@ -252,9 +253,10 @@ public DomainRegistry domainRegistry(final ConfigurableApplicationContext ctx) {
@Bean
public RuntimeDomainLoader runtimeDomainLoader(
final DomainHolder domainHolder,
final DomainRegistry domainRegistry) {
final DomainRegistry domainRegistry,
final ApplicationContext ctx) {

return new RuntimeDomainLoader(domainHolder, domainRegistry);
return new RuntimeDomainLoader(domainHolder, domainRegistry, ctx);
}

@ConditionalOnMissingBean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
import org.apache.syncope.core.persistence.api.DomainHolder;
import org.apache.syncope.core.persistence.api.DomainRegistry;
import org.apache.syncope.core.persistence.api.SyncopeCoreLoader;
import org.apache.syncope.core.spring.ApplicationContextProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.support.AopUtils;
import org.springframework.context.ApplicationContext;

public class RuntimeDomainLoader implements DomainWatcher {

Expand All @@ -37,12 +37,16 @@ public class RuntimeDomainLoader implements DomainWatcher {

protected final DomainRegistry domainRegistry;

protected final ApplicationContext ctx;

public RuntimeDomainLoader(
final DomainHolder domainHolder,
final DomainRegistry domainRegistry) {
final DomainRegistry domainRegistry,
final ApplicationContext ctx) {

this.domainHolder = domainHolder;
this.domainRegistry = domainRegistry;
this.ctx = ctx;
}

@Override
Expand All @@ -54,9 +58,9 @@ public void added(final Domain domain) {

domainRegistry.register(domain);

ApplicationContextProvider.getApplicationContext().getBeansOfType(SyncopeCoreLoader.class).values().
ctx.getBeansOfType(SyncopeCoreLoader.class).values().
stream().sorted(Comparator.comparing(SyncopeCoreLoader::getOrder)).
forEach(loader -> {
forEachOrdered(loader -> {
String loaderName = AopUtils.getTargetClass(loader).getName();

LOG.debug("[{}] Starting on domain '{}'", loaderName, domain);
Expand All @@ -73,7 +77,7 @@ public void removed(final String domain) {
if (domainHolder.getDomains().containsKey(domain)) {
LOG.info("Domain {} unregistration", domain);

ApplicationContextProvider.getApplicationContext().getBeansOfType(SyncopeCoreLoader.class).values().
ctx.getBeansOfType(SyncopeCoreLoader.class).values().
stream().sorted(Comparator.comparing(SyncopeCoreLoader::getOrder).reversed()).
forEachOrdered(loader -> {
String loaderName = AopUtils.getTargetClass(loader).getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.lang.reflect.Method;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import org.apache.syncope.common.keymaster.client.api.DomainWatcher;
import org.apache.syncope.common.keymaster.client.api.KeymasterException;
Expand Down Expand Up @@ -60,10 +61,8 @@ public List<Domain> list() {

@PreAuthorize("@environment.getProperty('keymaster.username') == authentication.name")
public Domain read(final String key) {
DomainEntity domain = domainDAO.find(key);
if (domain == null) {
throw new NotFoundException("Domain " + key);
}
DomainEntity domain = Optional.ofNullable(domainDAO.find(key)).
orElseThrow(() -> new NotFoundException("Domain " + key));

return domain.get();
}
Expand All @@ -90,10 +89,8 @@ public Domain create(final Domain domain) {

@PreAuthorize("@environment.getProperty('keymaster.username') == authentication.name")
public void changeAdminPassword(final String key, final String password, final CipherAlgorithm cipherAlgorithm) {
DomainEntity domain = domainDAO.find(key);
if (domain == null) {
throw new NotFoundException("Domain " + key);
}
DomainEntity domain = Optional.ofNullable(domainDAO.find(key)).
orElseThrow(() -> new NotFoundException("Domain " + key));

Domain domainObj = domain.get();
domainObj.setAdminPassword(password);
Expand All @@ -104,10 +101,8 @@ public void changeAdminPassword(final String key, final String password, final C

@PreAuthorize("@environment.getProperty('keymaster.username') == authentication.name")
public void adjustPoolSize(final String key, final int poolMaxActive, final int poolMinIdle) {
DomainEntity domain = domainDAO.find(key);
if (domain == null) {
throw new NotFoundException("Domain " + key);
}
DomainEntity domain = Optional.ofNullable(domainDAO.find(key)).
orElseThrow(() -> new NotFoundException("Domain " + key));

Domain domainObj = domain.get();
domainObj.setPoolMaxActive(poolMaxActive);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ under the License.

<slf4j.version>1.7.36</slf4j.version>

<elasticsearch.version>8.9.1</elasticsearch.version>
<elasticsearch.version>8.9.2</elasticsearch.version>

<log4j2.version>2.20.0</log4j2.version>
<disruptor.version>3.4.4</disruptor.version>
Expand Down

0 comments on commit 0cb5081

Please sign in to comment.