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      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      public Transporter newInstance(RepositorySystemSession session, RemoteRepository repository)
69              throws NoTransporterException {
70          requireNonNull(session, "session cannot be null");
71          requireNonNull(repository, "repository cannot be null");
72  
73          FileTransporter.FileOp fileOp = FileTransporter.FileOp.COPY;
74          String repositoryUrl = repository.getUrl();
75          if (repositoryUrl.startsWith("symlink+")) {
76              fileOp = FileTransporter.FileOp.SYMLINK;
77              repositoryUrl = repositoryUrl.substring("symlink+".length());
78          } else if (repositoryUrl.startsWith("hardlink+")) {
79              fileOp = FileTransporter.FileOp.HARDLINK;
80              repositoryUrl = repositoryUrl.substring("hardlink+".length());
81          }
82          try {
83              return new FileTransporter(
84                      Paths.get(RepositoryUriUtils.toUri(repositoryUrl)).toAbsolutePath(), fileOp);
85          } catch (FileSystemNotFoundException | IllegalArgumentException e) {
86              throw new NoTransporterException(repository, e);
87          }
88      }
89  }