Merge "Better exception handling when instantiating YangModelBindingProvider, fix...
authorTony Tkacik <ttkacik@cisco.com>
Tue, 25 Feb 2014 14:58:10 +0000 (14:58 +0000)
committerGerrit Code Review <gerrit@opendaylight.org>
Tue, 25 Feb 2014 14:58:10 +0000 (14:58 +0000)
opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/mapping/ModuleInfoBundleTracker.java
opendaylight/netconf/config-netconf-connector/src/main/java/org/opendaylight/controller/netconf/confignetconfconnector/osgi/Activator.java

index 8ba290f30632e10101cc2185367844d29ee73f19..a8fdfda7d7201f9cb00f50f590265e64825d2751 100644 (file)
@@ -20,7 +20,6 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import java.io.InputStream;
-import java.lang.reflect.InvocationTargetException;
 import java.net.URL;
 import java.util.Collection;
 import java.util.LinkedList;
@@ -98,31 +97,26 @@ public final class ModuleInfoBundleTracker implements BundleTrackerCustomizer<Co
             errorMessage = logMessage("Class {} does not implement {} in bundle {}", clazz, YangModelBindingProvider.class, bundle);
             throw new IllegalStateException(errorMessage);
         }
-
+        YangModelBindingProvider instance;
         try {
-            Object instance = clazz.newInstance();
-            Object result = clazz.getMethod(GET_MODULE_INFO_METHOD).invoke(instance);
-
-            if (YangModuleInfo.class.isAssignableFrom(result.getClass()) == false) {
-                errorMessage = logMessage("Error invoking method not found {} in bundle {}, reason {}",
-                        GET_MODULE_INFO_METHOD, bundle, "Not assignable from " + YangModuleInfo.class);
-            } else {
-                return (YangModuleInfo) result;
-            }
-
+            Object instanceObj = clazz.newInstance();
+            instance = YangModelBindingProvider.class.cast(instanceObj);
         } catch (InstantiationException e) {
             errorMessage = logMessage("Could not instantiate {} in bundle {}, reason {}", moduleInfoClass, bundle, e);
+            throw new IllegalStateException(errorMessage, e);
         } catch (IllegalAccessException e) {
             errorMessage = logMessage("Illegal access during instatiation of class {} in bundle {}, reason {}",
                     moduleInfoClass, bundle, e);
-        } catch (NoSuchMethodException e) {
-            errorMessage = logMessage("Method not found {} in bundle {}, reason {}", GET_MODULE_INFO_METHOD, bundle, e);
-        } catch (InvocationTargetException e) {
-            errorMessage = logMessage("Error invoking method {} in bundle {}, reason {}", GET_MODULE_INFO_METHOD,
-                    bundle, e);
+            throw new IllegalStateException(errorMessage, e);
         }
+        try{
+            return instance.getModuleInfo();
+        } catch (NoClassDefFoundError e) {
 
-        throw new IllegalStateException(errorMessage);
+
+            logger.error("Error while executing getModuleInfo on {}", instance, e);
+            throw e;
+        }
     }
 
     private static Class<?> loadClass(String moduleInfoClass, Bundle bundle) {
index 83029c44e665ec77bc5796535353277fe481ea43..5642cc7188788fd74a7aeccf7ca8ffa97abd009e 100644 (file)
@@ -25,7 +25,8 @@ public class Activator implements BundleActivator, YangStoreServiceTracker.YangS
     private static final Logger logger = LoggerFactory.getLogger(Activator.class);
 
     private BundleContext context;
-    ServiceRegistration osgiRegistration;
+    private ServiceRegistration osgiRegistration;
+    private ConfigRegistryLookupThread configRegistryLookup = null;
 
     @Override
     public void start(BundleContext context) throws Exception {
@@ -36,20 +37,42 @@ public class Activator implements BundleActivator, YangStoreServiceTracker.YangS
 
     @Override
     public void stop(BundleContext context) throws Exception {
+        if (configRegistryLookup != null) {
+            configRegistryLookup.interrupt();
+        }
     }
 
     @Override
     public synchronized void onYangStoreAdded(YangStoreService yangStoreService) {
-        checkState(osgiRegistration == null, "More than one onYangStoreAdded received");
-        NetconfOperationServiceFactoryImpl factory = new NetconfOperationServiceFactoryImpl(yangStoreService);
-        logger.debug("Registering into OSGi");
-        osgiRegistration = context.registerService(new String[]{NetconfOperationServiceFactory.class.getName()}, factory,
-                new Hashtable<String, Object>());
+        checkState(configRegistryLookup  == null, "More than one onYangStoreAdded received");
+        configRegistryLookup = new ConfigRegistryLookupThread(yangStoreService);
+        configRegistryLookup.start();
     }
 
     @Override
     public synchronized void onYangStoreRemoved() {
-        osgiRegistration.unregister();
+        configRegistryLookup.interrupt();
+        if (osgiRegistration != null) {
+            osgiRegistration.unregister();
+        }
         osgiRegistration = null;
+        configRegistryLookup = null;
+    }
+
+    private class ConfigRegistryLookupThread extends Thread {
+        private final YangStoreService yangStoreService;
+
+        private ConfigRegistryLookupThread(YangStoreService yangStoreService) {
+            super("config-registry-lookup");
+            this.yangStoreService = yangStoreService;
+        }
+
+        @Override
+        public void run() {
+            NetconfOperationServiceFactoryImpl factory = new NetconfOperationServiceFactoryImpl(yangStoreService);
+            logger.debug("Registering into OSGi");
+            osgiRegistration = context.registerService(new String[]{NetconfOperationServiceFactory.class.getName()}, factory,
+                    new Hashtable<String, Object>());
+        }
     }
 }