raducotescu commented on a change in pull request #13:
URL: 
https://github.com/apache/sling-org-apache-sling-scripting-core/pull/13#discussion_r802516365



##########
File path: src/main/java/org/apache/sling/scripting/core/impl/ServiceCache.java
##########
@@ -63,70 +70,151 @@ public void dispose() {
      * @return The service or <code>null</code>
      */
     @SuppressWarnings("unchecked")
+    @Nullable
     public <ServiceType> ServiceType getService(Class<ServiceType> type) {
-        final String key = type.getName();
-        Reference reference = this.cache.get(key);
-        if (reference == null) {
-
-            // get the service
-            ServiceReference ref = this.bundleContext.getServiceReference(key);
-            if (ref != null) {
-                final Object service = this.bundleContext.getService(ref);
-                if (service != null) {
-                    reference = new Reference();
-                    reference.service = service;
-                    reference.reference = ref;
-                } else {
-                    ref = null;
-                }
-            }
-
-            // assume missing service
-            if (reference == null) {
-                reference = NULL_REFERENCE;
+        SortedSet<Reference> references = getCachedReferences(type);
+        for (Reference reference : references) {
+            ServiceType service = (ServiceType) reference.getService();
+            if (service != null) {
+                return service;
             }
+        }
+        return null;
+    }
 
-            // check to see whether another thread has not done the same thing
-            synchronized (this) {
-                Reference existing = this.cache.get(key);
-                if (existing == null) {
-                    this.cache.put(key, reference);
-                    ref = null;
-                } else {
-                    reference = existing;
+    @SuppressWarnings("unchecked")
+    @Nullable
+    public <ServiceType> ServiceType[] getServices(Class<ServiceType> type, 
String filter) {
+        List<ServiceType> result = new ArrayList<>();
+        try {
+            SortedSet<Reference> cachedReferences = getCachedReferences(type);
+            final Collection<ServiceReference<ServiceType>> filteredReferences 
= this.bundleContext.getServiceReferences(type, filter);
+            if (!filteredReferences.isEmpty()) {
+                List<ServiceReference<ServiceType>> localFilteredReferences = 
new ArrayList<>(filteredReferences);

Review comment:
       We do need this list, as we need to sort the `ServiceReferences` 
according to their ranking, in order to be able to return the array with the 
`ServiceTypes`, the latter being objects that don't implement `Comparable`.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@sling.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to