KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > osgi > service > component > ComponentContext


1 /*
2  * $Header: /cvshome/build/org.osgi.service.component/src/org/osgi/service/component/ComponentContext.java,v 1.20 2006/06/16 16:31:26 hargrave Exp $
3  *
4  * Copyright (c) OSGi Alliance (2004, 2006). All Rights Reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 package org.osgi.service.component;
20
21 import java.util.Dictionary JavaDoc;
22
23 import org.osgi.framework.*;
24
25 /**
26  * A Component Context object is used by a component instance to interact with
27  * its execution context including locating services by reference name. Each
28  * component instance has a unique Component Context.
29  *
30  * <p>
31  * A component's implementation class may optionaly implement an activate
32  * method:
33  *
34  * <pre>
35  * protected void activate(ComponentContext context);
36  * </pre>
37  *
38  * If a component implements this method, this method will be called when a
39  * component configuration is activated to provide the component instance's
40  * Component Context object.
41  *
42  * <p>
43  * A component's implementation class may optionaly implement a deactivate
44  * method:
45  *
46  * <pre>
47  * protected void deactivate(ComponentContext context);
48  * </pre>
49  *
50  * If a component implements this method, this method will be called when the
51  * component configuration is deactivated.
52  *
53  * <p>
54  * The activate and deactivate methods will be called using reflection and must
55  * be protected or public accessible. These methods should not be public methods
56  * so that they do not appear as public methods on the component instance when
57  * used as a service object. These methods will be located by looking through
58  * the component's implementation class hierarchy for the first declaration of
59  * the method. If the method is found, if it is declared protected or public,
60  * the method will be called. Otherwise, the method will not be called.
61  *
62  * @version $Revision: 1.20 $
63  */

64 public interface ComponentContext {
65     /**
66      * Returns the component properties for this Component Context.
67      *
68      * @return The properties for this Component Context. The Dictionary is read
69      * only and cannot be modified.
70      */

71     public Dictionary JavaDoc getProperties();
72
73     /**
74      * Returns the service object for the specified reference name.
75      *
76      * <p>
77      * If the cardinality of the reference is <code>0..n</code> or
78      * <code>1..n</code> and multiple services are bound to the reference, the
79      * service with the highest ranking (as specified in its
80      * <code>Constants.SERVICE_RANKING</code> property) is returned. If there
81      * is a tie in ranking, the service with the lowest service ID (as specified
82      * in its <code>Constants.SERVICE_ID</code> property); that is, the
83      * service that was registered first is returned.
84      *
85      * @param name The name of a reference as specified in a
86      * <code>reference</code> element in this component's description.
87      * @return A service object for the referenced service or <code>null</code>
88      * if the reference cardinality is <code>0..1</code> or
89      * <code>0..n</code> and no bound service is available.
90      * @throws ComponentException If the Service Component Runtime catches an
91      * exception while activating the bound service.
92      */

93     public Object JavaDoc locateService(String JavaDoc name);
94
95     /**
96      * Returns the service object for the specified reference name and
97      * <code>ServiceReference</code>.
98      *
99      * @param name The name of a reference as specified in a
100      * <code>reference</code> element in this component's description.
101      * @param reference The <code>ServiceReference</code> to a bound service.
102      * This must be a <code>ServiceReference</code> provided to the
103      * component via the bind or unbind method for the specified
104      * reference name.
105      * @return A service object for the referenced service or <code>null</code>
106      * if the specified <code>ServiceReference</code> is not a bound
107      * service for the specified reference name.
108      * @throws ComponentException If the Service Component Runtime catches an
109      * exception while activating the bound service.
110      */

111     public Object JavaDoc locateService(String JavaDoc name, ServiceReference reference);
112
113     /**
114      * Returns the service objects for the specified reference name.
115      *
116      * @param name The name of a reference as specified in a
117      * <code>reference</code> element in this component's description.
118      * @return An array of service objects for the referenced service or
119      * <code>null</code> if the reference cardinality is
120      * <code>0..1</code> or <code>0..n</code> and no bound service
121      * is available.
122      * @throws ComponentException If the Service Component Runtime catches an
123      * exception while activating a bound service.
124      */

125     public Object JavaDoc[] locateServices(String JavaDoc name);
126
127     /**
128      * Returns the <code>BundleContext</code> of the bundle which contains
129      * this component.
130      *
131      * @return The <code>BundleContext</code> of the bundle containing this
132      * component.
133      */

134     public BundleContext getBundleContext();
135
136     /**
137      * If the component instance is registered as a service using the
138      * <code>servicefactory=&quot;true&quot;</code> attribute, then this
139      * method returns the bundle using the service provided by the component
140      * instance.
141      * <p>
142      * This method will return <code>null</code> if:
143      * <ul>
144      * <li>The component instance is not a service, then no bundle can be using
145      * it as a service.
146      * <li>The component instance is a service but did not specify the
147      * <code>servicefactory=&quot;true&quot;</code> attribute, then all
148      * bundles using the service provided by the component instance will share
149      * the same component instance.
150      * <li>The service provided by the component instance is not currently
151      * being used by any bundle.
152      * </ul>
153      *
154      * @return The bundle using the component instance as a service or
155      * <code>null</code>.
156      */

157     public Bundle getUsingBundle();
158
159     /**
160      * Returns the Component Instance object for the component instance
161      * associated with this Component Context.
162      *
163      * @return The Component Instance object for the component instance.
164      */

165     public ComponentInstance getComponentInstance();
166
167     /**
168      * Enables the specified component name. The specified component name must
169      * be in the same bundle as this component.
170      *
171      * @param name The name of a component or <code>null</code> to indicate
172      * all components in the bundle.
173      */

174     public void enableComponent(String JavaDoc name);
175
176     /**
177      * Disables the specified component name. The specified component name must
178      * be in the same bundle as this component.
179      *
180      * @param name The name of a component.
181      */

182     public void disableComponent(String JavaDoc name);
183
184     /**
185      * If the component instance is registered as a service using the
186      * <code>service</code> element, then this method returns the service
187      * reference of the service provided by this component instance.
188      * <p>
189      * This method will return <code>null</code> if the component instance is
190      * not registered as a service.
191      *
192      * @return The <code>ServiceReference</code> object for the component
193      * instance or <code>null</code> if the component instance is not
194      * registered as a service.
195      */

196     public ServiceReference getServiceReference();
197
198 }
199
Popular Tags