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;
024import org.osgi.framework.ServiceReference;
025
026/**
027 * A service to influence the visibility of services within a service dependency.
028 * This service is called to determine which services from the tracker (base set) is going to the matching set.
029 *
030 * Several tracking interceptors can be plugged to the same service dependency. In this case,
031 * a chain is created where all interceptor can influence the next one. If the dependency has a filter,
032 * a tracking interceptor using this filter is the last interceptor of the chain.
033 *
034 * Obviously an interceptor can be plugged to several dependencies. Conversely, several tracking interceptor can be
035 * plugged to one dependency.
036 *
037 * @since 1.10.1
038 */
039public interface ServiceTrackingInterceptor extends DependencyInterceptor {
040
041    /**
042     * Does the interceptor accepts the reference of not ?
043     * This methods has two goals. It can filter out undesirable services by returning {@literal null}. In addition,
044     * it can <em>transform</em> the service reference to add / remove service properties. In this case,
045     * it must return the <strong>same</strong> instance of {@link TransformedServiceReference},
046     * but with the new set of properties.
047     *
048     * So to filter out the service, return {@literal null}. To accept the service,
049     * return the reference as it is. To transform the service update the service reference and return it.
050     *
051     * When several interceptors are collaborating on the same dependency, a chain is created. The received reference
052     * is the reference modified by the preceding interceptor. Notice that once an interceptor returns {@literal
053     * null} the chain is interrupted and the service rejected.
054     *
055     * @param dependency the dependency
056     * @param context the context of the dependency
057     * @param ref the reference
058     * @param <S> the type of service
059     * @return {@literal null} to filter out the service, the, optionally updated, reference to accept it.
060     */
061    public <S> TransformedServiceReference<S> accept(DependencyModel dependency, BundleContext context,
062                                                     TransformedServiceReference<S> ref);
063
064}