This is an automated email from the ASF dual-hosted git repository. dklco pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-app-cms.git
commit 4faa59464e8304e4485f49ce2cea147ec26646f2 Author: Dan Klco <[email protected]> AuthorDate: Mon Sep 21 16:13:50 2020 -0400 Adding support for dynamically updating the distribution targets based on a discovery topology --- core/pom.xml | 4 + .../ForwardAgentEndpointSynchronization.java | 100 +++++++++++++++++++++ .../ForwardAgentEndpointSynchronizationConfig.java | 27 ++++++ .../publication/PublicationPropertyProvider.java | 62 +++++++++++++ .../PublicationPropertyProviderConfig.java | 28 ++++++ 5 files changed, 221 insertions(+) diff --git a/core/pom.xml b/core/pom.xml index 139bfc4..af4f12d 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -237,5 +237,9 @@ <artifactId>org.apache.sling.distribution.api</artifactId> <groupId>org.apache.sling</groupId> </dependency> + <dependency> + <groupId>org.apache.sling</groupId> + <artifactId>org.apache.sling.discovery.api</artifactId> + </dependency> </dependencies> </project> diff --git a/core/src/main/java/org/apache/sling/cms/core/publication/ForwardAgentEndpointSynchronization.java b/core/src/main/java/org/apache/sling/cms/core/publication/ForwardAgentEndpointSynchronization.java new file mode 100644 index 0000000..940dc35 --- /dev/null +++ b/core/src/main/java/org/apache/sling/cms/core/publication/ForwardAgentEndpointSynchronization.java @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.cms.core.publication; + +import java.io.IOException; +import java.util.Arrays; +import java.util.Dictionary; +import java.util.Set; +import java.util.stream.Collectors; + +import org.apache.sling.cms.publication.INSTANCE_TYPE; +import org.apache.sling.discovery.InstanceDescription; +import org.apache.sling.discovery.TopologyEvent; +import org.apache.sling.discovery.TopologyEventListener; +import org.osgi.framework.InvalidSyntaxException; +import org.osgi.service.cm.Configuration; +import org.osgi.service.cm.ConfigurationAdmin; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.metatype.annotations.Designate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Component(service = TopologyEventListener.class, immediate = true) +@Designate(ocd = ForwardAgentEndpointSynchronizationConfig.class) +public class ForwardAgentEndpointSynchronization implements TopologyEventListener { + + private static final Logger log = LoggerFactory.getLogger(ForwardAgentEndpointSynchronization.class); + + private ForwardAgentEndpointSynchronizationConfig config; + + public static final String ENDPOINT_PROPERTY = "packageImporter.endpoints"; + + @Reference + private ConfigurationAdmin configAdmin; + + @Activate + public void activate(ForwardAgentEndpointSynchronizationConfig config) { + log.info("activate"); + this.config = config; + } + + private void updateInstances(Set<InstanceDescription> instances) { + log.info("updateInstances"); + + String[] endpoints = instances.stream().map(id -> { + String endpointBase = id.getProperty(InstanceDescription.PROPERTY_ENDPOINTS).split("\\,")[0]; + return endpointBase + id.getProperty(PublicationPropertyProvider.ENDPOINT_PATHS); + }).collect(Collectors.toList()).toArray(new String[0]); + if (log.isDebugEnabled()) { + log.debug("Updating with endpoints: [{}]", Arrays.stream(endpoints).collect(Collectors.joining(","))); + } + try { + log.debug("Updating configurations matching: {}", config.agentTarget()); + Configuration[] configurations = configAdmin.listConfigurations( + "(&(service.factoryPid=org.apache.sling.distribution.agent.impl.ForwardDistributionAgentFactory)" + + config.agentTarget() + ")"); + if (configurations != null) { + for (Configuration cfg : configurations) { + log.debug("Updating configuration {}", cfg.getPid()); + Dictionary<String, Object> properties = cfg.getProperties(); + if (!Arrays.equals(endpoints, (String[]) properties.get(ENDPOINT_PROPERTY))) { + properties.put(ENDPOINT_PROPERTY, endpoints); + cfg.update(properties); + log.debug("Configurations updated!"); + } else { + log.debug("Configurations match, not updating"); + } + } + } else { + log.warn("No applicable configurations found"); + } + } catch (IOException | InvalidSyntaxException e) { + log.error("Failed to update configuration", e); + } + } + + @Override + public void handleTopologyEvent(TopologyEvent event) { + Set<InstanceDescription> renderers = event.getNewView().findInstances(id -> INSTANCE_TYPE.RENDERER.toString() + .equals(id.getProperty(PublicationPropertyProvider.INSTANCE_TYPE))); + updateInstances(renderers); + } + +} diff --git a/core/src/main/java/org/apache/sling/cms/core/publication/ForwardAgentEndpointSynchronizationConfig.java b/core/src/main/java/org/apache/sling/cms/core/publication/ForwardAgentEndpointSynchronizationConfig.java new file mode 100644 index 0000000..1234c47 --- /dev/null +++ b/core/src/main/java/org/apache/sling/cms/core/publication/ForwardAgentEndpointSynchronizationConfig.java @@ -0,0 +1,27 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.cms.core.publication; + +import org.osgi.service.metatype.annotations.AttributeDefinition; +import org.osgi.service.metatype.annotations.ObjectClassDefinition; + +@ObjectClassDefinition(name = "%forwardagentsync.config.name", description = "%forwardagentsync.config.description", localization = "OSGI-INF/l10n/bundle") +public @interface ForwardAgentEndpointSynchronizationConfig { + + @AttributeDefinition(name = "%forwardagentsync.param.agentTarget.name", description = "%forwardagentsync.param.agentTarget.description") + String agentTarget(); +} diff --git a/core/src/main/java/org/apache/sling/cms/core/publication/PublicationPropertyProvider.java b/core/src/main/java/org/apache/sling/cms/core/publication/PublicationPropertyProvider.java new file mode 100644 index 0000000..7d46c57 --- /dev/null +++ b/core/src/main/java/org/apache/sling/cms/core/publication/PublicationPropertyProvider.java @@ -0,0 +1,62 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.cms.core.publication; + +import org.apache.sling.cms.publication.PublicationManagerFactory; +import org.apache.sling.discovery.PropertyProvider; +import org.osgi.service.component.annotations.Activate; +import org.osgi.service.component.annotations.Component; +import org.osgi.service.component.annotations.Reference; +import org.osgi.service.metatype.annotations.Designate; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +@Component(service = PropertyProvider.class, property = { + PropertyProvider.PROPERTY_PROPERTIES + "=" + PublicationPropertyProvider.ENDPOINT_PATHS, + PropertyProvider.PROPERTY_PROPERTIES + "=" + PublicationPropertyProvider.INSTANCE_TYPE }) +@Designate(ocd = PublicationPropertyProviderConfig.class) +public class PublicationPropertyProvider implements PropertyProvider { + + private static final Logger log = LoggerFactory.getLogger(PublicationPropertyProvider.class); + public static final String ENDPOINT_PATHS = "pub.endpointPaths"; + public static final String INSTANCE_TYPE = "pub.instanceType"; + + @Reference + private PublicationManagerFactory publicationManagerFactory; + + private PublicationPropertyProviderConfig config; + + @Activate + public void activate(PublicationPropertyProviderConfig config) { + log.info("activate"); + this.config = config; + } + + @Override + public String getProperty(String name) { + log.trace("getProperty({})", name); + if (ENDPOINT_PATHS.equals(name)) { + return config.endpointPath(); + } else if (INSTANCE_TYPE.equals(name)) { + return publicationManagerFactory.getInstanceType().toString(); + } + return null; + } + + + +} diff --git a/core/src/main/java/org/apache/sling/cms/core/publication/PublicationPropertyProviderConfig.java b/core/src/main/java/org/apache/sling/cms/core/publication/PublicationPropertyProviderConfig.java new file mode 100644 index 0000000..1e79c9f --- /dev/null +++ b/core/src/main/java/org/apache/sling/cms/core/publication/PublicationPropertyProviderConfig.java @@ -0,0 +1,28 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.sling.cms.core.publication; + +import org.osgi.service.metatype.annotations.AttributeDefinition; +import org.osgi.service.metatype.annotations.ObjectClassDefinition; + +@ObjectClassDefinition(name = "%publicationpropprovider.config.name", description = "%publicationpropprovider.config.description", localization = "OSGI-INF/l10n/bundle") +public @interface PublicationPropertyProviderConfig { + + @AttributeDefinition(name = "%publicationpropprovider.param.endpointPath.name", description = "%publicationpropprovider.param.endpointPath.description") + String endpointPath() default "libs/sling/distribution/services/importers/default" ; + +}
