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.util;
020
021import java.io.IOException;
022import java.nio.file.Files;
023import java.nio.file.Path;
024import java.nio.file.Paths;
025
026import org.eclipse.aether.RepositorySystemSession;
027import org.eclipse.aether.repository.LocalRepository;
028
029import static java.util.Objects.requireNonNull;
030
031/**
032 * A utility class to calculate (and create if needed) paths backed by directories using configuration properties from
033 * repository system session and others.
034 *
035 * @see RepositorySystemSession#getConfigProperties()
036 * @see RepositorySystemSession#getLocalRepository()
037 * @since 1.9.0
038 */
039public final class DirectoryUtils {
040    private DirectoryUtils() {
041        // hide constructor
042    }
043
044    /**
045     * Creates {@link Path} instance out of passed in {@code name} parameter. May create a directory on resulting path,
046     * if not exist, when invoked with {@code mayCreate} being {@code true}. Never returns {@code null}.
047     * <p>
048     * Following outcomes may happen:
049     * <ul>
050     *     <li>{@code name} is absolute path - results in {@link Path} instance created directly from name.</li>
051     *     <li>{@code name} is relative path - results in {@link Path} instance resolved against {@code base} parameter.
052     *     </li>
053     * </ul>
054     * Resulting path is being checked is a directory, and if not, it will be created if {@code mayCreate} is
055     * {@code true}. If resulting path exist but is not a directory, this method will throw.
056     *
057     * @param name      The name to create directory with, cannot be {@code null}.
058     * @param base      The base {@link Path} to resolve name, if it is relative path, cannot be {@code null}.
059     * @param mayCreate If resulting path does not exist, should it create?
060     * @return The {@link Path} instance that is resolved and backed by existing directory.
061     * @throws IOException If some IO related errors happens.
062     */
063    public static Path resolveDirectory(String name, Path base, boolean mayCreate) throws IOException {
064        requireNonNull(name, "name is null");
065        requireNonNull(base, "base is null");
066        final Path namePath = Paths.get(name);
067        final Path result;
068        if (namePath.isAbsolute()) {
069            result = namePath.normalize();
070        } else {
071            result = base.resolve(name).normalize();
072        }
073
074        if (!Files.exists(result)) {
075            if (mayCreate) {
076                Files.createDirectories(result);
077            }
078        } else if (!Files.isDirectory(result)) {
079            throw new IOException("Path exists, but is not a directory: " + result);
080        }
081        return result;
082    }
083
084    /**
085     * Creates {@link Path} instance out of session configuration, and (if relative) resolve it against local
086     * repository basedir. Pre-populates values and invokes {@link #resolveDirectory(String, Path, boolean)}.
087     * <p>
088     * For this method to work, {@link LocalRepository#getBasePath()} must return
089     * non-{@code null} value, otherwise {@link NullPointerException} is thrown.
090     *
091     * @param session     The session, may not be {@code null}.
092     * @param defaultName The default value if not present in session configuration, may not be {@code null}.
093     * @param nameKey     The key to look up for in session configuration to obtain user set value.
094     * @param mayCreate   If resulting path does not exist, should it create?
095     * @return The {@link Path} instance that is resolved and backed by existing directory.
096     * @throws IOException If some IO related errors happens.
097     * @see #resolveDirectory(String, Path, boolean)
098     */
099    public static Path resolveDirectory(
100            RepositorySystemSession session, String defaultName, String nameKey, boolean mayCreate) throws IOException {
101        requireNonNull(session, "session is null");
102        requireNonNull(defaultName, "defaultName is null");
103        requireNonNull(nameKey, "nameKey is null");
104        requireNonNull(session.getLocalRepository().getBasePath(), "session.localRepository.basePath is null");
105        return resolveDirectory(
106                ConfigUtils.getString(session, defaultName, nameKey),
107                session.getLocalRepository().getBasePath(),
108                mayCreate);
109    }
110}