001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.transport.file;
020
021import javax.inject.Named;
022
023import java.nio.file.FileSystemNotFoundException;
024import java.nio.file.Paths;
025
026import org.eclipse.aether.RepositorySystemSession;
027import org.eclipse.aether.repository.RemoteRepository;
028import org.eclipse.aether.repository.RepositoryUriUtils;
029import org.eclipse.aether.spi.connector.transport.Transporter;
030import org.eclipse.aether.spi.connector.transport.TransporterFactory;
031import org.eclipse.aether.transfer.NoTransporterException;
032
033import static java.util.Objects.requireNonNull;
034
035/**
036 * A transporter factory for repositories using the {@code file:} protocol.
037 */
038@Named(FileTransporterFactory.NAME)
039public final class FileTransporterFactory implements TransporterFactory {
040    public static final String NAME = "file";
041
042    private float priority;
043
044    /**
045     * Creates an (uninitialized) instance of this transporter factory. <em>Note:</em> In case of manual instantiation
046     * by clients, the new factory needs to be configured via its various mutators before first use or runtime errors
047     * will occur.
048     */
049    public FileTransporterFactory() {
050        // enables default constructor
051    }
052
053    @Override
054    public float getPriority() {
055        return priority;
056    }
057
058    /**
059     * Sets the priority of this component.
060     *
061     * @param priority The priority.
062     * @return This component for chaining, never {@code null}.
063     */
064    public FileTransporterFactory setPriority(float priority) {
065        this.priority = priority;
066        return this;
067    }
068
069    @Override
070    public Transporter newInstance(RepositorySystemSession session, RemoteRepository repository)
071            throws NoTransporterException {
072        requireNonNull(session, "session cannot be null");
073        requireNonNull(repository, "repository cannot be null");
074
075        // special case in file transport: to support custom FS providers (like JIMFS), we cannot
076        // cover "all possible protocols" to throw NoTransporterEx, but we rely on FS rejecting the URI
077        FileTransporter.FileOp fileOp = FileTransporter.FileOp.COPY;
078        String repositoryUrl = repository.getUrl();
079        if (repositoryUrl.startsWith("symlink+")) {
080            fileOp = FileTransporter.FileOp.SYMLINK;
081            repositoryUrl = repositoryUrl.substring("symlink+".length());
082        } else if (repositoryUrl.startsWith("hardlink+")) {
083            fileOp = FileTransporter.FileOp.HARDLINK;
084            repositoryUrl = repositoryUrl.substring("hardlink+".length());
085        }
086        try {
087            return new FileTransporter(
088                    Paths.get(RepositoryUriUtils.toUri(repositoryUrl)).toAbsolutePath(), fileOp);
089        } catch (FileSystemNotFoundException | IllegalArgumentException e) {
090            throw new NoTransporterException(repository, e);
091        }
092    }
093}