001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020package org.apache.felix.ipojo.dependency.impl;
021
022import org.apache.felix.ipojo.dependency.interceptors.ServiceRankingInterceptor;
023import org.apache.felix.ipojo.util.DependencyModel;
024import org.osgi.framework.ServiceReference;
025
026import java.util.ArrayList;
027import java.util.Collections;
028import java.util.Comparator;
029import java.util.List;
030
031/**
032 * A comparator based version of the ranking interceptor.
033 */
034public class ComparatorBasedServiceRankingInterceptor implements ServiceRankingInterceptor {
035
036    private final Comparator<ServiceReference> m_comparator;
037
038    public ComparatorBasedServiceRankingInterceptor(Comparator<ServiceReference> cmp) {
039        this.m_comparator = cmp;
040    }
041
042
043    public void open(DependencyModel dependency) {    }
044
045    public List<ServiceReference> getServiceReferences(DependencyModel dependency, List<ServiceReference> matching) {
046        List<ServiceReference> copy = new ArrayList<ServiceReference>(matching);
047        Collections.sort(copy, m_comparator);
048        return copy;
049    }
050
051    public List<ServiceReference> onServiceArrival(DependencyModel dependency, List<ServiceReference> matching, ServiceReference<?> reference) {
052        return getServiceReferences(dependency, matching);
053    }
054
055    public List<ServiceReference> onServiceDeparture(DependencyModel dependency, List<ServiceReference> matching,
056                                                     ServiceReference<?> reference) {
057        return getServiceReferences(dependency, matching);
058    }
059
060    public List<ServiceReference> onServiceModified(DependencyModel dependency, List<ServiceReference> matching, ServiceReference<?> reference) {
061        return getServiceReferences(dependency, matching);
062    }
063
064    public void close(DependencyModel dependency) {  }
065}