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( repository, "remote repository cannot be null" ); 094 095 PrioritizedComponents<TransporterFactory> factories = new PrioritizedComponents<>( session ); 096 for ( TransporterFactory factory : this.factories ) 097 { 098 factories.add( factory, factory.getPriority() ); 099 } 100 101 List<NoTransporterException> errors = new ArrayList<>(); 102 for ( PrioritizedComponent<TransporterFactory> factory : factories.getEnabled() ) 103 { 104 try 105 { 106 Transporter transporter = factory.getComponent().newInstance( session, repository ); 107 108 if ( LOGGER.isDebugEnabled() ) 109 { 110 StringBuilder buffer = new StringBuilder( 256 ); 111 buffer.append( "Using transporter " ).append( transporter.getClass().getSimpleName() ); 112 Utils.appendClassLoader( buffer, transporter ); 113 buffer.append( " with priority " ).append( factory.getPriority() ); 114 buffer.append( " for " ).append( repository.getUrl() ); 115 LOGGER.debug( buffer.toString() ); 116 } 117 118 return transporter; 119 } 120 catch ( NoTransporterException e ) 121 { 122 // continue and try next factory 123 errors.add( e ); 124 } 125 } 126 if ( LOGGER.isDebugEnabled() && errors.size() > 1 ) 127 { 128 for ( Exception e : errors ) 129 { 130 LOGGER.debug( "Could not obtain transporter factory for {}", repository, e ); 131 } 132 } 133 134 StringBuilder buffer = new StringBuilder( 256 ); 135 if ( factories.isEmpty() ) 136 { 137 buffer.append( "No transporter factories registered" ); 138 } 139 else 140 { 141 buffer.append( "Cannot access " ).append( repository.getUrl() ); 142 buffer.append( " using the registered transporter factories: " ); 143 factories.list( buffer ); 144 } 145 146 throw new NoTransporterException( repository, buffer.toString(), errors.size() == 1 ? errors.get( 0 ) : null ); 147 } 148 149}