1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.eclipse.aether.util;
20
21 import java.io.IOException;
22 import java.nio.file.Files;
23 import java.nio.file.Path;
24 import java.nio.file.Paths;
25
26 import org.eclipse.aether.RepositorySystemSession;
27 import org.eclipse.aether.repository.LocalRepository;
28
29 import static java.util.Objects.requireNonNull;
30
31 /**
32 * A utility class to calculate (and create if needed) paths backed by directories using configuration properties from
33 * repository system session and others.
34 *
35 * @see RepositorySystemSession#getConfigProperties()
36 * @see RepositorySystemSession#getLocalRepository()
37 * @since 1.9.0
38 */
39 public final class DirectoryUtils {
40 private DirectoryUtils() {
41 // hide constructor
42 }
43
44 /**
45 * Creates {@link Path} instance out of passed in {@code name} parameter. May create a directory on resulting path,
46 * if not exist, when invoked with {@code mayCreate} being {@code true}. Never returns {@code null}.
47 * <p>
48 * Following outcomes may happen:
49 * <ul>
50 * <li>{@code name} is absolute path - results in {@link Path} instance created directly from name.</li>
51 * <li>{@code name} is relative path - results in {@link Path} instance resolved against {@code base} parameter.
52 * </li>
53 * </ul>
54 * Resulting path is being checked is a directory, and if not, it will be created if {@code mayCreate} is
55 * {@code true}. If resulting path exist but is not a directory, this method will throw.
56 *
57 * @param name the name to create directory with, cannot be {@code null}
58 * @param base the base {@link Path} to resolve name, if it is relative path, cannot be {@code null}
59 * @param mayCreate if resulting path does not exist, should it create?
60 * @return the {@link Path} instance that is resolved and backed by existing directory
61 * @throws IOException if some IO related errors happens
62 */
63 public static Path resolveDirectory(String name, Path base, boolean mayCreate) throws IOException {
64 requireNonNull(name, "name is null");
65 requireNonNull(base, "base is null");
66 final Path namePath = Paths.get(name);
67 final Path result;
68 if (namePath.isAbsolute()) {
69 result = namePath.normalize();
70 } else {
71 result = base.resolve(name).normalize();
72 }
73
74 if (!Files.exists(result)) {
75 if (mayCreate) {
76 Files.createDirectories(result);
77 }
78 } else if (!Files.isDirectory(result)) {
79 throw new IOException("Path exists, but is not a directory: " + result);
80 }
81 return result;
82 }
83
84 /**
85 * Creates {@link Path} instance out of session configuration, and (if relative) resolve it against local
86 * repository basedir. Pre-populates values and invokes {@link #resolveDirectory(String, Path, boolean)}.
87 * <p>
88 * For this method to work, {@link LocalRepository#getBasePath()} must return
89 * non-{@code null} value, otherwise {@link NullPointerException} is thrown.
90 *
91 * @param session the session, may not be {@code null}
92 * @param defaultName the default value if not present in session configuration, may not be {@code null}
93 * @param nameKey the key to look up for in session configuration to obtain user set value
94 * @param mayCreate if resulting path does not exist, should it create?
95 * @return the {@link Path} instance that is resolved and backed by existing directory
96 * @throws IOException if some IO related errors happens
97 * @see #resolveDirectory(String, Path, boolean)
98 */
99 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 }