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.eclipse.aether.spi.log.Logger; 040import org.eclipse.aether.spi.log.LoggerFactory; 041import org.eclipse.aether.spi.log.NullLoggerFactory; 042 043/** 044 */ 045@Named 046public class DefaultLocalRepositoryProvider 047 implements LocalRepositoryProvider, Service 048{ 049 050 private Logger logger = NullLoggerFactory.LOGGER; 051 052 private Collection<LocalRepositoryManagerFactory> managerFactories = new ArrayList<LocalRepositoryManagerFactory>(); 053 054 public DefaultLocalRepositoryProvider() 055 { 056 // enables default constructor 057 } 058 059 @Inject 060 DefaultLocalRepositoryProvider( Set<LocalRepositoryManagerFactory> factories, LoggerFactory loggerFactory ) 061 { 062 setLocalRepositoryManagerFactories( factories ); 063 setLoggerFactory( loggerFactory ); 064 } 065 066 public void initService( ServiceLocator locator ) 067 { 068 setLoggerFactory( locator.getService( LoggerFactory.class ) ); 069 setLocalRepositoryManagerFactories( locator.getServices( LocalRepositoryManagerFactory.class ) ); 070 } 071 072 public DefaultLocalRepositoryProvider setLoggerFactory( LoggerFactory loggerFactory ) 073 { 074 this.logger = NullLoggerFactory.getSafeLogger( loggerFactory, getClass() ); 075 return this; 076 } 077 078 public DefaultLocalRepositoryProvider addLocalRepositoryManagerFactory( LocalRepositoryManagerFactory factory ) 079 { 080 managerFactories.add( requireNonNull( factory, "local repository manager factory cannot be null" ) ); 081 return this; 082 } 083 084 public DefaultLocalRepositoryProvider setLocalRepositoryManagerFactories( Collection<LocalRepositoryManagerFactory> factories ) 085 { 086 if ( factories == null ) 087 { 088 managerFactories = new ArrayList<LocalRepositoryManagerFactory>( 2 ); 089 } 090 else 091 { 092 managerFactories = factories; 093 } 094 return this; 095 } 096 097 public LocalRepositoryManager newLocalRepositoryManager( RepositorySystemSession session, LocalRepository repository ) 098 throws NoLocalRepositoryManagerException 099 { 100 PrioritizedComponents<LocalRepositoryManagerFactory> factories = 101 new PrioritizedComponents<LocalRepositoryManagerFactory>( session ); 102 for ( LocalRepositoryManagerFactory factory : this.managerFactories ) 103 { 104 factories.add( factory, factory.getPriority() ); 105 } 106 107 List<NoLocalRepositoryManagerException> errors = new ArrayList<NoLocalRepositoryManagerException>(); 108 for ( PrioritizedComponent<LocalRepositoryManagerFactory> factory : factories.getEnabled() ) 109 { 110 try 111 { 112 LocalRepositoryManager manager = factory.getComponent().newInstance( session, repository ); 113 114 if ( logger.isDebugEnabled() ) 115 { 116 StringBuilder buffer = new StringBuilder( 256 ); 117 buffer.append( "Using manager " ).append( manager.getClass().getSimpleName() ); 118 Utils.appendClassLoader( buffer, manager ); 119 buffer.append( " with priority " ).append( factory.getPriority() ); 120 buffer.append( " for " ).append( repository.getBasedir() ); 121 122 logger.debug( buffer.toString() ); 123 } 124 125 return manager; 126 } 127 catch ( NoLocalRepositoryManagerException e ) 128 { 129 // continue and try next factory 130 errors.add( e ); 131 } 132 } 133 if ( logger.isDebugEnabled() && errors.size() > 1 ) 134 { 135 String msg = "Could not obtain local repository manager for " + repository; 136 for ( Exception e : errors ) 137 { 138 logger.debug( msg, e ); 139 } 140 } 141 142 StringBuilder buffer = new StringBuilder( 256 ); 143 if ( factories.isEmpty() ) 144 { 145 buffer.append( "No local repository managers registered" ); 146 } 147 else 148 { 149 buffer.append( "Cannot access " ).append( repository.getBasedir() ); 150 buffer.append( " with type " ).append( repository.getContentType() ); 151 buffer.append( " using the available factories " ); 152 factories.list( buffer ); 153 } 154 155 throw new NoLocalRepositoryManagerException( repository, buffer.toString(), errors.size() == 1 ? errors.get( 0 ) 156 : null ); 157 } 158 159}