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    public float getPriority() {
054        return priority;
055    }
056
057    /**
058     * Sets the priority of this component.
059     *
060     * @param priority The priority.
061     * @return This component for chaining, never {@code null}.
062     */
063    public FileTransporterFactory setPriority(float priority) {
064        this.priority = priority;
065        return this;
066    }
067
068    public Transporter newInstance(RepositorySystemSession session, RemoteRepository repository)
069            throws NoTransporterException {
070        requireNonNull(session, "session cannot be null");
071        requireNonNull(repository, "repository cannot be null");
072
073        FileTransporter.FileOp fileOp = FileTransporter.FileOp.COPY;
074        String repositoryUrl = repository.getUrl();
075        if (repositoryUrl.startsWith("symlink+")) {
076            fileOp = FileTransporter.FileOp.SYMLINK;
077            repositoryUrl = repositoryUrl.substring("symlink+".length());
078        } else if (repositoryUrl.startsWith("hardlink+")) {
079            fileOp = FileTransporter.FileOp.HARDLINK;
080            repositoryUrl = repositoryUrl.substring("hardlink+".length());
081        }
082        try {
083            return new FileTransporter(
084                    Paths.get(RepositoryUriUtils.toUri(repositoryUrl)).toAbsolutePath(), fileOp);
085        } catch (FileSystemNotFoundException | IllegalArgumentException e) {
086            throw new NoTransporterException(repository, e);
087        }
088    }
089}