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