2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.config.manager.impl.osgi;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.List;
14 import org.opendaylight.controller.config.manager.impl.factoriesresolver.ModuleFactoriesResolver;
15 import org.opendaylight.controller.config.spi.ModuleFactory;
16 import org.osgi.framework.BundleContext;
17 import org.osgi.framework.InvalidSyntaxException;
18 import org.osgi.framework.ServiceReference;
21 * Retrieves list of currently registered Module Factories using bundlecontext.
23 public class BundleContextBackedModuleFactoriesResolver implements
24 ModuleFactoriesResolver {
25 private final BundleContext bundleContext;
27 public BundleContextBackedModuleFactoriesResolver(
28 BundleContext bundleContext) {
29 this.bundleContext = bundleContext;
33 public List<? extends ModuleFactory> getAllFactories() {
34 Collection<ServiceReference<ModuleFactory>> serviceReferences;
36 serviceReferences = bundleContext.getServiceReferences(
37 ModuleFactory.class, null);
38 } catch (InvalidSyntaxException e) {
39 throw new IllegalStateException(e);
41 List<ModuleFactory> result = new ArrayList<>(serviceReferences.size());
42 for (ServiceReference<ModuleFactory> serviceReference : serviceReferences) {
43 ModuleFactory service = bundleContext.getService(serviceReference);
44 // null if the service is not registered, the service object
45 // returned by a ServiceFactory does not
46 // implement the classes under which it was registered or the
47 // ServiceFactory threw an exception.
48 if (service != null) {