001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.eclipse.aether.internal.impl; 020 021import javax.inject.Inject; 022import javax.inject.Named; 023import javax.inject.Singleton; 024 025import java.util.ArrayList; 026import java.util.Collections; 027import java.util.List; 028import java.util.Map; 029 030import org.eclipse.aether.RepositorySystemSession; 031import org.eclipse.aether.impl.LocalRepositoryProvider; 032import org.eclipse.aether.repository.LocalRepository; 033import org.eclipse.aether.repository.LocalRepositoryManager; 034import org.eclipse.aether.repository.NoLocalRepositoryManagerException; 035import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory; 036import org.slf4j.Logger; 037import org.slf4j.LoggerFactory; 038 039import static java.util.Objects.requireNonNull; 040 041/** 042 */ 043@Singleton 044@Named 045public class DefaultLocalRepositoryProvider implements LocalRepositoryProvider { 046 047 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultLocalRepositoryProvider.class); 048 049 private final Map<String, LocalRepositoryManagerFactory> localRepositoryManagerFactories; 050 051 @Inject 052 public DefaultLocalRepositoryProvider(Map<String, LocalRepositoryManagerFactory> localRepositoryManagerFactories) { 053 this.localRepositoryManagerFactories = Collections.unmodifiableMap(localRepositoryManagerFactories); 054 } 055 056 @Override 057 public LocalRepositoryManager newLocalRepositoryManager(RepositorySystemSession session, LocalRepository repository) 058 throws NoLocalRepositoryManagerException { 059 requireNonNull(session, "session cannot be null"); 060 requireNonNull(repository, "repository cannot be null"); 061 062 PrioritizedComponents<LocalRepositoryManagerFactory> factories = PrioritizedComponents.reuseOrCreate( 063 session, 064 LocalRepositoryManagerFactory.class, 065 localRepositoryManagerFactories, 066 LocalRepositoryManagerFactory::getPriority); 067 068 LOGGER.debug("Selecting LocalRepositoryManager for {}", repository); 069 070 List<NoLocalRepositoryManagerException> errors = new ArrayList<>(); 071 for (PrioritizedComponent<LocalRepositoryManagerFactory> factory : factories.getEnabled()) { 072 try { 073 LocalRepositoryManager manager = factory.getComponent().newInstance(session, repository); 074 075 if (LOGGER.isDebugEnabled()) { 076 StringBuilder buffer = new StringBuilder(256); 077 buffer.append("Using manager ").append(manager.getClass().getSimpleName()); 078 Utils.appendClassLoader(buffer, manager); 079 buffer.append(" with priority ").append(factory.getPriority()); 080 buffer.append(" for ").append(repository.getBasePath()); 081 082 LOGGER.debug(buffer.toString()); 083 } 084 085 return manager; 086 } catch (NoLocalRepositoryManagerException e) { 087 // continue and try next factory 088 if (LOGGER.isTraceEnabled()) { 089 LOGGER.trace( 090 "LRM factory {} did not provide LRM for {}", 091 factory.getComponent().getClass(), 092 repository, 093 e); 094 } else { 095 LOGGER.debug( 096 "LRM factory {} did not provide LRM for {}: {}", 097 factory.getComponent().getClass(), 098 repository, 099 e.getMessage()); 100 } 101 errors.add(e); 102 } 103 } 104 105 StringBuilder buffer = new StringBuilder(256); 106 if (factories.isEmpty()) { 107 buffer.append("No local repository managers registered"); 108 } else { 109 buffer.append("Cannot access ").append(repository.getBasePath()); 110 buffer.append(" with type ").append(repository.getContentType()); 111 buffer.append(" using the available factories "); 112 factories.list(buffer); 113 } 114 115 // create exception: if one error, make it cause 116 NoLocalRepositoryManagerException ex = new NoLocalRepositoryManagerException( 117 repository, buffer.toString(), errors.size() == 1 ? errors.get(0) : null); 118 // if more errors, make them all suppressed 119 if (errors.size() > 1) { 120 errors.forEach(ex::addSuppressed); 121 } 122 throw ex; 123 } 124}