Richard S. Hall | c88fca3 | 2006-10-18 22:01:22 +0000 | [diff] [blame] | 1 | /* |
Richard S. Hall | f28d676 | 2009-06-08 19:31:06 +0000 | [diff] [blame] | 2 | * Copyright (c) OSGi Alliance (2000, 2008). All Rights Reserved. |
Richard S. Hall | c88fca3 | 2006-10-18 22:01:22 +0000 | [diff] [blame] | 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 | /** |
| 20 | * Allows services to provide customized service objects in the OSGi |
| 21 | * environment. |
| 22 | * |
| 23 | * <p> |
| 24 | * When registering a service, a <code>ServiceFactory</code> object can be |
| 25 | * used instead of a service object, so that the bundle developer can gain |
| 26 | * control of the specific service object granted to a bundle that is using the |
| 27 | * service. |
| 28 | * |
| 29 | * <p> |
| 30 | * When this happens, the |
| 31 | * <code>BundleContext.getService(ServiceReference)</code> method calls the |
| 32 | * <code>ServiceFactory.getService</code> method to create a service object |
| 33 | * specifically for the requesting bundle. The service object returned by the |
Richard S. Hall | 53e70d3 | 2008-08-01 19:31:32 +0000 | [diff] [blame] | 34 | * <code>ServiceFactory</code> is cached by the Framework until the bundle |
| 35 | * releases its use of the service. |
Richard S. Hall | c88fca3 | 2006-10-18 22:01:22 +0000 | [diff] [blame] | 36 | * |
| 37 | * <p> |
| 38 | * When the bundle's use count for the service equals zero (including the bundle |
| 39 | * stopping or the service being unregistered), the |
| 40 | * <code>ServiceFactory.ungetService</code> method is called. |
| 41 | * |
| 42 | * <p> |
| 43 | * <code>ServiceFactory</code> objects are only used by the Framework and are |
Richard S. Hall | 53e70d3 | 2008-08-01 19:31:32 +0000 | [diff] [blame] | 44 | * not made available to other bundles in the OSGi environment. The Framework |
| 45 | * may concurrently call a <code>ServiceFactory</code>. |
Richard S. Hall | c88fca3 | 2006-10-18 22:01:22 +0000 | [diff] [blame] | 46 | * |
Richard S. Hall | c88fca3 | 2006-10-18 22:01:22 +0000 | [diff] [blame] | 47 | * @see BundleContext#getService |
Richard S. Hall | 53e70d3 | 2008-08-01 19:31:32 +0000 | [diff] [blame] | 48 | * @ThreadSafe |
Richard S. Hall | f28d676 | 2009-06-08 19:31:06 +0000 | [diff] [blame] | 49 | * @version $Revision: 5967 $ |
Richard S. Hall | c88fca3 | 2006-10-18 22:01:22 +0000 | [diff] [blame] | 50 | */ |
| 51 | |
| 52 | public interface ServiceFactory { |
| 53 | /** |
| 54 | * Creates a new service object. |
| 55 | * |
| 56 | * <p> |
| 57 | * The Framework invokes this method the first time the specified |
| 58 | * <code>bundle</code> requests a service object using the |
| 59 | * <code>BundleContext.getService(ServiceReference)</code> method. The |
| 60 | * service factory can then return a specific service object for each |
| 61 | * bundle. |
| 62 | * |
| 63 | * <p> |
| 64 | * The Framework caches the value returned (unless it is <code>null</code>), |
| 65 | * and will return the same service object on any future call to |
Richard S. Hall | f28d676 | 2009-06-08 19:31:06 +0000 | [diff] [blame] | 66 | * <code>BundleContext.getService</code> for the same bundle. This means the |
| 67 | * Framework must not allow this method to be concurrently called for the |
| 68 | * same bundle. |
Richard S. Hall | c88fca3 | 2006-10-18 22:01:22 +0000 | [diff] [blame] | 69 | * |
| 70 | * <p> |
| 71 | * The Framework will check if the returned service object is an instance of |
| 72 | * all the classes named when the service was registered. If not, then |
| 73 | * <code>null</code> is returned to the bundle. |
| 74 | * |
| 75 | * @param bundle The bundle using the service. |
| 76 | * @param registration The <code>ServiceRegistration</code> object for the |
| 77 | * service. |
Richard S. Hall | f28d676 | 2009-06-08 19:31:06 +0000 | [diff] [blame] | 78 | * @return A service object that <strong>must</strong> be an instance of all |
| 79 | * the classes named when the service was registered. |
Richard S. Hall | c88fca3 | 2006-10-18 22:01:22 +0000 | [diff] [blame] | 80 | * @see BundleContext#getService |
| 81 | */ |
Richard S. Hall | 53e70d3 | 2008-08-01 19:31:32 +0000 | [diff] [blame] | 82 | public Object getService(Bundle bundle, ServiceRegistration registration); |
Richard S. Hall | c88fca3 | 2006-10-18 22:01:22 +0000 | [diff] [blame] | 83 | |
| 84 | /** |
| 85 | * Releases a service object. |
| 86 | * |
| 87 | * <p> |
| 88 | * The Framework invokes this method when a service has been released by a |
| 89 | * bundle. The service object may then be destroyed. |
| 90 | * |
| 91 | * @param bundle The bundle releasing the service. |
| 92 | * @param registration The <code>ServiceRegistration</code> object for the |
| 93 | * service. |
| 94 | * @param service The service object returned by a previous call to the |
| 95 | * <code>ServiceFactory.getService</code> method. |
| 96 | * @see BundleContext#ungetService |
| 97 | */ |
Richard S. Hall | 53e70d3 | 2008-08-01 19:31:32 +0000 | [diff] [blame] | 98 | public void ungetService(Bundle bundle, ServiceRegistration registration, |
| 99 | Object service); |
Richard S. Hall | c88fca3 | 2006-10-18 22:01:22 +0000 | [diff] [blame] | 100 | } |