private SortedSet getCachedReferences()

in src/main/java/org/apache/sling/scripting/core/impl/ServiceCache.java [146:175]


    private <T> SortedSet<Reference> getCachedReferences(Class<T> type) {
        String key = type.getName();
        SortedSet<Reference> references = cache.get(key);
        if (references == null) {
            references = new ConcurrentSkipListSet<>(Comparator.reverseOrder());
            try {
                Collection<ServiceReference<T>> serviceReferences = this.bundleContext.getServiceReferences(type, null);
                if (!serviceReferences.isEmpty()) {
                    List<ServiceReference<T>> localReferences = new ArrayList<>(serviceReferences);
                    Collections.sort(localReferences);
                    Collections.reverse(localReferences);
                    for (ServiceReference<T> ref : localReferences) {
                        references.add(new Reference(ref));
                    }
                    synchronized (this) {
                        SortedSet<Reference> existing = this.cache.get(key);
                        if (existing != null) {
                            existing.addAll(references);
                            references = existing;
                        } else {
                            this.cache.put(key, references);
                        }
                    }
                }
            } catch (InvalidSyntaxException e) {
                LOGGER.error(String.format("Unable to retrieve the services of type %s.", type.getName()), e);
            }
        }
        return references;
    }