You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2021/09/19 05:08:07 UTC
[felix-dev] branch scrR8 updated: FELIX-6458 : (#101)
This is an automated email from the ASF dual-hosted git repository.
cziegeler pushed a commit to branch scrR8
in repository https://gitbox.apache.org/repos/asf/felix-dev.git
The following commit(s) were added to refs/heads/scrR8 by this push:
new 4e8021e FELIX-6458 : (#101)
4e8021e is described below
commit 4e8021e99acf75606ebda5a7d7416054ed0bc00a
Author: Carsten Ziegeler <cz...@apache.org>
AuthorDate: Sun Sep 19 07:07:46 2021 +0200
FELIX-6458 : (#101)
> Use ServiceReference.adaptTo(ServiceReferenceDTO)
---
scr/pom.xml | 4 +-
.../impl/runtime/ServiceComponentRuntimeImpl.java | 76 +---------------------
.../runtime/ServiceComponentRuntimeImplTest.java | 9 +--
3 files changed, 5 insertions(+), 84 deletions(-)
diff --git a/scr/pom.xml b/scr/pom.xml
index 8208173..f96db8a 100644
--- a/scr/pom.xml
+++ b/scr/pom.xml
@@ -79,7 +79,7 @@
<dependency>
<groupId>org.osgi</groupId>
<artifactId>osgi.core</artifactId>
- <version>6.0.0</version>
+ <version>8.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
@@ -385,7 +385,7 @@
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.framework</artifactId>
- <version>6.0.4</version>
+ <version>7.0.1</version>
<scope>test</scope>
</dependency>
</dependencies>
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImpl.java b/scr/src/main/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImpl.java
index 7df365e..0f13941 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImpl.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImpl.java
@@ -18,14 +18,12 @@
*/
package org.apache.felix.scr.impl.runtime;
-import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
import org.apache.felix.scr.impl.ComponentRegistry;
import org.apache.felix.scr.impl.manager.ComponentHolder;
@@ -36,11 +34,6 @@ import org.apache.felix.scr.impl.metadata.ReferenceMetadata;
import org.osgi.dto.DTO;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
-import org.osgi.framework.BundleEvent;
-import org.osgi.framework.BundleListener;
-import org.osgi.framework.Constants;
-import org.osgi.framework.ServiceEvent;
-import org.osgi.framework.ServiceListener;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.dto.BundleDTO;
import org.osgi.framework.dto.ServiceReferenceDTO;
@@ -53,21 +46,17 @@ import org.osgi.service.component.runtime.dto.UnsatisfiedReferenceDTO;
import org.osgi.util.promise.Promise;
import org.osgi.util.promise.Promises;
-public class ServiceComponentRuntimeImpl implements ServiceComponentRuntime, ServiceListener, BundleListener
+public class ServiceComponentRuntimeImpl implements ServiceComponentRuntime
{
private static final String[] EMPTY = {};
private final BundleContext context;
private final ComponentRegistry componentRegistry;
- private volatile SoftReference<ConcurrentHashMap<Long, ServiceReferenceDTO[]>> dtoCache = new SoftReference<>(new ConcurrentHashMap<Long, ServiceReferenceDTO[]>());
-
public ServiceComponentRuntimeImpl(final BundleContext context, final ComponentRegistry componentRegistry)
{
this.context = context;
this.componentRegistry = componentRegistry;
- this.context.addBundleListener(this);
- this.context.addServiceListener(this);
}
/**
@@ -288,44 +277,7 @@ public class ServiceComponentRuntimeImpl implements ServiceComponentRuntime, Ser
{
if (serviceRef == null)
return null;
- final Bundle bundle = serviceRef.getBundle();
- if (bundle == null) {
- return null;
- }
- final long bundleId = bundle.getBundleId();
- ConcurrentHashMap<Long, ServiceReferenceDTO[]> cache = dtoCache.get();
- if (cache == null) {
- cache = new ConcurrentHashMap<>();
- dtoCache = new SoftReference<>(cache);
- }
- ServiceReferenceDTO[] dtos = cache.get(bundleId);
- if (dtos == null) {
- dtos = bundle.adapt(ServiceReferenceDTO[].class);
- if (dtos == null) {
- dtos = new ServiceReferenceDTO[0];
- }
- cache.put(bundleId, dtos);
- }
- final long id = (Long) serviceRef.getProperty(Constants.SERVICE_ID);
- for (final ServiceReferenceDTO dto : dtos)
- {
- if (dto.id == id)
- {
- // we need to return a copy!
- final ServiceReferenceDTO result = new ServiceReferenceDTO();
- result.bundle = dto.bundle;
- result.id = dto.id;
- result.properties = new HashMap<>(dto.properties);
- if (dto.usingBundles != null) {
- result.usingBundles = new long[dto.usingBundles.length];
- if (dto.usingBundles.length > 0) {
- System.arraycopy(dto.usingBundles, 0, result.usingBundles, 0, result.usingBundles.length);
- }
- }
- return result;
- }
- }
- return null;
+ return serviceRef.adapt(ServiceReferenceDTO.class);
}
/**
@@ -476,28 +428,4 @@ public class ServiceComponentRuntimeImpl implements ServiceComponentRuntime, Ser
return null;
}
}
-
- @Override
- public void bundleChanged(final BundleEvent event) {
- ConcurrentHashMap<Long, ServiceReferenceDTO[]> cache = dtoCache.get();
- if (cache != null)
- {
- cache.remove(event.getBundle().getBundleId());
- }
- }
-
- @Override
- public void serviceChanged(final ServiceEvent event) {
- if (event.getServiceReference() != null) {
- ConcurrentHashMap<Long, ServiceReferenceDTO[]> cache = dtoCache.get();
- if (cache != null)
- {
- // using bundle id property incase the service has gotten unregistered
- // before we could get the bundle object
- cache.remove(event.getServiceReference().getProperty(Constants.SERVICE_BUNDLEID));
- }
- }
- }
-
-
}
diff --git a/scr/src/test/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImplTest.java b/scr/src/test/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImplTest.java
index 4b6cf80..a557c16 100644
--- a/scr/src/test/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImplTest.java
+++ b/scr/src/test/java/org/apache/felix/scr/impl/runtime/ServiceComponentRuntimeImplTest.java
@@ -44,18 +44,11 @@ public class ServiceComponentRuntimeImplTest extends TestCase
Mockito.when(sr.getProperty(Constants.SERVICE_ID)).thenReturn(327L);
Mockito.when(sr.getPropertyKeys()).thenReturn(new String[] {});
Mockito.when(sr.getBundle()).thenReturn(b);
-
- ServiceReferenceDTO one = new ServiceReferenceDTO();
- one.id = 5;
- ServiceReferenceDTO two = new ServiceReferenceDTO();
- two.id = 825;
- ServiceReferenceDTO three = new ServiceReferenceDTO();
- three.id = 19;
ServiceReferenceDTO real = new ServiceReferenceDTO();
real.id = 327;
real.properties = new HashMap<>();
+ Mockito.when(sr.adapt(ServiceReferenceDTO.class)).thenReturn(real);
- Mockito.when(b.adapt(ServiceReferenceDTO[].class)).thenReturn(new ServiceReferenceDTO[] {one, two, real, three});
ServiceComponentRuntimeImpl scr = new ServiceComponentRuntimeImpl(Mockito.mock(BundleContext.class), null);
Method m = scr.getClass().getDeclaredMethod("serviceReferenceToDTO", ServiceReference.class);
m.setAccessible(true);