blob: dddc800e8a3466490b04810962a00d5439a5d4d1 [file] [log] [blame]
Marcel Offermans516d38d2006-03-25 20:46:19 +00001/*
Marcel Offermans56db6d32007-06-06 09:11:21 +00002 * 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
Marcel Offermans516d38d2006-03-25 20:46:19 +00009 *
Marcel Offermans56db6d32007-06-06 09:11:21 +000010 * http://www.apache.org/licenses/LICENSE-2.0
Marcel Offermans516d38d2006-03-25 20:46:19 +000011 *
Marcel Offermans56db6d32007-06-06 09:11:21 +000012 * 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.
Marcel Offermans516d38d2006-03-25 20:46:19 +000018 */
19package org.apache.felix.dependencymanager;
20
21import java.util.Dictionary;
22import java.util.List;
23
24import org.osgi.framework.ServiceRegistration;
25
26/**
27 * Service interface.
28 *
Marcel Offermans56db6d32007-06-06 09:11:21 +000029 * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
Marcel Offermans516d38d2006-03-25 20:46:19 +000030 */
31public interface Service {
32 /**
33 * Adds a new dependency to this service.
34 *
35 * @param dependency the dependency to add
36 * @return this service
37 */
38 public Service add(Dependency dependency);
39
40 /**
41 * Removes a dependency from this service.
42 *
43 * @param dependency the dependency to remove
44 * @return this service
45 */
46 public Service remove(Dependency dependency);
47
48 /**
49 * Sets the public interface under which this service should be registered
50 * in the OSGi service registry.
51 *
52 * @param serviceName the name of the service interface
53 * @param properties the properties for this service
54 * @return this service
55 */
56 public Service setInterface(String serviceName, Dictionary properties);
Marcel Offermansabca7852009-01-28 20:48:16 +000057
Marcel Offermans516d38d2006-03-25 20:46:19 +000058 /**
59 * Sets the public interfaces under which this service should be registered
60 * in the OSGi service registry.
61 *
62 * @param serviceNames the names of the service interface
63 * @param properties the properties for this service
64 * @return this service
65 */
66 public Service setInterface(String[] serviceNames, Dictionary properties);
67
68 /**
69 * Sets the implementation for this service. You can actually specify
70 * an instance you have instantiated manually, or a <code>Class</code>
71 * that will be instantiated using its default constructor when the
72 * required dependencies are resolved (effectively giving you a lazy
73 * instantiation mechanism).
74 *
75 * There are four special methods that are called when found through
76 * reflection to give you some life-cycle management options:
77 * <ol>
Marcel Offermansd8ee3892007-05-02 15:54:56 +000078 * <li><code>init()</code> is invoked right after the instance has been
79 * created, and before any dependencies are resolved, and can be used to
80 * initialize the internal state of the instance</li>
81 * <li><code>start()</code> is invoked after the required dependencies
82 * are resolved and injected, and before the service is registered</li>
83 * <li><code>stop()</code> is invoked right after the service is
84 * unregistered</li>
85 * <li><code>destroy()</code> is invoked after all dependencies are
86 * removed</li>
Marcel Offermans516d38d2006-03-25 20:46:19 +000087 * </ol>
88 * In short, this allows you to initialize your instance before it is
89 * registered, perform some post-initialization and pre-destruction code
90 * as well as final cleanup. If a method is not defined, it simply is not
Marcel Offermansd8ee3892007-05-02 15:54:56 +000091 * called, so you can decide which one(s) you need. If you need even more
92 * fine-grained control, you can register as a service state listener too.
Marcel Offermans516d38d2006-03-25 20:46:19 +000093 *
94 * @param implementation the implementation
95 * @return this service
Marcel Offermansd8ee3892007-05-02 15:54:56 +000096 * @see ServiceStateListener
Marcel Offermans516d38d2006-03-25 20:46:19 +000097 */
98 public Service setImplementation(Object implementation);
99
100 /**
101 * Returns a list of dependencies.
102 *
103 * @return a list of dependencies
104 */
105 public List getDependencies();
106
107 /**
108 * Returns the service registration for this service. The method
109 * will return <code>null</code> if no service registration is
110 * available.
111 *
112 * @return the service registration
113 */
114 public ServiceRegistration getServiceRegistration();
115
116 /**
117 * Returns the service instance for this service. The method will
118 * return <code>null</code> if no service instance is available.
119 *
120 * @return the service instance
121 */
122 public Object getService();
123
124 /**
125 * Returns the service properties associated with the service.
126 *
127 * @return the properties or <code>null</code> if there are none
128 */
129 public Dictionary getServiceProperties();
130
131 /**
132 * Sets the service properties associated with the service. If the service
133 * was already registered, it will be updated.
134 *
135 * @param serviceProperties the properties
136 */
137 public void setServiceProperties(Dictionary serviceProperties);
138
139 /**
140 * Sets the names of the methods used as callbacks. These methods, when found, are
141 * invoked as part of the life-cycle management of the service implementation. The
142 * methods should not have any parameters.
143 *
144 * @param init the name of the init method
145 * @param start the name of the start method
146 * @param stop the name of the stop method
147 * @param destroy the name of the destroy method
148 * @return the service instance
149 */
150 public Service setCallbacks(String init, String start, String stop, String destroy);
151
152 // listener
153 /**
154 * Adds a service state listener to this service.
155 *
156 * @param listener the state listener
157 */
158 public void addStateListener(ServiceStateListener listener);
159
160 /**
161 * Removes a service state listener from this service.
162 *
163 * @param listener the state listener
164 */
165 public void removeStateListener(ServiceStateListener listener);
166
167 // events, must be fired when the dependency is started/active
168
169 /**
170 * Will be called when the dependency becomes available.
171 *
172 * @param dependency the dependency
173 */
174 public void dependencyAvailable(Dependency dependency);
175
176 /**
177 * Will be called when the dependency changes.
178 *
179 * @param dependency the dependency
180 */
181 public void dependencyUnavailable(Dependency dependency);
182
183 /**
184 * Will be called when the dependency becomes unavailable.
185 *
186 * @param dependency the dependency
187 */
188 public void dependencyChanged(Dependency dependency);
189
190 /**
191 * Starts the service. This activates the dependency tracking mechanism
192 * for this service.
193 */
194 public void start();
195
196 /**
197 * Stops the service. This deactivates the dependency tracking mechanism
198 * for this service.
199 */
200 public void stop();
Marcel Offermansf038e8d2008-01-30 10:44:55 +0000201
202 /**
203 * Sets the factory to use to create the implementation. You can specify
204 * both the factory class and method to invoke. The method should return
205 * the implementation, and can use any method to create it. Actually, this
206 * can be used together with <code>setComposition</code> to create a
207 * composition of instances that work together to implement a service. The
208 * factory itself can also be instantiated lazily by not specifying an
209 * instance, but a <code>Class</code>.
210 *
211 * @param factory the factory instance or class
212 * @param createMethod the name of the create method
213 */
214 public Service setFactory(Object factory, String createMethod);
215
216 /**
217 * Sets the factory to use to create the implementation. You specify the
218 * method to invoke. The method should return the implementation, and can
219 * use any method to create it. Actually, this can be used together with
220 * <code>setComposition</code> to create a composition of instances that
Marcel Offermansabca7852009-01-28 20:48:16 +0000221 * work together to implement a service.
222 * <p>
223 * Note that currently, there is no default for the factory, so please use
224 * <code>setFactory(factory, createMethod)</code> instead.
Marcel Offermansf038e8d2008-01-30 10:44:55 +0000225 *
226 * @param createMethod the name of the create method
227 */
228 public Service setFactory(String createMethod);
229
230 /**
231 * Sets the instance and method to invoke to get back all instances that
232 * are part of a composition and need dependencies injected. All of them
233 * will be searched for any of the dependencies. The method that is
234 * invoked must return an <code>Object[]</code>.
235 *
236 * @param instance the instance that has the method
237 * @param getMethod the method to invoke
238 */
239 public Service setComposition(Object instance, String getMethod);
240
241 /**
242 * Sets the method to invoke on the service implementation to get back all
243 * instances that are part of a composition and need dependencies injected.
244 * All of them will be searched for any of the dependencies. The method that
245 * is invoked must return an <code>Object[]</code>.
246 *
247 * @param getMethod the method to invoke
248 */
249 public Service setComposition(String getMethod);
Marcel Offermans516d38d2006-03-25 20:46:19 +0000250}