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