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  import org.eclipse.aether.util.PathUtils;
30  
31  import static java.util.Objects.requireNonNull;
32  
33  /**
34   * Artifact GAV {@link NameMapper}, uses artifact and metadata coordinates to name their corresponding locks. Is not
35   * considering local repository, only the artifact coordinates. May use custom prefixes and suffixes and separators,
36   * hence this instance may or may not be filesystem friendly (depends on strings used).
37   */
38  public class GAVNameMapper implements NameMapper {
39      private final boolean fileSystemFriendly;
40  
41      private final String artifactPrefix;
42  
43      private final String artifactSuffix;
44  
45      private final String metadataPrefix;
46  
47      private final String metadataSuffix;
48  
49      private final String fieldSeparator;
50  
51      public GAVNameMapper(
52              boolean fileSystemFriendly,
53              String artifactPrefix,
54              String artifactSuffix,
55              String metadataPrefix,
56              String metadataSuffix,
57              String fieldSeparator) {
58          this.fileSystemFriendly = fileSystemFriendly;
59          this.artifactPrefix = requireNonNull(artifactPrefix);
60          this.artifactSuffix = requireNonNull(artifactSuffix);
61          this.metadataPrefix = requireNonNull(metadataPrefix);
62          this.metadataSuffix = requireNonNull(metadataSuffix);
63          this.fieldSeparator = requireNonNull(fieldSeparator);
64      }
65  
66      @Override
67      public boolean isFileSystemFriendly() {
68          return fileSystemFriendly;
69      }
70  
71      @Override
72      public Collection<NamedLockKey> nameLocks(
73              final RepositorySystemSession session,
74              final Collection<? extends Artifact> artifacts,
75              final Collection<? extends Metadata> metadatas) {
76          // Deadlock prevention: https://stackoverflow.com/a/16780988/696632
77          // We must acquire multiple locks always in the same order!
78          TreeSet<NamedLockKey> keys = new TreeSet<>(Comparator.comparing(NamedLockKey::name));
79          if (artifacts != null) {
80              for (Artifact artifact : artifacts) {
81                  keys.add(NamedLockKey.of(
82                          getArtifactName(artifact, artifactPrefix, fieldSeparator, artifactSuffix),
83                          getArtifactName(artifact, "", ":", "")));
84              }
85          }
86  
87          if (metadatas != null) {
88              for (Metadata metadata : metadatas) {
89                  keys.add(NamedLockKey.of(
90                          getMetadataName(metadata, metadataPrefix, fieldSeparator, metadataSuffix),
91                          getMetadataName(metadata, "", ":", "")));
92              }
93          }
94          return keys;
95      }
96  
97      private static String getArtifactName(Artifact artifact, String prefix, String separator, String suffix) {
98          return prefix
99                  + artifact.getGroupId()
100                 + separator
101                 + artifact.getArtifactId()
102                 + separator
103                 + artifact.getBaseVersion()
104                 + suffix;
105     }
106 
107     private static final String MAVEN_METADATA = "maven-metadata.xml";
108 
109     private static String getMetadataName(Metadata metadata, String prefix, String separator, String suffix) {
110         String name = prefix;
111         if (!metadata.getGroupId().isEmpty()) {
112             name += metadata.getGroupId();
113             if (!metadata.getArtifactId().isEmpty()) {
114                 name += separator + metadata.getArtifactId();
115                 if (!metadata.getVersion().isEmpty()) {
116                     name += separator + metadata.getVersion();
117                 }
118             }
119             if (!MAVEN_METADATA.equals(metadata.getType())) {
120                 name += separator + PathUtils.stringToPathSegment(metadata.getType());
121             }
122         } else {
123             if (!MAVEN_METADATA.equals(metadata.getType())) {
124                 name += PathUtils.stringToPathSegment(metadata.getType());
125             }
126         }
127         return name + suffix;
128     }
129 
130     public static NameMapper gav() {
131         return new GAVNameMapper(false, "artifact:", "", "metadata:", "", ":");
132     }
133 
134     public static NameMapper fileGav() {
135         return new GAVNameMapper(true, "artifact~", ".lock", "metadata~", ".lock", "~");
136     }
137 }