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