X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fconfig%2Fconfig-manager%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconfig%2Fmanager%2Fimpl%2Fosgi%2FModuleFactoryBundleTracker.java;h=8ca5da282511b88ee4cf5e7ea2887ecfba634b4b;hb=refs%2Fchanges%2F73%2F46573%2F5;hp=1ece5627e456193ccfc7fd637eabfe976013a4fe;hpb=0fdfbf0687003d0ef5353ea82ae8e1c7ed97b5c3;p=controller.git diff --git a/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/ModuleFactoryBundleTracker.java b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/ModuleFactoryBundleTracker.java index 1ece5627e4..8ca5da2825 100644 --- a/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/ModuleFactoryBundleTracker.java +++ b/opendaylight/config/config-manager/src/main/java/org/opendaylight/controller/config/manager/impl/osgi/ModuleFactoryBundleTracker.java @@ -7,27 +7,15 @@ */ package org.opendaylight.controller.config.manager.impl.osgi; -import static java.lang.String.format; import com.google.common.annotations.VisibleForTesting; -import com.google.common.base.Charsets; -import com.google.common.base.Stopwatch; -import com.google.common.collect.HashMultimap; -import com.google.common.collect.Multimap; import com.google.common.io.Resources; -import com.google.common.util.concurrent.Uninterruptibles; import java.io.IOException; import java.net.URL; -import java.util.AbstractMap; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Map; -import java.util.Map.Entry; -import java.util.concurrent.TimeUnit; -import javax.annotation.concurrent.GuardedBy; +import java.nio.charset.StandardCharsets; import org.opendaylight.controller.config.spi.ModuleFactory; import org.osgi.framework.Bundle; -import org.osgi.framework.BundleContext; import org.osgi.framework.BundleEvent; +import org.osgi.framework.ServiceRegistration; import org.osgi.util.tracker.BundleTrackerCustomizer; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,95 +29,61 @@ import org.slf4j.LoggerFactory; * the services are unregistered automatically. * Code based on http://www.toedter.com/blog/?p=236 */ -public class ModuleFactoryBundleTracker implements BundleTrackerCustomizer { - private static final Logger LOG = LoggerFactory.getLogger(ModuleFactoryBundleTracker.class); - private static final long BUNDLE_CONTEXT_TIMEOUT = TimeUnit.MILLISECONDS.convert(60, TimeUnit.SECONDS); - +public class ModuleFactoryBundleTracker implements BundleTrackerCustomizer { private final BlankTransactionServiceTracker blankTransactionServiceTracker; - - @GuardedBy(value = "bundleModuleFactoryMap") - private final Multimap bundleModuleFactoryMap = HashMultimap.create(); + private static final Logger LOG = LoggerFactory.getLogger(ModuleFactoryBundleTracker.class); public ModuleFactoryBundleTracker(BlankTransactionServiceTracker blankTransactionServiceTracker) { this.blankTransactionServiceTracker = blankTransactionServiceTracker; } @Override - public Object addingBundle(Bundle bundle, BundleEvent event) { - if(event != null && (event.getType() == BundleEvent.STOPPED || event.getType() == BundleEvent.STOPPING)) { - // We're tracking RESOLVED, STARTING, and ACTIVE states but not STOPPING. So when a bundle transitions - // to STOPPING, removedBundle gets called and we remove the ModuleFactory entries. However the - // bundle will then transition to RESOLVED which will cause the tracker to notify addingBundle. - // In this case we don't want to re-add the ModuleFactory entries so we check if the BundleEvent - // is STOPPED. - return null; - } - + public Boolean addingBundle(Bundle bundle, BundleEvent event) { URL resource = bundle.getEntry("META-INF/services/" + ModuleFactory.class.getName()); LOG.trace("Got addingBundle event of bundle {}, resource {}, event {}", bundle, resource, event); if (resource != null) { try { - for (String factoryClassName : Resources.readLines(resource, Charsets.UTF_8)) { - Entry moduleFactoryEntry = registerFactory(factoryClassName, bundle); - synchronized (bundleModuleFactoryMap) { - bundleModuleFactoryMap.put(new BundleKey(moduleFactoryEntry.getValue()), - moduleFactoryEntry.getKey()); - } + for (String factoryClassName : Resources.readLines(resource, StandardCharsets.UTF_8)) { + registerFactory(factoryClassName, bundle); } + + return Boolean.TRUE; } catch (IOException e) { LOG.error("Error while reading {}", resource, e); throw new RuntimeException(e); } } - return bundle; + + return Boolean.FALSE; } @Override - public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) { + public void modifiedBundle(Bundle bundle, BundleEvent event, Boolean hasFactory) { // NOOP } @Override - public void removedBundle(Bundle bundle, BundleEvent event, Object object) { - synchronized (bundleModuleFactoryMap) { - bundleModuleFactoryMap.removeAll(new BundleKey(bundle)); + public void removedBundle(Bundle bundle, BundleEvent event, Boolean hasFactory) { + if(hasFactory) { + // workaround for service tracker not getting removed service event + blankTransactionServiceTracker.blankTransactionSync(); } - - // workaround for service tracker not getting removed service event - blankTransactionServiceTracker.blankTransaction(); - } - - public Collection> getModuleFactoryEntries() { - Collection> entries; - synchronized (bundleModuleFactoryMap) { - entries = new ArrayList<>(bundleModuleFactoryMap.entries()); - } - - Collection> result = new ArrayList<>(entries.size()); - for(Entry entry: entries) { - BundleContext context = entry.getKey().getBundleContext(); - if(context == null) { - LOG.warn("Bundle context for {} ModuleFactory not found", entry.getValue()); - } else { - result.add(new AbstractMap.SimpleImmutableEntry<>(entry.getValue(), context)); - } - } - - return result; } @VisibleForTesting - protected static Map.Entry registerFactory(String factoryClassName, Bundle bundle) { + protected static ServiceRegistration registerFactory(String factoryClassName, Bundle bundle) { String errorMessage; Exception ex = null; try { Class clazz = bundle.loadClass(factoryClassName); if (ModuleFactory.class.isAssignableFrom(clazz)) { try { - LOG.debug("Registering {} in bundle {}", clazz.getName(), bundle); - - return new AbstractMap.SimpleImmutableEntry<>((ModuleFactory)clazz.newInstance(), bundle); + LOG.debug("Registering {} in bundle {}", + clazz.getName(), bundle); + return bundle.getBundleContext().registerService( + ModuleFactory.class.getName(), clazz.newInstance(), + null); } catch (InstantiationException e) { errorMessage = logMessage( "Could not instantiate {} in bundle {}, reason {}", @@ -140,11 +94,6 @@ public class ModuleFactoryBundleTracker implements BundleTrackerCustomizer