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.interceptors;
021
022import org.apache.felix.ipojo.util.DependencyModel;
023import org.osgi.framework.ServiceReference;
024
025import java.util.ArrayList;
026import java.util.List;
027
028/**
029 * Default implementation of the default service ranking interceptor.
030 * This implementation does not sort the given set, so returns it as it is.
031 *
032 * It also provides an `invalidateSelectedServices` method notifying all managed dependencies of a change in the
033 * selection service set.
034 *
035 * onDeparture, onArrival and onModified methods delegates to the getServiceReferences method.
036 */
037public class DefaultServiceRankingInterceptor extends DefaultDependencyInterceptor implements
038        ServiceRankingInterceptor {
039
040    /**
041     * Notifies the managed dependencies of a change in the set of services selected by this interceptor.
042     * The dependency will call the getServiceReferences method to recompute the set of selected services.
043     */
044    public void invalidateSelectedServices() {
045        List<DependencyModel> list = new ArrayList<DependencyModel>();
046        synchronized (this) {
047            list.addAll(dependencies);
048        }
049
050        for (DependencyModel dep : list) {
051            dep.invalidateSelectedServices();
052        }
053    }
054
055
056    public List<ServiceReference> getServiceReferences(DependencyModel dependency, List<ServiceReference> matching) {
057        return matching;
058    }
059
060    public List<ServiceReference> onServiceArrival(DependencyModel dependency, List<ServiceReference> matching, ServiceReference<?> reference) {
061        return getServiceReferences(dependency, matching);
062    }
063
064    public List<ServiceReference> onServiceDeparture(DependencyModel dependency, List<ServiceReference> matching, ServiceReference<?> reference) {
065        return getServiceReferences(dependency, matching);
066    }
067
068    public List<ServiceReference> onServiceModified(DependencyModel dependency, List<ServiceReference> matching, ServiceReference<?> reference) {
069        return getServiceReferences(dependency, matching);
070    }
071}