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.util.Collection;
22 import java.util.Comparator;
23 import java.util.TreeSet;
24
25 import org.eclipse.aether.RepositorySystemSession;
26 import org.eclipse.aether.artifact.Artifact;
27 import org.eclipse.aether.metadata.Metadata;
28 import org.eclipse.aether.named.NamedLockKey;
29
30 import static java.util.Objects.requireNonNull;
31
32
33
34
35
36
37 public class GAVNameMapper implements NameMapper {
38 private final boolean fileSystemFriendly;
39
40 private final String artifactPrefix;
41
42 private final String artifactSuffix;
43
44 private final String metadataPrefix;
45
46 private final String metadataSuffix;
47
48 private final String fieldSeparator;
49
50 public GAVNameMapper(
51 boolean fileSystemFriendly,
52 String artifactPrefix,
53 String artifactSuffix,
54 String metadataPrefix,
55 String metadataSuffix,
56 String fieldSeparator) {
57 this.fileSystemFriendly = fileSystemFriendly;
58 this.artifactPrefix = requireNonNull(artifactPrefix);
59 this.artifactSuffix = requireNonNull(artifactSuffix);
60 this.metadataPrefix = requireNonNull(metadataPrefix);
61 this.metadataSuffix = requireNonNull(metadataSuffix);
62 this.fieldSeparator = requireNonNull(fieldSeparator);
63 }
64
65 @Override
66 public boolean isFileSystemFriendly() {
67 return fileSystemFriendly;
68 }
69
70 @Override
71 public Collection<NamedLockKey> nameLocks(
72 final RepositorySystemSession session,
73 final Collection<? extends Artifact> artifacts,
74 final Collection<? extends Metadata> metadatas) {
75
76
77 TreeSet<NamedLockKey> keys = new TreeSet<>(Comparator.comparing(NamedLockKey::name));
78 if (artifacts != null) {
79 for (Artifact artifact : artifacts) {
80 keys.add(NamedLockKey.of(
81 getArtifactName(artifact, artifactPrefix, fieldSeparator, artifactSuffix),
82 getArtifactName(artifact, "", ":", "")));
83 }
84 }
85
86 if (metadatas != null) {
87 for (Metadata metadata : metadatas) {
88 keys.add(NamedLockKey.of(
89 getMetadataName(metadata, metadataPrefix, fieldSeparator, metadataSuffix),
90 getMetadataName(metadata, "", ":", "")));
91 }
92 }
93 return keys;
94 }
95
96 private static String getArtifactName(Artifact artifact, String prefix, String separator, String suffix) {
97 return prefix
98 + artifact.getGroupId()
99 + separator
100 + artifact.getArtifactId()
101 + separator
102 + artifact.getBaseVersion()
103 + suffix;
104 }
105
106 private static String getMetadataName(Metadata metadata, String prefix, String separator, String suffix) {
107 String name = prefix;
108 if (!metadata.getGroupId().isEmpty()) {
109 name += metadata.getGroupId();
110 if (!metadata.getArtifactId().isEmpty()) {
111 name += separator + metadata.getArtifactId();
112 if (!metadata.getVersion().isEmpty()) {
113 name += separator + metadata.getVersion();
114 }
115 }
116 }
117 return name + suffix;
118 }
119
120 public static NameMapper gav() {
121 return new GAVNameMapper(false, "artifact:", "", "metadata:", "", ":");
122 }
123
124 public static NameMapper fileGav() {
125 return new GAVNameMapper(true, "artifact~", ".lock", "metadata~", ".lock", "~");
126 }
127 }