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