Initial code drop of yang model driven configuration system
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / BundleContextBackedModuleFactoriesResolver.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.manager.impl.osgi;
9
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.List;
13
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;
19
20 /**
21  * Retrieves list of currently registered Module Factories using bundlecontext.
22  */
23 public class BundleContextBackedModuleFactoriesResolver implements
24         ModuleFactoriesResolver {
25     private final BundleContext bundleContext;
26
27     public BundleContextBackedModuleFactoriesResolver(
28             BundleContext bundleContext) {
29         this.bundleContext = bundleContext;
30     }
31
32     @Override
33     public List<? extends ModuleFactory> getAllFactories() {
34         Collection<ServiceReference<ModuleFactory>> serviceReferences;
35         try {
36             serviceReferences = bundleContext.getServiceReferences(
37                     ModuleFactory.class, null);
38         } catch (InvalidSyntaxException e) {
39             throw new IllegalStateException(e);
40         }
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) {
49                 result.add(service);
50             }
51         }
52         return result;
53     }
54 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.