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