View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.internal.impl;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.util.Locale;
25  import java.util.concurrent.ConcurrentHashMap;
26  import java.util.concurrent.ConcurrentMap;
27  
28  import org.eclipse.aether.ConfigurationProperties;
29  import org.eclipse.aether.Keys;
30  import org.eclipse.aether.RepositorySystemSession;
31  import org.eclipse.aether.repository.RemoteRepository;
32  import org.eclipse.aether.repository.RepositoryKeyFunction;
33  import org.eclipse.aether.spi.remoterepo.RepositoryKeyFunctionFactory;
34  import org.eclipse.aether.util.ConfigUtils;
35  import org.eclipse.aether.util.repository.RepositoryIdHelper;
36  
37  import static java.util.Objects.requireNonNull;
38  
39  @Singleton
40  @Named
41  public class DefaultRepositoryKeyFunctionFactory implements RepositoryKeyFunctionFactory {
42  
43      @Override
44      public RepositoryKeyFunction trackingRepositoryKeyFunction(RepositorySystemSession session) {
45          return doRepositoryKeyFunction(
46                  RepositoryKeyFunctionFactory.class,
47                  session,
48                  ConfigurationProperties.DEFAULT_REPOSITORY_TRACKING_REPOSITORY_KEY_FUNCTION,
49                  ConfigurationProperties.REPOSITORY_TRACKING_REPOSITORY_KEY_FUNCTION,
50                  ConfigurationProperties.REPOSITORY_SYSTEM_REPOSITORY_KEY_FUNCTION);
51      }
52  
53      @Override
54      public RepositoryKeyFunction repositoryKeyFunction(
55              Class<?> owner, RepositorySystemSession session, String defaultValue, String configurationKey) {
56          return doRepositoryKeyFunction(owner, session, defaultValue, configurationKey);
57      }
58  
59      /**
60       * Method that based on configuration returns the "repository key function". The returned function will be session
61       * cached if session is equipped with cache, otherwise it will be non cached. Method never returns {@code null}.
62       * Only the {@code configurationKey} parameter may be {@code null} in which case no configuration lookup happens
63       * but the {@code defaultValue} is directly used instead.
64       */
65      @SuppressWarnings("unchecked")
66      private RepositoryKeyFunction doRepositoryKeyFunction(
67              Class<?> owner, RepositorySystemSession session, String defaultValue, String... configurationKeys) {
68          requireNonNull(session);
69          requireNonNull(defaultValue);
70          RepositoryIdHelper.RepositoryKeyType type =
71                  RepositoryIdHelper.RepositoryKeyType.valueOf((configurationKeys != null
72                                  ? ConfigUtils.getString(session, defaultValue, configurationKeys)
73                                  : defaultValue)
74                          .toUpperCase(Locale.ENGLISH));
75          final RepositoryKeyFunction repositoryKeyFunction = RepositoryIdHelper.getRepositoryKeyFunction(type.name());
76          if (session.getCache() != null) {
77              // both are expensive methods; cache it in session (repo -> context -> ID)
78              return (repository, context) -> ((ConcurrentMap<RemoteRepository, ConcurrentMap<String, String>>)
79                              session.getCache().computeIfAbsent(session, Keys.of(owner, type), ConcurrentHashMap::new))
80                      .computeIfAbsent(repository, k1 -> new ConcurrentHashMap<>())
81                      .computeIfAbsent(
82                              context == null ? "" : context, k2 -> repositoryKeyFunction.apply(repository, context));
83          } else {
84              return repositoryKeyFunction;
85          }
86      }
87  }