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