You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by jo...@apache.org on 2022/11/15 10:13:12 UTC
[sling-org-apache-sling-models-impl] 01/01: SLING-11029 improve OSGI Injector for the common case of no filter
This is an automated email from the ASF dual-hosted git repository.
joerghoh pushed a commit to branch SLING-11029-improve-OsgiInjector
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-impl.git
commit 07e37d8a1fce536f14dfe7bc88ce5515819a1bb2
Author: Joerg Hoh <jo...@apache.org>
AuthorDate: Mon Nov 14 19:17:24 2022 +0100
SLING-11029 improve OSGI Injector for the common case of no filter
---
.../models/impl/injectors/OSGiServiceInjector.java | 40 ++++++++++++++--------
.../impl/InjectorSpecificAnnotationTest.java | 6 ++--
.../sling/models/impl/OSGiInjectionTest.java | 6 ++--
.../sling/models/impl/OptionalObjectsTest.java | 3 +-
4 files changed, 31 insertions(+), 24 deletions(-)
diff --git a/src/main/java/org/apache/sling/models/impl/injectors/OSGiServiceInjector.java b/src/main/java/org/apache/sling/models/impl/injectors/OSGiServiceInjector.java
index ce4d30d..66b6723 100644
--- a/src/main/java/org/apache/sling/models/impl/injectors/OSGiServiceInjector.java
+++ b/src/main/java/org/apache/sling/models/impl/injectors/OSGiServiceInjector.java
@@ -101,23 +101,35 @@ public class OSGiServiceInjector implements Injector, StaticInjectAnnotationProc
private <T> Object getService(Object adaptable, Class<T> type, String filter,
DisposalCallbackRegistry callbackRegistry, BundleContext modelContext) {
- // cannot use SlingScriptHelper since it does not support ordering by service ranking due to https://issues.apache.org/jira/browse/SLING-5665
- try {
- ServiceReference<?>[] refs = modelContext.getServiceReferences(type.getName(), filter);
- if (refs != null && refs.length > 0) {
- // sort by reverse service ranking (highest first) (see ServiceReference.compareTo)
- List<ServiceReference<?>> references = Arrays.asList(refs);
- Collections.sort(references, Collections.reverseOrder());
- for(final ServiceReference<?> ref : references) {
- final Object obj = modelContext.getService(ref);
- if ( obj != null ) {
- callbackRegistry.addDisposalCallback(new Callback(new ServiceReference[] {ref}, modelContext));
- return obj;
+
+ // if there's not filter we can use a simplified version
+ if (filter == null) {
+ ServiceReference<?> ref = modelContext.getServiceReference(type.getName());
+ if (ref != null) {
+ final Object obj = modelContext.getService(ref);
+ if ( obj != null ) {
+ callbackRegistry.addDisposalCallback(new Callback(new ServiceReference[] {ref}, modelContext));
+ return obj;
+ }
+ }
+ } else {
+ try {
+ ServiceReference<?>[] refs = modelContext.getServiceReferences(type.getName(), filter);
+ if (refs != null && refs.length > 0) {
+ // sort by reverse service ranking (highest first) (see ServiceReference.compareTo)
+ List<ServiceReference<?>> references = Arrays.asList(refs);
+ Collections.sort(references, Collections.reverseOrder());
+ for(final ServiceReference<?> ref : references) {
+ final Object obj = modelContext.getService(ref);
+ if ( obj != null ) {
+ callbackRegistry.addDisposalCallback(new Callback(new ServiceReference[] {ref}, modelContext));
+ return obj;
+ }
}
}
+ } catch (InvalidSyntaxException e) {
+ log.error("invalid filter expression", e);
}
- } catch (InvalidSyntaxException e) {
- log.error("invalid filter expression", e);
}
return null;
}
diff --git a/src/test/java/org/apache/sling/models/impl/InjectorSpecificAnnotationTest.java b/src/test/java/org/apache/sling/models/impl/InjectorSpecificAnnotationTest.java
index 2b96c43..32079b8 100644
--- a/src/test/java/org/apache/sling/models/impl/InjectorSpecificAnnotationTest.java
+++ b/src/test/java/org/apache/sling/models/impl/InjectorSpecificAnnotationTest.java
@@ -153,8 +153,7 @@ public class InjectorSpecificAnnotationTest {
public void testOSGiServiceField() throws InvalidSyntaxException {
ServiceReference ref = mock(ServiceReference.class);
Logger log = mock(Logger.class);
- when(bundleContext.getServiceReferences(Logger.class.getName(), null)).thenReturn(
- new ServiceReference[] { ref });
+ when(bundleContext.getServiceReference(Logger.class.getName())).thenReturn(ref);
when(bundleContext.getService(ref)).thenReturn(log);
InjectorSpecificAnnotationModel model = factory.getAdapter(request, InjectorSpecificAnnotationModel.class);
@@ -242,8 +241,7 @@ public class InjectorSpecificAnnotationTest {
public void testOSGiServiceConstructor() throws InvalidSyntaxException {
ServiceReference ref = mock(ServiceReference.class);
Logger log = mock(Logger.class);
- when(bundleContext.getServiceReferences(Logger.class.getName(), null)).thenReturn(
- new ServiceReference[] { ref });
+ when(bundleContext.getServiceReference(Logger.class.getName())).thenReturn(ref);
when(bundleContext.getService(ref)).thenReturn(log);
org.apache.sling.models.testmodels.classes.constructorinjection.InjectorSpecificAnnotationModel model
diff --git a/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java b/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java
index 044b7c6..6bed4b9 100644
--- a/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java
+++ b/src/test/java/org/apache/sling/models/impl/OSGiInjectionTest.java
@@ -83,8 +83,7 @@ public class OSGiInjectionTest {
public void testSimpleOSGiModelField() throws Exception {
ServiceReference ref = mock(ServiceReference.class);
ServiceInterface service = mock(ServiceInterface.class);
- when(bundleContext.getServiceReferences(ServiceInterface.class.getName(), null)).thenReturn(
- new ServiceReference[] { ref });
+ when(bundleContext.getServiceReference(ServiceInterface.class.getName())).thenReturn(ref);
when(bundleContext.getService(ref)).thenReturn(service);
Resource res = mock(Resource.class);
@@ -228,8 +227,7 @@ public class OSGiInjectionTest {
public void testSimpleOSGiModelConstructor() throws Exception {
ServiceReference ref = mock(ServiceReference.class);
ServiceInterface service = mock(ServiceInterface.class);
- when(bundleContext.getServiceReferences(ServiceInterface.class.getName(), null)).thenReturn(
- new ServiceReference[] { ref });
+ when(bundleContext.getServiceReference(ServiceInterface.class.getName())).thenReturn(ref);
when(bundleContext.getService(ref)).thenReturn(service);
Resource res = mock(Resource.class);
diff --git a/src/test/java/org/apache/sling/models/impl/OptionalObjectsTest.java b/src/test/java/org/apache/sling/models/impl/OptionalObjectsTest.java
index 250714d..bc1d9ad 100644
--- a/src/test/java/org/apache/sling/models/impl/OptionalObjectsTest.java
+++ b/src/test/java/org/apache/sling/models/impl/OptionalObjectsTest.java
@@ -215,8 +215,7 @@ public class OptionalObjectsTest {
public void testFieldInjectionsOSGiService() throws InvalidSyntaxException {
ServiceReference ref = mock(ServiceReference.class);
Logger log = mock(Logger.class);
- when(bundleContext.getServiceReferences(Logger.class.getName(), null)).thenReturn(
- new ServiceReference[]{ref});
+ when(bundleContext.getServiceReference(Logger.class.getName())).thenReturn(ref);
when(bundleContext.getService(ref)).thenReturn(log);
OptionalObjectsModel model = factory.getAdapter(request, OptionalObjectsModel.class);