001package org.eclipse.aether.internal.impl.synccontext.named;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.eclipse.aether.RepositorySystemSession;
023import org.eclipse.aether.artifact.Artifact;
024import org.eclipse.aether.metadata.Metadata;
025
026import javax.inject.Named;
027import javax.inject.Singleton;
028import java.util.Collection;
029import java.util.TreeSet;
030
031/**
032 * Artifact GAV {@link NameMapper}, uses artifact and metadata coordinates to name their corresponding locks. Is not
033 * considering local repository, only the artifact coordinates.
034 */
035@Singleton
036@Named( GAVNameMapper.NAME )
037public class GAVNameMapper implements NameMapper
038{
039    public static final String NAME = "gav";
040
041    @Override
042    public Collection<String> nameLocks( final RepositorySystemSession session,
043                                         final Collection<? extends Artifact> artifacts,
044                                         final Collection<? extends Metadata> metadatas )
045    {
046        // Deadlock prevention: https://stackoverflow.com/a/16780988/696632
047        // We must acquire multiple locks always in the same order!
048        Collection<String> keys = new TreeSet<>();
049        if ( artifacts != null )
050        {
051            for ( Artifact artifact : artifacts )
052            {
053                String key = "artifact:" + artifact.getGroupId()
054                             + ":" + artifact.getArtifactId()
055                             + ":" + artifact.getBaseVersion();
056                keys.add( key );
057            }
058        }
059
060        if ( metadatas != null )
061        {
062            for ( Metadata metadata : metadatas )
063            {
064                StringBuilder key = new StringBuilder( "metadata:" );
065                if ( !metadata.getGroupId().isEmpty() )
066                {
067                    key.append( metadata.getGroupId() );
068                    if ( !metadata.getArtifactId().isEmpty() )
069                    {
070                        key.append( ':' ).append( metadata.getArtifactId() );
071                        if ( !metadata.getVersion().isEmpty() )
072                        {
073                            key.append( ':' ).append( metadata.getVersion() );
074                        }
075                    }
076                }
077                keys.add( key.toString() );
078            }
079        }
080        return keys;
081    }
082}