View Javadoc
1   package org.eclipse.aether.internal.impl;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   * 
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.List;
25  import java.util.Set;
26  
27  import javax.inject.Inject;
28  import javax.inject.Named;
29  
30  import org.eclipse.aether.RepositorySystemSession;
31  import org.eclipse.aether.repository.RemoteRepository;
32  import org.eclipse.aether.spi.connector.transport.Transporter;
33  import org.eclipse.aether.spi.connector.transport.TransporterFactory;
34  import org.eclipse.aether.spi.connector.transport.TransporterProvider;
35  import org.eclipse.aether.spi.locator.Service;
36  import org.eclipse.aether.spi.locator.ServiceLocator;
37  import org.eclipse.aether.spi.log.Logger;
38  import org.eclipse.aether.spi.log.LoggerFactory;
39  import org.eclipse.aether.spi.log.NullLoggerFactory;
40  import org.eclipse.aether.transfer.NoTransporterException;
41  
42  /**
43   */
44  @Named
45  public final class DefaultTransporterProvider
46      implements TransporterProvider, Service
47  {
48  
49      private Logger logger = NullLoggerFactory.LOGGER;
50  
51      private Collection<TransporterFactory> factories = new ArrayList<TransporterFactory>();
52  
53      public DefaultTransporterProvider()
54      {
55          // enables default constructor
56      }
57  
58      @Inject
59      DefaultTransporterProvider( Set<TransporterFactory> transporterFactories, LoggerFactory loggerFactory )
60      {
61          setLoggerFactory( loggerFactory );
62          setTransporterFactories( transporterFactories );
63      }
64  
65      public void initService( ServiceLocator locator )
66      {
67          setLoggerFactory( locator.getService( LoggerFactory.class ) );
68          setTransporterFactories( locator.getServices( TransporterFactory.class ) );
69      }
70  
71      public DefaultTransporterProvider setLoggerFactory( LoggerFactory loggerFactory )
72      {
73          this.logger = NullLoggerFactory.getSafeLogger( loggerFactory, getClass() );
74          return this;
75      }
76  
77      public DefaultTransporterProvider addTransporterFactory( TransporterFactory factory )
78      {
79          if ( factory == null )
80          {
81              throw new IllegalArgumentException( "transporter factory has not been specified" );
82          }
83          factories.add( factory );
84          return this;
85      }
86  
87      public DefaultTransporterProvider setTransporterFactories( Collection<TransporterFactory> factories )
88      {
89          if ( factories == null )
90          {
91              this.factories = new ArrayList<TransporterFactory>();
92          }
93          else
94          {
95              this.factories = factories;
96          }
97          return this;
98      }
99  
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 }