View Javadoc
1   package org.eclipse.aether.internal.impl.synccontext.named;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.eclipse.aether.RepositorySystemSession;
23  import org.eclipse.aether.artifact.Artifact;
24  import org.eclipse.aether.metadata.Metadata;
25  
26  import javax.inject.Named;
27  import javax.inject.Singleton;
28  import java.util.Collection;
29  import java.util.TreeSet;
30  
31  /**
32   * Artifact GAV {@link NameMapper}, uses artifact and metadata coordinates to name their corresponding locks. Is not
33   * considering local repository, only the artifact coordinates.
34   */
35  @Singleton
36  @Named( GAVNameMapper.NAME )
37  public class GAVNameMapper implements NameMapper
38  {
39      public static final String NAME = "gav";
40  
41      @Override
42      public Collection<String> nameLocks( final RepositorySystemSession session,
43                                           final Collection<? extends Artifact> artifacts,
44                                           final Collection<? extends Metadata> metadatas )
45      {
46          // Deadlock prevention: https://stackoverflow.com/a/16780988/696632
47          // We must acquire multiple locks always in the same order!
48          Collection<String> keys = new TreeSet<>();
49          if ( artifacts != null )
50          {
51              for ( Artifact artifact : artifacts )
52              {
53                  String key = "artifact:" + artifact.getGroupId()
54                               + ":" + artifact.getArtifactId()
55                               + ":" + artifact.getBaseVersion();
56                  keys.add( key );
57              }
58          }
59  
60          if ( metadatas != null )
61          {
62              for ( Metadata metadata : metadatas )
63              {
64                  StringBuilder key = new StringBuilder( "metadata:" );
65                  if ( !metadata.getGroupId().isEmpty() )
66                  {
67                      key.append( metadata.getGroupId() );
68                      if ( !metadata.getArtifactId().isEmpty() )
69                      {
70                          key.append( ':' ).append( metadata.getArtifactId() );
71                          if ( !metadata.getVersion().isEmpty() )
72                          {
73                              key.append( ':' ).append( metadata.getVersion() );
74                          }
75                      }
76                  }
77                  keys.add( key.toString() );
78              }
79          }
80          return keys;
81      }
82  }