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