All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.sling.commons.osgi.SortingServiceTracker Maven / Gradle / Ivy

There is a newer version: 6.5.21
Show newest version
/*
 * 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
    extends ServiceTracker  {

    private int lastCount = -1;

    private int lastRefCount = -1;

    private List sortedServiceCache;

    private List sortedReferences;

    /**
     * Constructor
     * @param context Bundle context
     * @param clazz Class
     */
    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.
     * @return Service list
     */
    public List getSortedServices() {
        List 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();
                for(int i=0;i getSortedServiceReferences() {
        List 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();
                for(int i=0;i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy