Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +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 | */ |
| 19 | package org.apache.felix.dm; |
| 20 | |
| 21 | import org.osgi.framework.BundleActivator; |
| 22 | import org.osgi.framework.BundleContext; |
| 23 | |
| 24 | /** |
| 25 | * Base bundle activator class. Subclass this activator if you want to use dependency |
| 26 | * management in your bundle. There are two methods you should implement: |
| 27 | * <code>init()</code> and <code>destroy()</code>. Both methods take two arguments, |
| 28 | * the bundle context and the dependency manager. The dependency manager can be used |
| 29 | * to define all the dependencies. |
| 30 | * |
| 31 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 32 | */ |
| 33 | public abstract class DependencyActivatorBase implements BundleActivator { |
| 34 | private BundleContext m_context; |
| 35 | private DependencyManager m_manager; |
| 36 | private Logger m_logger; |
| 37 | |
| 38 | /** |
| 39 | * Initialize the dependency manager. Here you can add all components and their dependencies. |
| 40 | * If something goes wrong and you do not want your bundle to be started, you can throw an |
| 41 | * exception. This exception will be passed on to the <code>start()</code> method of the |
| 42 | * bundle activator, causing the bundle not to start. |
| 43 | * |
| 44 | * @param context the bundle context |
| 45 | * @param manager the dependency manager |
| 46 | * @throws Exception if the initialization fails |
| 47 | */ |
| 48 | public abstract void init(BundleContext context, DependencyManager manager) throws Exception; |
| 49 | |
| 50 | /** |
| 51 | * Destroy the dependency manager. Here you can remove all components and their dependencies. |
| 52 | * Actually, the base class will clean up your dependencies anyway, so most of the time you |
| 53 | * don't need to do anything here. |
| 54 | * <p> |
| 55 | * If something goes wrong and you do not want your bundle to be stopped, you can throw an |
| 56 | * exception. This exception will be passed on to the <code>stop()</code> method of the |
| 57 | * bundle activator, causing the bundle not to stop. |
| 58 | * |
| 59 | * @param context the bundle context |
| 60 | * @param manager the dependency manager |
| 61 | * @throws Exception if the destruction fails |
| 62 | */ |
| 63 | public void destroy(BundleContext context, DependencyManager manager) throws Exception { } |
| 64 | |
| 65 | /** |
| 66 | * Start method of the bundle activator. Initializes the dependency manager |
| 67 | * and calls <code>init()</code>. |
| 68 | * |
| 69 | * @param context the bundle context |
| 70 | */ |
| 71 | public void start(BundleContext context) throws Exception { |
| 72 | m_context = context; |
| 73 | m_logger = new Logger(context); |
| 74 | m_manager = new DependencyManager(context, m_logger); |
| 75 | init(m_context, m_manager); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Stop method of the bundle activator. Calls the <code>destroy()</code> method |
| 80 | * and cleans up all left over dependencies. |
| 81 | * |
| 82 | * @param context the bundle context |
| 83 | */ |
| 84 | public void stop(BundleContext context) throws Exception { |
| 85 | destroy(m_context, m_manager); |
| 86 | m_manager.clear(); |
| 87 | m_manager = null; |
| 88 | m_context = null; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Returns the bundle context that is associated with this bundle. |
| 93 | * |
| 94 | * @return the bundle context |
| 95 | */ |
| 96 | public BundleContext getBundleContext() { |
| 97 | return m_context; |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Returns the dependency manager that is associated with this bundle. |
| 102 | * |
| 103 | * @return the dependency manager |
| 104 | */ |
| 105 | public DependencyManager getDependencyManager() { |
| 106 | return m_manager; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Returns the logger that is associated with this bundle. A logger instance |
| 111 | * is a proxy that will log to a real OSGi logservice if available and standard |
| 112 | * out if not. |
| 113 | * |
| 114 | * @return the logger |
| 115 | */ |
| 116 | public Logger getLogger() { |
| 117 | return m_logger; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Creates a new component. |
| 122 | * |
| 123 | * @return the new component |
| 124 | */ |
| 125 | public Component createComponent() { |
| 126 | return m_manager.createComponent(); |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * Creates a new service dependency. |
| 131 | * |
| 132 | * @return the service dependency |
| 133 | */ |
| 134 | public ServiceDependency createServiceDependency() { |
| 135 | return m_manager.createServiceDependency(); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Creates a new temporal service dependency. |
| 140 | * |
| 141 | * @param timeout the max number of milliseconds to wait for a service availability. |
| 142 | * @return the service dependency |
| 143 | */ |
| 144 | public ServiceDependency createTemporalServiceDependency(long timeout) { |
| 145 | return m_manager.createTemporalServiceDependency(timeout); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Creates a new configuration dependency. |
| 150 | * |
| 151 | * @return the configuration dependency |
| 152 | */ |
| 153 | public ConfigurationDependency createConfigurationDependency() { |
| 154 | return m_manager.createConfigurationDependency(); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Creates a new configuration property metadata. |
| 159 | * |
| 160 | * @return the configuration property metadata |
| 161 | */ |
| 162 | public PropertyMetaData createPropertyMetaData() { |
| 163 | return m_manager.createPropertyMetaData(); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Creates a new bundle dependency. |
| 168 | * |
| 169 | * @return the bundle dependency |
| 170 | */ |
| 171 | public BundleDependency createBundleDependency() { |
| 172 | return m_manager.createBundleDependency(); |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Creates a new resource dependency. |
| 177 | * |
| 178 | * @return the resource dependency |
| 179 | */ |
| 180 | public ResourceDependency createResourceDependency() { |
| 181 | return m_manager.createResourceDependency(); |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * Creates a new aspect service. |
| 186 | * |
| 187 | * @return the aspect service |
| 188 | * @see DependencyManager#createAspectService(Class, String, int, String) |
| 189 | */ |
| 190 | public Component createAspectService(Class<?> serviceInterface, String serviceFilter, int ranking, String attributeName) { |
| 191 | return m_manager.createAspectService(serviceInterface, serviceFilter, ranking, attributeName); |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Creates a new aspect service. |
| 196 | * |
| 197 | * @return the aspect service |
| 198 | * @see DependencyManager#createAspectService(Class, String, int) |
| 199 | */ |
| 200 | public Component createAspectService(Class<?> serviceInterface, String serviceFilter, int ranking) { |
| 201 | return m_manager.createAspectService(serviceInterface, serviceFilter, ranking); |
| 202 | } |
| 203 | |
| 204 | /** |
| 205 | * Creates a new aspect service. |
| 206 | * |
| 207 | * @return the aspect service |
| 208 | * @see DependencyManager#createAspectService(Class, String, int, String, String, String) |
| 209 | */ |
| 210 | public Component createAspectService(Class<?> serviceInterface, String serviceFilter, int ranking, String add, String change, String remove) { |
| 211 | return m_manager.createAspectService(serviceInterface, serviceFilter, ranking, add, change, remove); |
| 212 | } |
| 213 | |
| 214 | /** |
| 215 | * Creates a new aspect service. |
| 216 | * |
| 217 | * @return the aspect service |
| 218 | * @see DependencyManager#createAspectService(Class, String, int, String, String, String, String) |
| 219 | */ |
| 220 | public Component createAspectService(Class<?> serviceInterface, String serviceFilter, int ranking, String add, String change, String remove, String swap) { |
| 221 | return m_manager.createAspectService(serviceInterface, serviceFilter, ranking, add, change, remove, swap); |
| 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Creates a new adapter service. |
| 226 | * |
| 227 | * @return the adapter service |
| 228 | * @see DependencyManager#createAdapterService(Class, String) |
| 229 | */ |
| 230 | public Component createAdapterService(Class<?> serviceInterface, String serviceFilter) { |
| 231 | return m_manager.createAdapterService(serviceInterface, serviceFilter); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Creates a new adapter service. |
| 236 | * |
| 237 | * @return the adapter service |
| 238 | * @see DependencyManager#createAdapterService(Class, String, String) |
| 239 | */ |
| 240 | public Component createAdapterService(Class<?> serviceInterface, String serviceFilter, String autoConfig) { |
| 241 | return m_manager.createAdapterService(serviceInterface, serviceFilter, autoConfig); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Creates a new adapter service. |
| 246 | * |
| 247 | * @return the adapter service |
| 248 | * @see DependencyManager#createAdapterService(Class, String, String, String, String) |
| 249 | */ |
| 250 | public Component createAdapterService(Class<?> serviceInterface, String serviceFilter, String add, String change, String remove) { |
| 251 | return m_manager.createAdapterService(serviceInterface, serviceFilter, add, change, remove); |
| 252 | } |
| 253 | |
| 254 | /** |
| 255 | * Creates a new adapter service. |
| 256 | * @return the adapter service |
| 257 | * @see DependencyManager#createAdapterService(Class, String, String, String, String, String) |
| 258 | */ |
| 259 | public Component createAdapterService(Class<?> serviceInterface, String serviceFilter, String add, String change, String remove, String swap) { |
| 260 | return m_manager.createAdapterService(serviceInterface, serviceFilter, add, change, remove, swap); |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * Creates a new adapter service. |
| 265 | * @return the adapter service |
| 266 | * @see DependencyManager#createAdapterService(Class, String, String, Object, String, String, String, String, boolean) |
| 267 | */ |
| 268 | public Component createAdapterService(Class<?> serviceInterface, String serviceFilter, |
| 269 | String autoConfig, Object callbackInstance, String add, String change, String remove, String swap) { |
| 270 | return m_manager.createAdapterService(serviceInterface, serviceFilter, autoConfig, callbackInstance, add, change, remove, swap, true); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Creates a new adapter service. |
| 275 | * @return the adapter service |
| 276 | * @see DependencyManager#createAdapterService(Class, String, String, Object, String, String, String, String, boolean) |
| 277 | */ |
| 278 | public Component createAdapterService(Class<?> serviceInterface, String serviceFilter, |
| 279 | String autoConfig, Object callbackInstance, String add, String change, String remove, String swap, boolean propagate) { |
| 280 | return m_manager.createAdapterService(serviceInterface, serviceFilter, autoConfig, callbackInstance, add, change, remove, swap, propagate); |
| 281 | } |
| 282 | |
| 283 | /** |
| 284 | * Creates a new resource adapter service. |
| 285 | * |
| 286 | * @return the resource adapter service |
| 287 | */ |
| 288 | public Component createResourceAdapter(String resourceFilter, boolean propagate, Object callbackInstance, String callbackChanged) { |
| 289 | return m_manager.createResourceAdapterService(resourceFilter, propagate, callbackInstance, callbackChanged); |
| 290 | } |
| 291 | |
| 292 | /** |
| 293 | * Creates a new resource adapter service. |
| 294 | * |
| 295 | * @return the resource adapter service |
| 296 | */ |
| 297 | public Component createResourceAdapter(String resourceFilter, boolean propagate, Object callbackInstance, String callbackSet, String callbackChanged) { |
| 298 | return m_manager.createResourceAdapterService(resourceFilter, propagate, callbackInstance, callbackSet, callbackChanged); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Creates a new resource adapter service. |
| 303 | * |
| 304 | * @return the resource adapter service |
| 305 | */ |
| 306 | public Component createResourceAdapter(String resourceFilter, Object propagateCallbackInstance, String propagateCallbackMethod, Object callbackInstance, String callbackChanged) { |
| 307 | return m_manager.createResourceAdapterService(resourceFilter, propagateCallbackInstance, propagateCallbackMethod, callbackInstance, null, callbackChanged); |
| 308 | } |
| 309 | |
| 310 | /** |
| 311 | * Creates a new resource adapter service. |
| 312 | * |
| 313 | * @return the resource adapter service |
| 314 | */ |
| 315 | public Component createResourceAdapter(String resourceFilter, Object propagateCallbackInstance, String propagateCallbackMethod, Object callbackInstance, String callbackSet, String callbackChanged) { |
| 316 | return m_manager.createResourceAdapterService(resourceFilter, propagateCallbackInstance, propagateCallbackMethod, callbackInstance, callbackSet, callbackChanged); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * Creates a new bundle adapter service. |
| 321 | * |
| 322 | * @return the bundle adapter service |
| 323 | */ |
| 324 | public Component createBundleAdapterService(int bundleStateMask, String bundleFilter, boolean propagate) { |
| 325 | return m_manager.createBundleAdapterService(bundleStateMask, bundleFilter, propagate); |
| 326 | } |
| 327 | |
| 328 | /** |
Pierre De Rop | c40d93f | 2015-05-04 20:25:57 +0000 | [diff] [blame^] | 329 | * Creates a new bundle adapter service, using a specific callback instance |
| 330 | * |
| 331 | * @return the bundle adapter service |
| 332 | */ |
| 333 | public Component createBundleAdapterService(int bundleStateMask, String bundleFilter, boolean propagate, |
| 334 | Object callbackInstance, String add, String change, String remove) { |
| 335 | return m_manager.createBundleAdapterService(bundleStateMask, bundleFilter, propagate, callbackInstance, add, change, remove); |
| 336 | } |
| 337 | |
| 338 | /** |
Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 339 | * Creates a new factory configuration adapter service. |
| 340 | * |
| 341 | * @return the factory configuration adapter service |
| 342 | */ |
| 343 | public Component createFactoryConfigurationAdapterService(String factoryPid, String update, boolean propagate) { |
| 344 | return m_manager.createFactoryConfigurationAdapterService(factoryPid, update, propagate); |
| 345 | } |
| 346 | |
| 347 | /** |
Pierre De Rop | c40d93f | 2015-05-04 20:25:57 +0000 | [diff] [blame^] | 348 | * Creates a new factory configuration adapter service, using a specific callback instance |
| 349 | * |
| 350 | * @return the factory configuration adapter service |
| 351 | */ |
| 352 | public Component createFactoryConfigurationAdapterService(String factoryPid, String update, boolean propagate, Object callbackInstance) { |
| 353 | return m_manager.createFactoryConfigurationAdapterService(factoryPid, update, propagate, callbackInstance); |
| 354 | } |
| 355 | |
| 356 | /** |
Pierre De Rop | 3a00a21 | 2015-03-01 09:27:46 +0000 | [diff] [blame] | 357 | * Creates a new factory configuration adapter service. |
| 358 | * |
| 359 | * @return the factory configuration adapter service |
| 360 | */ |
| 361 | public Component createFactoryConfigurationAdapterService(String factoryPid, String update, boolean propagate, String heading, String desc, String localization, PropertyMetaData[] propertiesMetaData) { |
| 362 | return m_manager.createAdapterFactoryConfigurationService(factoryPid, update, propagate, heading, desc, localization, propertiesMetaData); |
| 363 | } |
| 364 | } |