001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.internal.impl.synccontext.named;
020
021import java.io.IOException;
022import java.io.UncheckedIOException;
023import java.nio.file.Path;
024import java.util.Collection;
025import java.util.stream.Collectors;
026
027import org.eclipse.aether.RepositorySystemSession;
028import org.eclipse.aether.artifact.Artifact;
029import org.eclipse.aether.metadata.Metadata;
030import org.eclipse.aether.util.DirectoryUtils;
031
032import static java.util.Objects.requireNonNull;
033
034/**
035 * Wrapping {@link NameMapper} class that is file system friendly: it wraps another
036 * {@link NameMapper} and resolves the resulting "file system friendly" names against local
037 * repository basedir.
038 *
039 * @since 1.9.0
040 */
041public class BasedirNameMapper implements NameMapper {
042    private static final String CONFIG_PROP_LOCKS_DIR = "aether.syncContext.named.basedir.locksDir";
043
044    private final NameMapper delegate;
045
046    public BasedirNameMapper(final NameMapper delegate) {
047        this.delegate = requireNonNull(delegate);
048    }
049
050    @Override
051    public boolean isFileSystemFriendly() {
052        return delegate.isFileSystemFriendly();
053    }
054
055    @Override
056    public Collection<String> nameLocks(
057            final RepositorySystemSession session,
058            final Collection<? extends Artifact> artifacts,
059            final Collection<? extends Metadata> metadatas) {
060        try {
061            final Path basedir = DirectoryUtils.resolveDirectory(session, ".locks", CONFIG_PROP_LOCKS_DIR, false);
062
063            return delegate.nameLocks(session, artifacts, metadatas).stream()
064                    .map(name -> basedir.resolve(name).toAbsolutePath().toString())
065                    .collect(Collectors.toList());
066        } catch (IOException e) {
067            throw new UncheckedIOException(e);
068        }
069    }
070}