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.util.DirectoryUtils;
31  
32  import static java.util.Objects.requireNonNull;
33  
34  /**
35   * Wrapping {@link NameMapper} class that is file system friendly: it wraps another
36   * {@link NameMapper} and resolves the resulting "file system friendly" names against local
37   * repository basedir.
38   *
39   * @since 1.9.0
40   */
41  public class BasedirNameMapper implements NameMapper {
42      /**
43       * The location of the directory toi use for locks. If relative path, it is resolved from the local repository root.
44       *
45       * @configurationSource {@link RepositorySystemSession#getConfigProperties()}
46       * @configurationType {@link java.lang.String}
47       * @configurationDefaultValue {@link #DEFAULT_LOCKS_DIR}
48       */
49      public static final String CONFIG_PROP_LOCKS_DIR = NamedLockFactoryAdapter.CONFIG_PROPS_PREFIX + "basedir.locksDir";
50  
51      public static final String DEFAULT_LOCKS_DIR = ".locks";
52  
53      private final NameMapper delegate;
54  
55      public BasedirNameMapper(final NameMapper delegate) {
56          this.delegate = requireNonNull(delegate);
57      }
58  
59      @Override
60      public boolean isFileSystemFriendly() {
61          return delegate.isFileSystemFriendly();
62      }
63  
64      @Override
65      public Collection<String> nameLocks(
66              final RepositorySystemSession session,
67              final Collection<? extends Artifact> artifacts,
68              final Collection<? extends Metadata> metadatas) {
69          try {
70              final Path basedir =
71                      DirectoryUtils.resolveDirectory(session, DEFAULT_LOCKS_DIR, CONFIG_PROP_LOCKS_DIR, false);
72  
73              return delegate.nameLocks(session, artifacts, metadatas).stream()
74                      .map(name -> basedir.resolve(name).toAbsolutePath().toString())
75                      .collect(Collectors.toList());
76          } catch (IOException e) {
77              throw new UncheckedIOException(e);
78          }
79      }
80  }