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