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 | 5be5f14 | 2011-04-26 10:47:12 +0000 | [diff] [blame^] | 21 | import java.lang.ref.WeakReference; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 22 | import java.util.ArrayList; |
| 23 | import java.util.Collections; |
Marcel Offermans | 5be5f14 | 2011-04-26 10:47:12 +0000 | [diff] [blame^] | 24 | import java.util.HashSet; |
| 25 | import java.util.Iterator; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 26 | import java.util.List; |
Marcel Offermans | 5be5f14 | 2011-04-26 10:47:12 +0000 | [diff] [blame^] | 27 | import java.util.Set; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 28 | |
Pierre De Rop | 3423158 | 2010-05-23 20:05:16 +0000 | [diff] [blame] | 29 | import org.apache.felix.dm.impl.AdapterServiceImpl; |
Pierre De Rop | 19476fe | 2010-05-23 08:13:58 +0000 | [diff] [blame] | 30 | import org.apache.felix.dm.impl.AspectServiceImpl; |
Pierre De Rop | 13dd63d | 2010-05-23 21:58:28 +0000 | [diff] [blame] | 31 | import org.apache.felix.dm.impl.BundleAdapterServiceImpl; |
Marcel Offermans | faaed47 | 2010-09-08 10:07:32 +0000 | [diff] [blame] | 32 | import org.apache.felix.dm.impl.ComponentImpl; |
Pierre De Rop | 3e10037 | 2010-05-24 12:43:44 +0000 | [diff] [blame] | 33 | import org.apache.felix.dm.impl.FactoryConfigurationAdapterServiceImpl; |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 34 | import org.apache.felix.dm.impl.Logger; |
Pierre De Rop | 445ddec | 2010-05-23 21:05:27 +0000 | [diff] [blame] | 35 | import org.apache.felix.dm.impl.ResourceAdapterServiceImpl; |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 36 | import org.apache.felix.dm.impl.dependencies.BundleDependencyImpl; |
| 37 | import org.apache.felix.dm.impl.dependencies.ConfigurationDependencyImpl; |
| 38 | import org.apache.felix.dm.impl.dependencies.ResourceDependencyImpl; |
| 39 | import org.apache.felix.dm.impl.dependencies.ServiceDependencyImpl; |
| 40 | import org.apache.felix.dm.impl.dependencies.TemporalServiceDependencyImpl; |
Pierre De Rop | a0204f5 | 2010-03-06 22:23:57 +0000 | [diff] [blame] | 41 | import org.apache.felix.dm.impl.metatype.PropertyMetaDataImpl; |
Marcel Offermans | fd7deb0 | 2011-04-20 11:26:09 +0000 | [diff] [blame] | 42 | import org.apache.felix.dm.index.AspectFilterIndex; |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 43 | import org.apache.felix.dm.index.MultiPropertyExactFilter; |
| 44 | import org.apache.felix.dm.index.ServiceRegistryCache; |
Marcel Offermans | b8405a5 | 2009-12-22 19:01:31 +0000 | [diff] [blame] | 45 | import org.osgi.framework.BundleContext; |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 46 | import org.osgi.framework.FrameworkUtil; |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 47 | |
| 48 | /** |
Marcel Offermans | faaed47 | 2010-09-08 10:07:32 +0000 | [diff] [blame] | 49 | * The dependency manager manages all components and their dependencies. Using |
| 50 | * this API you can declare all components and their dependencies. Under normal |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 51 | * circumstances, you get passed an instance of this class through the |
| 52 | * <code>DependencyActivatorBase</code> subclass you use as your |
| 53 | * <code>BundleActivator</code>, but it is also possible to create your |
| 54 | * own instance. |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 55 | * |
| 56 | * @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a> |
| 57 | */ |
| 58 | public class DependencyManager { |
Marcel Offermans | ad76067 | 2010-03-03 15:30:01 +0000 | [diff] [blame] | 59 | public static final String ASPECT = "org.apache.felix.dependencymanager.aspect"; |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 60 | public static final String SERVICEREGISTRY_CACHE_INDICES = "dm.index"; // TODO rename |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 61 | private final BundleContext m_context; |
| 62 | private final Logger m_logger; |
Marcel Offermans | 5be5f14 | 2011-04-26 10:47:12 +0000 | [diff] [blame^] | 63 | private List m_components = Collections.synchronizedList(new ArrayList()); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 64 | |
| 65 | /** |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 66 | * Creates a new dependency manager. You need to supply the |
| 67 | * <code>BundleContext</code> to be used by the dependency |
| 68 | * manager to register services and communicate with the |
| 69 | * framework. |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 70 | * |
| 71 | * @param context the bundle context |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 72 | */ |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 73 | public DependencyManager(BundleContext context) { |
| 74 | this(context, new Logger(context)); |
| 75 | } |
| 76 | |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 77 | DependencyManager(BundleContext context, Logger logger) { |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 78 | m_context = createContext(context); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 79 | m_logger = logger; |
Marcel Offermans | 5be5f14 | 2011-04-26 10:47:12 +0000 | [diff] [blame^] | 80 | synchronized (m_dependencyManagers) { |
| 81 | m_dependencyManagers.add(new WeakReference(this)); |
| 82 | } |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 83 | } |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 84 | |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 85 | // service registry cache |
| 86 | private static ServiceRegistryCache m_serviceRegistryCache; |
Marcel Offermans | 5be5f14 | 2011-04-26 10:47:12 +0000 | [diff] [blame^] | 87 | private static final Set /* WeakReference<DependencyManager> */ m_dependencyManagers = new HashSet(); |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 88 | static { |
| 89 | String index = System.getProperty(SERVICEREGISTRY_CACHE_INDICES); |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 90 | if (index != null) { |
Marcel Offermans | fd7deb0 | 2011-04-20 11:26:09 +0000 | [diff] [blame] | 91 | m_serviceRegistryCache = new ServiceRegistryCache(FrameworkUtil.getBundle(DependencyManager.class).getBundleContext()); |
| 92 | m_serviceRegistryCache.open(); // TODO close it somewhere |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 93 | String[] props = index.split(";"); |
| 94 | for (int i = 0; i < props.length; i++) { |
Marcel Offermans | fd7deb0 | 2011-04-20 11:26:09 +0000 | [diff] [blame] | 95 | if (props[i].equals("*aspect*")) { |
| 96 | m_serviceRegistryCache.addFilterIndex(new AspectFilterIndex()); |
| 97 | } |
| 98 | else { |
| 99 | String[] propList = props[i].split(","); |
| 100 | m_serviceRegistryCache.addFilterIndex(new MultiPropertyExactFilter(propList)); |
| 101 | } |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
Marcel Offermans | 5be5f14 | 2011-04-26 10:47:12 +0000 | [diff] [blame^] | 104 | } |
| 105 | |
| 106 | public static List getDependencyManagers() { |
| 107 | List /* DependencyManager */ result = new ArrayList(); |
| 108 | synchronized (m_dependencyManagers) { |
| 109 | Iterator iterator = m_dependencyManagers.iterator(); |
| 110 | while (iterator.hasNext()) { |
| 111 | WeakReference reference = (WeakReference) iterator.next(); |
| 112 | DependencyManager manager = (DependencyManager) reference.get(); |
| 113 | if (manager != null) { |
| 114 | result.add(manager); |
| 115 | } |
| 116 | else { |
| 117 | iterator.remove(); |
| 118 | } |
| 119 | } |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 120 | } |
Marcel Offermans | 5be5f14 | 2011-04-26 10:47:12 +0000 | [diff] [blame^] | 121 | return result; |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | private BundleContext createContext(BundleContext context) { |
| 125 | if (m_serviceRegistryCache != null) { |
| 126 | // System.out.println("DM: Enabling bundle context interceptor for bundle #" + context.getBundle().getBundleId()); |
| 127 | return m_serviceRegistryCache.createBundleContextInterceptor(context); |
| 128 | } |
| 129 | else { |
| 130 | return context; |
| 131 | } |
| 132 | } |
Marcel Offermans | 5be5f14 | 2011-04-26 10:47:12 +0000 | [diff] [blame^] | 133 | |
| 134 | public BundleContext getBundleContext() { |
| 135 | return m_context; |
| 136 | } |
Marcel Offermans | 227dd71 | 2011-04-19 07:14:22 +0000 | [diff] [blame] | 137 | |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 138 | /** |
| 139 | * Adds a new service to the dependency manager. After the service was added |
| 140 | * it will be started immediately. |
| 141 | * |
| 142 | * @param service the service to add |
| 143 | */ |
Marcel Offermans | faaed47 | 2010-09-08 10:07:32 +0000 | [diff] [blame] | 144 | public void add(Component service) { |
Marcel Offermans | 5be5f14 | 2011-04-26 10:47:12 +0000 | [diff] [blame^] | 145 | m_components.add(service); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 146 | service.start(); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Removes a service from the dependency manager. Before the service is removed |
| 151 | * it is stopped first. |
| 152 | * |
| 153 | * @param service the service to remove |
| 154 | */ |
Marcel Offermans | faaed47 | 2010-09-08 10:07:32 +0000 | [diff] [blame] | 155 | public void remove(Component service) { |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 156 | service.stop(); |
Marcel Offermans | 5be5f14 | 2011-04-26 10:47:12 +0000 | [diff] [blame^] | 157 | m_components.remove(service); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Creates a new service. |
| 162 | * |
| 163 | * @return the new service |
| 164 | */ |
Marcel Offermans | faaed47 | 2010-09-08 10:07:32 +0000 | [diff] [blame] | 165 | public Component createComponent() { |
| 166 | return new ComponentImpl(m_context, this, m_logger); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Creates a new service dependency. |
| 171 | * |
| 172 | * @return the service dependency |
| 173 | */ |
| 174 | public ServiceDependency createServiceDependency() { |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 175 | return new ServiceDependencyImpl(m_context, m_logger); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 176 | } |
| 177 | |
Marcel Offermans | 74363c3 | 2009-11-23 19:56:08 +0000 | [diff] [blame] | 178 | /** |
| 179 | * Creates a new temporal service dependency. |
| 180 | * |
Pierre De Rop | c3dfbb7 | 2010-05-24 13:16:35 +0000 | [diff] [blame] | 181 | * @return a new temporal service dependency |
Marcel Offermans | 74363c3 | 2009-11-23 19:56:08 +0000 | [diff] [blame] | 182 | */ |
| 183 | public TemporalServiceDependency createTemporalServiceDependency() { |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 184 | return new TemporalServiceDependencyImpl(m_context, m_logger); |
Marcel Offermans | 74363c3 | 2009-11-23 19:56:08 +0000 | [diff] [blame] | 185 | } |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 186 | |
| 187 | /** |
| 188 | * Creates a new configuration dependency. |
| 189 | * |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 190 | * @return the configuration dependency |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 191 | */ |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 192 | public ConfigurationDependency createConfigurationDependency() { |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 193 | return new ConfigurationDependencyImpl(m_context, m_logger); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 196 | /** |
Marcel Offermans | faaed47 | 2010-09-08 10:07:32 +0000 | [diff] [blame] | 197 | * Creates a new configuration property metadata. |
| 198 | * |
| 199 | * @return the configuration property metadata. |
Pierre De Rop | a0204f5 | 2010-03-06 22:23:57 +0000 | [diff] [blame] | 200 | */ |
| 201 | public PropertyMetaData createPropertyMetaData() { |
| 202 | return new PropertyMetaDataImpl(); |
| 203 | } |
| 204 | |
| 205 | /** |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 206 | * Creates a new bundle dependency. |
| 207 | * |
Pierre De Rop | c3dfbb7 | 2010-05-24 13:16:35 +0000 | [diff] [blame] | 208 | * @return a new BundleDependency instance. |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 209 | */ |
Marcel Offermans | d66c5ce | 2009-11-26 09:58:44 +0000 | [diff] [blame] | 210 | public BundleDependency createBundleDependency() { |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 211 | return new BundleDependencyImpl(m_context, m_logger); |
Marcel Offermans | d66c5ce | 2009-11-26 09:58:44 +0000 | [diff] [blame] | 212 | } |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 213 | |
| 214 | /** |
| 215 | * Creates a new resource dependency. |
| 216 | * |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 217 | * @return the resource dependency |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 218 | */ |
| 219 | public ResourceDependency createResourceDependency() { |
Pierre De Rop | ae4a5db | 2009-12-04 22:08:05 +0000 | [diff] [blame] | 220 | return new ResourceDependencyImpl(m_context, m_logger); |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 221 | } |
Marcel Offermans | 80eeafe | 2009-12-01 22:12:26 +0000 | [diff] [blame] | 222 | |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 223 | /** |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 224 | * Creates a new aspect. The aspect will be applied to any service that |
| 225 | * matches the specified interface and filter. For each matching service |
| 226 | * an aspect will be created based on the aspect implementation class. |
| 227 | * The aspect will be registered with the same interface and properties |
| 228 | * as the original service, plus any extra properties you supply here. |
| 229 | * It will also inherit all dependencies, and if you declare the original |
| 230 | * service as a member it will be injected. |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 231 | * |
Pierre De Rop | 19476fe | 2010-05-23 08:13:58 +0000 | [diff] [blame] | 232 | * <h3>Usage Example</h3> |
| 233 | * |
Pierre De Rop | c3dfbb7 | 2010-05-24 13:16:35 +0000 | [diff] [blame] | 234 | * <blockquote><pre> |
Marcel Offermans | a8818d5 | 2011-03-09 10:03:35 +0000 | [diff] [blame] | 235 | * manager.createAspectService(ExistingService.class, "(foo=bar)", 10, "m_service") |
| 236 | * .setImplementation(ExistingServiceAspect.class) |
| 237 | * ); |
Pierre De Rop | c3dfbb7 | 2010-05-24 13:16:35 +0000 | [diff] [blame] | 238 | * </pre></blockquote> |
Pierre De Rop | 19476fe | 2010-05-23 08:13:58 +0000 | [diff] [blame] | 239 | * |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 240 | * @param serviceInterface the service interface to apply the aspect to |
| 241 | * @param serviceFilter the filter condition to use with the service interface |
Pierre De Rop | 19476fe | 2010-05-23 08:13:58 +0000 | [diff] [blame] | 242 | * @param ranking the level used to organize the aspect chain ordering |
Marcel Offermans | a8818d5 | 2011-03-09 10:03:35 +0000 | [diff] [blame] | 243 | * @param autoConfig the aspect implementation field name where to inject original service. |
Marcel Offermans | faaed47 | 2010-09-08 10:07:32 +0000 | [diff] [blame] | 244 | * If null, any field matching the original service will be injected. |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 245 | * @return a service that acts as a factory for generating aspects |
Marcel Offermans | 2925f17 | 2009-12-02 13:03:39 +0000 | [diff] [blame] | 246 | */ |
Marcel Offermans | a8818d5 | 2011-03-09 10:03:35 +0000 | [diff] [blame] | 247 | public Component createAspectService(Class serviceInterface, String serviceFilter, int ranking, String autoConfig) { |
| 248 | return new AspectServiceImpl(this, serviceInterface, serviceFilter, ranking, autoConfig, null, null, null); |
| 249 | } |
| 250 | /** |
| 251 | * Creates a new aspect. The aspect will be applied to any service that |
| 252 | * matches the specified interface and filter. For each matching service |
| 253 | * an aspect will be created based on the aspect implementation class. |
| 254 | * The aspect will be registered with the same interface and properties |
| 255 | * as the original service, plus any extra properties you supply here. |
| 256 | * It will also inherit all dependencies, and if you declare the original |
| 257 | * service as a member it will be injected. |
| 258 | * |
| 259 | * <h3>Usage Example</h3> |
| 260 | * |
| 261 | * <blockquote><pre> |
| 262 | * manager.createAspectService(ExistingService.class, "(foo=bar)", 10) |
| 263 | * .setImplementation(ExistingServiceAspect.class) |
| 264 | * ); |
| 265 | * </pre></blockquote> |
| 266 | * |
| 267 | * @param serviceInterface the service interface to apply the aspect to |
| 268 | * @param serviceFilter the filter condition to use with the service interface |
| 269 | * @param ranking the level used to organize the aspect chain ordering |
| 270 | * @return a service that acts as a factory for generating aspects |
| 271 | */ |
| 272 | public Component createAspectService(Class serviceInterface, String serviceFilter, int ranking) { |
| 273 | return new AspectServiceImpl(this, serviceInterface, serviceFilter, ranking, null, null, null, null); |
| 274 | } |
| 275 | /** |
| 276 | * Creates a new aspect. The aspect will be applied to any service that |
| 277 | * matches the specified interface and filter. For each matching service |
| 278 | * an aspect will be created based on the aspect implementation class. |
| 279 | * The aspect will be registered with the same interface and properties |
| 280 | * as the original service, plus any extra properties you supply here. |
| 281 | * It will also inherit all dependencies, and if you declare the original |
| 282 | * service as a member it will be injected. |
| 283 | * |
| 284 | * <h3>Usage Example</h3> |
| 285 | * |
| 286 | * <blockquote><pre> |
| 287 | * manager.createAspectService(ExistingService.class, "(foo=bar)", 10, "add", "change", "remove") |
| 288 | * .setImplementation(ExistingServiceAspect.class) |
| 289 | * ); |
| 290 | * </pre></blockquote> |
| 291 | * |
| 292 | * @param serviceInterface the service interface to apply the aspect to |
| 293 | * @param serviceFilter the filter condition to use with the service interface |
| 294 | * @param ranking the level used to organize the aspect chain ordering |
| 295 | * @param add name of the callback method to invoke on add |
| 296 | * @param change name of the callback method to invoke on change |
| 297 | * @param remove name of the callback method to invoke on remove |
| 298 | * @return a service that acts as a factory for generating aspects |
| 299 | */ |
| 300 | public Component createAspectService(Class serviceInterface, String serviceFilter, int ranking, String add, String change, String remove) { |
| 301 | return new AspectServiceImpl(this, serviceInterface, serviceFilter, ranking, null, add, change, remove); |
Marcel Offermans | 80eeafe | 2009-12-01 22:12:26 +0000 | [diff] [blame] | 302 | } |
Marcel Offermans | e9c13d9 | 2010-07-01 14:01:02 +0000 | [diff] [blame] | 303 | |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 304 | /** |
| 305 | * Creates a new adapter. The adapter will be applied to any service that |
| 306 | * matches the specified interface and filter. For each matching service |
| 307 | * an adapter will be created based on the adapter implementation class. |
| 308 | * The adapter will be registered with the specified interface and existing properties |
| 309 | * from the original service plus any extra properties you supply here. |
| 310 | * It will also inherit all dependencies, and if you declare the original |
| 311 | * service as a member it will be injected. |
| 312 | * |
Pierre De Rop | 3423158 | 2010-05-23 20:05:16 +0000 | [diff] [blame] | 313 | * <h3>Usage Example</h3> |
| 314 | * |
Pierre De Rop | c3dfbb7 | 2010-05-24 13:16:35 +0000 | [diff] [blame] | 315 | * <blockquote><pre> |
Marcel Offermans | a8818d5 | 2011-03-09 10:03:35 +0000 | [diff] [blame] | 316 | * manager.createAdapterService(AdapteeService.class, "(foo=bar)") |
| 317 | * .setInterface(AdapterService.class, new Hashtable() {{ put("extra", "property"); }}) |
| 318 | * .setImplementation(AdapterImpl.class); |
Pierre De Rop | c3dfbb7 | 2010-05-24 13:16:35 +0000 | [diff] [blame] | 319 | * </pre></blockquote> |
Marcel Offermans | a8818d5 | 2011-03-09 10:03:35 +0000 | [diff] [blame] | 320 | * |
Marcel Offermans | d665eaf | 2010-02-16 15:56:35 +0000 | [diff] [blame] | 321 | * @param serviceInterface the service interface to apply the adapter to |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 322 | * @param serviceFilter the filter condition to use with the service interface |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 323 | * @return a service that acts as a factory for generating adapters |
| 324 | */ |
Marcel Offermans | faaed47 | 2010-09-08 10:07:32 +0000 | [diff] [blame] | 325 | public Component createAdapterService(Class serviceInterface, String serviceFilter) { |
Marcel Offermans | a8818d5 | 2011-03-09 10:03:35 +0000 | [diff] [blame] | 326 | return new AdapterServiceImpl(this, serviceInterface, serviceFilter, null, null, null, null); |
| 327 | } |
| 328 | /** |
| 329 | * Creates a new adapter. The adapter will be applied to any service that |
| 330 | * matches the specified interface and filter. For each matching service |
| 331 | * an adapter will be created based on the adapter implementation class. |
| 332 | * The adapter will be registered with the specified interface and existing properties |
| 333 | * from the original service plus any extra properties you supply here. |
| 334 | * It will also inherit all dependencies, and if you declare the original |
| 335 | * service as a member it will be injected. |
| 336 | * |
| 337 | * <h3>Usage Example</h3> |
| 338 | * |
| 339 | * <blockquote><pre> |
| 340 | * manager.createAdapterService(AdapteeService.class, "(foo=bar)", "m_service") |
| 341 | * .setInterface(AdapterService.class, new Hashtable() {{ put("extra", "property"); }}) |
| 342 | * .setImplementation(AdapterImpl.class); |
| 343 | * </pre></blockquote> |
| 344 | * |
| 345 | * @param serviceInterface the service interface to apply the adapter to |
| 346 | * @param serviceFilter the filter condition to use with the service interface |
| 347 | * @param autoConfig the name of the member to inject the service into |
| 348 | * @return a service that acts as a factory for generating adapters |
| 349 | */ |
| 350 | public Component createAdapterService(Class serviceInterface, String serviceFilter, String autoConfig) { |
| 351 | return new AdapterServiceImpl(this, serviceInterface, serviceFilter, autoConfig, null, null, null); |
| 352 | } |
| 353 | /** |
| 354 | * Creates a new adapter. The adapter will be applied to any service that |
| 355 | * matches the specified interface and filter. For each matching service |
| 356 | * an adapter will be created based on the adapter implementation class. |
| 357 | * The adapter will be registered with the specified interface and existing properties |
| 358 | * from the original service plus any extra properties you supply here. |
| 359 | * It will also inherit all dependencies, and if you declare the original |
| 360 | * service as a member it will be injected. |
| 361 | * |
| 362 | * <h3>Usage Example</h3> |
| 363 | * |
| 364 | * <blockquote><pre> |
| 365 | * manager.createAdapterService(AdapteeService.class, "(foo=bar)", "add", "change", "remove") |
| 366 | * .setInterface(AdapterService.class, new Hashtable() {{ put("extra", "property"); }}) |
| 367 | * .setImplementation(AdapterImpl.class); |
| 368 | * </pre></blockquote> |
| 369 | * |
| 370 | * @param serviceInterface the service interface to apply the adapter to |
| 371 | * @param serviceFilter the filter condition to use with the service interface |
| 372 | * @param add name of the callback method to invoke on add |
| 373 | * @param change name of the callback method to invoke on change |
| 374 | * @param remove name of the callback method to invoke on remove |
| 375 | * @return a service that acts as a factory for generating adapters |
| 376 | */ |
| 377 | public Component createAdapterService(Class serviceInterface, String serviceFilter, String add, String change, String remove) { |
| 378 | return new AdapterServiceImpl(this, serviceInterface, serviceFilter, null, add, change, remove); |
Marcel Offermans | 61a8114 | 2010-04-02 15:16:50 +0000 | [diff] [blame] | 379 | } |
Pierre De Rop | 3423158 | 2010-05-23 20:05:16 +0000 | [diff] [blame] | 380 | |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 381 | /** |
| 382 | * Creates a new resource adapter. The adapter will be applied to any resource that |
| 383 | * matches the specified filter condition. For each matching resource |
| 384 | * an adapter will be created based on the adapter implementation class. |
| 385 | * The adapter will be registered with the specified interface and existing properties |
| 386 | * from the original resource plus any extra properties you supply here. |
| 387 | * It will also inherit all dependencies, and if you declare the original |
| 388 | * service as a member it will be injected. |
| 389 | * |
Pierre De Rop | 445ddec | 2010-05-23 21:05:27 +0000 | [diff] [blame] | 390 | * <h3>Usage Example</h3> |
| 391 | * |
Pierre De Rop | c3dfbb7 | 2010-05-24 13:16:35 +0000 | [diff] [blame] | 392 | * <blockquote><pre> |
Pierre De Rop | 445ddec | 2010-05-23 21:05:27 +0000 | [diff] [blame] | 393 | * manager.createResourceAdapterService("(&(path=/test)(repository=TestRepository))", true) |
| 394 | * // The interface to use when registering adapter |
Pierre De Rop | c3dfbb7 | 2010-05-24 13:16:35 +0000 | [diff] [blame] | 395 | * .setInterface(AdapterService.class.getName(), new Hashtable() {{ put("foo", "bar"); }}) |
Pierre De Rop | 445ddec | 2010-05-23 21:05:27 +0000 | [diff] [blame] | 396 | * // the implementation of the adapter |
Pierre De Rop | c3dfbb7 | 2010-05-24 13:16:35 +0000 | [diff] [blame] | 397 | * .setImplementation(AdapterServiceImpl.class); |
| 398 | * </pre></blockquote> |
Pierre De Rop | 445ddec | 2010-05-23 21:05:27 +0000 | [diff] [blame] | 399 | * |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 400 | * @param resourceFilter the filter condition to use with the resource |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 401 | * @param propagate <code>true</code> if properties from the resource should be propagated to the service |
Marcel Offermans | e9c13d9 | 2010-07-01 14:01:02 +0000 | [diff] [blame] | 402 | * @param callbackInstance |
| 403 | * @param callbackChanged |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 404 | * @return a service that acts as a factory for generating resource adapters |
| 405 | * @see Resource |
| 406 | */ |
Marcel Offermans | faaed47 | 2010-09-08 10:07:32 +0000 | [diff] [blame] | 407 | public Component createResourceAdapterService(String resourceFilter, boolean propagate, Object callbackInstance, String callbackChanged) { |
Marcel Offermans | e9c13d9 | 2010-07-01 14:01:02 +0000 | [diff] [blame] | 408 | return new ResourceAdapterServiceImpl(this, resourceFilter, propagate, callbackInstance, callbackChanged); |
Marcel Offermans | 61a8114 | 2010-04-02 15:16:50 +0000 | [diff] [blame] | 409 | } |
Pierre De Rop | 445ddec | 2010-05-23 21:05:27 +0000 | [diff] [blame] | 410 | |
Marcel Offermans | faaed47 | 2010-09-08 10:07:32 +0000 | [diff] [blame] | 411 | public Component createResourceAdapterService(String resourceFilter, Object propagateCallbackInstance, String propagateCallbackMethod, Object callbackInstance, String callbackChanged) { |
Marcel Offermans | 26081d3 | 2010-07-12 12:43:42 +0000 | [diff] [blame] | 412 | return new ResourceAdapterServiceImpl(this, resourceFilter, propagateCallbackInstance, propagateCallbackMethod, callbackInstance, callbackChanged); |
| 413 | } |
| 414 | |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 415 | /** |
| 416 | * Creates a new bundle adapter. The adapter will be applied to any bundle that |
| 417 | * matches the specified bundle state mask and filter condition. For each matching |
| 418 | * bundle an adapter will be created based on the adapter implementation class. |
| 419 | * The adapter will be registered with the specified interface |
| 420 | * |
| 421 | * TODO and existing properties from the original resource plus any extra properties you supply here. |
| 422 | * It will also inherit all dependencies, and if you declare the original |
| 423 | * service as a member it will be injected. |
| 424 | * |
Pierre De Rop | 13dd63d | 2010-05-23 21:58:28 +0000 | [diff] [blame] | 425 | * <h3>Usage Example</h3> |
| 426 | * |
Pierre De Rop | c3dfbb7 | 2010-05-24 13:16:35 +0000 | [diff] [blame] | 427 | * <blockquote><pre> |
Pierre De Rop | 13dd63d | 2010-05-23 21:58:28 +0000 | [diff] [blame] | 428 | * manager.createBundleAdapterService(Bundle.INSTALLED | Bundle.RESOLVED | Bundle.ACTIVE, |
| 429 | * "(Bundle-SymbolicName=org.apache.felix.dependencymanager)", |
| 430 | * true) |
| 431 | * // The interface to use when registering adapter |
Pierre De Rop | c3dfbb7 | 2010-05-24 13:16:35 +0000 | [diff] [blame] | 432 | * .setInterface(AdapterService.class.getName(), new Hashtable() {{ put("foo", "bar"); }}) |
Pierre De Rop | 13dd63d | 2010-05-23 21:58:28 +0000 | [diff] [blame] | 433 | * // the implementation of the adapter |
Pierre De Rop | c3dfbb7 | 2010-05-24 13:16:35 +0000 | [diff] [blame] | 434 | * .setImplementation(AdapterServiceImpl.class); |
| 435 | * </pre></blockquote> |
Pierre De Rop | 13dd63d | 2010-05-23 21:58:28 +0000 | [diff] [blame] | 436 | * |
Marcel Offermans | 4fd903f | 2009-12-29 09:18:05 +0000 | [diff] [blame] | 437 | * @param bundleStateMask the bundle state mask to apply |
| 438 | * @param bundleFilter the filter to apply to the bundle manifest |
Marcel Offermans | d665eaf | 2010-02-16 15:56:35 +0000 | [diff] [blame] | 439 | * @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] | 440 | * @return a service that acts as a factory for generating bundle adapters |
| 441 | */ |
Marcel Offermans | faaed47 | 2010-09-08 10:07:32 +0000 | [diff] [blame] | 442 | public Component createBundleAdapterService(int bundleStateMask, String bundleFilter, boolean propagate) { |
Pierre De Rop | 13dd63d | 2010-05-23 21:58:28 +0000 | [diff] [blame] | 443 | return new BundleAdapterServiceImpl(this, bundleStateMask, bundleFilter, propagate); |
Marcel Offermans | 61a8114 | 2010-04-02 15:16:50 +0000 | [diff] [blame] | 444 | } |
Marcel Offermans | 80eeafe | 2009-12-01 22:12:26 +0000 | [diff] [blame] | 445 | |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 446 | /** |
Pierre De Rop | ef94c88 | 2010-04-17 18:23:35 +0000 | [diff] [blame] | 447 | * Creates a new Managed Service Factory Configuration Adapter. For each new Config Admin factory configuration matching |
| 448 | * the factoryPid, an adapter will be created based on the adapter implementation class. |
| 449 | * The adapter will be registered with the specified interface, and with the specified adapter service properties. |
| 450 | * Depending on the <code>propagate</code> parameter, every public factory configuration properties |
| 451 | * (which don't start with ".") will be propagated along with the adapter service properties. |
| 452 | * It will also inherit all dependencies. |
| 453 | * |
Pierre De Rop | 3e10037 | 2010-05-24 12:43:44 +0000 | [diff] [blame] | 454 | * <h3>Usage Example</h3> |
| 455 | * |
Pierre De Rop | c3dfbb7 | 2010-05-24 13:16:35 +0000 | [diff] [blame] | 456 | * <blockquote><pre> |
Pierre De Rop | 3e10037 | 2010-05-24 12:43:44 +0000 | [diff] [blame] | 457 | * manager.createFactoryConfigurationAdapterService("MyFactoryPid", "update", true) |
| 458 | * // The interface to use when registering adapter |
Pierre De Rop | c3dfbb7 | 2010-05-24 13:16:35 +0000 | [diff] [blame] | 459 | * .setInterface(AdapterService.class.getName(), new Hashtable() {{ put("foo", "bar"); }}) |
Pierre De Rop | 3e10037 | 2010-05-24 12:43:44 +0000 | [diff] [blame] | 460 | * // the implementation of the adapter |
| 461 | * .setImplementation(AdapterServiceImpl.class); |
Pierre De Rop | c3dfbb7 | 2010-05-24 13:16:35 +0000 | [diff] [blame] | 462 | * </pre></blockquote> |
| 463 | * |
Pierre De Rop | ef94c88 | 2010-04-17 18:23:35 +0000 | [diff] [blame] | 464 | * @param factoryPid the pid matching the factory configuration |
| 465 | * @param update the adapter method name that will be notified when the factory configuration is created/updated. |
Pierre De Rop | ef94c88 | 2010-04-17 18:23:35 +0000 | [diff] [blame] | 466 | * @param propagate true if public factory configuration should be propagated to the adapter service properties |
| 467 | * @return a service that acts as a factory for generating the managed service factory configuration adapter |
| 468 | */ |
Marcel Offermans | faaed47 | 2010-09-08 10:07:32 +0000 | [diff] [blame] | 469 | public Component createFactoryConfigurationAdapterService(String factoryPid, String update, boolean propagate) { |
Pierre De Rop | 3e10037 | 2010-05-24 12:43:44 +0000 | [diff] [blame] | 470 | return new FactoryConfigurationAdapterServiceImpl(this, factoryPid, update, propagate); |
Pierre De Rop | ef94c88 | 2010-04-17 18:23:35 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | /** |
Pierre De Rop | 3e10037 | 2010-05-24 12:43:44 +0000 | [diff] [blame] | 474 | * Creates a new Managed Service Factory Configuration Adapter with meta type support. For each new Config Admin |
| 475 | * factory configuration matching the factoryPid, an adapter will be created based on the adapter implementation |
| 476 | * class. The adapter will be registered with the specified interface, and with the specified adapter service |
| 477 | * properties. Depending on the <code>propagate</code> parameter, every public factory configuration properties |
Pierre De Rop | ef94c88 | 2010-04-17 18:23:35 +0000 | [diff] [blame] | 478 | * (which don't start with ".") will be propagated along with the adapter service properties. |
| 479 | * It will also inherit all dependencies. |
| 480 | * |
Pierre De Rop | c3dfbb7 | 2010-05-24 13:16:35 +0000 | [diff] [blame] | 481 | * <h3>Usage Example</h3> |
| 482 | * |
| 483 | * <blockquote><pre> |
| 484 | * PropertyMetaData[] propertiesMetaData = new PropertyMetaData[] { |
| 485 | * manager.createPropertyMetaData() |
| 486 | * .setCardinality(Integer.MAX_VALUE) |
| 487 | * .setType(String.class) |
| 488 | * .setHeading("English words") |
| 489 | * .setDescription("Declare here some valid english words") |
| 490 | * .setDefaults(new String[] {"hello", "world"}) |
| 491 | * .setId("words") |
| 492 | * }; |
| 493 | * |
| 494 | * manager.add(createFactoryConfigurationAdapterService("FactoryPid", |
| 495 | * "updated", |
| 496 | * true, // propagate CM settings |
| 497 | * "EnglishDictionary", |
| 498 | * "English dictionary configuration properties", |
| 499 | * null, |
| 500 | * propertiesMetaData) |
| 501 | * .setImplementation(Adapter.class)); |
| 502 | * </pre></blockquote> |
| 503 | * |
Pierre De Rop | ef94c88 | 2010-04-17 18:23:35 +0000 | [diff] [blame] | 504 | * @param factoryPid the pid matching the factory configuration |
| 505 | * @param update the adapter method name that will be notified when the factory configuration is created/updated. |
Pierre De Rop | ef94c88 | 2010-04-17 18:23:35 +0000 | [diff] [blame] | 506 | * @param propagate true if public factory configuration should be propagated to the adapter service properties |
Pierre De Rop | 3e10037 | 2010-05-24 12:43:44 +0000 | [diff] [blame] | 507 | * @param heading The label used to display the tab name (or section) where the properties are displayed. |
| 508 | * Example: "Printer Service" |
| 509 | * @param desc A human readable description of the factory PID this configuration is associated with. |
| 510 | * Example: "Configuration for the PrinterService bundle" |
Pierre De Rop | 9a8ebfd | 2010-04-25 22:25:34 +0000 | [diff] [blame] | 511 | * @param localization Points to the basename of the Properties file that can localize the Meta Type informations. |
| 512 | * The default localization base name for the properties is OSGI-INF/l10n/bundle, but can |
Pierre De Rop | 3e10037 | 2010-05-24 12:43:44 +0000 | [diff] [blame] | 513 | * be overridden by the manifest Bundle-Localization header (see core specification, in section Localization |
| 514 | * on page 68). You can specify a specific localization basename file using this parameter |
| 515 | * (e.g. <code>"person"</code> will match person_du_NL.properties in the root bundle directory). |
Pierre De Rop | 9a8ebfd | 2010-04-25 22:25:34 +0000 | [diff] [blame] | 516 | * @param propertiesMetaData Array of MetaData regarding configuration properties |
| 517 | * @return a service that acts as a factory for generating the managed service factory configuration adapter |
| 518 | */ |
Marcel Offermans | faaed47 | 2010-09-08 10:07:32 +0000 | [diff] [blame] | 519 | public Component createFactoryConfigurationAdapterService(String factoryPid, String update, boolean propagate, |
Pierre De Rop | 3e10037 | 2010-05-24 12:43:44 +0000 | [diff] [blame] | 520 | String heading, String desc, String localization, |
| 521 | PropertyMetaData[] propertiesMetaData) |
Pierre De Rop | 9a8ebfd | 2010-04-25 22:25:34 +0000 | [diff] [blame] | 522 | { |
Pierre De Rop | 3e10037 | 2010-05-24 12:43:44 +0000 | [diff] [blame] | 523 | return new FactoryConfigurationAdapterServiceImpl(this, factoryPid, update, propagate, m_context, m_logger, |
| 524 | heading, desc, localization, propertiesMetaData); |
Pierre De Rop | 9a8ebfd | 2010-04-25 22:25:34 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /** |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 528 | * Returns a list of services. |
| 529 | * |
| 530 | * @return a list of services |
| 531 | */ |
Marcel Offermans | 5be5f14 | 2011-04-26 10:47:12 +0000 | [diff] [blame^] | 532 | public List getComponents() { |
| 533 | return Collections.unmodifiableList(m_components); |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 534 | } |
Marcel Offermans | a962bc9 | 2009-11-21 17:59:33 +0000 | [diff] [blame] | 535 | } |