1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
39
40
41
42
43
44
45
46 public class DiscriminatingNameMapper implements NameMapper {
47
48
49
50 private static final String CONFIG_PROP_DISCRIMINATOR = "aether.syncContext.named.discriminating.discriminator";
51
52
53
54
55 private static final String CONFIG_PROP_HOSTNAME = "aether.syncContext.named.discriminating.hostname";
56
57 private static final String DEFAULT_DISCRIMINATOR_DIGEST = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
58
59 private static final String DEFAULT_HOSTNAME = "localhost";
60
61 private static final Logger LOGGER = LoggerFactory.getLogger(DiscriminatingNameMapper.class);
62
63 private final NameMapper delegate;
64
65 private final String hostname;
66
67 public DiscriminatingNameMapper(final NameMapper delegate) {
68 this.delegate = requireNonNull(delegate);
69 this.hostname = getHostname();
70 }
71
72 @Override
73 public boolean isFileSystemFriendly() {
74 return false;
75 }
76
77 @Override
78 public Collection<String> nameLocks(
79 final RepositorySystemSession session,
80 final Collection<? extends Artifact> artifacts,
81 final Collection<? extends Metadata> metadatas) {
82 String discriminator = createDiscriminator(session);
83 return delegate.nameLocks(session, artifacts, metadatas).stream()
84 .map(s -> discriminator + ":" + s)
85 .collect(toList());
86 }
87
88 private String getHostname() {
89 try {
90 return InetAddress.getLocalHost().getHostName();
91 } catch (UnknownHostException e) {
92 LOGGER.warn("Failed to get hostname, using '{}'", DEFAULT_HOSTNAME, e);
93 return DEFAULT_HOSTNAME;
94 }
95 }
96
97 private String createDiscriminator(final RepositorySystemSession session) {
98 String discriminator = ConfigUtils.getString(session, null, CONFIG_PROP_DISCRIMINATOR);
99
100 if (discriminator == null || discriminator.isEmpty()) {
101 String hostname = ConfigUtils.getString(session, this.hostname, CONFIG_PROP_HOSTNAME);
102 File basedir = session.getLocalRepository().getBasedir();
103 discriminator = hostname + ":" + basedir;
104 try {
105 return StringDigestUtil.sha1(discriminator);
106 } catch (Exception e) {
107 LOGGER.warn("Failed to calculate discriminator digest, using '{}'", DEFAULT_DISCRIMINATOR_DIGEST, e);
108 return DEFAULT_DISCRIMINATOR_DIGEST;
109 }
110 }
111 return discriminator;
112 }
113 }