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;
10 import com.google.common.base.Preconditions;
11 import org.opendaylight.controller.config.api.DependencyResolver;
12 import org.opendaylight.controller.config.api.DependencyResolverFactory;
13 import org.opendaylight.controller.config.api.DynamicMBeanWithInstance;
14 import org.opendaylight.controller.config.api.ModuleIdentifier;
15 import org.opendaylight.controller.config.api.annotations.AbstractServiceInterface;
16 import org.opendaylight.controller.config.manager.impl.util.InterfacesHelper;
17 import org.opendaylight.controller.config.spi.Module;
18 import org.opendaylight.controller.config.spi.ModuleFactory;
19 import org.osgi.framework.BundleContext;
21 import java.lang.reflect.Constructor;
22 import java.lang.reflect.InvocationTargetException;
23 import java.util.Arrays;
24 import java.util.HashSet;
25 import java.util.List;
29 * Creates new modules by reflection. Provided class must have this constructor:
30 * ctor(DynamicMBeanWithInstance.class, ModuleIdentifier.class).
31 * When reconfiguring, both parameters will be non null. When creating new
32 * instance first parameter will be null.
35 public class ClassBasedModuleFactory implements ModuleFactory {
36 private final String implementationName;
37 private final Class<? extends Module> configBeanClass;
40 * @param implementationName
41 * @param configBeanClass
42 * class that will be instantiated when createModule is called.
43 * This class must implement Module interface and all exported
46 public ClassBasedModuleFactory(String implementationName,
47 Class<? extends Module> configBeanClass) {
48 this.implementationName = implementationName;
49 this.configBeanClass = configBeanClass;
53 public String getImplementationName() {
54 return implementationName;
58 public Module createModule(String instanceName,
59 DependencyResolver dependencyResolver, DynamicMBeanWithInstance old, BundleContext bundleContext)
61 Preconditions.checkNotNull(old);
62 return constructModule(instanceName, dependencyResolver, old);
65 private Module constructModule(String instanceName, DependencyResolver dependencyResolver, DynamicMBeanWithInstance old) throws InstantiationException, IllegalAccessException, InvocationTargetException {
66 Preconditions.checkNotNull(dependencyResolver);
67 ModuleIdentifier moduleIdentifier = new ModuleIdentifier(implementationName, instanceName);
68 Constructor<? extends Module> declaredConstructor;
70 declaredConstructor = configBeanClass.getDeclaredConstructor(DynamicMBeanWithInstance.class, ModuleIdentifier.class);
71 } catch (NoSuchMethodException e) {
72 throw new IllegalStateException(
73 "Did not find constructor with parameters (DynamicMBeanWithInstance) in "
74 + configBeanClass, e);
76 Preconditions.checkState(declaredConstructor != null);
77 return declaredConstructor.newInstance(old, moduleIdentifier);
81 public Module createModule(String instanceName,
82 DependencyResolver dependencyResolver, BundleContext bundleContext) {
84 return constructModule(instanceName, dependencyResolver, null);
85 } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
86 throw new RuntimeException(e);
91 public boolean isModuleImplementingServiceInterface(
92 Class<? extends AbstractServiceInterface> serviceInterface) {
93 Class<?>[] classes = configBeanClass.getInterfaces();
94 List<Class<?>> ifc = Arrays.asList(classes);
95 if (ifc.contains(serviceInterface)) {
98 for (Class<?> c : classes) {
99 ifc = Arrays.asList(c.getInterfaces());
100 if (ifc.contains(serviceInterface)) {
108 public Set<Module> getDefaultModules(DependencyResolverFactory dependencyResolverFactory, BundleContext bundleContext) {
109 return new HashSet<Module>();
113 public Set<Class<? extends AbstractServiceInterface>> getImplementedServiceIntefaces() {
114 return InterfacesHelper.getAllAbstractServiceClasses(configBeanClass);