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 modify / monitor the service bindings.
028 * This service is notified every time the dependency is weaving a new service bindings on un-weaves an existing one.
029 *
030 * Several binding interceptors can be plugged to the same service dependency. In this case,
031 * a chain is created where all interceptor are called in a sequence.
032 *
033 * A binding interceptor cannot modify the existing bindings.
034 *
035 * Obviously an interceptor can be plugged to several dependencies.
036 *
037 * @since 1.11.0
038 */
039public interface ServiceBindingInterceptor extends DependencyInterceptor {
040
041    /**
042     * Notification method when a dependency is weaving a new service binding.
043     * The interceptor can modify the service object. It must <strong>never</strong> return a {@code null} object,
044     * but the receive service object if it does not want to do anything with the service object.
045     *
046     * When the interceptor <em>modifies</em> the service object, the returned object <strong>must</strong> be
047     * compatible with the dependency specification.
048     *
049     * The received service object may already have been <em>wrapped</em> by binding interceptors called before the
050     * current one.
051     *
052     * @param dependency the dependency
053     * @param reference the service reference bound
054     * @param service the service object
055     * @param <S> the service specification
056     * @return the service object to be injected within the component. Must never be {@code null}.
057     */
058    public <S> S getService(DependencyModel dependency, ServiceReference<S> reference, S service);
059
060    /**
061     * Notification method when a dependency is un-weaving a service binding.
062     * The interceptor must released all objects related to this service binding.
063     *
064     * @param dependency the dependency
065     * @param reference the unbound service reference
066     * @param <S> the service specification
067     */
068    public <S> void ungetService(DependencyModel dependency, ServiceReference<S> reference);
069
070}