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.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, localRepositoryManagerFactories, LocalRepositoryManagerFactory::getPriority);
64  
65          List<NoLocalRepositoryManagerException> errors = new ArrayList<>();
66          for (PrioritizedComponent<LocalRepositoryManagerFactory> factory : factories.getEnabled()) {
67              try {
68                  LocalRepositoryManager manager = factory.getComponent().newInstance(session, repository);
69  
70                  if (LOGGER.isDebugEnabled()) {
71                      StringBuilder buffer = new StringBuilder(256);
72                      buffer.append("Using manager ").append(manager.getClass().getSimpleName());
73                      Utils.appendClassLoader(buffer, manager);
74                      buffer.append(" with priority ").append(factory.getPriority());
75                      buffer.append(" for ").append(repository.getBasedir());
76  
77                      LOGGER.debug(buffer.toString());
78                  }
79  
80                  return manager;
81              } catch (NoLocalRepositoryManagerException e) {
82                  // continue and try next factory
83                  errors.add(e);
84              }
85          }
86          if (LOGGER.isDebugEnabled() && errors.size() > 1) {
87              for (Exception e : errors) {
88                  LOGGER.debug("Could not obtain local repository manager for {}", repository, e);
89              }
90          }
91  
92          StringBuilder buffer = new StringBuilder(256);
93          if (factories.isEmpty()) {
94              buffer.append("No local repository managers registered");
95          } else {
96              buffer.append("Cannot access ").append(repository.getBasedir());
97              buffer.append(" with type ").append(repository.getContentType());
98              buffer.append(" using the available factories ");
99              factories.list(buffer);
100         }
101 
102         throw new NoLocalRepositoryManagerException(
103                 repository, buffer.toString(), errors.size() == 1 ? errors.get(0) : null);
104     }
105 }