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.Authentication;
032import org.eclipse.aether.repository.Proxy;
033import org.eclipse.aether.repository.RemoteRepository;
034import org.eclipse.aether.spi.connector.transport.Transporter;
035import org.eclipse.aether.spi.connector.transport.TransporterFactory;
036import org.eclipse.aether.spi.connector.transport.TransporterProvider;
037import org.eclipse.aether.transfer.NoTransporterException;
038import org.slf4j.Logger;
039import org.slf4j.LoggerFactory;
040
041import static java.util.Objects.requireNonNull;
042
043/**
044 */
045@Singleton
046@Named
047public final class DefaultTransporterProvider implements TransporterProvider {
048
049    private static final Logger LOGGER = LoggerFactory.getLogger(DefaultTransporterProvider.class);
050
051    private final Map<String, TransporterFactory> transporterFactories;
052
053    @Inject
054    public DefaultTransporterProvider(Map<String, TransporterFactory> transporterFactories) {
055        this.transporterFactories = Collections.unmodifiableMap(transporterFactories);
056    }
057
058    @Override
059    public Transporter newTransporter(RepositorySystemSession session, RemoteRepository repository)
060            throws NoTransporterException {
061        requireNonNull(session, "session cannot be null");
062        requireNonNull(repository, "repository cannot be null");
063
064        PrioritizedComponents<TransporterFactory> factories = PrioritizedComponents.reuseOrCreate(
065                session, TransporterFactory.class, transporterFactories, TransporterFactory::getPriority);
066
067        LOGGER.debug("Selecting Transporter for {}", repository);
068
069        List<NoTransporterException> errors = new ArrayList<>();
070        for (PrioritizedComponent<TransporterFactory> factory : factories.getEnabled()) {
071            try {
072                Transporter transporter = factory.getComponent().newInstance(session, repository);
073
074                if (LOGGER.isDebugEnabled()) {
075                    StringBuilder buffer = new StringBuilder(256);
076                    buffer.append("Using transporter ")
077                            .append(transporter.getClass().getSimpleName());
078                    Utils.appendClassLoader(buffer, transporter);
079                    buffer.append(" with priority ").append(factory.getPriority());
080                    buffer.append(" for ").append(repository.getUrl());
081
082                    Authentication auth = repository.getAuthentication();
083                    if (auth != null) {
084                        buffer.append(" with ").append(auth);
085                    }
086
087                    Proxy proxy = repository.getProxy();
088                    if (proxy != null) {
089                        buffer.append(" via ")
090                                .append(proxy.getHost())
091                                .append(':')
092                                .append(proxy.getPort());
093
094                        auth = proxy.getAuthentication();
095                        if (auth != null) {
096                            buffer.append(" with ").append(auth);
097                        }
098                    }
099
100                    LOGGER.debug(buffer.toString());
101                }
102
103                return transporter;
104            } catch (NoTransporterException e) {
105                // continue and try next factory
106                if (LOGGER.isTraceEnabled()) {
107                    LOGGER.trace(
108                            "Transporter factory {} did not provide Transporter for {}",
109                            factory.getComponent().getClass(),
110                            repository,
111                            e);
112                } else {
113                    LOGGER.debug(
114                            "Transporter factory {} did not provide Transporter for {}: {}",
115                            factory.getComponent().getClass(),
116                            repository,
117                            e.getMessage());
118                }
119                errors.add(e);
120            }
121        }
122
123        StringBuilder buffer = new StringBuilder(256);
124        if (factories.isEmpty()) {
125            buffer.append("No transporter factories registered");
126        } else {
127            buffer.append("Cannot access ").append(repository.getUrl());
128            buffer.append(" using the registered transporter factories: ");
129            factories.list(buffer);
130        }
131
132        // create exception: if one error, make it cause
133        NoTransporterException ex =
134                new NoTransporterException(repository, buffer.toString(), errors.size() == 1 ? errors.get(0) : null);
135        // if more errors, make them all suppressed
136        if (errors.size() > 1) {
137            errors.forEach(ex::addSuppressed);
138        }
139        throw ex;
140    }
141}