StrategyCache.java

  1. /*
  2.  * Licensed under the Apache License, Version 2.0 (the "License");
  3.  * you may not use this file except in compliance with the License.
  4.  * You may obtain a copy of the License at
  5.  *
  6.  * http://www.apache.org/licenses/LICENSE-2.0
  7.  *
  8.  * Unless required by applicable law or agreed to in writing, software
  9.  * distributed under the License is distributed on an "AS IS" BASIS,
  10.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11.  * See the License for the specific language governing permissions and
  12.  * limitations under the License.
  13.  */

  14. package org.basepom.mojo.dvc;

  15. import static com.google.common.base.Preconditions.checkNotNull;
  16. import static com.google.common.base.Preconditions.checkState;

  17. import org.basepom.mojo.dvc.model.ResolverDefinition;
  18. import org.basepom.mojo.dvc.strategy.Strategy;
  19. import org.basepom.mojo.dvc.strategy.StrategyProvider;

  20. import java.util.Arrays;
  21. import java.util.Map;
  22. import java.util.concurrent.ExecutionException;

  23. import com.google.common.cache.Cache;
  24. import com.google.common.cache.CacheBuilder;
  25. import com.google.common.collect.ImmutableMap;

  26. public final class StrategyCache {

  27.     /**
  28.      * name of an artifact to  version resolution strategy.
  29.      */
  30.     private final Cache<QualifiedName, Strategy> resolverCache = CacheBuilder.newBuilder().concurrencyLevel(10).build();

  31.     private final ImmutableMap<QualifiedNameMatcher, Strategy> resolverPatterns;
  32.     private final Strategy defaultStrategy;

  33.     StrategyCache(final StrategyProvider strategyProvider, final ResolverDefinition[] resolvers, final String defaultStrategyName) {
  34.         checkNotNull(strategyProvider, "strategyProvider is null");
  35.         checkNotNull(resolvers, "resolvers is null");
  36.         checkNotNull(defaultStrategyName, "defaultStrategyName is null");

  37.         this.defaultStrategy = strategyProvider.forName(defaultStrategyName);
  38.         checkState(defaultStrategy != null, "Could not locate default version strategy '%s'", defaultStrategyName);

  39.         final ImmutableMap.Builder<QualifiedNameMatcher, Strategy> builder = ImmutableMap.builder();
  40.         Arrays.stream(resolvers).forEach(r -> {
  41.             final Strategy strategy = strategyProvider.forName(r.getStrategy());
  42.             checkState(strategy != null, "Could not locate version strategy %s! Check for typos!", r.getStrategy());
  43.             r.getIncludes().forEach(include -> builder.put(include, strategy));
  44.         });
  45.         this.resolverPatterns = builder.build();
  46.     }

  47.     public Strategy forQualifiedName(final QualifiedName name) {
  48.         checkNotNull(name, "name is null");
  49.         try {
  50.             return resolverCache.get(name, () -> {
  51.                 for (final Map.Entry<QualifiedNameMatcher, Strategy> entry : resolverPatterns.entrySet()) {
  52.                     if (entry.getKey().matches(name)) {
  53.                         return entry.getValue();
  54.                     }
  55.                 }
  56.                 return defaultStrategy;
  57.             });
  58.         } catch (ExecutionException e) {
  59.             // ignore, never happens.
  60.             return defaultStrategy;
  61.         }
  62.     }
  63. }