Skip to content

Commit

Permalink
Better handling of ZookeeperDomainOps#afterPropertiesSet
Browse files Browse the repository at this point in the history
  • Loading branch information
ilgrosso committed Sep 12, 2023
1 parent 0cb5081 commit c6b2216
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ public class ZookeeperDomainOps implements DomainOps, InitializingBean {

protected static final String DOMAIN_PATH = "/domains";

protected static String buildDomainPath(final String... parts) {
String prefix = DOMAIN_PATH;
String suffix = StringUtils.EMPTY;
if (parts != null && parts.length > 0) {
suffix = '/' + String.join("/", parts);
}
return prefix + suffix;
}

@Autowired
protected CuratorFramework client;

Expand Down Expand Up @@ -93,15 +102,6 @@ public void afterPropertiesSet() throws Exception {
}
}

protected static String buildDomainPath(final String... parts) {
String prefix = DOMAIN_PATH;
String suffix = StringUtils.EMPTY;
if (parts != null && parts.length > 0) {
suffix = '/' + String.join("/", parts);
}
return prefix + suffix;
}

@Override
public List<Domain> list() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@
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 @@ -254,7 +253,7 @@ public DomainRegistry domainRegistry(final ConfigurableApplicationContext ctx) {
public RuntimeDomainLoader runtimeDomainLoader(
final DomainHolder domainHolder,
final DomainRegistry domainRegistry,
final ApplicationContext ctx) {
final ConfigurableApplicationContext ctx) {

return new RuntimeDomainLoader(domainHolder, domainRegistry, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,11 @@
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;
import org.springframework.context.ConfigurableApplicationContext;

public class RuntimeDomainLoader implements DomainWatcher {

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

protected final DomainRegistry domainRegistry;

protected final ApplicationContext ctx;

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

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

// only needed by ZookeeperDomainOps' early init on afterPropertiesSet
if (ApplicationContextProvider.getApplicationContext() == null) {
ApplicationContextProvider.setApplicationContext(ctx);
}
}

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

domainRegistry.register(domain);

ctx.getBeansOfType(SyncopeCoreLoader.class).values().
ApplicationContextProvider.getBeanFactory().getBeansOfType(SyncopeCoreLoader.class).values().
stream().sorted(Comparator.comparing(SyncopeCoreLoader::getOrder)).
forEachOrdered(loader -> {
String loaderName = AopUtils.getTargetClass(loader).getName();
Expand All @@ -77,7 +80,7 @@ public void removed(final String domain) {
if (domainHolder.getDomains().containsKey(domain)) {
LOG.info("Domain {} unregistration", domain);

ctx.getBeansOfType(SyncopeCoreLoader.class).values().
ApplicationContextProvider.getBeanFactory().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 @@ -112,36 +112,40 @@ protected void unregister(final String resourceKey) {
@SuppressWarnings("unchecked")
@Override
public void afterCommit(final RemoteCommitEvent event) {
((Collection<Object>) event.getPersistedObjectIds()).stream().
filter(StringId.class::isInstance).
map(StringId.class::cast).
forEach(id -> {
if (JPAExternalResource.class.isAssignableFrom(id.getType())) {
registerForExternalResource(id.getId());
} else if (JPAConnInstance.class.isAssignableFrom(id.getType())) {
registerForConnInstance(id.getId());
}
});

((Collection<Object>) event.getUpdatedObjectIds()).stream().
filter(StringId.class::isInstance).
map(StringId.class::cast).
forEach(id -> {
if (JPAExternalResource.class.isAssignableFrom(id.getType())) {
registerForExternalResource(id.getId());
} else if (JPAConnInstance.class.isAssignableFrom(id.getType())) {
registerForConnInstance(id.getId());
}
});

((Collection<Object>) event.getDeletedObjectIds()).stream().
filter(StringId.class::isInstance).
map(StringId.class::cast).
forEach(id -> {
if (JPAExternalResource.class.isAssignableFrom(id.getType())) {
unregister(id.getId());
}
});
if (event.getPayloadType() == RemoteCommitEvent.PAYLOAD_OIDS_WITH_ADDS) {
((Collection<Object>) event.getPersistedObjectIds()).stream().
filter(StringId.class::isInstance).
map(StringId.class::cast).
forEach(id -> {
if (JPAExternalResource.class.isAssignableFrom(id.getType())) {
registerForExternalResource(id.getId());
} else if (JPAConnInstance.class.isAssignableFrom(id.getType())) {
registerForConnInstance(id.getId());
}
});
}

if (event.getPayloadType() != RemoteCommitEvent.PAYLOAD_EXTENTS) {
((Collection<Object>) event.getUpdatedObjectIds()).stream().
filter(StringId.class::isInstance).
map(StringId.class::cast).
forEach(id -> {
if (JPAExternalResource.class.isAssignableFrom(id.getType())) {
registerForExternalResource(id.getId());
} else if (JPAConnInstance.class.isAssignableFrom(id.getType())) {
registerForConnInstance(id.getId());
}
});

((Collection<Object>) event.getDeletedObjectIds()).stream().
filter(StringId.class::isInstance).
map(StringId.class::cast).
forEach(id -> {
if (JPAExternalResource.class.isAssignableFrom(id.getType())) {
unregister(id.getId());
}
});
}
}

@Override
Expand Down

0 comments on commit c6b2216

Please sign in to comment.