1 package org.eclipse.aether.internal.impl.synccontext.named;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.eclipse.aether.RepositorySystemSession;
23 import org.eclipse.aether.artifact.Artifact;
24 import org.eclipse.aether.metadata.Metadata;
25 import org.eclipse.aether.util.ConfigUtils;
26 import org.eclipse.aether.util.StringDigestUtil;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import java.io.File;
31 import java.net.InetAddress;
32 import java.net.UnknownHostException;
33 import java.util.Collection;
34 import java.util.Objects;
35
36 import static java.util.stream.Collectors.toList;
37
38
39
40
41
42
43
44
45
46
47 public class DiscriminatingNameMapper implements NameMapper
48 {
49
50
51
52 private static final String CONFIG_PROP_DISCRIMINATOR = "aether.syncContext.named.discriminating.discriminator";
53
54
55
56
57 private static final String CONFIG_PROP_HOSTNAME = "aether.syncContext.named.discriminating.hostname";
58
59 private static final String DEFAULT_DISCRIMINATOR_DIGEST = "da39a3ee5e6b4b0d3255bfef95601890afd80709";
60
61 private static final String DEFAULT_HOSTNAME = "localhost";
62
63 private static final Logger LOGGER = LoggerFactory.getLogger( DiscriminatingNameMapper.class );
64
65 private final NameMapper delegate;
66
67 private final String hostname;
68
69 public DiscriminatingNameMapper( final NameMapper delegate )
70 {
71 this.delegate = Objects.requireNonNull( delegate );
72 this.hostname = getHostname();
73 }
74
75 @Override
76 public boolean isFileSystemFriendly()
77 {
78 return false;
79 }
80
81 @Override
82 public Collection<String> nameLocks( final RepositorySystemSession session,
83 final Collection<? extends Artifact> artifacts,
84 final Collection<? extends Metadata> metadatas )
85 {
86 String discriminator = createDiscriminator( session );
87 return delegate.nameLocks( session, artifacts, metadatas ).stream()
88 .map( s -> discriminator + ":" + s )
89 .collect( toList() );
90 }
91
92 private String getHostname()
93 {
94 try
95 {
96 return InetAddress.getLocalHost().getHostName();
97 }
98 catch ( UnknownHostException e )
99 {
100 LOGGER.warn( "Failed to get hostname, using '{}'", DEFAULT_HOSTNAME, e );
101 return DEFAULT_HOSTNAME;
102 }
103 }
104
105 private String createDiscriminator( final RepositorySystemSession session )
106 {
107 String discriminator = ConfigUtils.getString( session, null, CONFIG_PROP_DISCRIMINATOR );
108
109 if ( discriminator == null || discriminator.isEmpty() )
110 {
111 String hostname = ConfigUtils.getString( session, this.hostname, CONFIG_PROP_HOSTNAME );
112 File basedir = session.getLocalRepository().getBasedir();
113 discriminator = hostname + ":" + basedir;
114 try
115 {
116 return StringDigestUtil.sha1( discriminator );
117 }
118 catch ( Exception e )
119 {
120 LOGGER.warn( "Failed to calculate discriminator digest, using '{}'", DEFAULT_DISCRIMINATOR_DIGEST, e );
121 return DEFAULT_DISCRIMINATOR_DIGEST;
122 }
123 }
124 return discriminator;
125 }
126 }