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.internal.impl.synccontext.named;
20  
21  import java.io.IOException;
22  import java.io.UncheckedIOException;
23  import java.nio.file.Path;
24  import java.util.Collection;
25  import java.util.stream.Collectors;
26  
27  import org.eclipse.aether.RepositorySystemSession;
28  import org.eclipse.aether.artifact.Artifact;
29  import org.eclipse.aether.metadata.Metadata;
30  import org.eclipse.aether.named.NamedLockKey;
31  import org.eclipse.aether.util.DirectoryUtils;
32  
33  import static java.util.Objects.requireNonNull;
34  
35  /**
36   * Wrapping {@link NameMapper} class that is file system friendly: it wraps another
37   * {@link NameMapper} and resolves the resulting "file system friendly" names against local
38   * repository basedir.
39   *
40   * @since 1.9.0
41   */
42  public class BasedirNameMapper implements NameMapper {
43      /**
44       * The location of the directory toi use for locks. If relative path, it is resolved from the local repository root.
45       *
46       * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
47       * @configurationType {@link java.lang.String}
48       * @configurationDefaultValue {@link #DEFAULT_LOCKS_DIR}
49       */
50      public static final String CONFIG_PROP_LOCKS_DIR = NamedLockFactoryAdapter.CONFIG_PROPS_PREFIX + "basedir.locksDir";
51  
52      public static final String DEFAULT_LOCKS_DIR = ".locks";
53  
54      private final NameMapper delegate;
55  
56      private final Path basePath;
57  
58      public BasedirNameMapper(final NameMapper delegate) {
59          this(delegate, null);
60      }
61  
62      /**
63       * Creates basedir name mapper with provided path as base.
64       *
65       * @param delegate The delegate to resolve against basedir, must not be {@code null}. The delegate must be
66       *                 "file system friendly", see {@link NameMapper#isFileSystemFriendly()} method.
67       * @param path The basedir, may be {@code null} in which case given session local repository root is used as
68       *             basedir.
69       * @since 2.0.0
70       */
71      public BasedirNameMapper(final NameMapper delegate, final Path path) {
72          requireNonNull(delegate);
73          if (!delegate.isFileSystemFriendly()) {
74              throw new IllegalArgumentException("delegate must be file-system friendly");
75          }
76          this.delegate = delegate;
77          this.basePath = path;
78      }
79  
80      @Override
81      public boolean isFileSystemFriendly() {
82          return true; // it enforces delegate to be friendly, and itself produces friendly names
83      }
84  
85      @Override
86      public Collection<NamedLockKey> nameLocks(
87              final RepositorySystemSession session,
88              final Collection<? extends Artifact> artifacts,
89              final Collection<? extends Metadata> metadatas) {
90          try {
91              final Path basedir = basePath != null
92                      ? basePath
93                      : DirectoryUtils.resolveDirectory(session, DEFAULT_LOCKS_DIR, CONFIG_PROP_LOCKS_DIR, false);
94  
95              return delegate.nameLocks(session, artifacts, metadatas).stream()
96                      .map(k -> NamedLockKey.of(
97                              basedir.resolve(k.name()).toAbsolutePath().toUri().toASCIIString(), k.resources()))
98                      .collect(Collectors.toList());
99          } catch (IOException e) {
100             throw new UncheckedIOException(e);
101         }
102     }
103 }