Bump yangtools to 5.0.0-SNAPSHOT
[mdsal.git] / binding / mdsal-binding-dom-codec-osgi / src / main / java / org / opendaylight / mdsal / binding / dom / codec / osgi / impl / BindingClassLoadingStrategy.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2017 Pantheon Technologies, s.r.o. and others.  All rights reserved.
+ * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -7,29 +7,33 @@
  */
 package org.opendaylight.mdsal.binding.dom.codec.osgi.impl;
 
-import java.util.ArrayList;
-import java.util.List;
 import org.opendaylight.binding.runtime.api.ClassLoadingStrategy;
 import org.opendaylight.binding.runtime.spi.GeneratedClassLoadingStrategy;
 import org.opendaylight.binding.runtime.spi.ModuleInfoBackedContext;
-import org.osgi.framework.BundleActivator;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceRegistration;
+import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
+import org.osgi.service.component.ComponentContext;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.component.annotations.Reference;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-public final class Activator implements BundleActivator {
-    private static final Logger LOG = LoggerFactory.getLogger(Activator.class);
+@Component(immediate = true)
+public final class BindingClassLoadingStrategy implements ClassLoadingStrategy {
+    private static final Logger LOG = LoggerFactory.getLogger(BindingClassLoadingStrategy.class);
 
-    private final List<ServiceRegistration<?>> registrations = new ArrayList<>(2);
+    @Reference
+    YangParserFactory factory = null;
 
     private ModuleInfoBundleTracker bundleTracker = null;
+    private ModuleInfoBackedContext moduleInfoBackedContext = null;
 
-    @Override
-    public void start(final BundleContext context) {
+    @Activate
+    void activate(final ComponentContext ctx) {
         LOG.info("Binding-DOM codec starting");
 
-        final ModuleInfoBackedContext moduleInfoBackedContext = ModuleInfoBackedContext.create(
+        moduleInfoBackedContext = ModuleInfoBackedContext.create("binding-dom-codec", factory,
             // FIXME: This is the fallback strategy, it should not be needed
             GeneratedClassLoadingStrategy.getTCCLClassLoadingStrategy());
 
@@ -37,26 +41,24 @@ public final class Activator implements BundleActivator {
                 moduleInfoBackedContext);
 
         LOG.debug("Starting Binding-DOM codec bundle tracker");
-        bundleTracker = new ModuleInfoBundleTracker(context, registry);
+        bundleTracker = new ModuleInfoBundleTracker(ctx.getBundleContext(), registry);
         bundleTracker.open();
 
-        LOG.debug("Registering Binding-DOM codec services");
-        registrations.add(context.registerService(ClassLoadingStrategy.class, moduleInfoBackedContext, null));
-
         LOG.info("Binding-DOM codec started");
     }
 
-    @Override
-    public void stop(final BundleContext context) {
+    @Deactivate
+    void deactivate() {
         LOG.info("Binding-DOM codec stopping");
-
-        LOG.debug("Unregistering Binding-DOM codec services");
-        registrations.forEach(ServiceRegistration::unregister);
-        registrations.clear();
-
         LOG.debug("Stopping Binding-DOM codec bundle tracker");
         bundleTracker.close();
-
+        moduleInfoBackedContext = null;
+        bundleTracker = null;
         LOG.info("Binding-DOM codec stopped");
     }
+
+    @Override
+    public Class<?> loadClass(final String fullyQualifiedName) throws ClassNotFoundException {
+        return moduleInfoBackedContext.loadClass(fullyQualifiedName);
+    }
 }