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    /**
043     * The location of the directory toi use for locks. If relative path, it is resolved from the local repository root.
044     *
045     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
046     * @configurationType {@link java.lang.String}
047     * @configurationDefaultValue {@link #DEFAULT_LOCKS_DIR}
048     */
049    public static final String CONFIG_PROP_LOCKS_DIR = NamedLockFactoryAdapter.CONFIG_PROPS_PREFIX + "basedir.locksDir";
050
051    public static final String DEFAULT_LOCKS_DIR = ".locks";
052
053    private final NameMapper delegate;
054
055    public BasedirNameMapper(final NameMapper delegate) {
056        this.delegate = requireNonNull(delegate);
057    }
058
059    @Override
060    public boolean isFileSystemFriendly() {
061        return delegate.isFileSystemFriendly();
062    }
063
064    @Override
065    public Collection<String> nameLocks(
066            final RepositorySystemSession session,
067            final Collection<? extends Artifact> artifacts,
068            final Collection<? extends Metadata> metadatas) {
069        try {
070            final Path basedir =
071                    DirectoryUtils.resolveDirectory(session, DEFAULT_LOCKS_DIR, CONFIG_PROP_LOCKS_DIR, false);
072
073            return delegate.nameLocks(session, artifacts, metadatas).stream()
074                    .map(name -> basedir.resolve(name).toAbsolutePath().toString())
075                    .collect(Collectors.toList());
076        } catch (IOException e) {
077            throw new UncheckedIOException(e);
078        }
079    }
080}