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



##########
File path: 
src/main/java/org/apache/sling/scripting/core/impl/bundled/AbstractBundledRenderUnit.java
##########
@@ -101,83 +95,37 @@ public BundleContext getBundleContext() {
         return scriptExtension;
     }
 
+    @Override
+    public @NotNull ServiceCache getServiceCache() {
+        return serviceCache;
+    }
+
     @Override
     @Nullable
     @SuppressWarnings("unchecked")
     public <T> T getService(@NotNull String className) {
-        LOG.debug("Attempting to load class {} as an OSGi service.", 
className);
-        T result = (this.services == null ? null : (T) 
this.services.get(className));
-        if (result == null) {
-            final ServiceReference<?> ref = 
this.bundleContext.getServiceReference(className);
-            if (ref != null) {
-                result = (T) this.bundleContext.getService(ref);
-                if (result != null) {
-                    if (this.services == null) {
-                        this.services = new HashMap<>();
-                    }
-                    if (this.references == null) {
-                        this.references = new ArrayList<>();
-                    }
-                    this.references.add(ref);
-                    this.services.put(className, result);
-                    return result;
-                }
-            }
+        try {
+            ClassLoader bundleClassloader = 
getBundle().adapt(BundleWiring.class).getClassLoader();
+            return (T) 
serviceCache.getService(bundleClassloader.loadClass(className));
+        } catch (ClassNotFoundException e) {
+            LOG.error("Unable to retrieve a service of type " + className + " 
for bundled script " + path, e);
         }
-        return result;
+        return null;
     }
 
     @Override
     @Nullable
     @SuppressWarnings("unchecked")
     public <T> T[] getServices(@NotNull String className, @Nullable String 
filter) {
-        T[] result = null;
         try {
-            final ServiceReference<?>[] refs = 
this.bundleContext.getServiceReferences(className, filter);
-
-            if (refs != null) {
-                // sort by service ranking (lowest first) (see 
ServiceReference#compareTo(Object))
-                List<ServiceReference<?>> localReferences = 
Arrays.asList(refs);
-                Collections.sort(localReferences);
-                // get the highest ranking first
-                Collections.reverse(localReferences);
-
-                final List<T> objects = new ArrayList<>();
-                for (ServiceReference<?> reference : localReferences) {
-                    final T service = (T) 
this.bundleContext.getService(reference);
-                    if (service != null) {
-                        if (this.references == null) {
-                            this.references = new ArrayList<>();
-                        }
-                        this.references.add(reference);
-                        objects.add(service);
-                    }
-                }
-                if (!objects.isEmpty()) {
-                    T[] srv = (T[]) 
Array.newInstance(bundle.loadClass(className), objects.size());
-                    result = objects.toArray(srv);
-                }
-            }
-        } catch (Exception e) {
-            LOG.error(String.format("Unable to retrieve the services of type 
%s.", className), e);
+            ClassLoader bundleClassloader = 
getBundle().adapt(BundleWiring.class).getClassLoader();
+            return (T[]) 
serviceCache.getServices(bundleClassloader.loadClass(className), filter);
+        } catch (ClassNotFoundException e) {

Review comment:
       Same as above.




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to