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