View Javadoc
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  
28  import static java.util.Objects.requireNonNull;
29  
30  /**
31   * A utility class to calculate (and create if needed) paths backed by directories using configuration properties from
32   * repository system session and others.
33   *
34   * @see RepositorySystemSession#getConfigProperties()
35   * @see RepositorySystemSession#getLocalRepository()
36   * @since 1.9.0
37   */
38  public final class DirectoryUtils {
39      private DirectoryUtils() {
40          // hide constructor
41      }
42  
43      /**
44       * Creates {@link Path} instance out of passed in {@code name} parameter. May create a directory on resulting path,
45       * if not exist, when invoked with {@code mayCreate} being {@code true}. Never returns {@code null}.
46       * <p>
47       * Following outcomes may happen:
48       * <ul>
49       *     <li>{@code name} is absolute path - results in {@link Path} instance created directly from name.</li>
50       *     <li>{@code name} is relative path - results in {@link Path} instance resolved against {@code base} parameter.
51       *     </li>
52       * </ul>
53       * Resulting path is being checked is a directory, and if not, it will be created if {@code mayCreate} is
54       * {@code true}. If resulting path exist but is not a directory, this method will throw.
55       *
56       * @param name      The name to create directory with, cannot be {@code null}.
57       * @param base      The base {@link Path} to resolve name, if it is relative path, cannot be {@code null}.
58       * @param mayCreate If resulting path does not exist, should it create?
59       * @return The {@link Path} instance that is resolved and backed by existing directory.
60       * @throws IOException If some IO related errors happens.
61       */
62      public static Path resolveDirectory(String name, Path base, boolean mayCreate) throws IOException {
63          requireNonNull(name, "name is null");
64          requireNonNull(base, "base is null");
65          final Path namePath = Paths.get(name);
66          final Path result;
67          if (namePath.isAbsolute()) {
68              result = namePath.normalize();
69          } else {
70              result = base.resolve(namePath).normalize();
71          }
72  
73          if (!Files.exists(result)) {
74              if (mayCreate) {
75                  Files.createDirectories(result);
76              }
77          } else if (!Files.isDirectory(result)) {
78              throw new IOException("Path exists, but is not a directory: " + result);
79          }
80          return result;
81      }
82  
83      /**
84       * Creates {@link Path} instance out of session configuration, and (if relative) resolve it against local
85       * repository basedir. Pre-populates values and invokes {@link #resolveDirectory(String, Path, boolean)}.
86       * <p>
87       * For this method to work, {@link org.eclipse.aether.repository.LocalRepository#getBasedir()} must return
88       * non-{@code null} value, otherwise {@link NullPointerException} is thrown.
89       *
90       * @param session     The session, may not be {@code null}.
91       * @param defaultName The default value if not present in session configuration, may not be {@code null}.
92       * @param nameKey     The key to look up for in session configuration to obtain user set value.
93       * @param mayCreate   If resulting path does not exist, should it create?
94       * @return The {@link Path} instance that is resolved and backed by existing directory.
95       * @throws IOException If some IO related errors happens.
96       * @see #resolveDirectory(String, Path, boolean)
97       */
98      public static Path resolveDirectory(
99              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 }