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