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.synccontext.named;
20  
21  import java.net.InetAddress;
22  import java.net.UnknownHostException;
23  import java.nio.file.Path;
24  import java.util.Collection;
25  
26  import org.eclipse.aether.RepositorySystemSession;
27  import org.eclipse.aether.artifact.Artifact;
28  import org.eclipse.aether.metadata.Metadata;
29  import org.eclipse.aether.named.NamedLockKey;
30  import org.eclipse.aether.util.ConfigUtils;
31  import org.eclipse.aether.util.StringDigestUtil;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  import static java.util.Objects.requireNonNull;
36  import static java.util.stream.Collectors.toList;
37  
38  /**
39   * Wrapping {@link NameMapper}, that wraps another {@link NameMapper} and adds a "discriminator" as prefix, that
40   * makes lock names unique including the hostname and local repository (by default). The discriminator may be passed
41   * in via {@link RepositorySystemSession} or is automatically calculated based on the local hostname and repository
42   * path. The implementation retains order of collection elements as it got it from
43   * {@link NameMapper#nameLocks(RepositorySystemSession, Collection, Collection)} method.
44   * <p>
45   * The default setup wraps {@link GAVNameMapper}, but manually may be created any instance needed.
46   */
47  public class DiscriminatingNameMapper implements NameMapper {
48      /**
49       * Configuration property to pass in discriminator, if needed. If not present, it is auto-calculated.
50       *
51       * @since 1.7.0
52       * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
53       * @configurationType {@link java.lang.String}
54       */
55      public static final String CONFIG_PROP_DISCRIMINATOR =
56              NamedLockFactoryAdapter.CONFIG_PROPS_PREFIX + "discriminating.discriminator";
57  
58      /**
59       * Configuration property to pass in hostname, if needed. If not present, hostname as reported by system will be
60       * used.
61       *
62       * @since 1.7.0
63       * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
64       * @configurationType {@link java.lang.String}
65       */
66      public static final String CONFIG_PROP_HOSTNAME =
67              NamedLockFactoryAdapter.CONFIG_PROPS_PREFIX + "discriminating.hostname";
68  
69      private static final String DEFAULT_DISCRIMINATOR_DIGEST = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
70  
71      private static final String DEFAULT_HOSTNAME = "localhost";
72  
73      private static final Logger LOGGER = LoggerFactory.getLogger(DiscriminatingNameMapper.class);
74  
75      private final NameMapper delegate;
76  
77      private final String hostname;
78  
79      public DiscriminatingNameMapper(final NameMapper delegate) {
80          this.delegate = requireNonNull(delegate);
81          this.hostname = getHostname();
82      }
83  
84      @Override
85      public boolean isFileSystemFriendly() {
86          return false; // uses ":" in produced lock names
87      }
88  
89      @Override
90      public Collection<NamedLockKey> nameLocks(
91              final RepositorySystemSession session,
92              final Collection<? extends Artifact> artifacts,
93              final Collection<? extends Metadata> metadatas) {
94          String discriminator = createDiscriminator(session);
95          return delegate.nameLocks(session, artifacts, metadatas).stream()
96                  .map(k -> NamedLockKey.of(discriminator + ":" + k.name(), k.resources()))
97                  .collect(toList());
98      }
99  
100     private String getHostname() {
101         try {
102             return InetAddress.getLocalHost().getHostName();
103         } catch (UnknownHostException e) {
104             LOGGER.warn("Failed to get hostname, using '{}'", DEFAULT_HOSTNAME, e);
105             return DEFAULT_HOSTNAME;
106         }
107     }
108 
109     private String createDiscriminator(final RepositorySystemSession session) {
110         String discriminator = ConfigUtils.getString(session, null, CONFIG_PROP_DISCRIMINATOR);
111 
112         if (discriminator == null || discriminator.isEmpty()) {
113             String hostname = ConfigUtils.getString(session, this.hostname, CONFIG_PROP_HOSTNAME);
114             Path basedir = session.getLocalRepository().getBasePath();
115             discriminator = hostname + ":" + basedir;
116             try {
117                 return StringDigestUtil.sha1(discriminator);
118             } catch (Exception e) {
119                 LOGGER.warn("Failed to calculate discriminator digest, using '{}'", DEFAULT_DISCRIMINATOR_DIGEST, e);
120                 return DEFAULT_DISCRIMINATOR_DIGEST;
121             }
122         }
123         return discriminator;
124     }
125 }