From: Tony Tkacik Date: Mon, 26 May 2014 19:30:58 +0000 (+0000) Subject: Merge changes I7c36c88e,I7c5d97c7 X-Git-Tag: autorelease-tag-v20140601202136_82eb3f9~24 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=3b6353dd7144251c79e9454a319675a406f0bd7f;hp=f2b0b8646e5e8060dbb1a8278ddaf0f4b2a422c0 Merge changes I7c36c88e,I7c5d97c7 * changes: BUG-1070: make BundleContext required BUG-1070: allow for URL registration failing --- diff --git a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/config/yang/md/sal/dom/impl/SchemaServiceImplSingletonModule.java b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/config/yang/md/sal/dom/impl/SchemaServiceImplSingletonModule.java index 93284c98e1..fd24944018 100644 --- a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/config/yang/md/sal/dom/impl/SchemaServiceImplSingletonModule.java +++ b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/config/yang/md/sal/dom/impl/SchemaServiceImplSingletonModule.java @@ -19,29 +19,26 @@ import org.osgi.framework.ServiceReference; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -/** -* -*/ public final class SchemaServiceImplSingletonModule extends - org.opendaylight.controller.config.yang.md.sal.dom.impl.AbstractSchemaServiceImplSingletonModule { +org.opendaylight.controller.config.yang.md.sal.dom.impl.AbstractSchemaServiceImplSingletonModule { private static final Logger LOG = LoggerFactory.getLogger(SchemaServiceImplSingletonModule.class); BundleContext bundleContext; - public SchemaServiceImplSingletonModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, - org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) { + public SchemaServiceImplSingletonModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, + final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver) { super(identifier, dependencyResolver); } - public SchemaServiceImplSingletonModule(org.opendaylight.controller.config.api.ModuleIdentifier identifier, - org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, - SchemaServiceImplSingletonModule oldModule, java.lang.AutoCloseable oldInstance) { + public SchemaServiceImplSingletonModule(final org.opendaylight.controller.config.api.ModuleIdentifier identifier, + final org.opendaylight.controller.config.api.DependencyResolver dependencyResolver, + final SchemaServiceImplSingletonModule oldModule, final java.lang.AutoCloseable oldInstance) { super(identifier, dependencyResolver, oldModule, oldInstance); } @Override - public boolean canReuseInstance(AbstractSchemaServiceImplSingletonModule oldModule) { + public boolean canReuseInstance(final AbstractSchemaServiceImplSingletonModule oldModule) { return true; } @@ -49,7 +46,7 @@ public final class SchemaServiceImplSingletonModule extends return bundleContext; } - public void setBundleContext(BundleContext bundleContext) { + public void setBundleContext(final BundleContext bundleContext) { this.bundleContext = bundleContext; } @@ -65,8 +62,7 @@ public final class SchemaServiceImplSingletonModule extends return new GlobalSchemaServiceProxy(getBundleContext(), ref); } - GlobalBundleScanningSchemaServiceImpl newInstance = new GlobalBundleScanningSchemaServiceImpl(); - newInstance.setContext(getBundleContext()); + GlobalBundleScanningSchemaServiceImpl newInstance = new GlobalBundleScanningSchemaServiceImpl(getBundleContext()); newInstance.start(); return newInstance; } @@ -77,7 +73,7 @@ public final class SchemaServiceImplSingletonModule extends private ServiceReference reference; private SchemaService delegate; - public GlobalSchemaServiceProxy(BundleContext bundleContext, ServiceReference ref) { + public GlobalSchemaServiceProxy(final BundleContext bundleContext, final ServiceReference ref) { this.bundleContext = bundleContext; this.reference = ref; this.delegate = bundleContext.getService(reference); @@ -102,7 +98,7 @@ public final class SchemaServiceImplSingletonModule extends } @Override - public void addModule(Module arg0) { + public void addModule(final Module arg0) { delegate.addModule(arg0); } @@ -117,12 +113,12 @@ public final class SchemaServiceImplSingletonModule extends } @Override - public ListenerRegistration registerSchemaServiceListener(SchemaServiceListener arg0) { + public ListenerRegistration registerSchemaServiceListener(final SchemaServiceListener arg0) { return delegate.registerSchemaServiceListener(arg0); } @Override - public void removeModule(Module arg0) { + public void removeModule(final Module arg0) { delegate.removeModule(arg0); } diff --git a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/GlobalBundleScanningSchemaServiceImpl.java b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/GlobalBundleScanningSchemaServiceImpl.java index dab8fd5cd1..430963a884 100644 --- a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/GlobalBundleScanningSchemaServiceImpl.java +++ b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/GlobalBundleScanningSchemaServiceImpl.java @@ -10,7 +10,10 @@ package org.opendaylight.controller.sal.dom.broker; import static com.google.common.base.Preconditions.checkState; import java.net.URL; +import java.util.ArrayList; +import java.util.Collections; import java.util.Enumeration; +import java.util.List; import org.opendaylight.controller.sal.core.api.model.SchemaService; import org.opendaylight.controller.sal.dom.broker.impl.SchemaContextProvider; @@ -33,54 +36,34 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.google.common.base.Optional; -import com.google.common.collect.ImmutableSet; -import com.google.common.collect.ImmutableSet.Builder; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableList; -public class GlobalBundleScanningSchemaServiceImpl implements // - SchemaContextProvider, // - SchemaService, // - ServiceTrackerCustomizer, // - AutoCloseable { - private static final Logger logger = LoggerFactory.getLogger(GlobalBundleScanningSchemaServiceImpl.class); - - private ListenerRegistry listeners; - - private BundleContext context; - private final BundleScanner scanner = new BundleScanner(); - - private BundleTracker>> bundleTracker; +public class GlobalBundleScanningSchemaServiceImpl implements SchemaContextProvider, SchemaService, ServiceTrackerCustomizer, AutoCloseable { + private static final Logger LOG = LoggerFactory.getLogger(GlobalBundleScanningSchemaServiceImpl.class); + private final ListenerRegistry listeners = new ListenerRegistry<>(); private final URLSchemaContextResolver contextResolver = new URLSchemaContextResolver(); + private final BundleScanner scanner = new BundleScanner(); + private final BundleContext context; private ServiceTracker listenerTracker; - + private BundleTracker>> bundleTracker; private boolean starting = true; - public ListenerRegistry getListeners() { - return listeners; - } - - public void setListeners(final ListenerRegistry listeners) { - this.listeners = listeners; + public GlobalBundleScanningSchemaServiceImpl(final BundleContext context) { + this.context = Preconditions.checkNotNull(context); } public BundleContext getContext() { return context; } - public void setContext(final BundleContext context) { - this.context = context; - } - public void start() { checkState(context != null); - if (listeners == null) { - listeners = new ListenerRegistry<>(); - } listenerTracker = new ServiceTracker<>(context, SchemaServiceListener.class, GlobalBundleScanningSchemaServiceImpl.this); - bundleTracker = new BundleTracker>>(context, BundleEvent.RESOLVED - | BundleEvent.UNRESOLVED, scanner); + bundleTracker = new BundleTracker<>(context, BundleEvent.RESOLVED | BundleEvent.UNRESOLVED, scanner); bundleTracker.open(); listenerTracker.open(); starting = false; @@ -139,7 +122,7 @@ public class GlobalBundleScanningSchemaServiceImpl implements // try { listener.getInstance().onGlobalContextUpdated(snapshot); } catch (Exception e) { - logger.error("Exception occured during invoking listener", e); + LOG.error("Exception occured during invoking listener", e); } } if (services != null) { @@ -148,37 +131,47 @@ public class GlobalBundleScanningSchemaServiceImpl implements // try { listener.onGlobalContextUpdated(snapshot); } catch (Exception e) { - logger.error("Exception occured during invoking listener", e); + LOG.error("Exception occured during invoking listener {}", listener, e); } } } } - private class BundleScanner implements BundleTrackerCustomizer>> { + private class BundleScanner implements BundleTrackerCustomizer>> { @Override - public ImmutableSet> addingBundle(final Bundle bundle, final BundleEvent event) { + public Iterable> addingBundle(final Bundle bundle, final BundleEvent event) { if (bundle.getBundleId() == 0) { - return ImmutableSet.of(); + return Collections.emptyList(); } - Enumeration enumeration = bundle.findEntries("META-INF/yang", "*.yang", false); - Builder> builder = ImmutableSet.> builder(); - while (enumeration != null && enumeration.hasMoreElements()) { - Registration reg = contextResolver.registerSource(enumeration.nextElement()); - builder.add(reg); + final Enumeration enumeration = bundle.findEntries("META-INF/yang", "*.yang", false); + if (enumeration == null) { + return Collections.emptyList(); } - ImmutableSet> urls = builder.build(); - if(urls.isEmpty()) { - return urls; + + final List> urls = new ArrayList<>(); + while (enumeration.hasMoreElements()) { + final URL u = enumeration.nextElement(); + try { + urls.add(contextResolver.registerSource(u)); + LOG.debug("Registered {}", u); + } catch (Exception e) { + LOG.warn("Failed to register {}, ignoring it", e); + } } - tryToUpdateSchemaContext(); - return urls; + + if (!urls.isEmpty()) { + LOG.debug("Loaded {} new URLs, rebuilding schema context", urls.size()); + tryToUpdateSchemaContext(); + } + + return ImmutableList.copyOf(urls); } @Override - public void modifiedBundle(final Bundle bundle, final BundleEvent event, final ImmutableSet> object) { - logger.debug("Modified bundle {} {} {}", bundle, event, object); + public void modifiedBundle(final Bundle bundle, final BundleEvent event, final Iterable> object) { + LOG.debug("Modified bundle {} {} {}", bundle, event, object); } /** @@ -188,12 +181,12 @@ public class GlobalBundleScanningSchemaServiceImpl implements // */ @Override - public synchronized void removedBundle(final Bundle bundle, final BundleEvent event, final ImmutableSet> urls) { + public synchronized void removedBundle(final Bundle bundle, final BundleEvent event, final Iterable> urls) { for (Registration url : urls) { try { url.close(); } catch (Exception e) { - e.printStackTrace(); + LOG.warn("Failed do unregister URL {}, proceeding", url, e); } } tryToUpdateSchemaContext(); @@ -212,7 +205,7 @@ public class GlobalBundleScanningSchemaServiceImpl implements // } public synchronized void tryToUpdateSchemaContext() { - if(starting ) { + if (starting) { return; } Optional schema = contextResolver.tryToUpdateSchemaContext(); diff --git a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/osgi/SchemaServiceActivator.java b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/osgi/SchemaServiceActivator.java index 344b3f3276..f893f96d18 100644 --- a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/osgi/SchemaServiceActivator.java +++ b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/osgi/SchemaServiceActivator.java @@ -17,20 +17,19 @@ import org.osgi.framework.ServiceRegistration; public class SchemaServiceActivator implements BundleActivator { - + private ServiceRegistration schemaServiceReg; private GlobalBundleScanningSchemaServiceImpl schemaService; @Override - public void start(BundleContext context) throws Exception { - schemaService = new GlobalBundleScanningSchemaServiceImpl(); - schemaService.setContext(context); + public void start(final BundleContext context) { + schemaService = new GlobalBundleScanningSchemaServiceImpl(context); schemaService.start(); schemaServiceReg = context.registerService(SchemaService.class, schemaService, new Hashtable()); } - + @Override - public void stop(BundleContext context) throws Exception { + public void stop(final BundleContext context) throws Exception { schemaServiceReg.unregister(); schemaService.close(); }