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.ChecksumUtils;
26 import org.eclipse.aether.util.ConfigUtils;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import javax.inject.Singleton;
33 import java.io.File;
34 import java.net.InetAddress;
35 import java.net.UnknownHostException;
36 import java.nio.charset.StandardCharsets;
37 import java.util.Collection;
38 import java.util.Collections;
39 import java.util.Map;
40 import java.util.Objects;
41
42 import static java.util.stream.Collectors.toList;
43
44
45
46
47
48
49
50
51
52
53 @Singleton
54 @Named( DiscriminatingNameMapper.NAME )
55 public class DiscriminatingNameMapper implements NameMapper
56 {
57 public static final String NAME = "discriminating";
58
59
60
61
62 private static final String CONFIG_PROP_DISCRIMINATOR = "aether.syncContext.named.discriminating.discriminator";
63
64
65
66
67 private static final String CONFIG_PROP_HOSTNAME = "aether.syncContext.named.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 nameMapper;
76
77 private final String hostname;
78
79 @Inject
80 public DiscriminatingNameMapper( @Named( GAVNameMapper.NAME ) final NameMapper nameMapper )
81 {
82 this.nameMapper = Objects.requireNonNull( nameMapper );
83 this.hostname = getHostname();
84 }
85
86 @Override
87 public Collection<String> nameLocks( final RepositorySystemSession session,
88 final Collection<? extends Artifact> artifacts,
89 final Collection<? extends Metadata> metadatas )
90 {
91 String discriminator = createDiscriminator( session );
92 return nameMapper.nameLocks( session, artifacts, metadatas ).stream().map( s -> discriminator + ":" + s )
93 .collect( toList() );
94 }
95
96 private String getHostname()
97 {
98 try
99 {
100 return InetAddress.getLocalHost().getHostName();
101 }
102 catch ( UnknownHostException e )
103 {
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 {
111 String discriminator = ConfigUtils.getString( session, null, CONFIG_PROP_DISCRIMINATOR );
112
113 if ( discriminator == null || discriminator.isEmpty() )
114 {
115 String hostname = ConfigUtils.getString( session, this.hostname, CONFIG_PROP_HOSTNAME );
116 File basedir = session.getLocalRepository().getBasedir();
117 discriminator = hostname + ":" + basedir;
118 try
119 {
120 Map<String, Object> checksums = ChecksumUtils
121 .calc( discriminator.getBytes( StandardCharsets.UTF_8 ), Collections.singletonList( "SHA-1" ) );
122 Object checksum = checksums.get( "SHA-1" );
123
124 if ( checksum instanceof Exception )
125 {
126 throw (Exception) checksum;
127 }
128
129 return String.valueOf( checksum );
130 }
131 catch ( Exception e )
132 {
133 LOGGER.warn( "Failed to calculate discriminator digest, using '{}'", DEFAULT_DISCRIMINATOR_DIGEST, e );
134 return DEFAULT_DISCRIMINATOR_DIGEST;
135 }
136 }
137 return discriminator;
138 }
139 }