This is an automated email from the ASF dual-hosted git repository. rombert pushed a commit to annotated tag org.apache.sling.commons.osgi-2.2.0 in repository https://gitbox.apache.org/repos/asf/sling-org-apache-sling-commons-osgi.git
commit 39e39fdc1f154eae673198c44e48590ab0d4d9e3 Author: Mike Müller <[email protected]> AuthorDate: Thu Nov 1 18:53:12 2012 +0000 SLING-2645 Move SortingServiceTracker from org.apache.sling.installer.core.impl to org.apache.sling.commons.osgi git-svn-id: https://svn.apache.org/repos/asf/sling/trunk/bundles/commons/osgi@1404723 13f79535-47bb-0310-9956-ffa450edef68 --- .../sling/commons/osgi/SortingServiceTracker.java | 129 +++++++++++++++++++++ 1 file changed, 129 insertions(+) diff --git a/src/main/java/org/apache/sling/commons/osgi/SortingServiceTracker.java b/src/main/java/org/apache/sling/commons/osgi/SortingServiceTracker.java new file mode 100644 index 0000000..1bac6ce --- /dev/null +++ b/src/main/java/org/apache/sling/commons/osgi/SortingServiceTracker.java @@ -0,0 +1,129 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.sling.commons.osgi; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.osgi.util.tracker.ServiceTracker; + +/** + * Implementation providing a sorted list of services + * by service ranking. + */ +public class SortingServiceTracker<T> + extends ServiceTracker { + + private int lastCount = -1; + + private int lastRefCount = -1; + + private List<T> sortedServiceCache; + + private List<ServiceReference> sortedReferences; + + /** + * Constructor + */ + public SortingServiceTracker(final BundleContext context, + final String clazz) { + super(context, clazz, null); + } + + /** + * @see org.osgi.util.tracker.ServiceTracker#removedService(org.osgi.framework.ServiceReference, java.lang.Object) + */ + @Override + public void removedService(ServiceReference reference, Object service) { + this.sortedServiceCache = null; + this.sortedReferences = null; + this.context.ungetService(reference); + } + + /** + * @see org.osgi.util.tracker.ServiceTrackerCustomizer#modifiedService(org.osgi.framework.ServiceReference, java.lang.Object) + */ + @Override + public void modifiedService(ServiceReference reference, Object service) { + this.sortedServiceCache = null; + this.sortedReferences = null; + } + + /** + * @see org.osgi.util.tracker.ServiceTrackerCustomizer#addingService(org.osgi.framework.ServiceReference) + */ + @Override + public Object addingService(ServiceReference reference) { + this.sortedServiceCache = null; + this.sortedReferences = null; + return context.getService(reference); + } + + /** + * Return a sorted list of the services. + */ + public List<T> getSortedServices() { + List<T> result = this.sortedServiceCache; + if ( result == null || this.lastCount < this.getTrackingCount() ) { + this.lastCount = this.getTrackingCount(); + final ServiceReference[] references = this.getServiceReferences(); + if ( references == null || references.length == 0 ) { + result = Collections.emptyList(); + } else { + Arrays.sort(references); + result = new ArrayList<T>(); + for(int i=0;i<references.length;i++) { + @SuppressWarnings("unchecked") + final T service = (T)this.getService(references[references.length - 1 - i]); + if ( service != null ) { + result.add(service); + } + } + } + this.sortedServiceCache = result; + } + return result; + } + + /** + * Return a sorted list of the services references. + */ + public List<ServiceReference> getSortedServiceReferences() { + List<ServiceReference> result = this.sortedReferences; + if ( result == null || this.lastRefCount < this.getTrackingCount() ) { + this.lastRefCount = this.getTrackingCount(); + final ServiceReference[] references = this.getServiceReferences(); + if ( references == null || references.length == 0 ) { + result = Collections.emptyList(); + } else { + Arrays.sort(references); + result = new ArrayList<ServiceReference>(); + for(int i=0;i<references.length;i++) { + result.add(references[references.length - 1 - i]); + } + } + this.sortedReferences = result; + } + return result; + } +} -- To stop receiving notification emails like this one, please contact "[email protected]" <[email protected]>.
