You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by ap...@apache.org on 2022/06/23 15:35:21 UTC
[sling-org-apache-sling-discovery-support] branch master updated: SLING-11355: sling discovery bundles: update to sling 48 (#2)
This is an automated email from the ASF dual-hosted git repository.
apelluru pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-discovery-support.git
The following commit(s) were added to refs/heads/master by this push:
new 66ec820 SLING-11355: sling discovery bundles: update to sling 48 (#2)
66ec820 is described below
commit 66ec820e69a826b16864b0b6072adbb1f44d4c4f
Author: Ashok Pelluru <37...@users.noreply.github.com>
AuthorDate: Thu Jun 23 17:35:17 2022 +0200
SLING-11355: sling discovery bundles: update to sling 48 (#2)
* SLING-11355: sling discovery bundles: organize the imports
* SLING-11355: sling discovery bundles: fixing sonar cloud issues
* SLING-11355: upgraded to sling 48
* SLING-11355: use lambda syntax
---
pom.xml | 45 +++++----
.../discovery/impl/support/MetricReporter.java | 112 ++++-----------------
.../impl/support/StandardPropertyProvider.java | 58 ++++++-----
.../impl/support/StandardPropertyProviderTest.java | 70 +++++++++++++
4 files changed, 141 insertions(+), 144 deletions(-)
diff --git a/pom.xml b/pom.xml
index 16052b3..de4b59c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -22,13 +22,11 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.sling</groupId>
- <artifactId>sling</artifactId>
- <version>26</version>
- <relativePath />
+ <artifactId>sling-bundle-parent</artifactId>
+ <version>48</version>
</parent>
<artifactId>org.apache.sling.discovery.support</artifactId>
- <packaging>bundle</packaging>
<version>1.0.7-SNAPSHOT</version>
<name>Apache Sling Discovery Support Bundle</name>
@@ -36,26 +34,18 @@
Supporting services for the discovery
</description>
+ <properties>
+ <project.build.outputTimestamp>1</project.build.outputTimestamp>
+ <sling.java.version>8</sling.java.version>
+ </properties>
+
<scm>
<connection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-discovery-support.git</connection>
<developerConnection>scm:git:https://gitbox.apache.org/repos/asf/sling-org-apache-sling-discovery-support.git</developerConnection>
<url>https://gitbox.apache.org/repos/asf?p=sling-org-apache-sling-discovery-support.git</url>
- <tag>HEAD</tag>
- </scm>
+ <tag>HEAD</tag>
+ </scm>
- <build>
- <plugins>
- <plugin>
- <groupId>org.apache.felix</groupId>
- <artifactId>maven-scr-plugin</artifactId>
- </plugin>
- <plugin>
- <groupId>org.apache.felix</groupId>
- <artifactId>maven-bundle-plugin</artifactId>
- <extensions>true</extensions>
- </plugin>
- </plugins>
- </build>
<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
@@ -64,11 +54,16 @@
</dependency>
<dependency>
<groupId>org.osgi</groupId>
- <artifactId>org.osgi.core</artifactId>
+ <artifactId>osgi.core</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.osgi</groupId>
+ <artifactId>org.osgi.service.component.annotations</artifactId>
</dependency>
<dependency>
<groupId>org.osgi</groupId>
- <artifactId>org.osgi.compendium</artifactId>
+ <artifactId>org.osgi.service.http</artifactId>
+ <version>1.2.1</version>
</dependency>
<dependency>
<groupId>org.apache.sling</groupId>
@@ -79,7 +74,7 @@
<dependency>
<groupId>org.apache.sling</groupId>
<artifactId>org.apache.sling.settings</artifactId>
- <version>1.3.0</version>
+ <version>1.4.2</version>
<scope>provided</scope>
</dependency>
<dependency>
@@ -100,5 +95,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.sling</groupId>
+ <artifactId>org.apache.sling.testing.osgi-mock.junit4</artifactId>
+ <version>3.2.2</version>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
diff --git a/src/main/java/org/apache/sling/discovery/impl/support/MetricReporter.java b/src/main/java/org/apache/sling/discovery/impl/support/MetricReporter.java
index 48db238..172ebf9 100644
--- a/src/main/java/org/apache/sling/discovery/impl/support/MetricReporter.java
+++ b/src/main/java/org/apache/sling/discovery/impl/support/MetricReporter.java
@@ -24,17 +24,16 @@ import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
-import org.apache.felix.scr.annotations.Activate;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Deactivate;
-import org.apache.felix.scr.annotations.Reference;
-import org.apache.felix.scr.annotations.Service;
import org.apache.sling.discovery.ClusterView;
import org.apache.sling.discovery.InstanceDescription;
import org.apache.sling.discovery.TopologyEvent;
import org.apache.sling.discovery.TopologyEventListener;
import org.apache.sling.discovery.TopologyView;
import org.apache.sling.discovery.commons.InstancesDiff;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -46,8 +45,7 @@ import com.codahale.metrics.MetricRegistry;
* (by virtue of being a TopologyEventListener) and exposing
* metrics (via dropwizard).
*/
-@Service(value = { TopologyEventListener.class })
-@Component(immediate = true)
+@Component(immediate = true, service = { TopologyEventListener.class })
public class MetricReporter implements TopologyEventListener {
// event counters
@@ -75,7 +73,7 @@ public class MetricReporter implements TopologyEventListener {
@Reference(target = "(name=sling)")
MetricRegistry metricRegistry;
- private final List<String> registeredGaugeNameList = new LinkedList<String>();
+ private final List<String> registeredGaugeNameList = new LinkedList<>();
/**
* for init there would really only be 2 values needed: 0 and 1
@@ -175,103 +173,33 @@ public class MetricReporter implements TopologyEventListener {
protected void activate() {
logger.debug("activate: start");
- createGauge(METRICS_NAME_TOPOLOGY_INIT_EVENTS, new Gauge<Integer>() {
- @Override
- public Integer getValue() {
- return initEvents.get();
- }
- });
+ createGauge(METRICS_NAME_TOPOLOGY_INIT_EVENTS, (Gauge<Integer>) initEvents::get);
- createGauge(METRICS_NAME_TOPOLOGY_CHANGING_EVENTS, new Gauge<Integer>() {
- @Override
- public Integer getValue() {
- return changingEvents.get();
- }
- });
+ createGauge(METRICS_NAME_TOPOLOGY_CHANGING_EVENTS, (Gauge<Integer>) changingEvents::get);
- createGauge(METRICS_NAME_TOPOLOGY_CHANGED_EVENTS, new Gauge<Integer>() {
- @Override
- public Integer getValue() {
- return changedEvents.get();
- }
- });
+ createGauge(METRICS_NAME_TOPOLOGY_CHANGED_EVENTS, (Gauge<Integer>) changedEvents::get);
- createGauge(METRICS_NAME_PROPERTY_CHANGED_EVENTS, new Gauge<Long>() {
- @Override
- public Long getValue() {
- return propertyChangedEvents.get();
- }
- });
+ createGauge(METRICS_NAME_PROPERTY_CHANGED_EVENTS, (Gauge<Long>) propertyChangedEvents::get);
- createGauge(METRICS_NAME_TOPOLOGY_IS_UNDEFINED, new Gauge<Integer>() {
- @Override
- public Integer getValue() {
- return topologyIsUndefined.get();
- }
- });
+ createGauge(METRICS_NAME_TOPOLOGY_IS_UNDEFINED, (Gauge<Integer>) topologyIsUndefined::get);
- createGauge(METRICS_NAME_LOCAL_CLUSTER_INSTANCES, new Gauge<Integer>() {
- @Override
- public Integer getValue() {
- return localClusterInstances.get();
- }
- });
+ createGauge(METRICS_NAME_LOCAL_CLUSTER_INSTANCES, (Gauge<Integer>) localClusterInstances::get);
- createGauge(METRICS_NAME_LOCAL_CLUSTER_JOINS, new Gauge<Integer>() {
- @Override
- public Integer getValue() {
- return localClusterJoins.get();
- }
- });
+ createGauge(METRICS_NAME_LOCAL_CLUSTER_JOINS, (Gauge<Integer>) localClusterJoins::get);
- createGauge(METRICS_NAME_LOCAL_CLUSTER_LEAVES, new Gauge<Integer>() {
- @Override
- public Integer getValue() {
- return localClusterLeaves.get();
- }
- });
+ createGauge(METRICS_NAME_LOCAL_CLUSTER_LEAVES, (Gauge<Integer>) localClusterLeaves::get);
- createGauge(METRICS_NAME_LOCAL_CLUSTER_LEADER_SWITCHES, new Gauge<Integer>() {
- @Override
- public Integer getValue() {
- return localClusterLeaderSwitches.get();
- }
- });
+ createGauge(METRICS_NAME_LOCAL_CLUSTER_LEADER_SWITCHES, (Gauge<Integer>) localClusterLeaderSwitches::get);
- createGauge(METRICS_NAME_LOCAL_CLUSTER_PROPERTIES, new Gauge<Integer>() {
- @Override
- public Integer getValue() {
- return localClusterProperties.get();
- }
- });
+ createGauge(METRICS_NAME_LOCAL_CLUSTER_PROPERTIES, (Gauge<Integer>) localClusterProperties::get);
- createGauge(METRICS_NAME_OWN_IS_LEADER, new Gauge<Integer>() {
- @Override
- public Integer getValue() {
- return ownIsLeader.get();
- }
- });
+ createGauge(METRICS_NAME_OWN_IS_LEADER, (Gauge<Integer>) ownIsLeader::get);
- createGauge(METRICS_NAME_OWN_PROPERTIES, new Gauge<Integer>() {
- @Override
- public Integer getValue() {
- return ownProperties.get();
- }
- });
+ createGauge(METRICS_NAME_OWN_PROPERTIES, (Gauge<Integer>) ownProperties::get);
- createGauge(METRICS_NAME_REMOTE_CLUSTERS, new Gauge<Integer>() {
- @Override
- public Integer getValue() {
- return remoteClusters.get();
- }
- });
+ createGauge(METRICS_NAME_REMOTE_CLUSTERS, (Gauge<Integer>) remoteClusters::get);
- createGauge(METRICS_NAME_REMOTE_INSTANCES, new Gauge<Integer>() {
- @Override
- public Integer getValue() {
- return remoteInstances.get();
- }
- });
+ createGauge(METRICS_NAME_REMOTE_INSTANCES, (Gauge<Integer>) remoteInstances::get);
logger.info("activate: done.");
}
diff --git a/src/main/java/org/apache/sling/discovery/impl/support/StandardPropertyProvider.java b/src/main/java/org/apache/sling/discovery/impl/support/StandardPropertyProvider.java
index 48a0fcf..ac8e545 100644
--- a/src/main/java/org/apache/sling/discovery/impl/support/StandardPropertyProvider.java
+++ b/src/main/java/org/apache/sling/discovery/impl/support/StandardPropertyProvider.java
@@ -26,13 +26,6 @@ import java.util.Hashtable;
import java.util.List;
import java.util.Map;
-import org.apache.felix.scr.annotations.Activate;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Deactivate;
-import org.apache.felix.scr.annotations.Modified;
-import org.apache.felix.scr.annotations.Reference;
-import org.apache.felix.scr.annotations.ReferenceCardinality;
-import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.sling.discovery.InstanceDescription;
import org.apache.sling.discovery.PropertyProvider;
import org.apache.sling.settings.SlingSettingsService;
@@ -40,15 +33,19 @@ import org.osgi.framework.Constants;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.component.ComponentContext;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Modified;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.component.annotations.ReferenceCardinality;
+import org.osgi.service.component.annotations.ReferencePolicy;
import org.osgi.service.http.HttpService;
/**
* This service provides the standard instance properties (if available)
*/
@Component(immediate=true)
-@Reference(referenceInterface=HttpService.class,
- cardinality=ReferenceCardinality.OPTIONAL_MULTIPLE,
- policy=ReferencePolicy.DYNAMIC)
public class StandardPropertyProvider {
/** Official endpoint service registration property from Http Whiteboard spec */
@@ -61,14 +58,15 @@ public class StandardPropertyProvider {
private ServiceRegistration propagationService;
- private final Map<Long, String[]> endpoints = new HashMap<Long, String[]>();
+ private final Map<Long, String[]> endpoints = new HashMap<>();
private String endpointString;
- @Reference SlingSettingsService settings;
+ @Reference
+ SlingSettingsService settings;
private Dictionary<String, Object> getRegistrationProperties() {
- final List<String> names = new ArrayList<String>();
+ final List<String> names = new ArrayList<>();
names.add(InstanceDescription.PROPERTY_NAME);
names.add(InstanceDescription.PROPERTY_DESCRIPTION);
names.add(InstanceDescription.PROPERTY_ENDPOINTS);
@@ -89,7 +87,7 @@ public class StandardPropertyProvider {
}
this.endpointString = sb.toString();
- final Dictionary<String, Object> serviceProps = new Hashtable<String, Object>();
+ final Dictionary<String, Object> serviceProps = new Hashtable<>();
serviceProps.put(PropertyProvider.PROPERTY_PROPERTIES, names.toArray(new String[names.size()]));
// we add a changing property to the service registration
// to make sure a modification event is really sent
@@ -107,21 +105,17 @@ public class StandardPropertyProvider {
@Modified
protected void modified(final ComponentContext cc) {
this.propagationService = cc.getBundleContext().registerService(PropertyProvider.class.getName(),
- new PropertyProvider() {
-
- @Override
- public String getProperty(final String name) {
- if ( InstanceDescription.PROPERTY_DESCRIPTION.equals(name) ) {
- return settings.getSlingDescription();
- }
- if ( InstanceDescription.PROPERTY_NAME.equals(name) ) {
- return settings.getSlingName();
- }
- if ( InstanceDescription.PROPERTY_ENDPOINTS.equals(name) ) {
- return endpointString;
- }
- return null;
+ (PropertyProvider) name -> {
+ if ( InstanceDescription.PROPERTY_DESCRIPTION.equals(name) ) {
+ return settings.getSlingDescription();
}
+ if ( InstanceDescription.PROPERTY_NAME.equals(name) ) {
+ return settings.getSlingName();
+ }
+ if ( InstanceDescription.PROPERTY_ENDPOINTS.equals(name) ) {
+ return endpointString;
+ }
+ return null;
}, this.getRegistrationProperties());
}
@@ -136,6 +130,10 @@ public class StandardPropertyProvider {
/**
* Bind a http service
*/
+ @Reference(service = HttpService.class,
+ cardinality = ReferenceCardinality.MULTIPLE,
+ policy = ReferencePolicy.DYNAMIC,
+ bind = "bindHttpService", unbind = "unbindHttpService")
protected void bindHttpService(final ServiceReference reference) {
String[] endpointUrls = toStringArray(reference.getProperty(REG_PROPERTY_ENDPOINTS));
if ( endpointUrls == null ) {
@@ -182,7 +180,7 @@ public class StandardPropertyProvider {
} else if (propValue.getClass().isArray()) {
// other array
Object[] valueArray = (Object[]) propValue;
- List<String> values = new ArrayList<String>(valueArray.length);
+ List<String> values = new ArrayList<>(valueArray.length);
for (Object value : valueArray) {
if (value != null) {
values.add(value.toString());
@@ -193,7 +191,7 @@ public class StandardPropertyProvider {
} else if (propValue instanceof Collection<?>) {
// collection
Collection<?> valueCollection = (Collection<?>) propValue;
- List<String> valueList = new ArrayList<String>(valueCollection.size());
+ List<String> valueList = new ArrayList<>(valueCollection.size());
for (Object value : valueCollection) {
if (value != null) {
valueList.add(value.toString());
diff --git a/src/test/java/org/apache/sling/discovery/impl/support/StandardPropertyProviderTest.java b/src/test/java/org/apache/sling/discovery/impl/support/StandardPropertyProviderTest.java
new file mode 100644
index 0000000..8f946b1
--- /dev/null
+++ b/src/test/java/org/apache/sling/discovery/impl/support/StandardPropertyProviderTest.java
@@ -0,0 +1,70 @@
+/*
+ * 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.discovery.impl.support;
+
+import org.apache.sling.settings.SlingSettingsService;
+import org.apache.sling.settings.impl.SlingSettingsServiceImpl;
+import org.apache.sling.testing.mock.osgi.junit.OsgiContext;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.osgi.framework.ServiceReference;
+
+import static org.junit.Assert.assertNotNull;
+
+public class StandardPropertyProviderTest {
+
+ private StandardPropertyProvider standardPropertyProvider;
+
+ @Rule
+ public final OsgiContext context = new OsgiContext();
+
+ @Before
+ public void setup() {
+ final SlingSettingsService settings = new SlingSettingsServiceImpl("");
+ context.registerService(settings);
+ standardPropertyProvider = context.registerInjectActivateService(new StandardPropertyProvider(),
+ "test.property", "Test");
+ }
+
+ @Test
+ public void testNotNull() {
+ ServiceReference reference1 = Mockito.mock(ServiceReference.class);
+
+ //REG_PROPERTY_ENDPOINTS is Empty
+ standardPropertyProvider.bindHttpService(reference1);
+ assertNotNull(standardPropertyProvider);
+ standardPropertyProvider.unbindHttpService(reference1);
+
+ //REG_PROPERTY_ENDPOINTS is String
+ Mockito.when(reference1.getProperty("osgi.http.endpoint")).thenReturn("http://localhost:8080/");
+
+ standardPropertyProvider.bindHttpService(reference1);
+ assertNotNull(standardPropertyProvider);
+ standardPropertyProvider.unbindHttpService(reference1);
+
+ //REG_PROPERTY_ENDPOINTS is String[]
+ Mockito.when(reference1.getProperty("osgi.http.endpoint")).thenReturn(new String[]{"http://localhost:8080/"});
+
+ standardPropertyProvider.bindHttpService(reference1);
+ assertNotNull(standardPropertyProvider);
+ standardPropertyProvider.unbindHttpService(reference1);
+ }
+}
\ No newline at end of file