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