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