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 java.io.IOException;
023import java.io.UncheckedIOException;
024import java.nio.file.Path;
025import java.util.Collection;
026import java.util.stream.Collectors;
027
028import org.eclipse.aether.RepositorySystemSession;
029import org.eclipse.aether.artifact.Artifact;
030import org.eclipse.aether.metadata.Metadata;
031import org.eclipse.aether.util.DirectoryUtils;
032
033import static java.util.Objects.requireNonNull;
034
035/**
036 * Wrapping {@link NameMapper} class that is file system friendly: it wraps another
037 * {@link NameMapper} and resolves the resulting "file system friendly" names against local
038 * repository basedir.
039 *
040 * @since 1.9.0
041 */
042public class BasedirNameMapper implements NameMapper
043{
044    private static final String CONFIG_PROP_LOCKS_DIR = "aether.syncContext.named.basedir.locksDir";
045
046    private final NameMapper delegate;
047
048    public BasedirNameMapper( final NameMapper delegate )
049    {
050        this.delegate = requireNonNull( delegate );
051    }
052
053    @Override
054    public boolean isFileSystemFriendly()
055    {
056        return delegate.isFileSystemFriendly();
057    }
058
059    @Override
060    public Collection<String> nameLocks( final RepositorySystemSession session,
061                                         final Collection<? extends Artifact> artifacts,
062                                         final Collection<? extends Metadata> metadatas )
063    {
064        try
065        {
066            final Path basedir = DirectoryUtils.resolveDirectory(
067                    session, ".locks", CONFIG_PROP_LOCKS_DIR, false );
068
069            return delegate.nameLocks( session, artifacts, metadatas ).stream()
070                    .map( name -> basedir.resolve( name ).toAbsolutePath().toString() )
071                    .collect( Collectors.toList() );
072        }
073        catch ( IOException e )
074        {
075            throw new UncheckedIOException( e );
076        }
077    }
078}