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.internal.impl;
020
021import javax.inject.Inject;
022import javax.inject.Named;
023import javax.inject.Singleton;
024
025import java.util.ArrayList;
026import java.util.Collections;
027import java.util.List;
028import java.util.Map;
029
030import org.eclipse.aether.RepositorySystemSession;
031import org.eclipse.aether.repository.RemoteRepository;
032import org.eclipse.aether.spi.connector.transport.Transporter;
033import org.eclipse.aether.spi.connector.transport.TransporterFactory;
034import org.eclipse.aether.spi.connector.transport.TransporterProvider;
035import org.eclipse.aether.transfer.NoTransporterException;
036import org.slf4j.Logger;
037import org.slf4j.LoggerFactory;
038
039import static java.util.Objects.requireNonNull;
040
041/**
042 */
043@Singleton
044@Named
045public final class DefaultTransporterProvider implements TransporterProvider {
046
047    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultTransporterProvider.class);
048
049    private final Map<String, TransporterFactory> transporterFactories;
050
051    @Inject
052    public DefaultTransporterProvider(Map<String, TransporterFactory> transporterFactories) {
053        this.transporterFactories = Collections.unmodifiableMap(transporterFactories);
054    }
055
056    @Override
057    public Transporter newTransporter(RepositorySystemSession session, RemoteRepository repository)
058            throws NoTransporterException {
059        requireNonNull(session, "session cannot be null");
060        requireNonNull(repository, "repository cannot be null");
061
062        PrioritizedComponents<TransporterFactory> factories =
063                PrioritizedComponents.reuseOrCreate(session, transporterFactories, TransporterFactory::getPriority);
064
065        List<NoTransporterException> errors = new ArrayList<>();
066        for (PrioritizedComponent<TransporterFactory> factory : factories.getEnabled()) {
067            try {
068                Transporter transporter = factory.getComponent().newInstance(session, repository);
069
070                if (LOGGER.isDebugEnabled()) {
071                    StringBuilder buffer = new StringBuilder(256);
072                    buffer.append("Using transporter ")
073                            .append(transporter.getClass().getSimpleName());
074                    Utils.appendClassLoader(buffer, transporter);
075                    buffer.append(" with priority ").append(factory.getPriority());
076                    buffer.append(" for ").append(repository.getUrl());
077                    LOGGER.debug(buffer.toString());
078                }
079
080                return transporter;
081            } catch (NoTransporterException e) {
082                // continue and try next factory
083                errors.add(e);
084            }
085        }
086        if (LOGGER.isDebugEnabled() && errors.size() > 1) {
087            for (Exception e : errors) {
088                LOGGER.debug("Could not obtain transporter factory for {}", repository, e);
089            }
090        }
091
092        StringBuilder buffer = new StringBuilder(256);
093        if (factories.isEmpty()) {
094            buffer.append("No transporter factories registered");
095        } else {
096            buffer.append("Cannot access ").append(repository.getUrl());
097            buffer.append(" using the registered transporter factories: ");
098            factories.list(buffer);
099        }
100
101        throw new NoTransporterException(repository, buffer.toString(), errors.size() == 1 ? errors.get(0) : null);
102    }
103}