1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.Collection;
27 import java.util.List;
28 import java.util.Set;
29
30 import org.eclipse.aether.RepositorySystemSession;
31 import org.eclipse.aether.repository.RemoteRepository;
32 import org.eclipse.aether.spi.connector.transport.Transporter;
33 import org.eclipse.aether.spi.connector.transport.TransporterFactory;
34 import org.eclipse.aether.spi.connector.transport.TransporterProvider;
35 import org.eclipse.aether.spi.locator.Service;
36 import org.eclipse.aether.spi.locator.ServiceLocator;
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, Service {
48
49 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultTransporterProvider.class);
50
51 private Collection<TransporterFactory> factories = new ArrayList<>();
52
53 @Deprecated
54 public DefaultTransporterProvider() {
55
56 }
57
58 @Inject
59 public DefaultTransporterProvider(Set<TransporterFactory> transporterFactories) {
60 setTransporterFactories(transporterFactories);
61 }
62
63 public void initService(ServiceLocator locator) {
64 setTransporterFactories(locator.getServices(TransporterFactory.class));
65 }
66
67 public DefaultTransporterProvider addTransporterFactory(TransporterFactory factory) {
68 factories.add(requireNonNull(factory, "transporter factory cannot be null"));
69 return this;
70 }
71
72 public DefaultTransporterProvider setTransporterFactories(Collection<TransporterFactory> factories) {
73 if (factories == null) {
74 this.factories = new ArrayList<>();
75 } else {
76 this.factories = factories;
77 }
78 return this;
79 }
80
81 public Transporter newTransporter(RepositorySystemSession session, RemoteRepository repository)
82 throws NoTransporterException {
83 requireNonNull(session, "session cannot be null");
84 requireNonNull(repository, "repository cannot be null");
85
86 PrioritizedComponents<TransporterFactory> factories = new PrioritizedComponents<>(session);
87 for (TransporterFactory factory : this.factories) {
88 factories.add(factory, factory.getPriority());
89 }
90
91 List<NoTransporterException> errors = new ArrayList<>();
92 for (PrioritizedComponent<TransporterFactory> factory : factories.getEnabled()) {
93 try {
94 Transporter transporter = factory.getComponent().newInstance(session, repository);
95
96 if (LOGGER.isDebugEnabled()) {
97 StringBuilder buffer = new StringBuilder(256);
98 buffer.append("Using transporter ")
99 .append(transporter.getClass().getSimpleName());
100 Utils.appendClassLoader(buffer, transporter);
101 buffer.append(" with priority ").append(factory.getPriority());
102 buffer.append(" for ").append(repository.getUrl());
103 LOGGER.debug(buffer.toString());
104 }
105
106 return transporter;
107 } catch (NoTransporterException e) {
108
109 errors.add(e);
110 }
111 }
112 if (LOGGER.isDebugEnabled() && errors.size() > 1) {
113 for (Exception e : errors) {
114 LOGGER.debug("Could not obtain transporter factory for {}", repository, e);
115 }
116 }
117
118 StringBuilder buffer = new StringBuilder(256);
119 if (factories.isEmpty()) {
120 buffer.append("No transporter factories registered");
121 } else {
122 buffer.append("Cannot access ").append(repository.getUrl());
123 buffer.append(" using the registered transporter factories: ");
124 factories.list(buffer);
125 }
126
127 throw new NoTransporterException(repository, buffer.toString(), errors.size() == 1 ? errors.get(0) : null);
128 }
129 }