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