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 | |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.Collections; |
Marcel Offermans | 80eeafe | 2009-12-01 22:12:26 +0000 | [diff] [blame] | 23 | import java.util.Dictionary; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 24 | import java.util.List; |
| 25 | |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 26 | import org.apache.felix.dm.dependencies.BundleDependency; |
| 27 | import org.apache.felix.dm.dependencies.ConfigurationDependency; |
| 28 | import org.apache.felix.dm.dependencies.ResourceDependency; |
| 29 | import org.apache.felix.dm.dependencies.ServiceDependency; |
| 30 | import org.apache.felix.dm.dependencies.TemporalServiceDependency; |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 31 | import org.apache.felix.dm.impl.AdapterImpl; |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 32 | import org.apache.felix.dm.impl.AspectImpl; |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 33 | import org.apache.felix.dm.impl.BundleAdapterImpl; |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 34 | import org.apache.felix.dm.impl.Logger; |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 35 | import org.apache.felix.dm.impl.ResourceAdapterImpl; |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 36 | import org.apache.felix.dm.impl.ServiceImpl; |
| 37 | import org.apache.felix.dm.impl.dependencies.BundleDependencyImpl; |
| 38 | import org.apache.felix.dm.impl.dependencies.ConfigurationDependencyImpl; |
| 39 | import org.apache.felix.dm.impl.dependencies.ResourceDependencyImpl; |
| 40 | import org.apache.felix.dm.impl.dependencies.ServiceDependencyImpl; |
| 41 | import org.apache.felix.dm.impl.dependencies.TemporalServiceDependencyImpl; |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 42 | import org.apache.felix.dm.resources.Resource; |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 43 | import org.apache.felix.dm.service.Service; |
Marcel Offermans | b8405a5 | 2009-12-22 19:01:31 +0000 | [diff] [blame] | 44 | import org.osgi.framework.BundleContext; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 45 | |
| 46 | /** |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 47 | * The dependency manager manages all services and their dependencies. Using |
| 48 | * this API you can declare all services and their dependencies. Under normal |
| 49 | * circumstances, you get passed an instance of this class through the |
| 50 | * <code>DependencyActivatorBase</code> subclass you use as your |
| 51 | * <code>BundleActivator</code>, but it is also possible to create your |
| 52 | * own instance. |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 53 | * |
| 54 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 55 | */ |
| 56 | public class DependencyManager { |
Marcel Offermans | ad76067 | 2010-03-03 15:30:01 +0000 | [diff] [blame] | 57 | public static final String ASPECT = "org.apache.felix.dependencymanager.aspect"; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 58 | private final BundleContext m_context; |
| 59 | private final Logger m_logger; |
| 60 | private List m_services = Collections.synchronizedList(new ArrayList()); |
| 61 | |
| 62 | /** |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 63 | * Creates a new dependency manager. You need to supply the |
| 64 | * <code>BundleContext</code> to be used by the dependency |
| 65 | * manager to register services and communicate with the |
| 66 | * framework. |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 67 | * |
| 68 | * @param context the bundle context |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 69 | */ |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 70 | public DependencyManager(BundleContext context) { |
| 71 | this(context, new Logger(context)); |
| 72 | } |
| 73 | |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 74 | DependencyManager(BundleContext context, Logger logger) { |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 75 | m_context = context; |
| 76 | m_logger = logger; |
| 77 | } |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 78 | |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 79 | /** |
| 80 | * Adds a new service to the dependency manager. After the service was added |
| 81 | * it will be started immediately. |
| 82 | * |
| 83 | * @param service the service to add |
| 84 | */ |
| 85 | public void add(Service service) { |
| 86 | m_services.add(service); |
| 87 | service.start(); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Removes a service from the dependency manager. Before the service is removed |
| 92 | * it is stopped first. |
| 93 | * |
| 94 | * @param service the service to remove |
| 95 | */ |
| 96 | public void remove(Service service) { |
| 97 | service.stop(); |
| 98 | m_services.remove(service); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Creates a new service. |
| 103 | * |
| 104 | * @return the new service |
| 105 | */ |
| 106 | public Service createService() { |
| 107 | return new ServiceImpl(m_context, this, m_logger); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Creates a new service dependency. |
| 112 | * |
| 113 | * @return the service dependency |
| 114 | */ |
| 115 | public ServiceDependency createServiceDependency() { |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 116 | return new ServiceDependencyImpl(m_context, m_logger); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Marcel Offermans | 74363c3 | 2009-11-23 19:56:08 +0000 | [diff] [blame] | 119 | /** |
| 120 | * Creates a new temporal service dependency. |
| 121 | * |
| 122 | * @param timeout the max number of milliseconds to wait for a service availability. |
| 123 | * @return the service dependency |
| 124 | */ |
| 125 | public TemporalServiceDependency createTemporalServiceDependency() { |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 126 | return new TemporalServiceDependencyImpl(m_context, m_logger); |
Marcel Offermans | 74363c3 | 2009-11-23 19:56:08 +0000 | [diff] [blame] | 127 | } |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 128 | |
| 129 | /** |
| 130 | * Creates a new configuration dependency. |
| 131 | * |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 132 | * @return the configuration dependency |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 133 | */ |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 134 | public ConfigurationDependency createConfigurationDependency() { |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 135 | return new ConfigurationDependencyImpl(m_context, m_logger); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 138 | /** |
| 139 | * Creates a new bundle dependency. |
| 140 | * |
| 141 | * @return |
| 142 | */ |
Marcel Offermans | d66c5ce | 2009-11-26 09:58:44 +0000 | [diff] [blame] | 143 | public BundleDependency createBundleDependency() { |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 144 | return new BundleDependencyImpl(m_context, m_logger); |
Marcel Offermans | d66c5ce | 2009-11-26 09:58:44 +0000 | [diff] [blame] | 145 | } |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 146 | |
| 147 | /** |
| 148 | * Creates a new resource dependency. |
| 149 | * |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 150 | * @return the resource dependency |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 151 | */ |
| 152 | public ResourceDependency createResourceDependency() { |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 153 | return new ResourceDependencyImpl(m_context, m_logger); |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 154 | } |
Marcel Offermans | 80eeafe | 2009-12-01 22:12:26 +0000 | [diff] [blame] | 155 | |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 156 | /** |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 157 | * Creates a new aspect. The aspect will be applied to any service that |
| 158 | * matches the specified interface and filter. For each matching service |
| 159 | * an aspect will be created based on the aspect implementation class. |
| 160 | * The aspect will be registered with the same interface and properties |
| 161 | * as the original service, plus any extra properties you supply here. |
| 162 | * It will also inherit all dependencies, and if you declare the original |
| 163 | * service as a member it will be injected. |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 164 | * |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 165 | * @param serviceInterface the service interface to apply the aspect to |
| 166 | * @param serviceFilter the filter condition to use with the service interface |
| 167 | * @param aspectImplementation the implementation of the aspect |
| 168 | * @param aspectProperties additional properties to use with the aspect service registration |
| 169 | * @return a service that acts as a factory for generating aspects |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 170 | */ |
Marcel Offermans | ad76067 | 2010-03-03 15:30:01 +0000 | [diff] [blame] | 171 | public Service createAspectService(Class serviceInterface, String serviceFilter, int ranking, Object aspectImplementation, Dictionary aspectProperties) { |
Marcel Offermans | 80eeafe | 2009-12-01 22:12:26 +0000 | [diff] [blame] | 172 | return createService() |
Marcel Offermans | ad76067 | 2010-03-03 15:30:01 +0000 | [diff] [blame] | 173 | .setImplementation(new AspectImpl(serviceInterface, serviceFilter, ranking, aspectImplementation, aspectProperties)) |
Marcel Offermans | 80eeafe | 2009-12-01 22:12:26 +0000 | [diff] [blame] | 174 | .add(createServiceDependency() |
Marcel Offermans | ad76067 | 2010-03-03 15:30:01 +0000 | [diff] [blame] | 175 | .setService(serviceInterface, createAspectFilter(serviceFilter)) |
Marcel Offermans | 80eeafe | 2009-12-01 22:12:26 +0000 | [diff] [blame] | 176 | .setAutoConfig(false) |
| 177 | .setCallbacks("added", "removed") |
| 178 | ); |
| 179 | } |
Marcel Offermans | ad76067 | 2010-03-03 15:30:01 +0000 | [diff] [blame] | 180 | public Service createAspectService(Class serviceInterface, String serviceFilter, int ranking, Object factory, String factoryCreateMethod, Dictionary aspectProperties) { |
| 181 | return createService() |
| 182 | .setImplementation(new AspectImpl(serviceInterface, serviceFilter, ranking, factory, factoryCreateMethod, aspectProperties)) |
| 183 | .add(createServiceDependency() |
| 184 | .setService(serviceInterface, createAspectFilter(serviceFilter)) |
| 185 | .setAutoConfig(false) |
| 186 | .setCallbacks("added", "removed") |
| 187 | ); |
| 188 | } |
| 189 | private String createAspectFilter(String filter) { |
| 190 | // we only want to match services which are not themselves aspects |
| 191 | if (filter == null || filter.length() == 0) { |
| 192 | return "(!(" + ASPECT + "=*))"; |
| 193 | } |
| 194 | else { |
| 195 | return "(&(!(" + ASPECT + "=*))" + filter + ")"; |
| 196 | } |
| 197 | } |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 198 | |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 199 | /** |
| 200 | * Creates a new adapter. The adapter will be applied to any service that |
| 201 | * matches the specified interface and filter. For each matching service |
| 202 | * an adapter will be created based on the adapter implementation class. |
| 203 | * The adapter will be registered with the specified interface and existing properties |
| 204 | * from the original service plus any extra properties you supply here. |
| 205 | * It will also inherit all dependencies, and if you declare the original |
| 206 | * service as a member it will be injected. |
| 207 | * |
Marcel Offermans | d665eaf | 2010-02-16 15:56:35 +0000 | [diff] [blame] | 208 | * @param serviceInterface the service interface to apply the adapter to |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 209 | * @param serviceFilter the filter condition to use with the service interface |
| 210 | * @param adapterInterface the interface to use when registering adapters |
| 211 | * @param adapterImplementation the implementation of the adapter |
| 212 | * @param adapterProperties additional properties to use with the adapter service registration |
| 213 | * @return a service that acts as a factory for generating adapters |
| 214 | */ |
| 215 | public Service createAdapterService(Class serviceInterface, String serviceFilter, Class adapterInterface, Object adapterImplementation, Dictionary adapterProperties) { |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 216 | return createService() |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 217 | .setImplementation(new AdapterImpl(serviceInterface, serviceFilter, adapterImplementation, adapterInterface.getName(), adapterProperties)) |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 218 | .add(createServiceDependency() |
Marcel Offermans | 3e1998c | 2009-12-16 20:47:25 +0000 | [diff] [blame] | 219 | .setService(serviceInterface) |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 220 | .setAutoConfig(false) |
| 221 | .setCallbacks("added", "removed") |
Marcel Offermans | 626260c | 2009-12-22 17:06:46 +0000 | [diff] [blame] | 222 | ); |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 225 | /** |
| 226 | * Creates a new resource adapter. The adapter will be applied to any resource that |
| 227 | * matches the specified filter condition. For each matching resource |
| 228 | * an adapter will be created based on the adapter implementation class. |
| 229 | * The adapter will be registered with the specified interface and existing properties |
| 230 | * from the original resource plus any extra properties you supply here. |
| 231 | * It will also inherit all dependencies, and if you declare the original |
| 232 | * service as a member it will be injected. |
| 233 | * |
| 234 | * @param resourceFilter the filter condition to use with the resource |
| 235 | * @param adapterInterface the interface to use when registering adapters |
| 236 | * @param adapterProperties additional properties to use with the adapter service registration |
| 237 | * @param adapterImplementation the implementation of the adapter |
| 238 | * @param propagate <code>true</code> if properties from the resource should be propagated to the service |
| 239 | * @return a service that acts as a factory for generating resource adapters |
| 240 | * @see Resource |
| 241 | */ |
| 242 | public Service createResourceAdapterService(String resourceFilter, Class adapterInterface, Dictionary adapterProperties, Object adapterImplementation, boolean propagate) { |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 243 | return createService() |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 244 | .setImplementation(new ResourceAdapterImpl(resourceFilter, adapterImplementation, adapterInterface.getName(), adapterProperties, propagate)) |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 245 | .add(createResourceDependency() |
| 246 | .setFilter(resourceFilter) |
| 247 | .setAutoConfig(false) |
| 248 | .setCallbacks("added", "removed") |
Marcel Offermans | 626260c | 2009-12-22 17:06:46 +0000 | [diff] [blame] | 249 | ); |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 250 | } |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 251 | |
| 252 | /** |
| 253 | * Creates a new bundle adapter. The adapter will be applied to any bundle that |
| 254 | * matches the specified bundle state mask and filter condition. For each matching |
| 255 | * bundle an adapter will be created based on the adapter implementation class. |
| 256 | * The adapter will be registered with the specified interface |
| 257 | * |
| 258 | * TODO and existing properties from the original resource plus any extra properties you supply here. |
| 259 | * It will also inherit all dependencies, and if you declare the original |
| 260 | * service as a member it will be injected. |
| 261 | * |
| 262 | * @param bundleStateMask the bundle state mask to apply |
| 263 | * @param bundleFilter the filter to apply to the bundle manifest |
| 264 | * @param adapterImplementation the implementation of the adapter |
| 265 | * @param adapterInterface the interface to use when registering adapters |
Marcel Offermans | d665eaf | 2010-02-16 15:56:35 +0000 | [diff] [blame] | 266 | * @param adapterProperties additional properties to use with the service registration |
| 267 | * @param propagate <code>true</code> if properties from the bundle should be propagated to the service |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 268 | * @return a service that acts as a factory for generating bundle adapters |
| 269 | */ |
Marcel Offermans | d665eaf | 2010-02-16 15:56:35 +0000 | [diff] [blame] | 270 | public Service createBundleAdapterService(int bundleStateMask, String bundleFilter, Object adapterImplementation, String adapterInterface, Dictionary adapterProperties, boolean propagate) { |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 271 | return createService() |
Marcel Offermans | d665eaf | 2010-02-16 15:56:35 +0000 | [diff] [blame] | 272 | .setImplementation(new BundleAdapterImpl(bundleStateMask, bundleFilter, adapterImplementation, adapterInterface, adapterProperties, propagate)) |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 273 | .add(createBundleDependency() |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 274 | .setFilter(bundleFilter) |
| 275 | .setStateMask(bundleStateMask) |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 276 | .setCallbacks("added", "removed") |
Marcel Offermans | 626260c | 2009-12-22 17:06:46 +0000 | [diff] [blame] | 277 | ); |
Marcel Offermans | 001db05 | 2009-12-08 08:58:40 +0000 | [diff] [blame] | 278 | } |
Marcel Offermans | 80eeafe | 2009-12-01 22:12:26 +0000 | [diff] [blame] | 279 | |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 280 | /** |
| 281 | * Returns a list of services. |
| 282 | * |
| 283 | * @return a list of services |
| 284 | */ |
| 285 | public List getServices() { |
| 286 | return Collections.unmodifiableList(m_services); |
| 287 | } |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 288 | } |