001package org.eclipse.aether.internal.impl; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.util.ArrayList; 023import java.util.Collection; 024import java.util.List; 025import static java.util.Objects.requireNonNull; 026import java.util.Set; 027 028import javax.inject.Inject; 029import javax.inject.Named; 030import javax.inject.Singleton; 031 032import org.eclipse.aether.RepositorySystemSession; 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.spi.locator.Service; 038import org.eclipse.aether.spi.locator.ServiceLocator; 039import org.eclipse.aether.transfer.NoTransporterException; 040import org.slf4j.Logger; 041import org.slf4j.LoggerFactory; 042 043/** 044 */ 045@Singleton 046@Named 047public final class DefaultTransporterProvider 048 implements TransporterProvider, Service 049{ 050 051 private static final Logger LOGGER = LoggerFactory.getLogger( DefaultTransporterProvider.class ); 052 053 private Collection<TransporterFactory> factories = new ArrayList<>(); 054 055 public DefaultTransporterProvider() 056 { 057 // enables default constructor 058 } 059 060 @Inject 061 DefaultTransporterProvider( Set<TransporterFactory> transporterFactories ) 062 { 063 setTransporterFactories( transporterFactories ); 064 } 065 066 public void initService( ServiceLocator locator ) 067 { 068 setTransporterFactories( locator.getServices( TransporterFactory.class ) ); 069 } 070 071 public DefaultTransporterProvider addTransporterFactory( TransporterFactory factory ) 072 { 073 factories.add( requireNonNull( factory, "transporter factory cannot be null" ) ); 074 return this; 075 } 076 077 public DefaultTransporterProvider setTransporterFactories( Collection<TransporterFactory> factories ) 078 { 079 if ( factories == null ) 080 { 081 this.factories = new ArrayList<>(); 082 } 083 else 084 { 085 this.factories = factories; 086 } 087 return this; 088 } 089 090 public Transporter newTransporter( RepositorySystemSession session, RemoteRepository repository ) 091 throws NoTransporterException 092 { 093 requireNonNull( "session", "session cannot be null" ); 094 requireNonNull( "repository", "repository cannot be null" ); 095 096 PrioritizedComponents<TransporterFactory> factories = new PrioritizedComponents<>( session ); 097 for ( TransporterFactory factory : this.factories ) 098 { 099 factories.add( factory, factory.getPriority() ); 100 } 101 102 List<NoTransporterException> errors = new ArrayList<>(); 103 for ( PrioritizedComponent<TransporterFactory> factory : factories.getEnabled() ) 104 { 105 try 106 { 107 Transporter transporter = factory.getComponent().newInstance( session, repository ); 108 109 if ( LOGGER.isDebugEnabled() ) 110 { 111 StringBuilder buffer = new StringBuilder( 256 ); 112 buffer.append( "Using transporter " ).append( transporter.getClass().getSimpleName() ); 113 Utils.appendClassLoader( buffer, transporter ); 114 buffer.append( " with priority " ).append( factory.getPriority() ); 115 buffer.append( " for " ).append( repository.getUrl() ); 116 LOGGER.debug( buffer.toString() ); 117 } 118 119 return transporter; 120 } 121 catch ( NoTransporterException e ) 122 { 123 // continue and try next factory 124 errors.add( e ); 125 } 126 } 127 if ( LOGGER.isDebugEnabled() && errors.size() > 1 ) 128 { 129 for ( Exception e : errors ) 130 { 131 LOGGER.debug( "Could not obtain transporter factory for {}", repository, e ); 132 } 133 } 134 135 StringBuilder buffer = new StringBuilder( 256 ); 136 if ( factories.isEmpty() ) 137 { 138 buffer.append( "No transporter factories registered" ); 139 } 140 else 141 { 142 buffer.append( "Cannot access " ).append( repository.getUrl() ); 143 buffer.append( " using the registered transporter factories: " ); 144 factories.list( buffer ); 145 } 146 147 throw new NoTransporterException( repository, buffer.toString(), errors.size() == 1 ? errors.get( 0 ) : null ); 148 } 149 150}