View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * Artifact GAV {@link NameMapper}, uses artifact and metadata coordinates to name their corresponding locks. Is not
34   * considering local repository, only the artifact coordinates. May use custom prefixes and suffixes and separators,
35   * hence this instance may or may not be filesystem friendly (depends on strings used).
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          // Deadlock prevention: https://stackoverflow.com/a/16780988/696632
76          // We must acquire multiple locks always in the same order!
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 }