You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cs...@apache.org on 2020/05/25 12:32:56 UTC
[sling-org-apache-sling-distribution-journal] branch
SLING-9464-ManagedServiceFactory created (now 709cfd7)
This is an automated email from the ASF dual-hosted git repository.
cschneider pushed a change to branch SLING-9464-ManagedServiceFactory
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-distribution-journal.git.
at 709cfd7 SLING-9464 - Use a ManagedServiceFactory to avoid polling
This branch includes the following new commits:
new 709cfd7 SLING-9464 - Use a ManagedServiceFactory to avoid polling
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[sling-org-apache-sling-distribution-journal] 01/01: SLING-9464 -
Use a ManagedServiceFactory to avoid polling
Posted by cs...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
cschneider pushed a commit to branch SLING-9464-ManagedServiceFactory
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-distribution-journal.git
commit 709cfd70a924c51b95d54ab1aa945ec98ed94d93
Author: Christian Schneider <cs...@adobe.com>
AuthorDate: Mon May 25 14:32:34 2020 +0200
SLING-9464 - Use a ManagedServiceFactory to avoid polling
---
.../shared/PublisherConfigurationAvailable.java | 49 ++++-----
.../PublisherConfigurationAvailableTest.java | 116 ---------------------
2 files changed, 20 insertions(+), 145 deletions(-)
diff --git a/src/main/java/org/apache/sling/distribution/journal/impl/shared/PublisherConfigurationAvailable.java b/src/main/java/org/apache/sling/distribution/journal/impl/shared/PublisherConfigurationAvailable.java
index c0a6db4..eb3bf67 100644
--- a/src/main/java/org/apache/sling/distribution/journal/impl/shared/PublisherConfigurationAvailable.java
+++ b/src/main/java/org/apache/sling/distribution/journal/impl/shared/PublisherConfigurationAvailable.java
@@ -18,14 +18,15 @@
*/
package org.apache.sling.distribution.journal.impl.shared;
-import java.io.IOException;
+import java.util.Dictionary;
import org.apache.sling.distribution.journal.impl.publisher.DistributionPublisher;
import org.osgi.framework.BundleContext;
-import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.framework.Constants;
import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.cm.Configuration;
import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.cm.ConfigurationException;
+import org.osgi.service.cm.ManagedServiceFactory;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;
@@ -33,11 +34,6 @@ import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import static org.apache.sling.commons.scheduler.Scheduler.PROPERTY_SCHEDULER_CONCURRENT;
-import static org.apache.sling.commons.scheduler.Scheduler.PROPERTY_SCHEDULER_IMMEDIATE;
-import static org.apache.sling.commons.scheduler.Scheduler.PROPERTY_SCHEDULER_PERIOD;
-import static org.osgi.service.cm.ConfigurationAdmin.SERVICE_FACTORYPID;
-
/**
* This task periodically checks for DistributionPublisher agent
* configuration availability and registers the marker service
@@ -47,13 +43,9 @@ import static org.osgi.service.cm.ConfigurationAdmin.SERVICE_FACTORYPID;
* This task is meant to be executed on every instance, even in a cluster.
*/
@Component(
- property = {
- PROPERTY_SCHEDULER_CONCURRENT + ":Boolean=false",
- PROPERTY_SCHEDULER_IMMEDIATE + ":Boolean=false",
- PROPERTY_SCHEDULER_PERIOD + ":Long=" + 60, // 1 minute
- }
+ property = { Constants.SERVICE_PID + "=" + DistributionPublisher.FACTORY_PID}
)
-public class PublisherConfigurationAvailable implements Runnable {
+public class PublisherConfigurationAvailable implements ManagedServiceFactory {
private static final Logger LOG = LoggerFactory.getLogger(PublisherConfigurationAvailable.class);
@@ -81,28 +73,27 @@ public class PublisherConfigurationAvailable implements Runnable {
}
}
+ protected boolean isAvailable() {
+ return reg != null;
+ }
+
@Override
- public void run() {
+ public String getName() {
+ return "DistributionPublisher marker";
+ }
+
+ @Override
+ public void updated(String pid, Dictionary<String, ?> properties) throws ConfigurationException {
synchronized (lock) {
- if (reg == null && hasPublisherConfigurations()) {
+ if (reg == null) {
reg = context.registerService(PublisherConfigurationAvailable.class, this, null);
LOG.info("Registered marker service");
}
}
}
- protected boolean isAvailable() {
- return reg != null;
- }
-
- private boolean hasPublisherConfigurations() {
- String filter = "(" + SERVICE_FACTORYPID + "=" + DistributionPublisher.FACTORY_PID + ")";
- try {
- Configuration[] configs = configAdmin.listConfigurations(filter);
- return configs != null && configs.length > 0;
- } catch (IOException | InvalidSyntaxException e) {
- LOG.warn("Failed to search for DistributionPublisher agent configurations", e);
- }
- return false;
+ @Override
+ public void deleted(String pid) {
+ // We keep the registration up even if the config goes away
}
}
diff --git a/src/test/java/org/apache/sling/distribution/journal/impl/shared/PublisherConfigurationAvailableTest.java b/src/test/java/org/apache/sling/distribution/journal/impl/shared/PublisherConfigurationAvailableTest.java
deleted file mode 100644
index 9b968e9..0000000
--- a/src/test/java/org/apache/sling/distribution/journal/impl/shared/PublisherConfigurationAvailableTest.java
+++ /dev/null
@@ -1,116 +0,0 @@
-/*
- * 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.distribution.journal.impl.shared;
-
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.mockito.InjectMocks;
-import org.mockito.Mock;
-import org.mockito.runners.MockitoJUnitRunner;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.cm.Configuration;
-import org.osgi.service.cm.ConfigurationAdmin;
-
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.*;
-import static org.mockito.Mockito.when;
-
-@RunWith(MockitoJUnitRunner.class)
-public class PublisherConfigurationAvailableTest {
-
- @InjectMocks
- private PublisherConfigurationAvailable pca;
-
- @Mock
- private BundleContext context;
-
- @Mock
- private ConfigurationAdmin configAdmin;
-
- @Before
- public void before() throws Exception {
- ServiceRegistration<PublisherConfigurationAvailable> serviceReg = mock(ServiceRegistration.class);
- when(context.registerService(eq(PublisherConfigurationAvailable.class), any(PublisherConfigurationAvailable.class), any()))
- .thenReturn(serviceReg);
- when(configAdmin.listConfigurations(anyString()))
- .thenReturn(null);
- pca.activate(context);
- }
-
- @After
- public void after() {
- pca.deactivate();
- }
-
- @Test
- public void testNoConfiguration() {
- assertFalse(pca.isAvailable());
- pca.run();
- assertFalse(pca.isAvailable());
- }
-
- @Test
- public void testWithZeroConfiguration() throws Exception {
- assertFalse(pca.isAvailable());
- addConfigurations(0);
- pca.run();
- assertFalse(pca.isAvailable());
- }
-
- @Test
- public void testWithOneConfiguration() throws Exception {
- assertFalse(pca.isAvailable());
- addConfigurations(1);
- pca.run();
- assertTrue(pca.isAvailable());
- }
-
- @Test
- public void testWithManyConfigurations() throws Exception {
- assertFalse(pca.isAvailable());
- addConfigurations(10);
- pca.run();
- assertTrue(pca.isAvailable());
- }
-
- @Test
- public void testRemainAvailable() throws Exception {
- addConfigurations(1);
- pca.run();
- assertTrue(pca.isAvailable());
- removeAllConfigurations();
- pca.run();
- assertTrue(pca.isAvailable());
- }
-
- private void removeAllConfigurations() throws Exception {
- when(configAdmin.listConfigurations(anyString()))
- .thenReturn(null);
- }
-
- private void addConfigurations(int nbConfigurations) throws Exception {
- when(configAdmin.listConfigurations(anyString()))
- .thenReturn(new Configuration[nbConfigurations]);
- }
-
-}
\ No newline at end of file