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.impl.RemoteRepositoryFilterManager; 034import org.eclipse.aether.impl.RepositoryConnectorProvider; 035import org.eclipse.aether.internal.impl.filter.FilteringRepositoryConnector; 036import org.eclipse.aether.repository.Authentication; 037import org.eclipse.aether.repository.Proxy; 038import org.eclipse.aether.repository.RemoteRepository; 039import org.eclipse.aether.spi.connector.RepositoryConnector; 040import org.eclipse.aether.spi.connector.RepositoryConnectorFactory; 041import org.eclipse.aether.spi.connector.filter.RemoteRepositoryFilter; 042import org.eclipse.aether.spi.locator.Service; 043import org.eclipse.aether.spi.locator.ServiceLocator; 044import org.eclipse.aether.transfer.NoRepositoryConnectorException; 045import org.slf4j.Logger; 046import org.slf4j.LoggerFactory; 047 048/** 049 */ 050@Singleton 051@Named 052public class DefaultRepositoryConnectorProvider 053 implements RepositoryConnectorProvider, Service 054{ 055 056 private static final Logger LOGGER = LoggerFactory.getLogger( DefaultRepositoryConnectorProvider.class ); 057 058 private Collection<RepositoryConnectorFactory> connectorFactories = new ArrayList<>(); 059 060 private RemoteRepositoryFilterManager remoteRepositoryFilterManager; 061 062 public DefaultRepositoryConnectorProvider() 063 { 064 // enables default constructor 065 } 066 067 @Inject 068 DefaultRepositoryConnectorProvider( Set<RepositoryConnectorFactory> connectorFactories, 069 RemoteRepositoryFilterManager remoteRepositoryFilterManager ) 070 { 071 setRepositoryConnectorFactories( connectorFactories ); 072 setRemoteRepositoryFilterManager( remoteRepositoryFilterManager ); 073 } 074 075 public void initService( ServiceLocator locator ) 076 { 077 setRepositoryConnectorFactories( locator.getServices( RepositoryConnectorFactory.class ) ); 078 setRemoteRepositoryFilterManager( locator.getService( RemoteRepositoryFilterManager.class ) ); 079 } 080 081 public DefaultRepositoryConnectorProvider addRepositoryConnectorFactory( RepositoryConnectorFactory factory ) 082 { 083 connectorFactories.add( requireNonNull( factory, "repository connector factory cannot be null" ) ); 084 return this; 085 } 086 087 public DefaultRepositoryConnectorProvider setRepositoryConnectorFactories( 088 Collection<RepositoryConnectorFactory> factories ) 089 { 090 if ( factories == null ) 091 { 092 this.connectorFactories = new ArrayList<>(); 093 } 094 else 095 { 096 this.connectorFactories = factories; 097 } 098 return this; 099 } 100 101 public DefaultRepositoryConnectorProvider setRemoteRepositoryFilterManager( 102 RemoteRepositoryFilterManager remoteRepositoryFilterManager ) 103 { 104 this.remoteRepositoryFilterManager = requireNonNull( remoteRepositoryFilterManager ); 105 return this; 106 } 107 108 public RepositoryConnector newRepositoryConnector( RepositorySystemSession session, RemoteRepository repository ) 109 throws NoRepositoryConnectorException 110 { 111 requireNonNull( repository, "remote repository cannot be null" ); 112 113 if ( repository.isBlocked() ) 114 { 115 if ( repository.getMirroredRepositories().isEmpty() ) 116 { 117 throw new NoRepositoryConnectorException( repository, "Blocked repository: " + repository ); 118 } 119 else 120 { 121 throw new NoRepositoryConnectorException( repository, "Blocked mirror for repositories: " 122 + repository.getMirroredRepositories() ); 123 } 124 } 125 126 RemoteRepositoryFilter filter = remoteRepositoryFilterManager.getRemoteRepositoryFilter( session ); 127 128 PrioritizedComponents<RepositoryConnectorFactory> factories = new PrioritizedComponents<>( session ); 129 for ( RepositoryConnectorFactory factory : this.connectorFactories ) 130 { 131 factories.add( factory, factory.getPriority() ); 132 } 133 134 List<NoRepositoryConnectorException> errors = new ArrayList<>(); 135 for ( PrioritizedComponent<RepositoryConnectorFactory> factory : factories.getEnabled() ) 136 { 137 try 138 { 139 RepositoryConnector connector = factory.getComponent().newInstance( session, repository ); 140 141 if ( LOGGER.isDebugEnabled() ) 142 { 143 StringBuilder buffer = new StringBuilder( 256 ); 144 buffer.append( "Using connector " ).append( connector.getClass().getSimpleName() ); 145 Utils.appendClassLoader( buffer, connector ); 146 buffer.append( " with priority " ).append( factory.getPriority() ); 147 buffer.append( " for " ).append( repository.getUrl() ); 148 149 Authentication auth = repository.getAuthentication(); 150 if ( auth != null ) 151 { 152 buffer.append( " with " ).append( auth ); 153 } 154 155 Proxy proxy = repository.getProxy(); 156 if ( proxy != null ) 157 { 158 buffer.append( " via " ).append( proxy.getHost() ).append( ':' ).append( proxy.getPort() ); 159 160 auth = proxy.getAuthentication(); 161 if ( auth != null ) 162 { 163 buffer.append( " with " ).append( auth ); 164 } 165 } 166 167 LOGGER.debug( buffer.toString() ); 168 } 169 170 if ( filter != null ) 171 { 172 return new FilteringRepositoryConnector( 173 repository, 174 connector, 175 filter 176 ); 177 } 178 else 179 { 180 return connector; 181 } 182 } 183 catch ( NoRepositoryConnectorException e ) 184 { 185 // continue and try next factory 186 errors.add( e ); 187 } 188 } 189 if ( LOGGER.isDebugEnabled() && errors.size() > 1 ) 190 { 191 for ( Exception e : errors ) 192 { 193 LOGGER.debug( "Could not obtain connector factory for {}", repository, e ); 194 } 195 } 196 197 StringBuilder buffer = new StringBuilder( 256 ); 198 if ( factories.isEmpty() ) 199 { 200 buffer.append( "No connector factories available" ); 201 } 202 else 203 { 204 buffer.append( "Cannot access " ).append( repository.getUrl() ); 205 buffer.append( " with type " ).append( repository.getContentType() ); 206 buffer.append( " using the available connector factories: " ); 207 factories.list( buffer ); 208 } 209 210 throw new NoRepositoryConnectorException( repository, buffer.toString(), errors.size() == 1 ? errors.get( 0 ) 211 : null ); 212 } 213 214}