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 | */ |
| 19 | package org.apache.felix.dependencymanager; |
| 20 | |
| 21 | import java.util.List; |
| 22 | |
| 23 | import org.osgi.framework.BundleActivator; |
| 24 | import org.osgi.framework.BundleContext; |
| 25 | |
| 26 | /** |
| 27 | * Base bundle activator class. Subclass this activator if you want to use dependency |
| 28 | * management in your bundle. There are two methods you should implement: |
| 29 | * <code>init()</code> and <code>destroy()</code>. Both methods take two arguments, |
| 30 | * the bundle context and the dependency manager. The dependency manager can be used |
| 31 | * to define all the dependencies. |
| 32 | * |
| 33 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 34 | */ |
| 35 | public abstract class DependencyActivatorBase implements BundleActivator { |
| 36 | private BundleContext m_context; |
| 37 | private DependencyManager m_manager; |
| 38 | private Logger m_logger; |
| 39 | |
| 40 | /** |
| 41 | * Initialize the dependency manager. Here you can add all services and their dependencies. |
| 42 | * If something goes wrong and you do not want your bundle to be started, you can throw an |
| 43 | * exception. This exception will be passed on to the <code>start()</code> method of the |
| 44 | * bundle activator, causing the bundle not to start. |
| 45 | * |
| 46 | * @param context the bundle context |
| 47 | * @param manager the dependency manager |
| 48 | * @throws Exception if the initialization fails |
| 49 | */ |
| 50 | public abstract void init(BundleContext context, DependencyManager manager) throws Exception; |
| 51 | |
| 52 | /** |
| 53 | * Destroy the dependency manager. Here you can remove all services and their dependencies. |
| 54 | * Actually, the base class will clean up your dependencies anyway, so most of the time you |
| 55 | * don't need to do anything here. |
| 56 | * If something goes wrong and you do not want your bundle to be stopped, you can throw an |
| 57 | * exception. This exception will be passed on to the <code>stop()</code> method of the |
| 58 | * bundle activator, causing the bundle not to stop. |
| 59 | * |
| 60 | * @param context the bundle context |
| 61 | * @param manager the dependency manager |
| 62 | * @throws Exception if the destruction fails |
| 63 | */ |
| 64 | public abstract void destroy(BundleContext context, DependencyManager manager) throws Exception; |
| 65 | |
| 66 | /** |
| 67 | * Start method of the bundle activator. Initializes the dependency manager |
| 68 | * and calls <code>init()</code>. |
| 69 | * |
| 70 | * @param context the bundle context |
| 71 | */ |
| 72 | public void start(BundleContext context) throws Exception { |
| 73 | m_context = context; |
| 74 | m_logger = new Logger(context); |
| 75 | m_manager = new DependencyManager(context, m_logger); |
| 76 | init(m_context, m_manager); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Stop method of the bundle activator. Calls the <code>destroy()</code> method |
| 81 | * and cleans up all left over dependencies. |
| 82 | * |
| 83 | * @param context the bundle context |
| 84 | */ |
| 85 | public void stop(BundleContext context) throws Exception { |
| 86 | destroy(m_context, m_manager); |
| 87 | cleanup(m_manager); |
| 88 | m_manager = null; |
| 89 | m_context = null; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Creates a new service. |
| 94 | * |
| 95 | * @return the new service |
| 96 | */ |
| 97 | public Service createService() { |
| 98 | return new ServiceImpl(m_context, m_manager, m_logger); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Creates a new service dependency. |
| 103 | * |
| 104 | * @return the service dependency |
| 105 | */ |
| 106 | public ServiceDependency createServiceDependency() { |
| 107 | return new ServiceDependency(m_context, m_logger); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Creates a new configuration dependency. |
| 112 | * |
| 113 | * @return the configuration dependency |
| 114 | */ |
| 115 | public ConfigurationDependency createConfigurationDependency() { |
| 116 | return new ConfigurationDependency(m_context, m_logger); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * Cleans up all services and their dependencies. |
| 121 | * |
| 122 | * @param manager the dependency manager |
| 123 | */ |
| 124 | private void cleanup(DependencyManager manager) { |
| 125 | List services = manager.getServices(); |
| 126 | for (int i = services.size() - 1; i >= 0; i--) { |
| 127 | Service service = (Service) services.get(i); |
| 128 | manager.remove(service); |
| 129 | // remove any state listeners that are still registered |
| 130 | if (service instanceof ServiceImpl) { |
| 131 | ServiceImpl si = (ServiceImpl) service; |
| 132 | si.removeStateListeners(); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | } |