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.transport.file;
20  
21  import javax.inject.Named;
22  
23  import java.io.IOException;
24  import java.io.UncheckedIOException;
25  import java.net.URI;
26  import java.nio.file.FileSystem;
27  import java.nio.file.FileSystemNotFoundException;
28  import java.nio.file.FileSystems;
29  import java.nio.file.Path;
30  import java.nio.file.Paths;
31  import java.util.HashMap;
32  import java.util.Map;
33  
34  import org.eclipse.aether.RepositorySystemSession;
35  import org.eclipse.aether.repository.RemoteRepository;
36  import org.eclipse.aether.repository.RepositoryUriUtils;
37  import org.eclipse.aether.spi.connector.transport.Transporter;
38  import org.eclipse.aether.spi.connector.transport.TransporterFactory;
39  import org.eclipse.aether.transfer.NoTransporterException;
40  
41  import static java.util.Objects.requireNonNull;
42  
43  /**
44   * A transporter factory for repositories using the {@code file:} or {@code bundle:} protocol.
45   */
46  @Named(FileTransporterFactory.NAME)
47  public final class FileTransporterFactory implements TransporterFactory {
48      public static final String NAME = "file";
49  
50      private float priority;
51  
52      @Override
53      public float getPriority() {
54          return priority;
55      }
56  
57      /**
58       * Sets the priority of this component.
59       *
60       * @param priority The priority.
61       * @return This component for chaining, never {@code null}.
62       */
63      public FileTransporterFactory setPriority(float priority) {
64          this.priority = priority;
65          return this;
66      }
67  
68      /**
69       * Creates new instance of {@link FileTransporter}.
70       *
71       * @param session The session.
72       * @param repository The remote repository.
73       */
74      @Override
75      public Transporter newInstance(RepositorySystemSession session, RemoteRepository repository)
76              throws NoTransporterException {
77          requireNonNull(session, "session cannot be null");
78          requireNonNull(repository, "repository cannot be null");
79  
80          String repositoryUrl = repository.getUrl();
81          if (repositoryUrl.startsWith("bundle:")) {
82              try {
83                  repositoryUrl = repositoryUrl.substring("bundle:".length());
84                  URI bundlePath = URI.create("jar:"
85                          + Paths.get(RepositoryUriUtils.toUri(repositoryUrl))
86                                  .toAbsolutePath()
87                                  .toUri()
88                                  .toASCIIString());
89                  Map<String, String> env = new HashMap<>();
90                  FileSystem fileSystem = FileSystems.newFileSystem(bundlePath, env);
91                  return new FileTransporter(
92                          fileSystem,
93                          true,
94                          false,
95                          fileSystem.getPath(fileSystem.getSeparator()),
96                          FileTransporter.WriteOp.COPY);
97              } catch (IOException e) {
98                  throw new UncheckedIOException(e); // hard failure; most probably user error (ie wrong path or perm)
99              }
100         } else {
101             // special case in file: transport: to support custom FS providers (like JIMFS), we cannot
102             // cover all possible protocols (to throw NoTransporterEx), hence we rely on FS rejecting the URI
103             FileTransporter.WriteOp writeOp = FileTransporter.WriteOp.COPY;
104             if (repositoryUrl.startsWith("symlink+")) {
105                 writeOp = FileTransporter.WriteOp.SYMLINK;
106                 repositoryUrl = repositoryUrl.substring("symlink+".length());
107             } else if (repositoryUrl.startsWith("hardlink+")) {
108                 writeOp = FileTransporter.WriteOp.HARDLINK;
109                 repositoryUrl = repositoryUrl.substring("hardlink+".length());
110             }
111             try {
112                 Path basePath =
113                         Paths.get(RepositoryUriUtils.toUri(repositoryUrl)).toAbsolutePath();
114                 return new FileTransporter(
115                         basePath.getFileSystem(),
116                         false,
117                         !basePath.getFileSystem().isReadOnly(),
118                         basePath,
119                         writeOp);
120             } catch (FileSystemNotFoundException | IllegalArgumentException e) {
121                 throw new NoTransporterException(repository, e);
122             }
123         }
124     }
125 }