You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by np...@apache.org on 2019/10/06 16:16:01 UTC

[sling-org-apache-sling-models-impl] branch master updated: SLING-8754 when possible use bindings servicecache (#15)

This is an automated email from the ASF dual-hosted git repository.

npeltier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-models-impl.git


The following commit(s) were added to refs/heads/master by this push:
     new f2e948b  SLING-8754 when possible use bindings servicecache (#15)
f2e948b is described below

commit f2e948b4c0f338dcebeedce8c09f7d6f283e4183
Author: Nicolas Peltier <np...@apache.org>
AuthorDate: Sun Oct 6 18:15:57 2019 +0200

    SLING-8754 when possible use bindings servicecache (#15)
---
 .../models/impl/injectors/OSGiServiceInjector.java    | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

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 c4d0127..8c28684 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
@@ -37,6 +37,8 @@ import org.apache.sling.models.spi.Injector;
 import org.apache.sling.models.spi.injectorspecific.AbstractInjectAnnotationProcessor2;
 import org.apache.sling.models.spi.injectorspecific.InjectAnnotationProcessor2;
 import org.apache.sling.models.spi.injectorspecific.StaticInjectAnnotationProcessorFactory;
+import org.apache.sling.api.SlingHttpServletRequest;
+import org.apache.sling.api.scripting.SlingBindings;
 import org.jetbrains.annotations.NotNull;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
@@ -79,9 +81,26 @@ public class OSGiServiceInjector implements Injector, StaticInjectAnnotationProc
                 filterString = filter.value();
             }
         }
+        if (adaptable instanceof SlingHttpServletRequest && StringUtils.isBlank(filterString) && type instanceof Class<?>) {
+            //in that case, we'll try to serve the service reference from the request bindings,
+            //as it's probably already cached.
+            SlingHttpServletRequest request = (SlingHttpServletRequest) adaptable;
+            Object service = getValueFromBindings(request, (Class<?>)type);
+            if (service != null) {
+                return service;
+            }
+        }
         return getValue(adaptable, type, filterString, callbackRegistry);
     }
 
+    private <T> Object getValueFromBindings(final SlingHttpServletRequest request, Class<T> type) {
+        SlingBindings bindings = (SlingBindings) request.getAttribute(SlingBindings.class.getName());
+        if (bindings != null && bindings.getSling() != null) {
+            return bindings.getSling().getService(type);
+        }
+        return null;
+    }
+
     private <T> Object getService(Object adaptable, Class<T> type, String filter,
             DisposalCallbackRegistry callbackRegistry) {
         // cannot use SlingScriptHelper since it does not support ordering by service ranking due to https://issues.apache.org/jira/browse/SLING-5665