X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=yang%2Fyang-model-util%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fyangtools%2Fyang%2Fmodel%2Frepo%2Futil%2FAbstractSchemaRepository.java;h=41b637482ac069bb40ac2857267b0491e4e1421f;hb=d559b39d9c161ab05f7304f522eae0ce31fdb67a;hp=722ac918e08c995d391546d085fcf6b4aa36b95b;hpb=153362f50d57c7e3b39e07b626d8acf238aa88ed;p=yangtools.git diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/repo/util/AbstractSchemaRepository.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/repo/util/AbstractSchemaRepository.java index 722ac918e0..41b637482a 100644 --- a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/repo/util/AbstractSchemaRepository.java +++ b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/repo/util/AbstractSchemaRepository.java @@ -3,20 +3,21 @@ * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, - * and is available at http://www.eclipse.org/legal/eplv10.html + * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.yang.model.repo.util; +import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateFailedFluentFuture; + import com.google.common.annotations.Beta; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ListMultimap; import com.google.common.collect.Lists; import com.google.common.collect.Multimap; -import com.google.common.util.concurrent.CheckedFuture; import com.google.common.util.concurrent.FutureCallback; -import com.google.common.util.concurrent.FutureFallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.MoreExecutors; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; @@ -24,12 +25,10 @@ import java.util.Comparator; import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import javax.annotation.Nonnull; import javax.annotation.concurrent.GuardedBy; -import org.opendaylight.yangtools.util.concurrent.ExceptionMapper; -import org.opendaylight.yangtools.util.concurrent.ReflectiveExceptionMapper; import org.opendaylight.yangtools.yang.model.repo.api.MissingSchemaSourceException; import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository; -import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException; import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceFilter; import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceRepresentation; import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier; @@ -50,14 +49,14 @@ import org.slf4j.LoggerFactory; @Beta public abstract class AbstractSchemaRepository implements SchemaRepository, SchemaSourceRegistry { private static final Logger LOG = LoggerFactory.getLogger(AbstractSchemaRepository.class); - private static final ExceptionMapper FETCH_MAPPER = ReflectiveExceptionMapper.create("Schema source fetch", SchemaSourceException.class); /* * Source identifier -> representation -> provider map. We usually are looking for * a specific representation of a source. */ @GuardedBy("this") - private final Map, AbstractSchemaSourceRegistration>> sources = new HashMap<>(); + private final Map, + AbstractSchemaSourceRegistration>> sources = new HashMap<>(); /* * Schema source listeners. @@ -65,44 +64,49 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche @GuardedBy("this") private final Collection listeners = new ArrayList<>(); - private static final CheckedFuture fetchSource(final SourceIdentifier id, final Iterator> it) { + @SuppressWarnings("unchecked") + private static ListenableFuture fetchSource( + final SourceIdentifier id, final Iterator> it) { final AbstractSchemaSourceRegistration reg = it.next(); - @SuppressWarnings("unchecked") - final CheckedFuture f = ((SchemaSourceProvider)reg.getProvider()).getSource(id); - - return Futures.makeChecked(Futures.withFallback(f, new FutureFallback() { - @Override - public ListenableFuture create(final Throwable t) throws SchemaSourceException { - LOG.debug("Failed to acquire source from {}", reg, t); + return Futures.catchingAsync(((SchemaSourceProvider)reg.getProvider()).getSource(id), Throwable.class, + input -> { + LOG.debug("Failed to acquire source from {}", reg, input); if (it.hasNext()) { return fetchSource(id, it); } - throw new MissingSchemaSourceException("All available providers exhausted", id, t); - } - }), FETCH_MAPPER); + throw new MissingSchemaSourceException("All available providers exhausted", id, input); + }, MoreExecutors.directExecutor()); } @Override - public CheckedFuture getSchemaSource(final SourceIdentifier id, final Class representation) { - final ListMultimap, AbstractSchemaSourceRegistration> srcs = sources.get(id); - if (srcs == null) { - return Futures.immediateFailedCheckedFuture(new MissingSchemaSourceException("No providers registered for source" + id, id)); + public ListenableFuture getSchemaSource( + @Nonnull final SourceIdentifier id, @Nonnull final Class representation) { + final ArrayList> sortedSchemaSourceRegistrations; + + synchronized (this) { + final ListMultimap, AbstractSchemaSourceRegistration> srcs = + sources.get(id); + if (srcs == null) { + return immediateFailedFluentFuture(new MissingSchemaSourceException( + "No providers registered for source" + id, id)); + } + + sortedSchemaSourceRegistrations = Lists.newArrayList(srcs.get(representation)); } // TODO, remove and make sources keep sorted multimap (e.g. ArrayListMultimap with SortedLists) - final ArrayList> sortedSchemaSourceRegistrations = Lists.newArrayList(srcs.get(representation)); - Collections.sort(sortedSchemaSourceRegistrations, SchemaProviderCostComparator.INSTANCE); + sortedSchemaSourceRegistrations.sort(SchemaProviderCostComparator.INSTANCE); final Iterator> regs = sortedSchemaSourceRegistrations.iterator(); if (!regs.hasNext()) { - return Futures.immediateFailedCheckedFuture( - new MissingSchemaSourceException("No providers for source " + id + " representation " + representation + " available", id)); + return immediateFailedFluentFuture(new MissingSchemaSourceException( + "No providers for source " + id + " representation " + representation + " available", id)); } - CheckedFuture fetchSourceFuture = fetchSource(id, regs); + final ListenableFuture fetchSourceFuture = fetchSource(id, regs); // Add callback to notify cache listeners about encountered schema Futures.addCallback(fetchSourceFuture, new FutureCallback() { @Override @@ -113,31 +117,36 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche } @Override - public void onFailure(final Throwable t) { + @SuppressWarnings("checkstyle:parameterName") + public void onFailure(@Nonnull final Throwable t) { LOG.trace("Skipping notification for encountered source {}, fetching source failed", id, t); } - }); + }, MoreExecutors.directExecutor()); return fetchSourceFuture; } - private synchronized void addSource(final PotentialSchemaSource source, final AbstractSchemaSourceRegistration reg) { - ListMultimap, AbstractSchemaSourceRegistration> m = sources.get(source.getSourceIdentifier()); - if (m == null) { - m = ArrayListMultimap.create(); - sources.put(source.getSourceIdentifier(), m); + private synchronized void addSource(final PotentialSchemaSource source, + final AbstractSchemaSourceRegistration reg) { + ListMultimap, AbstractSchemaSourceRegistration> map = + sources.get(source.getSourceIdentifier()); + if (map == null) { + map = ArrayListMultimap.create(); + sources.put(source.getSourceIdentifier(), map); } - m.put(source.getRepresentation(), reg); + map.put(source.getRepresentation(), reg); - final Collection> reps = Collections.>singleton(source); + final Collection> reps = Collections.singleton(source); for (SchemaListenerRegistration l : listeners) { l.getInstance().schemaSourceRegistered(reps); } } - private synchronized void removeSource(final PotentialSchemaSource source, final SchemaSourceRegistration reg) { - final Multimap, AbstractSchemaSourceRegistration> m = sources.get(source.getSourceIdentifier()); + private synchronized void removeSource(final PotentialSchemaSource source, + final SchemaSourceRegistration reg) { + final Multimap, AbstractSchemaSourceRegistration> m = + sources.get(source.getSourceIdentifier()); if (m != null) { m.remove(source.getRepresentation(), reg); @@ -152,15 +161,18 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche } @Override - public SchemaSourceRegistration registerSchemaSource(final SchemaSourceProvider provider, final PotentialSchemaSource source) { - final AbstractSchemaSourceRegistration ret = new AbstractSchemaSourceRegistration(provider, source) { + public SchemaSourceRegistration registerSchemaSource( + final SchemaSourceProvider provider, final PotentialSchemaSource source) { + final PotentialSchemaSource src = source.cachedReference(); + + final AbstractSchemaSourceRegistration ret = new AbstractSchemaSourceRegistration(provider, src) { @Override protected void removeRegistration() { - removeSource(source, this); + removeSource(src, this); } }; - addSource(source, ret); + addSource(src, ret); return ret; } @@ -175,7 +187,8 @@ public abstract class AbstractSchemaRepository implements SchemaRepository, Sche synchronized (this) { final Collection> col = new ArrayList<>(); - for (Multimap, AbstractSchemaSourceRegistration> m : sources.values()) { + for (Multimap, AbstractSchemaSourceRegistration> m + : sources.values()) { for (AbstractSchemaSourceRegistration r : m.values()) { col.add(r.getInstance()); }