From: Robert Varga Date: Tue, 26 Dec 2023 22:33:01 +0000 (+0100) Subject: Use constructor injection in OSGiDOMSchemaService X-Git-Tag: v13.0.0~60 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=mdsal.git;a=commitdiff_plain;h=18cc77eca8b2440cb8c1f22db62c845c6fd7b9ae Use constructor injection in OSGiDOMSchemaService Constructor injections makes listenerFactory a final field, which is exactly what we want. Change-Id: Idc88f704e30a1bf00e367b31e0c8f168dd9761e6 Signed-off-by: Robert Varga --- diff --git a/dom/mdsal-dom-schema-osgi/src/main/java/org/opendaylight/mdsal/dom/schema/osgi/impl/OSGiDOMSchemaService.java b/dom/mdsal-dom-schema-osgi/src/main/java/org/opendaylight/mdsal/dom/schema/osgi/impl/OSGiDOMSchemaService.java index 50476c4e1a..0512f65cbe 100644 --- a/dom/mdsal-dom-schema-osgi/src/main/java/org/opendaylight/mdsal/dom/schema/osgi/impl/OSGiDOMSchemaService.java +++ b/dom/mdsal-dom-schema-osgi/src/main/java/org/opendaylight/mdsal/dom/schema/osgi/impl/OSGiDOMSchemaService.java @@ -42,42 +42,40 @@ import org.slf4j.LoggerFactory; public final class OSGiDOMSchemaService extends AbstractDOMSchemaService.WithYangTextSources { private static final Logger LOG = LoggerFactory.getLogger(OSGiDOMSchemaService.class); - @Reference(target = "(component.factory=" + EffectiveModelContextImpl.FACTORY_NAME + ")") - ComponentFactory listenerFactory = null; private final List listeners = new CopyOnWriteArrayList<>(); private final AtomicReference currentSnapshot = new AtomicReference<>(); + private final ComponentFactory listenerFactory; private boolean deactivated; - @Override - public @NonNull EffectiveModelContext getGlobalContext() { - return currentSnapshot.get().getEffectiveModelContext(); - } - - @Override - public Registration registerSchemaContextListener(final EffectiveModelContextListener listener) { - return registerListener(requireNonNull(listener)); + @Activate + public OSGiDOMSchemaService( + @Reference(target = "(component.factory=" + EffectiveModelContextImpl.FACTORY_NAME + ")") + final ComponentFactory listenerFactory) { + this.listenerFactory = requireNonNull(listenerFactory); + LOG.info("DOM Schema services activated"); } - @Override - public ListenableFuture getSource(final SourceIdentifier sourceIdentifier) { - return currentSnapshot.get().getSource(sourceIdentifier); + @Deactivate + void deactivate() { + LOG.info("DOM Schema services deactivated"); + deactivated = true; } @Reference(policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY) void bindSnapshot(final OSGiModuleInfoSnapshot newContext) { LOG.info("Updating context to generation {}", newContext.getGeneration()); - final ModuleInfoSnapshot snapshot = newContext.getService(); - final EffectiveModelContext ctx = snapshot.getEffectiveModelContext(); - final ModuleInfoSnapshot previous = currentSnapshot.getAndSet(snapshot); + final var snapshot = newContext.getService(); + final var modelContext = snapshot.getEffectiveModelContext(); + final var previous = currentSnapshot.getAndSet(snapshot); LOG.debug("Snapshot updated from {} to {}", previous, snapshot); - listeners.forEach(listener -> notifyListener(ctx, listener)); + listeners.forEach(listener -> notifyListener(modelContext, listener)); } void unbindSnapshot(final OSGiModuleInfoSnapshot oldContext) { - final ModuleInfoSnapshot snapshot = oldContext.getService(); + final var snapshot = oldContext.getService(); if (currentSnapshot.compareAndSet(snapshot, null) && !deactivated) { LOG.info("Lost final generation {}", oldContext.getGeneration()); } @@ -96,19 +94,13 @@ public final class OSGiDOMSchemaService extends AbstractDOMSchemaService.WithYan listeners.remove(listener); } - @Activate - @SuppressWarnings("static-method") - void activate() { - LOG.info("DOM Schema services activated"); - } - - @Deactivate - void deactivate() { - LOG.info("DOM Schema services deactivated"); - deactivated = true; + @Override + public @NonNull EffectiveModelContext getGlobalContext() { + return currentSnapshot.get().getEffectiveModelContext(); } - private @NonNull Registration registerListener(final @NonNull EffectiveModelContextListener listener) { + @Override + public Registration registerSchemaContextListener(final EffectiveModelContextListener listener) { final var reg = listenerFactory.newInstance(EffectiveModelContextImpl.props(listener)); return new AbstractRegistration() { @Override @@ -118,11 +110,16 @@ public final class OSGiDOMSchemaService extends AbstractDOMSchemaService.WithYan }; } + @Override + public ListenableFuture getSource(final SourceIdentifier sourceIdentifier) { + return currentSnapshot.get().getSource(sourceIdentifier); + } + @SuppressWarnings("checkstyle:illegalCatch") - private static void notifyListener(final @NonNull EffectiveModelContext context, + private static void notifyListener(final @NonNull EffectiveModelContext modelContext, final EffectiveModelContextListener listener) { try { - listener.onModelContextUpdated(context); + listener.onModelContextUpdated(modelContext); } catch (RuntimeException e) { LOG.warn("Failed to notify listener {}", listener, e); }