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.impl.LocalRepositoryProvider;
32  import org.eclipse.aether.repository.LocalRepository;
33  import org.eclipse.aether.repository.LocalRepositoryManager;
34  import org.eclipse.aether.repository.NoLocalRepositoryManagerException;
35  import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory;
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 class DefaultLocalRepositoryProvider implements LocalRepositoryProvider {
46  
47      private static final Logger LOGGER = LoggerFactory.getLogger(DefaultLocalRepositoryProvider.class);
48  
49      private final Map<String, LocalRepositoryManagerFactory> localRepositoryManagerFactories;
50  
51      @Inject
52      public DefaultLocalRepositoryProvider(Map<String, LocalRepositoryManagerFactory> localRepositoryManagerFactories) {
53          this.localRepositoryManagerFactories = Collections.unmodifiableMap(localRepositoryManagerFactories);
54      }
55  
56      @Override
57      public LocalRepositoryManager newLocalRepositoryManager(RepositorySystemSession session, LocalRepository repository)
58              throws NoLocalRepositoryManagerException {
59          requireNonNull(session, "session cannot be null");
60          requireNonNull(repository, "repository cannot be null");
61  
62          PrioritizedComponents<LocalRepositoryManagerFactory> factories = PrioritizedComponents.reuseOrCreate(
63                  session,
64                  LocalRepositoryManagerFactory.class,
65                  localRepositoryManagerFactories,
66                  LocalRepositoryManagerFactory::getPriority);
67  
68          List<NoLocalRepositoryManagerException> errors = new ArrayList<>();
69          for (PrioritizedComponent<LocalRepositoryManagerFactory> factory : factories.getEnabled()) {
70              try {
71                  LocalRepositoryManager manager = factory.getComponent().newInstance(session, repository);
72  
73                  if (LOGGER.isDebugEnabled()) {
74                      StringBuilder buffer = new StringBuilder(256);
75                      buffer.append("Using manager ").append(manager.getClass().getSimpleName());
76                      Utils.appendClassLoader(buffer, manager);
77                      buffer.append(" with priority ").append(factory.getPriority());
78                      buffer.append(" for ").append(repository.getBasePath());
79  
80                      LOGGER.debug(buffer.toString());
81                  }
82  
83                  return manager;
84              } catch (NoLocalRepositoryManagerException e) {
85                  
86                  LOGGER.debug("Could not obtain local repository manager for {}", repository, e);
87                  errors.add(e);
88              }
89          }
90  
91          StringBuilder buffer = new StringBuilder(256);
92          if (factories.isEmpty()) {
93              buffer.append("No local repository managers registered");
94          } else {
95              buffer.append("Cannot access ").append(repository.getBasePath());
96              buffer.append(" with type ").append(repository.getContentType());
97              buffer.append(" using the available factories ");
98              factories.list(buffer);
99          }
100 
101         
102         NoLocalRepositoryManagerException ex = new NoLocalRepositoryManagerException(
103                 repository, buffer.toString(), errors.size() == 1 ? errors.get(0) : null);
104         
105         if (errors.size() > 1) {
106             errors.forEach(ex::addSuppressed);
107         }
108         throw ex;
109     }
110 }