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; 030import javax.inject.Singleton; 031 032import org.eclipse.aether.RepositorySystemSession; 033import org.eclipse.aether.repository.RemoteRepository; 034import org.eclipse.aether.spi.connector.layout.RepositoryLayout; 035import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory; 036import org.eclipse.aether.spi.connector.layout.RepositoryLayoutProvider; 037import org.eclipse.aether.spi.locator.Service; 038import org.eclipse.aether.spi.locator.ServiceLocator; 039import org.eclipse.aether.transfer.NoRepositoryLayoutException; 040import org.slf4j.Logger; 041import org.slf4j.LoggerFactory; 042 043/** 044 */ 045@Singleton 046@Named 047public final class DefaultRepositoryLayoutProvider 048 implements RepositoryLayoutProvider, Service 049{ 050 051 private static final Logger LOGGER = LoggerFactory.getLogger( DefaultRepositoryLayoutProvider.class ); 052 053 private Collection<RepositoryLayoutFactory> factories = new ArrayList<>(); 054 055 public DefaultRepositoryLayoutProvider() 056 { 057 // enables default constructor 058 } 059 060 @Inject 061 DefaultRepositoryLayoutProvider( Set<RepositoryLayoutFactory> layoutFactories ) 062 { 063 setRepositoryLayoutFactories( layoutFactories ); 064 } 065 066 public void initService( ServiceLocator locator ) 067 { 068 setRepositoryLayoutFactories( locator.getServices( RepositoryLayoutFactory.class ) ); 069 } 070 071 public DefaultRepositoryLayoutProvider addRepositoryLayoutFactory( RepositoryLayoutFactory factory ) 072 { 073 factories.add( requireNonNull( factory, "layout factory cannot be null" ) ); 074 return this; 075 } 076 077 public DefaultRepositoryLayoutProvider setRepositoryLayoutFactories( Collection<RepositoryLayoutFactory> factories ) 078 { 079 if ( factories == null ) 080 { 081 this.factories = new ArrayList<>(); 082 } 083 else 084 { 085 this.factories = factories; 086 } 087 return this; 088 } 089 090 public RepositoryLayout newRepositoryLayout( RepositorySystemSession session, RemoteRepository repository ) 091 throws NoRepositoryLayoutException 092 { 093 requireNonNull( repository, "remote repository cannot be null" ); 094 095 PrioritizedComponents<RepositoryLayoutFactory> factories = new PrioritizedComponents<>( session ); 096 for ( RepositoryLayoutFactory factory : this.factories ) 097 { 098 factories.add( factory, factory.getPriority() ); 099 } 100 101 List<NoRepositoryLayoutException> errors = new ArrayList<>(); 102 for ( PrioritizedComponent<RepositoryLayoutFactory> factory : factories.getEnabled() ) 103 { 104 try 105 { 106 return factory.getComponent().newInstance( session, repository ); 107 } 108 catch ( NoRepositoryLayoutException e ) 109 { 110 // continue and try next factory 111 errors.add( e ); 112 } 113 } 114 if ( LOGGER.isDebugEnabled() && errors.size() > 1 ) 115 { 116 for ( Exception e : errors ) 117 { 118 LOGGER.debug( "Could not obtain layout factory for {}", repository, e ); 119 } 120 } 121 122 StringBuilder buffer = new StringBuilder( 256 ); 123 if ( factories.isEmpty() ) 124 { 125 buffer.append( "No layout factories registered" ); 126 } 127 else 128 { 129 buffer.append( "Cannot access " ).append( repository.getUrl() ); 130 buffer.append( " with type " ).append( repository.getContentType() ); 131 buffer.append( " using the available layout factories: " ); 132 factories.list( buffer ); 133 } 134 135 throw new NoRepositoryLayoutException( repository, buffer.toString(), errors.size() == 1 ? errors.get( 0 ) 136 : null ); 137 } 138 139}