David Jencks | 49e4a9e | 2014-06-21 20:15:30 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) OSGi Alliance (2012, 2014). All Rights Reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.osgi.framework; |
| 18 | |
| 19 | import org.osgi.annotation.versioning.ConsumerType; |
| 20 | |
| 21 | /** |
| 22 | * A factory for {@link Constants#SCOPE_PROTOTYPE prototype scope} services. The |
| 23 | * factory can provide multiple, customized service objects in the OSGi |
| 24 | * environment. |
| 25 | * |
| 26 | * <p> |
| 27 | * When registering a service, a {@code PrototypeServiceFactory} object can be |
| 28 | * used instead of a service object, so that the bundle developer can create a |
| 29 | * customized service object for each caller that is using the service. |
| 30 | * |
| 31 | * <p> |
| 32 | * When a caller uses a {@link ServiceObjects} to |
| 33 | * {@link ServiceObjects#getService() request} a service object, the framework |
| 34 | * calls the {@link #getService(Bundle, ServiceRegistration) getService} method |
| 35 | * to return a service object customized for the requesting caller. The caller |
| 36 | * can {@link ServiceObjects#ungetService(Object) release} the returned service |
| 37 | * object and the framework will call the |
| 38 | * {@link #ungetService(Bundle, ServiceRegistration, Object) ungetService} |
| 39 | * method with the service object. |
| 40 | * |
| 41 | * <p> |
| 42 | * When a bundle uses the {@link BundleContext#getService(ServiceReference)} |
| 43 | * method to obtain a service object, the framework must act as if the service |
| 44 | * has {@link Constants#SCOPE_BUNDLE bundle scope}. That is, the framework will |
| 45 | * call the {@link #getService(Bundle, ServiceRegistration) getService} method |
| 46 | * to obtain a bundle-scoped service object which will be cached and have a use |
| 47 | * count. See {@link ServiceFactory}. |
| 48 | * |
| 49 | * <p> |
| 50 | * A bundle can use both {@link ServiceObjects} and |
| 51 | * {@link BundleContext#getService(ServiceReference)} to obtain a service object |
| 52 | * for a service. {@link ServiceObjects#getService()} will always return a |
| 53 | * service object provided by a call to |
| 54 | * {@link #getService(Bundle, ServiceRegistration)} and |
| 55 | * {@link BundleContext#getService(ServiceReference)} will always return the |
| 56 | * bundle-scoped service object. |
| 57 | * |
| 58 | * <p> |
| 59 | * {@code PrototypeServiceFactory} objects are only used by the Framework and |
| 60 | * are not made available to other bundles in the OSGi environment. The |
| 61 | * Framework may concurrently call a {@code PrototypeServiceFactory}. |
| 62 | * |
| 63 | * @param <S> Type of Service |
| 64 | * @see BundleContext#getServiceObjects(ServiceReference) |
| 65 | * @see ServiceObjects |
| 66 | * @ThreadSafe |
| 67 | * @since 1.8 |
| 68 | * @author $Id: 12129fe6e66b1c9488f3cca84ac228f9baaea083 $ |
| 69 | */ |
| 70 | @ConsumerType |
| 71 | public interface PrototypeServiceFactory<S> extends ServiceFactory<S> { |
| 72 | /** |
| 73 | * Returns a service object for a caller. |
| 74 | * |
| 75 | * <p> |
| 76 | * The Framework invokes this method for each caller requesting a service |
| 77 | * object using {@link ServiceObjects#getService()}. The factory can then |
| 78 | * return a customized service object for the caller. |
| 79 | * |
| 80 | * <p> |
| 81 | * The Framework must check that the returned service object is valid. If |
| 82 | * the returned service object is {@code null} or is not an |
| 83 | * {@code instanceof} all the classes named when the service was registered, |
| 84 | * a framework event of type {@link FrameworkEvent#ERROR} is fired |
| 85 | * containing a service exception of type |
| 86 | * {@link ServiceException#FACTORY_ERROR} and {@code null} is returned to |
| 87 | * the caller. If this method throws an exception, a framework event of type |
| 88 | * {@link FrameworkEvent#ERROR} is fired containing a service exception of |
| 89 | * type {@link ServiceException#FACTORY_EXCEPTION} with the thrown exception |
| 90 | * as the cause and {@code null} is returned to the caller. |
| 91 | * |
| 92 | * @param bundle The bundle requesting the service. |
| 93 | * @param registration The {@code ServiceRegistration} object for the |
| 94 | * requested service. |
| 95 | * @return A service object that <strong>must</strong> be an instance of all |
| 96 | * the classes named when the service was registered. |
| 97 | * @see ServiceObjects#getService() |
| 98 | */ |
| 99 | public S getService(Bundle bundle, ServiceRegistration<S> registration); |
| 100 | |
| 101 | /** |
| 102 | * Releases a service object customized for a caller. |
| 103 | * |
| 104 | * <p> |
| 105 | * The Framework invokes this method when a service has been released by a |
| 106 | * bundle such as by calling {@link ServiceObjects#ungetService(Object)}. |
| 107 | * The service object may then be destroyed. |
| 108 | * |
| 109 | * <p> |
| 110 | * If this method throws an exception, a framework event of type |
| 111 | * {@link FrameworkEvent#ERROR} is fired containing a service exception of |
| 112 | * type {@link ServiceException#FACTORY_EXCEPTION} with the thrown exception |
| 113 | * as the cause. |
| 114 | * |
| 115 | * @param bundle The bundle releasing the service. |
| 116 | * @param registration The {@code ServiceRegistration} object for the |
| 117 | * service being released. |
| 118 | * @param service The service object returned by a previous call to the |
| 119 | * {@link #getService(Bundle, ServiceRegistration) getService} |
| 120 | * method. |
| 121 | * @see ServiceObjects#ungetService(Object) |
| 122 | */ |
| 123 | public void ungetService(Bundle bundle, ServiceRegistration<S> registration, S service); |
| 124 | } |