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          LOGGER.debug("Selecting Transporter for {}", repository);
68  
69          List<NoTransporterException> errors = new ArrayList<>();
70          for (PrioritizedComponent<TransporterFactory> factory : factories.getEnabled()) {
71              try {
72                  Transporter transporter = factory.getComponent().newInstance(session, repository);
73  
74                  if (LOGGER.isDebugEnabled()) {
75                      StringBuilder buffer = new StringBuilder(256);
76                      buffer.append("Using transporter ")
77                              .append(transporter.getClass().getSimpleName());
78                      Utils.appendClassLoader(buffer, transporter);
79                      buffer.append(" with priority ").append(factory.getPriority());
80                      buffer.append(" for ").append(repository.getUrl());
81  
82                      Authentication auth = repository.getAuthentication();
83                      if (auth != null) {
84                          buffer.append(" with ").append(auth);
85                      }
86  
87                      Proxy proxy = repository.getProxy();
88                      if (proxy != null) {
89                          buffer.append(" via ")
90                                  .append(proxy.getHost())
91                                  .append(':')
92                                  .append(proxy.getPort());
93  
94                          auth = proxy.getAuthentication();
95                          if (auth != null) {
96                              buffer.append(" with ").append(auth);
97                          }
98                      }
99  
100                     LOGGER.debug(buffer.toString());
101                 }
102 
103                 return transporter;
104             } catch (NoTransporterException e) {
105                 // continue and try next factory
106                 if (LOGGER.isTraceEnabled()) {
107                     LOGGER.trace(
108                             "Transporter factory {} did not provide Transporter for {}",
109                             factory.getComponent().getClass(),
110                             repository,
111                             e);
112                 } else {
113                     LOGGER.debug(
114                             "Transporter factory {} did not provide Transporter for {}: {}",
115                             factory.getComponent().getClass(),
116                             repository,
117                             e.getMessage());
118                 }
119                 errors.add(e);
120             }
121         }
122 
123         StringBuilder buffer = new StringBuilder(256);
124         if (factories.isEmpty()) {
125             buffer.append("No transporter factories registered");
126         } else {
127             buffer.append("Cannot access ").append(repository.getUrl());
128             buffer.append(" using the registered transporter factories: ");
129             factories.list(buffer);
130         }
131 
132         // create exception: if one error, make it cause
133         NoTransporterException ex =
134                 new NoTransporterException(repository, buffer.toString(), errors.size() == 1 ? errors.get(0) : null);
135         // if more errors, make them all suppressed
136         if (errors.size() > 1) {
137             errors.forEach(ex::addSuppressed);
138         }
139         throw ex;
140     }
141 }