blob: 0b618ad35688e9cd95783f6dc28b10e9c6cba750 [file] [log] [blame]
Pierre De Rop3a00a212015-03-01 09:27:46 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. 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,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.apache.felix.dm;
20
21import java.util.Dictionary;
22
23import org.osgi.framework.ServiceRegistration;
24
25
26/**
27 * Component interface. Components are the main building blocks for OSGi applications.
28 * They can publish themselves as a service, and they can have dependencies. These
29 * dependencies will influence their life cycle as component will only be activated
30 * when all required dependencies are available.
31 *
32 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
33 */
34public interface Component {
35 /**
36 * Sets the implementation for this component. You can actually specify
37 * an instance you have instantiated manually, or a <code>Class</code>
38 * that will be instantiated using its default constructor when the
39 * required dependencies are resolved, effectively giving you a lazy
40 * instantiation mechanism.
41 *
42 * There are four special methods that are called when found through
43 * reflection to give you life cycle management options:
44 * <ol>
45 * <li><code>init()</code> is invoked after the instance has been
46 * created and dependencies have been resolved, and can be used to
47 * initialize the internal state of the instance or even to add more
48 * dependencies based on runtime state</li>
49 * <li><code>start()</code> is invoked right before the service is
50 * registered</li>
51 * <li><code>stop()</code> is invoked right after the service is
52 * unregistered</li>
53 * <li><code>destroy()</code> is invoked after all dependencies are
54 * removed</li>
55 * </ol>
56 * In short, this allows you to initialize your instance before it is
57 * registered, perform some post-initialization and pre-destruction code
58 * as well as final cleanup. If a method is not defined, it simply is not
59 * called, so you can decide which one(s) you need. If you need even more
60 * fine-grained control, you can register as a service state listener too.
61 *
62 * @param implementation the implementation
63 * @return this component
64 * @see ComponentStateListener
65 */
66 public Component setImplementation(Object implementation);
67
68 /**
69 * Adds dependency(ies) to this component, atomically. If the component is already active or if you add
70 * dependencies from the init method, then you should add all the dependencies in one single add method call
71 * (using the varargs argument), because this method may trigger component activation (like
72 * the ServiceTracker.open() method does).
73 *
74 * @param dependencies the dependencies to add.
75 * @return this component
76 */
77 public Component add(Dependency ... dependencies);
78
79 /**
80 * Removes a dependency from the component.
81 * @param d the dependency to remove
82 * @return this component
83 */
84 public Component remove(Dependency d);
85
86 /**
87 * Adds a component state listener to this component.
88 *
89 * @param listener the state listener
90 */
91 public Component add(ComponentStateListener listener);
92
93 /**
94 * Removes a component state listener from this component.
95 *
96 * @param listener the state listener
97 */
98 public Component remove(ComponentStateListener listener);
99
100 /**
101 * Sets the public interface under which this component should be registered
102 * in the OSGi service registry.
103 *
104 * @param serviceName the name of the service interface
105 * @param properties the properties for this service
106 * @return this component
107 */
108 public Component setInterface(String serviceName, Dictionary<?,?> properties);
109
110 /**
111 * Sets the public interfaces under which this component should be registered
112 * in the OSGi service registry.
113 *
114 * @param serviceNames the names of the service interface
115 * @param properties the properties for these services
116 * @return this component
117 */
118 public Component setInterface(String[] serviceNames, Dictionary<?, ?> properties);
119
120 /**
121 * Configures auto configuration of injected classes in the component instance.
122 * The following injections are currently performed, unless you explicitly
123 * turn them off:
124 * <dl>
125 * <dt>BundleContext</dt><dd>the bundle context of the bundle</dd>
126 * <dt>ServiceRegistration</dt><dd>the service registration used to register your service</dd>
127 * <dt>DependencyManager</dt><dd>the dependency manager instance</dd>
128 * <dt>Component</dt><dd>the component instance of the dependency manager</dd>
129 * </dl>
130 *
131 * @param clazz the class (from the list above)
132 * @param autoConfig <code>false</code> to turn off auto configuration
133 */
134 public Component setAutoConfig(Class<?> clazz, boolean autoConfig);
135
136 /**
137 * Configures auto configuration of injected classes in the component instance.
138 *
139 * @param clazz the class (from the list above)
140 * @param instanceName the name of the instance to inject the class into
141 * @see #setAutoConfig(Class, boolean)
142 */
143 public Component setAutoConfig(Class<?> clazz, String instanceName);
144
145 /**
146 * Returns the service registration for this component. The method
147 * will return <code>null</code> if no service registration is
148 * available, for example if this component is not registered as a
149 * service at all.
150 *
151 * @return the service registration
152 */
153 public ServiceRegistration getServiceRegistration();
154
155 /**
156 * Returns the instance that make up this component. If the component has a composition of instances,
157 * then the first instance of the composition is returned. Null is returned if the component has not
158 * even been instantiated.
159 *
160 * @return the component instances
161 */
162 public <T> T getInstance();
163
164 /**
165 * Returns the composition instances that make up this component, or just the
166 * component instance if it does not have a composition, or an empty array if
167 * the component has not even been instantiated.
168 *
169 * @return the component instances
170 */
171 public Object[] getInstances();
172
173 /**
174 * Returns the service properties associated with the component.
175 *
176 * @return the properties or <code>null</code> if there are none
177 */
178 public <K,V> Dictionary<K,V> getServiceProperties();
179
180 /**
181 * Sets the service properties associated with the component. If the service
182 * was already registered, it will be updated.
183 *
184 * @param serviceProperties the properties
185 */
186 public Component setServiceProperties(Dictionary<?, ?> serviceProperties);
187
188 /**
189 * Sets the names of the methods used as callbacks. These methods, when found, are
190 * invoked as part of the life cycle management of the component implementation. The
191 * dependency manager will look for a method of this name with the following signatures,
192 * in this order:
193 * <ol>
194 * <li>method(Component component)</li>
195 * <li>method()</li>
196 * </ol>
197 *
198 * @param init the name of the init method
199 * @param start the name of the start method
200 * @param stop the name of the stop method
201 * @param destroy the name of the destroy method
202 * @return the component
203 */
204 public Component setCallbacks(String init, String start, String stop, String destroy);
205
206 /**
207 * Sets the names of the methods used as callbacks. These methods, when found, are
208 * invoked on the specified instance as part of the life cycle management of the component
209 * implementation.
210 * <p>
211 * See setCallbacks(String init, String start, String stop, String destroy) for more
212 * information on the signatures. Specifying an instance means you can create a manager
213 * that will be invoked whenever the life cycle of a component changes and this manager
214 * can then decide how to expose this life cycle to the actual component, offering an
215 * important indirection when developing your own component models.
216 *
217 * @return this component
218 */
219 public Component setCallbacks(Object instance, String init, String start, String stop, String destroy);
220
221 /**
222 * Sets the factory to use to create the implementation. You can specify
223 * both the factory class and method to invoke. The method should return
224 * the implementation, and can use any method to create it. Actually, this
225 * can be used together with <code>setComposition</code> to create a
226 * composition of instances that work together to implement a component. The
227 * factory itself can also be instantiated lazily by not specifying an
228 * instance, but a <code>Class</code>.
229 *
230 * @param factory the factory instance or class
231 * @param createMethod the name of the create method
232 * @return this component
233 */
234 public Component setFactory(Object factory, String createMethod);
235
236 /**
237 * Sets the factory to use to create the implementation. You specify the
238 * method to invoke. The method should return the implementation, and can
239 * use any method to create it. Actually, this can be used together with
240 * <code>setComposition</code> to create a composition of instances that
241 * work together to implement a component.
242 * <p>
243 * Note that currently, there is no default for the factory, so please use
244 * <code>setFactory(factory, createMethod)</code> instead.
245 *
246 * @param createMethod the name of the create method
247 * @return this component
248 */
249 public Component setFactory(String createMethod);
250
251 /**
252 * Sets the instance and method to invoke to get back all instances that
253 * are part of a composition and need dependencies injected. All of them
254 * will be searched for any of the dependencies. The method that is
255 * invoked must return an <code>Object[]</code>.
256 *
257 * @param instance the instance that has the method
258 * @param getMethod the method to invoke
259 * @return this component
260 */
261 public Component setComposition(Object instance, String getMethod);
262
263 /**
264 * Sets the method to invoke on the service implementation to get back all
265 * instances that are part of a composition and need dependencies injected.
266 * All of them will be searched for any of the dependencies. The method that
267 * is invoked must return an <code>Object[]</code>.
268 *
269 * @param getMethod the method to invoke
270 * @return this component
271 */
272 public Component setComposition(String getMethod);
273
274 /**
275 * Returns the dependency manager associated with this component.
276 * @return the dependency manager associated with this component.
277 */
278 public DependencyManager getDependencyManager();
279
280 /**
281 * Returns the component description (dependencies, service provided, etc ...).
282 * @return the component description (dependencies, service provided, etc ...).
283 */
284 public ComponentDeclaration getComponentDeclaration();
285
286 /**
287 * Activate debug for this component. Informations related to dependency processing will be displayed
288 * using osgi log service, our to standard output if no log service is currently available.
289 * @param label
290 */
291 public Component setDebug(String label);
292}