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.named.NamedLockKey;
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     * The location of the directory toi use for locks. If relative path, it is resolved from the local repository root.
045     *
046     * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
047     * @configurationType {@link java.lang.String}
048     * @configurationDefaultValue {@link #DEFAULT_LOCKS_DIR}
049     */
050    public static final String CONFIG_PROP_LOCKS_DIR = NamedLockFactoryAdapter.CONFIG_PROPS_PREFIX + "basedir.locksDir";
051
052    public static final String DEFAULT_LOCKS_DIR = ".locks";
053
054    private final NameMapper delegate;
055
056    private final Path basePath;
057
058    public BasedirNameMapper(final NameMapper delegate) {
059        this(delegate, null);
060    }
061
062    /**
063     * Creates basedir name mapper with provided path as base.
064     *
065     * @param delegate The delegate to resolve against basedir, must not be {@code null}. The delegate must be
066     *                 "file system friendly", see {@link NameMapper#isFileSystemFriendly()} method.
067     * @param path The basedir, may be {@code null} in which case given session local repository root is used as
068     *             basedir.
069     * @since 2.0.0
070     */
071    public BasedirNameMapper(final NameMapper delegate, final Path path) {
072        requireNonNull(delegate);
073        if (!delegate.isFileSystemFriendly()) {
074            throw new IllegalArgumentException("delegate must be file-system friendly");
075        }
076        this.delegate = delegate;
077        this.basePath = path;
078    }
079
080    @Override
081    public boolean isFileSystemFriendly() {
082        return true; // it enforces delegate to be friendly, and itself produces friendly names
083    }
084
085    @Override
086    public Collection<NamedLockKey> nameLocks(
087            final RepositorySystemSession session,
088            final Collection<? extends Artifact> artifacts,
089            final Collection<? extends Metadata> metadatas) {
090        try {
091            final Path basedir = basePath != null
092                    ? basePath
093                    : DirectoryUtils.resolveDirectory(session, DEFAULT_LOCKS_DIR, CONFIG_PROP_LOCKS_DIR, false);
094
095            return delegate.nameLocks(session, artifacts, metadatas).stream()
096                    .map(k -> NamedLockKey.of(
097                            basedir.resolve(k.name()).toAbsolutePath().toUri().toASCIIString(), k.resources()))
098                    .collect(Collectors.toList());
099        } catch (IOException e) {
100            throw new UncheckedIOException(e);
101        }
102    }
103}