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.concurrent.ConcurrentHashMap;
25  import java.util.concurrent.ConcurrentMap;
26  
27  import org.eclipse.aether.ConfigurationProperties;
28  import org.eclipse.aether.RepositorySystemSession;
29  import org.eclipse.aether.repository.RemoteRepository;
30  import org.eclipse.aether.repository.RepositoryKeyFunction;
31  import org.eclipse.aether.spi.remoterepo.RepositoryKeyFunctionFactory;
32  import org.eclipse.aether.util.ConfigUtils;
33  import org.eclipse.aether.util.repository.RepositoryIdHelper;
34  
35  import static java.util.Objects.requireNonNull;
36  
37  @Singleton
38  @Named
39  public class DefaultRepositoryKeyFunctionFactory implements RepositoryKeyFunctionFactory {
40      /**
41       * Returns system-wide repository key function.
42       *
43       * @since 2.0.14
44       * @see #repositoryKeyFunction(Class, RepositorySystemSession, String, String)
45       */
46      @Override
47      public RepositoryKeyFunction systemRepositoryKeyFunction(RepositorySystemSession session) {
48          return repositoryKeyFunction(
49                  DefaultRepositoryKeyFunctionFactory.class,
50                  session,
51                  ConfigurationProperties.DEFAULT_REPOSITORY_SYSTEM_REPOSITORY_KEY_FUNCTION,
52                  ConfigurationProperties.REPOSITORY_SYSTEM_REPOSITORY_KEY_FUNCTION);
53      }
54  
55      /**
56       * Method that based on configuration returns the "repository key function". The returned function will be session
57       * cached if session is equipped with cache, otherwise it will be non cached. Method never returns {@code null}.
58       * Only the {@code configurationKey} parameter may be {@code null} in which case no configuration lookup happens
59       * but the {@code defaultValue} is directly used instead.
60       *
61       * @since 2.0.14
62       */
63      @SuppressWarnings("unchecked")
64      @Override
65      public RepositoryKeyFunction repositoryKeyFunction(
66              Class<?> owner, RepositorySystemSession session, String defaultValue, String configurationKey) {
67          requireNonNull(session);
68          requireNonNull(defaultValue);
69          final RepositoryKeyFunction repositoryKeyFunction = RepositoryIdHelper.getRepositoryKeyFunction(
70                  configurationKey != null
71                          ? ConfigUtils.getString(session, defaultValue, configurationKey)
72                          : defaultValue);
73          if (session.getCache() != null) {
74              // both are expensive methods; cache it in session (repo -> context -> ID)
75              return (repository, context) -> ((ConcurrentMap<RemoteRepository, ConcurrentMap<String, String>>)
76                              session.getCache()
77                                      .computeIfAbsent(
78                                              session,
79                                              owner.getName() + ".repositoryKeyFunction",
80                                              ConcurrentHashMap::new))
81                      .computeIfAbsent(repository, k1 -> new ConcurrentHashMap<>())
82                      .computeIfAbsent(
83                              context == null ? "" : context, k2 -> repositoryKeyFunction.apply(repository, context));
84          } else {
85              return repositoryKeyFunction;
86          }
87      }
88  }