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