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.BundleContext;
024
025import java.util.ArrayList;
026import java.util.List;
027
028/**
029 * Default implementation of the default service tracking interceptor.
030 * It accepts all references and keeps the dependencies in the `dependencies` list. This list is guarded by the
031 * monitor lock.
032 *
033 * It also provides an `invalidateMatchingServices` method notifying all managed dependencies of a change in the
034 * matching service set.
035 */
036public class DefaultServiceTrackingInterceptor extends DefaultDependencyInterceptor implements ServiceTrackingInterceptor {
037
038    /**
039     * Default implementation of the accept method.
040     * The default behavior is to accept all services as they are (no transformation).
041     * @param dependency the dependency the dependency
042     * @param context the context of the dependency the bundle context used by the dependency
043     * @param ref the reference the reference to accept, transform or reject
044     * @param <S> the type of service
045     * @return the reference as it is.
046     */
047    public <S> TransformedServiceReference<S> accept(DependencyModel dependency, BundleContext context, TransformedServiceReference<S> ref) {
048        return ref;
049    }
050
051    /**
052     * Notifies the managed dependencies of a change in the set of services accepted by this interceptor.
053     * The dependency will call the accept method to recompute the set of matching services.
054     */
055    public void invalidateMatchingServices() {
056        List<DependencyModel> list = new ArrayList<DependencyModel>();
057        synchronized (this) {
058            list.addAll(dependencies);
059        }
060
061        for (DependencyModel dep : list) {
062            dep.invalidateMatchingServices();
063        }
064    }
065}