Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
Marcel Offermans | 8b93efa | 2010-07-02 18:27:21 +0000 | [diff] [blame] | 19 | package org.apache.felix.dm; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 20 | |
| 21 | import java.util.Dictionary; |
| 22 | import java.util.List; |
| 23 | |
| 24 | import org.osgi.framework.ServiceRegistration; |
| 25 | |
| 26 | /** |
| 27 | * Service interface. |
| 28 | * |
| 29 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 30 | */ |
| 31 | public 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); |
Marcel Offermans | 80eeafe | 2009-12-01 22:12:26 +0000 | [diff] [blame] | 39 | public Service add(List dependencies); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Removes a dependency from this service. |
| 43 | * |
| 44 | * @param dependency the dependency to remove |
| 45 | * @return this service |
| 46 | */ |
| 47 | public Service remove(Dependency dependency); |
| 48 | |
| 49 | /** |
| 50 | * Sets the public interface under which this service should be registered |
| 51 | * in the OSGi service registry. |
| 52 | * |
| 53 | * @param serviceName the name of the service interface |
| 54 | * @param properties the properties for this service |
| 55 | * @return this service |
| 56 | */ |
| 57 | public Service setInterface(String serviceName, Dictionary properties); |
| 58 | |
| 59 | /** |
| 60 | * Sets the public interfaces under which this service should be registered |
| 61 | * in the OSGi service registry. |
| 62 | * |
| 63 | * @param serviceNames the names of the service interface |
| 64 | * @param properties the properties for this service |
| 65 | * @return this service |
| 66 | */ |
| 67 | public Service setInterface(String[] serviceNames, Dictionary properties); |
| 68 | |
| 69 | /** |
| 70 | * Sets the implementation for this service. You can actually specify |
| 71 | * an instance you have instantiated manually, or a <code>Class</code> |
| 72 | * that will be instantiated using its default constructor when the |
| 73 | * required dependencies are resolved (effectively giving you a lazy |
| 74 | * instantiation mechanism). |
| 75 | * |
| 76 | * There are four special methods that are called when found through |
| 77 | * reflection to give you some life-cycle management options: |
| 78 | * <ol> |
| 79 | * <li><code>init()</code> is invoked right after the instance has been |
| 80 | * created, and before any dependencies are resolved, and can be used to |
| 81 | * initialize the internal state of the instance</li> |
| 82 | * <li><code>start()</code> is invoked after the required dependencies |
| 83 | * are resolved and injected, and before the service is registered</li> |
| 84 | * <li><code>stop()</code> is invoked right after the service is |
| 85 | * unregistered</li> |
| 86 | * <li><code>destroy()</code> is invoked after all dependencies are |
| 87 | * removed</li> |
| 88 | * </ol> |
| 89 | * In short, this allows you to initialize your instance before it is |
| 90 | * registered, perform some post-initialization and pre-destruction code |
| 91 | * as well as final cleanup. If a method is not defined, it simply is not |
| 92 | * called, so you can decide which one(s) you need. If you need even more |
| 93 | * fine-grained control, you can register as a service state listener too. |
| 94 | * |
| 95 | * @param implementation the implementation |
| 96 | * @return this service |
| 97 | * @see ServiceStateListener |
| 98 | */ |
| 99 | public Service setImplementation(Object implementation); |
| 100 | |
| 101 | /** |
| 102 | * Returns a list of dependencies. |
| 103 | * |
| 104 | * @return a list of dependencies |
| 105 | */ |
| 106 | public List getDependencies(); |
| 107 | |
| 108 | /** |
| 109 | * Returns the service registration for this service. The method |
| 110 | * will return <code>null</code> if no service registration is |
| 111 | * available. |
| 112 | * |
| 113 | * @return the service registration |
| 114 | */ |
| 115 | public ServiceRegistration getServiceRegistration(); |
| 116 | |
| 117 | /** |
| 118 | * Returns the service instance for this service. The method will |
| 119 | * return <code>null</code> if no service instance is available. |
| 120 | * |
| 121 | * @return the service instance |
| 122 | */ |
| 123 | public Object getService(); |
| 124 | |
| 125 | /** |
| 126 | * Returns the service properties associated with the service. |
| 127 | * |
| 128 | * @return the properties or <code>null</code> if there are none |
| 129 | */ |
| 130 | public Dictionary getServiceProperties(); |
| 131 | |
| 132 | /** |
| 133 | * Sets the service properties associated with the service. If the service |
| 134 | * was already registered, it will be updated. |
| 135 | * |
| 136 | * @param serviceProperties the properties |
| 137 | */ |
Pierre De Rop | 19476fe | 2010-05-23 08:13:58 +0000 | [diff] [blame] | 138 | public Service setServiceProperties(Dictionary serviceProperties); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * Sets the names of the methods used as callbacks. These methods, when found, are |
| 142 | * invoked as part of the life-cycle management of the service implementation. The |
| 143 | * methods should not have any parameters. |
| 144 | * |
| 145 | * @param init the name of the init method |
| 146 | * @param start the name of the start method |
| 147 | * @param stop the name of the stop method |
| 148 | * @param destroy the name of the destroy method |
| 149 | * @return the service instance |
| 150 | */ |
| 151 | public Service setCallbacks(String init, String start, String stop, String destroy); |
Marcel Offermans | 8055261 | 2010-04-02 15:28:33 +0000 | [diff] [blame] | 152 | public Service setCallbacks(Object instance, String init, String start, String stop, String destroy); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 153 | |
| 154 | // listener |
| 155 | /** |
| 156 | * Adds a service state listener to this service. |
| 157 | * |
| 158 | * @param listener the state listener |
| 159 | */ |
| 160 | public void addStateListener(ServiceStateListener listener); |
| 161 | |
| 162 | /** |
| 163 | * Removes a service state listener from this service. |
| 164 | * |
| 165 | * @param listener the state listener |
| 166 | */ |
| 167 | public void removeStateListener(ServiceStateListener listener); |
| 168 | |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 169 | /** |
| 170 | * Starts the service. This activates the dependency tracking mechanism |
| 171 | * for this service. |
| 172 | */ |
| 173 | public void start(); |
| 174 | |
| 175 | /** |
| 176 | * Stops the service. This deactivates the dependency tracking mechanism |
| 177 | * for this service. |
| 178 | */ |
| 179 | public void stop(); |
| 180 | |
| 181 | /** |
| 182 | * Sets the factory to use to create the implementation. You can specify |
| 183 | * both the factory class and method to invoke. The method should return |
| 184 | * the implementation, and can use any method to create it. Actually, this |
| 185 | * can be used together with <code>setComposition</code> to create a |
| 186 | * composition of instances that work together to implement a service. The |
| 187 | * factory itself can also be instantiated lazily by not specifying an |
| 188 | * instance, but a <code>Class</code>. |
| 189 | * |
| 190 | * @param factory the factory instance or class |
| 191 | * @param createMethod the name of the create method |
| 192 | */ |
Pierre De Rop | 5f4fbce | 2009-12-04 22:28:55 +0000 | [diff] [blame] | 193 | public Service setFactory(Object factory, String createMethod); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 194 | |
| 195 | /** |
| 196 | * Sets the factory to use to create the implementation. You specify the |
| 197 | * method to invoke. The method should return the implementation, and can |
| 198 | * use any method to create it. Actually, this can be used together with |
| 199 | * <code>setComposition</code> to create a composition of instances that |
| 200 | * work together to implement a service. |
| 201 | * <p> |
| 202 | * Note that currently, there is no default for the factory, so please use |
| 203 | * <code>setFactory(factory, createMethod)</code> instead. |
| 204 | * |
| 205 | * @param createMethod the name of the create method |
| 206 | */ |
| 207 | public Service setFactory(String createMethod); |
| 208 | |
| 209 | /** |
| 210 | * Sets the instance and method to invoke to get back all instances that |
| 211 | * are part of a composition and need dependencies injected. All of them |
| 212 | * will be searched for any of the dependencies. The method that is |
| 213 | * invoked must return an <code>Object[]</code>. |
| 214 | * |
| 215 | * @param instance the instance that has the method |
| 216 | * @param getMethod the method to invoke |
| 217 | */ |
| 218 | public Service setComposition(Object instance, String getMethod); |
| 219 | |
| 220 | /** |
| 221 | * Sets the method to invoke on the service implementation to get back all |
| 222 | * instances that are part of a composition and need dependencies injected. |
| 223 | * All of them will be searched for any of the dependencies. The method that |
| 224 | * is invoked must return an <code>Object[]</code>. |
| 225 | * |
| 226 | * @param getMethod the method to invoke |
| 227 | */ |
| 228 | public Service setComposition(String getMethod); |
Marcel Offermans | ea89b86 | 2010-06-24 13:14:43 +0000 | [diff] [blame] | 229 | |
| 230 | /** |
| 231 | * Returns the composition instances that make up this service, or just the |
| 232 | * service instance if it does not have a composition, or an empty array if |
| 233 | * the service has not even been instantiated. |
| 234 | */ |
| 235 | public Object[] getCompositionInstances(); |
| 236 | |
| 237 | /** |
| 238 | * Returns the dependency manager associated with this service. |
| 239 | */ |
| 240 | public DependencyManager getDependencyManager(); |
Marcel Offermans | 3d92121 | 2010-08-09 13:37:02 +0000 | [diff] [blame^] | 241 | |
| 242 | /** |
| 243 | * Configures auto configuration of injected classes in the service instance. |
| 244 | * The following injections are currently performed, unless you explicitly |
| 245 | * turn them off: |
| 246 | * <dl> |
| 247 | * <dt>BundleContext</dt><dd>the bundle context of the bundle</dd> |
| 248 | * <dt>ServiceRegistration</dt><dd>the service registration used to register your service</dd> |
| 249 | * <dt>DependencyManager</dt><dd>the dependency manager instance</dd> |
| 250 | * <dt>Service</dt><dd>the service instance of the dependency manager</dd> |
| 251 | * </dl> |
| 252 | * |
| 253 | * @param clazz the class (from the list above) |
| 254 | * @param autoConfig <code>false</code> to turn off auto configuration |
| 255 | */ |
| 256 | public Service setAutoConfig(Class clazz, boolean autoConfig); |
| 257 | |
| 258 | /** |
| 259 | * Configures auto configuration of injected classes in the service instance. |
| 260 | * |
| 261 | * @param clazz the class (from the list above) |
| 262 | * @param instanceName the name of the instance to inject the class into |
| 263 | * @see setAutoConfig(Class, boolean) |
| 264 | */ |
| 265 | public Service setAutoConfig(Class clazz, String instanceName); |
| 266 | |
| 267 | /** |
| 268 | * Returns the status of auto configuration of the specified class. |
| 269 | */ |
| 270 | public boolean getAutoConfig(Class clazz); |
| 271 | |
| 272 | /** |
| 273 | * Returns the instance variable name of auto configuration of the specified class. |
| 274 | */ |
| 275 | public String getAutoConfigInstance(Class clazz); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 276 | } |