View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.internal.impl;
20  
21  import javax.inject.Inject;
22  import javax.inject.Named;
23  import javax.inject.Singleton;
24  
25  import java.util.ArrayList;
26  import java.util.Collections;
27  import java.util.List;
28  import java.util.Map;
29  
30  import org.eclipse.aether.RepositorySystemSession;
31  import org.eclipse.aether.repository.Authentication;
32  import org.eclipse.aether.repository.Proxy;
33  import org.eclipse.aether.repository.RemoteRepository;
34  import org.eclipse.aether.spi.connector.transport.Transporter;
35  import org.eclipse.aether.spi.connector.transport.TransporterFactory;
36  import org.eclipse.aether.spi.connector.transport.TransporterProvider;
37  import org.eclipse.aether.transfer.NoTransporterException;
38  import org.slf4j.Logger;
39  import org.slf4j.LoggerFactory;
40  
41  import static java.util.Objects.requireNonNull;
42  
43  /**
44   */
45  @Singleton
46  @Named
47  public final class DefaultTransporterProvider implements TransporterProvider {
48  
49      private static final Logger LOGGER = LoggerFactory.getLogger(DefaultTransporterProvider.class);
50  
51      private final Map<String, TransporterFactory> transporterFactories;
52  
53      @Inject
54      public DefaultTransporterProvider(Map<String, TransporterFactory> transporterFactories) {
55          this.transporterFactories = Collections.unmodifiableMap(transporterFactories);
56      }
57  
58      @Override
59      public Transporter newTransporter(RepositorySystemSession session, RemoteRepository repository)
60              throws NoTransporterException {
61          requireNonNull(session, "session cannot be null");
62          requireNonNull(repository, "repository cannot be null");
63  
64          PrioritizedComponents<TransporterFactory> factories = PrioritizedComponents.reuseOrCreate(
65                  session, TransporterFactory.class, transporterFactories, TransporterFactory::getPriority);
66  
67          List<NoTransporterException> errors = new ArrayList<>();
68          for (PrioritizedComponent<TransporterFactory> factory : factories.getEnabled()) {
69              try {
70                  Transporter transporter = factory.getComponent().newInstance(session, repository);
71  
72                  if (LOGGER.isDebugEnabled()) {
73                      StringBuilder buffer = new StringBuilder(256);
74                      buffer.append("Using transporter ")
75                              .append(transporter.getClass().getSimpleName());
76                      Utils.appendClassLoader(buffer, transporter);
77                      buffer.append(" with priority ").append(factory.getPriority());
78                      buffer.append(" for ").append(repository.getUrl());
79  
80                      Authentication auth = repository.getAuthentication();
81                      if (auth != null) {
82                          buffer.append(" with ").append(auth);
83                      }
84  
85                      Proxy proxy = repository.getProxy();
86                      if (proxy != null) {
87                          buffer.append(" via ")
88                                  .append(proxy.getHost())
89                                  .append(':')
90                                  .append(proxy.getPort());
91  
92                          auth = proxy.getAuthentication();
93                          if (auth != null) {
94                              buffer.append(" with ").append(auth);
95                          }
96                      }
97  
98                      LOGGER.debug(buffer.toString());
99                  }
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 }