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 List<NoTransporterException> errors = new ArrayList<>(); 068 for (PrioritizedComponent<TransporterFactory> factory : factories.getEnabled()) { 069 try { 070 Transporter transporter = factory.getComponent().newInstance(session, repository); 071 072 if (LOGGER.isDebugEnabled()) { 073 StringBuilder buffer = new StringBuilder(256); 074 buffer.append("Using transporter ") 075 .append(transporter.getClass().getSimpleName()); 076 Utils.appendClassLoader(buffer, transporter); 077 buffer.append(" with priority ").append(factory.getPriority()); 078 buffer.append(" for ").append(repository.getUrl()); 079 080 Authentication auth = repository.getAuthentication(); 081 if (auth != null) { 082 buffer.append(" with ").append(auth); 083 } 084 085 Proxy proxy = repository.getProxy(); 086 if (proxy != null) { 087 buffer.append(" via ") 088 .append(proxy.getHost()) 089 .append(':') 090 .append(proxy.getPort()); 091 092 auth = proxy.getAuthentication(); 093 if (auth != null) { 094 buffer.append(" with ").append(auth); 095 } 096 } 097 098 LOGGER.debug(buffer.toString()); 099 } 100 101 return transporter; 102 } catch (NoTransporterException e) { 103 // continue and try next factory 104 LOGGER.debug("Could not obtain transporter factory for {}", repository, e); 105 errors.add(e); 106 } 107 } 108 109 StringBuilder buffer = new StringBuilder(256); 110 if (factories.isEmpty()) { 111 buffer.append("No transporter factories registered"); 112 } else { 113 buffer.append("Cannot access ").append(repository.getUrl()); 114 buffer.append(" using the registered transporter factories: "); 115 factories.list(buffer); 116 } 117 118 // create exception: if one error, make it cause 119 NoTransporterException ex = 120 new NoTransporterException(repository, buffer.toString(), errors.size() == 1 ? errors.get(0) : null); 121 // if more errors, make them all suppressed 122 if (errors.size() > 1) { 123 errors.forEach(ex::addSuppressed); 124 } 125 throw ex; 126 } 127}