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.named.support.FileSystemFriendly;
26
27 import javax.inject.Named;
28 import javax.inject.Singleton;
29
30 import java.io.File;
31 import java.io.IOException;
32 import java.io.UncheckedIOException;
33 import java.nio.file.Path;
34 import java.util.Collection;
35 import java.util.TreeSet;
36 import java.util.concurrent.ConcurrentHashMap;
37 import java.util.concurrent.ConcurrentMap;
38
39
40
41
42
43
44
45
46
47 @Singleton
48 @Named( FileGAVNameMapper.NAME )
49 public class FileGAVNameMapper
50 implements NameMapper, FileSystemFriendly
51 {
52 public static final String NAME = "file-gav";
53
54 private static final String LOCK_SUFFIX = ".resolverlock";
55
56 private static final char SEPARATOR = '~';
57
58 private final ConcurrentMap<String, Path> baseDirs;
59
60 public FileGAVNameMapper()
61 {
62 this.baseDirs = new ConcurrentHashMap<>();
63 }
64
65 @Override
66 public TreeSet<String> nameLocks( final RepositorySystemSession session,
67 final Collection<? extends Artifact> artifacts,
68 final Collection<? extends Metadata> metadatas )
69 {
70 File localRepositoryBasedir = session.getLocalRepository().getBasedir();
71
72 Path baseDir = baseDirs.computeIfAbsent(
73 localRepositoryBasedir.getPath(), k ->
74 {
75 try
76 {
77 return new File( localRepositoryBasedir, ".locks" ).getCanonicalFile().toPath();
78 }
79 catch ( IOException e )
80 {
81 throw new UncheckedIOException( e );
82 }
83 }
84 );
85
86 TreeSet<String> paths = new TreeSet<>();
87 if ( artifacts != null )
88 {
89 for ( Artifact artifact : artifacts )
90 {
91 paths.add( getPath( baseDir, artifact ) + LOCK_SUFFIX );
92 }
93 }
94 if ( metadatas != null )
95 {
96 for ( Metadata metadata : metadatas )
97 {
98 paths.add( getPath( baseDir, metadata ) + LOCK_SUFFIX );
99 }
100 }
101 return paths;
102 }
103
104 private String getPath( final Path baseDir, final Artifact artifact )
105 {
106
107 String path = artifact.getGroupId()
108 + SEPARATOR + artifact.getArtifactId()
109 + SEPARATOR + artifact.getBaseVersion();
110 return baseDir.resolve( path ).toAbsolutePath().toString();
111 }
112
113 private String getPath( final Path baseDir, final Metadata metadata )
114 {
115
116 String path = "";
117 if ( metadata.getGroupId().length() > 0 )
118 {
119 path += metadata.getGroupId();
120 if ( metadata.getArtifactId().length() > 0 )
121 {
122 path += SEPARATOR + metadata.getArtifactId();
123 if ( metadata.getVersion().length() > 0 )
124 {
125 path += SEPARATOR + metadata.getVersion();
126 }
127 }
128 }
129 return baseDir.resolve( path ).toAbsolutePath().toString();
130 }
131 }