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.nio.file.FileSystemNotFoundException;
24  import java.nio.file.Paths;
25  
26  import org.eclipse.aether.RepositorySystemSession;
27  import org.eclipse.aether.repository.RemoteRepository;
28  import org.eclipse.aether.repository.RepositoryUriUtils;
29  import org.eclipse.aether.spi.connector.transport.Transporter;
30  import org.eclipse.aether.spi.connector.transport.TransporterFactory;
31  import org.eclipse.aether.transfer.NoTransporterException;
32  
33  import static java.util.Objects.requireNonNull;
34  
35  /**
36   * A transporter factory for repositories using the {@code file:} protocol.
37   */
38  @Named(FileTransporterFactory.NAME)
39  public final class FileTransporterFactory implements TransporterFactory {
40      public static final String NAME = "file";
41  
42      private float priority;
43  
44      /**
45       * Creates an (uninitialized) instance of this transporter factory. <em>Note:</em> In case of manual instantiation
46       * by clients, the new factory needs to be configured via its various mutators before first use or runtime errors
47       * will occur.
48       */
49      public FileTransporterFactory() {
50          // enables default constructor
51      }
52  
53      @Override
54      public float getPriority() {
55          return priority;
56      }
57  
58      /**
59       * Sets the priority of this component.
60       *
61       * @param priority The priority.
62       * @return This component for chaining, never {@code null}.
63       */
64      public FileTransporterFactory setPriority(float priority) {
65          this.priority = priority;
66          return this;
67      }
68  
69      @Override
70      public Transporter newInstance(RepositorySystemSession session, RemoteRepository repository)
71              throws NoTransporterException {
72          requireNonNull(session, "session cannot be null");
73          requireNonNull(repository, "repository cannot be null");
74  
75          // special case in file transport: to support custom FS providers (like JIMFS), we cannot
76          // cover "all possible protocols" to throw NoTransporterEx, but we rely on FS rejecting the URI
77          FileTransporter.FileOp fileOp = FileTransporter.FileOp.COPY;
78          String repositoryUrl = repository.getUrl();
79          if (repositoryUrl.startsWith("symlink+")) {
80              fileOp = FileTransporter.FileOp.SYMLINK;
81              repositoryUrl = repositoryUrl.substring("symlink+".length());
82          } else if (repositoryUrl.startsWith("hardlink+")) {
83              fileOp = FileTransporter.FileOp.HARDLINK;
84              repositoryUrl = repositoryUrl.substring("hardlink+".length());
85          }
86          try {
87              return new FileTransporter(
88                      Paths.get(RepositoryUriUtils.toUri(repositoryUrl)).toAbsolutePath(), fileOp);
89          } catch (FileSystemNotFoundException | IllegalArgumentException e) {
90              throw new NoTransporterException(repository, e);
91          }
92      }
93  }