blob: c8f6bf3d78c63b112ffe5bc826345359ed4ff44b [file] [log] [blame]
Richard S. Hallc88fca32006-10-18 22:01:22 +00001/*
Richard S. Hallf28d6762009-06-08 19:31:06 +00002 * Copyright (c) OSGi Alliance (2000, 2008). All Rights Reserved.
Richard S. Hallc88fca32006-10-18 22:01:22 +00003 *
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
17package 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. Hall53e70d32008-08-01 19:31:32 +000034 * <code>ServiceFactory</code> is cached by the Framework until the bundle
35 * releases its use of the service.
Richard S. Hallc88fca32006-10-18 22:01:22 +000036 *
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. Hall53e70d32008-08-01 19:31:32 +000044 * not made available to other bundles in the OSGi environment. The Framework
45 * may concurrently call a <code>ServiceFactory</code>.
Richard S. Hallc88fca32006-10-18 22:01:22 +000046 *
Richard S. Hallc88fca32006-10-18 22:01:22 +000047 * @see BundleContext#getService
Richard S. Hall53e70d32008-08-01 19:31:32 +000048 * @ThreadSafe
Richard S. Hallf28d6762009-06-08 19:31:06 +000049 * @version $Revision: 5967 $
Richard S. Hallc88fca32006-10-18 22:01:22 +000050 */
51
52public 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. Hallf28d6762009-06-08 19:31:06 +000066 * <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. Hallc88fca32006-10-18 22:01:22 +000069 *
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. Hallf28d6762009-06-08 19:31:06 +000078 * @return A service object that <strong>must</strong> be an instance of all
79 * the classes named when the service was registered.
Richard S. Hallc88fca32006-10-18 22:01:22 +000080 * @see BundleContext#getService
81 */
Richard S. Hall53e70d32008-08-01 19:31:32 +000082 public Object getService(Bundle bundle, ServiceRegistration registration);
Richard S. Hallc88fca32006-10-18 22:01:22 +000083
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. Hall53e70d32008-08-01 19:31:32 +000098 public void ungetService(Bundle bundle, ServiceRegistration registration,
99 Object service);
Richard S. Hallc88fca32006-10-18 22:01:22 +0000100}