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.Collection;
27  import java.util.List;
28  import java.util.Set;
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.eclipse.aether.spi.locator.Service;
37  import org.eclipse.aether.spi.locator.ServiceLocator;
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 class DefaultLocalRepositoryProvider implements LocalRepositoryProvider, Service {
48  
49      private static final Logger LOGGER = LoggerFactory.getLogger(DefaultLocalRepositoryProvider.class);
50  
51      private Collection<LocalRepositoryManagerFactory> managerFactories = new ArrayList<>();
52  
53      @Deprecated
54      public DefaultLocalRepositoryProvider() {
55          // enables default constructor
56      }
57  
58      @Inject
59      public DefaultLocalRepositoryProvider(Set<LocalRepositoryManagerFactory> factories) {
60          setLocalRepositoryManagerFactories(factories);
61      }
62  
63      public void initService(ServiceLocator locator) {
64          setLocalRepositoryManagerFactories(locator.getServices(LocalRepositoryManagerFactory.class));
65      }
66  
67      public DefaultLocalRepositoryProvider addLocalRepositoryManagerFactory(LocalRepositoryManagerFactory factory) {
68          managerFactories.add(requireNonNull(factory, "local repository manager factory cannot be null"));
69          return this;
70      }
71  
72      public DefaultLocalRepositoryProvider setLocalRepositoryManagerFactories(
73              Collection<LocalRepositoryManagerFactory> factories) {
74          if (factories == null) {
75              managerFactories = new ArrayList<>(2);
76          } else {
77              managerFactories = factories;
78          }
79          return this;
80      }
81  
82      public LocalRepositoryManager newLocalRepositoryManager(RepositorySystemSession session, LocalRepository repository)
83              throws NoLocalRepositoryManagerException {
84          requireNonNull(session, "session cannot be null");
85          requireNonNull(repository, "repository cannot be null");
86          PrioritizedComponents<LocalRepositoryManagerFactory> factories = new PrioritizedComponents<>(session);
87          for (LocalRepositoryManagerFactory factory : this.managerFactories) {
88              factories.add(factory, factory.getPriority());
89          }
90  
91          List<NoLocalRepositoryManagerException> errors = new ArrayList<>();
92          for (PrioritizedComponent<LocalRepositoryManagerFactory> factory : factories.getEnabled()) {
93              try {
94                  LocalRepositoryManager manager = factory.getComponent().newInstance(session, repository);
95  
96                  if (LOGGER.isDebugEnabled()) {
97                      StringBuilder buffer = new StringBuilder(256);
98                      buffer.append("Using manager ").append(manager.getClass().getSimpleName());
99                      Utils.appendClassLoader(buffer, manager);
100                     buffer.append(" with priority ").append(factory.getPriority());
101                     buffer.append(" for ").append(repository.getBasedir());
102 
103                     LOGGER.debug(buffer.toString());
104                 }
105 
106                 return manager;
107             } catch (NoLocalRepositoryManagerException e) {
108                 // continue and try next factory
109                 errors.add(e);
110             }
111         }
112         if (LOGGER.isDebugEnabled() && errors.size() > 1) {
113             for (Exception e : errors) {
114                 LOGGER.debug("Could not obtain local repository manager for {}", repository, e);
115             }
116         }
117 
118         StringBuilder buffer = new StringBuilder(256);
119         if (factories.isEmpty()) {
120             buffer.append("No local repository managers registered");
121         } else {
122             buffer.append("Cannot access ").append(repository.getBasedir());
123             buffer.append(" with type ").append(repository.getContentType());
124             buffer.append(" using the available factories ");
125             factories.list(buffer);
126         }
127 
128         throw new NoLocalRepositoryManagerException(
129                 repository, buffer.toString(), errors.size() == 1 ? errors.get(0) : null);
130     }
131 }