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 | */ |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 19 | package org.apache.felix.dm; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 20 | |
Marcel Offermans | 80eeafe | 2009-12-01 22:12:26 +0000 | [diff] [blame] | 21 | import java.util.Dictionary; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 22 | import java.util.List; |
| 23 | |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 24 | import org.apache.felix.dm.dependencies.BundleDependency; |
| 25 | import org.apache.felix.dm.dependencies.ConfigurationDependency; |
Pierre De Rop | a0204f5 | 2010-03-06 22:23:57 +0000 | [diff] [blame] | 26 | import org.apache.felix.dm.dependencies.PropertyMetaData; |
Marcel Offermans | d665eaf | 2010-02-16 15:56:35 +0000 | [diff] [blame] | 27 | import org.apache.felix.dm.dependencies.ResourceDependency; |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 28 | import org.apache.felix.dm.dependencies.ServiceDependency; |
| 29 | import org.apache.felix.dm.dependencies.TemporalServiceDependency; |
| 30 | import org.apache.felix.dm.impl.Logger; |
| 31 | import org.apache.felix.dm.impl.ServiceImpl; |
| 32 | import org.apache.felix.dm.service.Service; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 33 | import org.osgi.framework.BundleActivator; |
| 34 | import org.osgi.framework.BundleContext; |
| 35 | |
| 36 | /** |
| 37 | * Base bundle activator class. Subclass this activator if you want to use dependency |
| 38 | * management in your bundle. There are two methods you should implement: |
| 39 | * <code>init()</code> and <code>destroy()</code>. Both methods take two arguments, |
| 40 | * the bundle context and the dependency manager. The dependency manager can be used |
| 41 | * to define all the dependencies. |
| 42 | * |
| 43 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 44 | */ |
| 45 | public abstract class DependencyActivatorBase implements BundleActivator { |
| 46 | private BundleContext m_context; |
| 47 | private DependencyManager m_manager; |
| 48 | private Logger m_logger; |
| 49 | |
| 50 | /** |
| 51 | * Initialize the dependency manager. Here you can add all services and their dependencies. |
| 52 | * If something goes wrong and you do not want your bundle to be started, you can throw an |
| 53 | * exception. This exception will be passed on to the <code>start()</code> method of the |
| 54 | * bundle activator, causing the bundle not to start. |
| 55 | * |
| 56 | * @param context the bundle context |
| 57 | * @param manager the dependency manager |
| 58 | * @throws Exception if the initialization fails |
| 59 | */ |
| 60 | public abstract void init(BundleContext context, DependencyManager manager) throws Exception; |
| 61 | |
| 62 | /** |
| 63 | * Destroy the dependency manager. Here you can remove all services and their dependencies. |
| 64 | * Actually, the base class will clean up your dependencies anyway, so most of the time you |
| 65 | * don't need to do anything here. |
| 66 | * If something goes wrong and you do not want your bundle to be stopped, you can throw an |
| 67 | * exception. This exception will be passed on to the <code>stop()</code> method of the |
| 68 | * bundle activator, causing the bundle not to stop. |
| 69 | * |
| 70 | * @param context the bundle context |
| 71 | * @param manager the dependency manager |
| 72 | * @throws Exception if the destruction fails |
| 73 | */ |
| 74 | public abstract void destroy(BundleContext context, DependencyManager manager) throws Exception; |
| 75 | |
| 76 | /** |
| 77 | * Start method of the bundle activator. Initializes the dependency manager |
| 78 | * and calls <code>init()</code>. |
| 79 | * |
| 80 | * @param context the bundle context |
| 81 | */ |
| 82 | public void start(BundleContext context) throws Exception { |
| 83 | m_context = context; |
| 84 | m_logger = new Logger(context); |
| 85 | m_manager = new DependencyManager(context, m_logger); |
| 86 | init(m_context, m_manager); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Stop method of the bundle activator. Calls the <code>destroy()</code> method |
| 91 | * and cleans up all left over dependencies. |
| 92 | * |
| 93 | * @param context the bundle context |
| 94 | */ |
| 95 | public void stop(BundleContext context) throws Exception { |
| 96 | destroy(m_context, m_manager); |
| 97 | cleanup(m_manager); |
| 98 | m_manager = null; |
| 99 | m_context = null; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Creates a new service. |
| 104 | * |
| 105 | * @return the new service |
| 106 | */ |
| 107 | public Service createService() { |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 108 | return m_manager.createService(); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Creates a new service dependency. |
| 113 | * |
| 114 | * @return the service dependency |
| 115 | */ |
| 116 | public ServiceDependency createServiceDependency() { |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 117 | return m_manager.createServiceDependency(); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
Marcel Offermans | 74363c3 | 2009-11-23 19:56:08 +0000 | [diff] [blame] | 121 | * Creates a new temporal service dependency. |
| 122 | * |
| 123 | * @param timeout the max number of milliseconds to wait for a service availability. |
| 124 | * @return the service dependency |
| 125 | */ |
| 126 | public TemporalServiceDependency createTemporalServiceDependency() { |
Marcel Offermans | e14b342 | 2009-11-25 23:04:32 +0000 | [diff] [blame] | 127 | return m_manager.createTemporalServiceDependency(); |
Marcel Offermans | 74363c3 | 2009-11-23 19:56:08 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 131 | * Creates a new configuration dependency. |
| 132 | * |
| 133 | * @return the configuration dependency |
| 134 | */ |
| 135 | public ConfigurationDependency createConfigurationDependency() { |
Marcel Offermans | d66c5ce | 2009-11-26 09:58:44 +0000 | [diff] [blame] | 136 | return m_manager.createConfigurationDependency(); |
| 137 | } |
Pierre De Rop | a0204f5 | 2010-03-06 22:23:57 +0000 | [diff] [blame] | 138 | |
| 139 | /** |
| 140 | * Creates a new configuration property MetaData. |
| 141 | * @return a new configuration property MetaData |
| 142 | */ |
| 143 | public PropertyMetaData createPropertyMetaData() { |
| 144 | return m_manager.createPropertyMetaData(); |
| 145 | } |
Marcel Offermans | d66c5ce | 2009-11-26 09:58:44 +0000 | [diff] [blame] | 146 | |
| 147 | /** |
| 148 | * Creates a new bundle dependency. |
| 149 | * |
| 150 | * @return the bundle dependency |
| 151 | */ |
| 152 | public BundleDependency createBundleDependency() { |
| 153 | return m_manager.createBundleDependency(); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 154 | } |
Marcel Offermans | d665eaf | 2010-02-16 15:56:35 +0000 | [diff] [blame] | 155 | |
| 156 | public ResourceDependency createResourceDependency() { |
| 157 | return m_manager.createResourceDependency(); |
| 158 | } |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 159 | |
Marcel Offermans | ad76067 | 2010-03-03 15:30:01 +0000 | [diff] [blame] | 160 | public Service createAspectService(Class serviceInterface, String serviceFilter, int ranking, Object aspectImplementation, Dictionary properties) { |
| 161 | return m_manager.createAspectService(serviceInterface, serviceFilter, ranking, aspectImplementation, properties); |
| 162 | } |
| 163 | public Service createAspectService(Class serviceInterface, String serviceFilter, int ranking, Object factory, String factoryCreateMethod, Dictionary properties) { |
| 164 | return m_manager.createAspectService(serviceInterface, serviceFilter, ranking, factory, factoryCreateMethod, properties); |
Marcel Offermans | 80eeafe | 2009-12-01 22:12:26 +0000 | [diff] [blame] | 165 | } |
Marcel Offermans | d665eaf | 2010-02-16 15:56:35 +0000 | [diff] [blame] | 166 | |
Marcel Offermans | 61a8114 | 2010-04-02 15:16:50 +0000 | [diff] [blame^] | 167 | public Service createAdapterService(Class serviceInterface, String serviceFilter, String adapterInterface, Object adapterImplementation, Dictionary adapterProperties) { |
| 168 | return m_manager.createAdapterService(serviceInterface, serviceFilter, adapterInterface, adapterImplementation, adapterProperties); |
| 169 | } |
| 170 | public Service createAdapterService(Class serviceInterface, String serviceFilter, String[] adapterInterface, Object adapterImplementation, Dictionary adapterProperties) { |
Marcel Offermans | d665eaf | 2010-02-16 15:56:35 +0000 | [diff] [blame] | 171 | return m_manager.createAdapterService(serviceInterface, serviceFilter, adapterInterface, adapterImplementation, adapterProperties); |
| 172 | } |
| 173 | |
Marcel Offermans | 61a8114 | 2010-04-02 15:16:50 +0000 | [diff] [blame^] | 174 | public Service createResourceAdapter(String resourceFilter, String adapterInterface, Dictionary adapterProperties, Object adapterImplementation, boolean propagate) { |
| 175 | return m_manager.createResourceAdapterService(resourceFilter, adapterInterface, adapterProperties, adapterImplementation, propagate); |
| 176 | } |
| 177 | public Service createResourceAdapter(String resourceFilter, String[] adapterInterface, Dictionary adapterProperties, Object adapterImplementation, boolean propagate) { |
Marcel Offermans | d665eaf | 2010-02-16 15:56:35 +0000 | [diff] [blame] | 178 | return m_manager.createResourceAdapterService(resourceFilter, adapterInterface, adapterProperties, adapterImplementation, propagate); |
| 179 | } |
| 180 | |
| 181 | public Service createBundleAdapterService(int bundleStateMask, String bundleFilter, Object adapterImplementation, String adapterInterface, Dictionary adapterProperties, boolean propagate) { |
| 182 | return m_manager.createBundleAdapterService(bundleStateMask, bundleFilter, adapterImplementation, adapterInterface, adapterProperties, propagate); |
| 183 | } |
Marcel Offermans | 61a8114 | 2010-04-02 15:16:50 +0000 | [diff] [blame^] | 184 | public Service createBundleAdapterService(int bundleStateMask, String bundleFilter, Object adapterImplementation, String[] adapterInterface, Dictionary adapterProperties, boolean propagate) { |
| 185 | return m_manager.createBundleAdapterService(bundleStateMask, bundleFilter, adapterImplementation, adapterInterface, adapterProperties, propagate); |
| 186 | } |
Marcel Offermans | 80eeafe | 2009-12-01 22:12:26 +0000 | [diff] [blame] | 187 | |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 188 | /** |
| 189 | * Cleans up all services and their dependencies. |
| 190 | * |
| 191 | * @param manager the dependency manager |
| 192 | */ |
| 193 | private void cleanup(DependencyManager manager) { |
| 194 | List services = manager.getServices(); |
| 195 | for (int i = services.size() - 1; i >= 0; i--) { |
| 196 | Service service = (Service) services.get(i); |
| 197 | manager.remove(service); |
| 198 | // remove any state listeners that are still registered |
| 199 | if (service instanceof ServiceImpl) { |
| 200 | ServiceImpl si = (ServiceImpl) service; |
| 201 | si.removeStateListeners(); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | } |