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.Collections;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.eclipse.aether.RepositorySystemSession;
31 import org.eclipse.aether.repository.RemoteRepository;
32 import org.eclipse.aether.spi.connector.layout.RepositoryLayout;
33 import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory;
34 import org.eclipse.aether.spi.connector.layout.RepositoryLayoutProvider;
35 import org.eclipse.aether.transfer.NoRepositoryLayoutException;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 import static java.util.Objects.requireNonNull;
40
41
42
43 @Singleton
44 @Named
45 public final class DefaultRepositoryLayoutProvider implements RepositoryLayoutProvider {
46
47 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultRepositoryLayoutProvider.class);
48
49 private final Map<String, RepositoryLayoutFactory> layoutFactories;
50
51 @Inject
52 public DefaultRepositoryLayoutProvider(Map<String, RepositoryLayoutFactory> layoutFactories) {
53 this.layoutFactories = Collections.unmodifiableMap(layoutFactories);
54 }
55
56 @Override
57 public RepositoryLayout newRepositoryLayout(RepositorySystemSession session, RemoteRepository repository)
58 throws NoRepositoryLayoutException {
59 requireNonNull(session, "session cannot be null");
60 requireNonNull(repository, "remote repository cannot be null");
61
62 PrioritizedComponents<RepositoryLayoutFactory> factories = PrioritizedComponents.reuseOrCreate(
63 session, RepositoryLayoutFactory.class, layoutFactories, RepositoryLayoutFactory::getPriority);
64
65 List<NoRepositoryLayoutException> errors = new ArrayList<>();
66 for (PrioritizedComponent<RepositoryLayoutFactory> factory : factories.getEnabled()) {
67 try {
68 return factory.getComponent().newInstance(session, repository);
69 } catch (NoRepositoryLayoutException e) {
70
71 LOGGER.debug("Could not obtain layout factory for {}", repository, e);
72 errors.add(e);
73 }
74 }
75
76 StringBuilder buffer = new StringBuilder(256);
77 if (factories.isEmpty()) {
78 buffer.append("No layout factories registered");
79 } else {
80 buffer.append("Cannot access ").append(repository.getUrl());
81 buffer.append(" with type ").append(repository.getContentType());
82 buffer.append(" using the available layout factories: ");
83 factories.list(buffer);
84 }
85
86
87 NoRepositoryLayoutException ex = new NoRepositoryLayoutException(
88 repository, buffer.toString(), errors.size() == 1 ? errors.get(0) : null);
89
90 if (errors.size() > 1) {
91 errors.forEach(ex::addSuppressed);
92 }
93 throw ex;
94 }
95 }