View Javadoc
1   package org.eclipse.aether.internal.impl.synccontext.named;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.io.UncheckedIOException;
24  import java.nio.file.Path;
25  import java.util.Collection;
26  import java.util.stream.Collectors;
27  
28  import org.eclipse.aether.RepositorySystemSession;
29  import org.eclipse.aether.artifact.Artifact;
30  import org.eclipse.aether.metadata.Metadata;
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      private static final String CONFIG_PROP_LOCKS_DIR = "aether.syncContext.named.basedir.locksDir";
45  
46      private final NameMapper delegate;
47  
48      public BasedirNameMapper( final NameMapper delegate )
49      {
50          this.delegate = requireNonNull( delegate );
51      }
52  
53      @Override
54      public boolean isFileSystemFriendly()
55      {
56          return delegate.isFileSystemFriendly();
57      }
58  
59      @Override
60      public Collection<String> nameLocks( final RepositorySystemSession session,
61                                           final Collection<? extends Artifact> artifacts,
62                                           final Collection<? extends Metadata> metadatas )
63      {
64          try
65          {
66              final Path basedir = DirectoryUtils.resolveDirectory(
67                      session, ".locks", CONFIG_PROP_LOCKS_DIR, false );
68  
69              return delegate.nameLocks( session, artifacts, metadatas ).stream()
70                      .map( name -> basedir.resolve( name ).toAbsolutePath().toString() )
71                      .collect( Collectors.toList() );
72          }
73          catch ( IOException e )
74          {
75              throw new UncheckedIOException( e );
76          }
77      }
78  }