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 static java.util.Objects.requireNonNull;
26  import java.util.Set;
27  
28  import javax.inject.Inject;
29  import javax.inject.Named;
30  
31  import org.eclipse.aether.RepositorySystemSession;
32  import org.eclipse.aether.repository.RemoteRepository;
33  import org.eclipse.aether.spi.connector.transport.Transporter;
34  import org.eclipse.aether.spi.connector.transport.TransporterFactory;
35  import org.eclipse.aether.spi.connector.transport.TransporterProvider;
36  import org.eclipse.aether.spi.locator.Service;
37  import org.eclipse.aether.spi.locator.ServiceLocator;
38  import org.eclipse.aether.spi.log.Logger;
39  import org.eclipse.aether.spi.log.LoggerFactory;
40  import org.eclipse.aether.spi.log.NullLoggerFactory;
41  import org.eclipse.aether.transfer.NoTransporterException;
42  
43  /**
44   */
45  @Named
46  public final class DefaultTransporterProvider
47      implements TransporterProvider, Service
48  {
49  
50      private Logger logger = NullLoggerFactory.LOGGER;
51  
52      private Collection<TransporterFactory> factories = new ArrayList<TransporterFactory>();
53  
54      public DefaultTransporterProvider()
55      {
56          // enables default constructor
57      }
58  
59      @Inject
60      DefaultTransporterProvider( Set<TransporterFactory> transporterFactories, LoggerFactory loggerFactory )
61      {
62          setLoggerFactory( loggerFactory );
63          setTransporterFactories( transporterFactories );
64      }
65  
66      public void initService( ServiceLocator locator )
67      {
68          setLoggerFactory( locator.getService( LoggerFactory.class ) );
69          setTransporterFactories( locator.getServices( TransporterFactory.class ) );
70      }
71  
72      public DefaultTransporterProvider setLoggerFactory( LoggerFactory loggerFactory )
73      {
74          this.logger = NullLoggerFactory.getSafeLogger( loggerFactory, getClass() );
75          return this;
76      }
77  
78      public DefaultTransporterProvider addTransporterFactory( TransporterFactory factory )
79      {
80          factories.add( requireNonNull( factory, "transporter factory cannot be null" ) );
81          return this;
82      }
83  
84      public DefaultTransporterProvider setTransporterFactories( Collection<TransporterFactory> factories )
85      {
86          if ( factories == null )
87          {
88              this.factories = new ArrayList<TransporterFactory>();
89          }
90          else
91          {
92              this.factories = factories;
93          }
94          return this;
95      }
96  
97      public Transporter newTransporter( RepositorySystemSession session, RemoteRepository repository )
98          throws NoTransporterException
99      {
100         requireNonNull( repository, "remote repository cannot be null" );
101 
102         PrioritizedComponents<TransporterFactory> factories = new PrioritizedComponents<TransporterFactory>( session );
103         for ( TransporterFactory factory : this.factories )
104         {
105             factories.add( factory, factory.getPriority() );
106         }
107 
108         List<NoTransporterException> errors = new ArrayList<NoTransporterException>();
109         for ( PrioritizedComponent<TransporterFactory> factory : factories.getEnabled() )
110         {
111             try
112             {
113                 Transporter transporter = factory.getComponent().newInstance( session, repository );
114 
115                 if ( logger.isDebugEnabled() )
116                 {
117                     StringBuilder buffer = new StringBuilder( 256 );
118                     buffer.append( "Using transporter " ).append( transporter.getClass().getSimpleName() );
119                     Utils.appendClassLoader( buffer, transporter );
120                     buffer.append( " with priority " ).append( factory.getPriority() );
121                     buffer.append( " for " ).append( repository.getUrl() );
122                     logger.debug( buffer.toString() );
123                 }
124 
125                 return transporter;
126             }
127             catch ( NoTransporterException e )
128             {
129                 // continue and try next factory
130                 errors.add( e );
131             }
132         }
133         if ( logger.isDebugEnabled() && errors.size() > 1 )
134         {
135             String msg = "Could not obtain transporter factory for " + repository;
136             for ( Exception e : errors )
137             {
138                 logger.debug( msg, e );
139             }
140         }
141 
142         StringBuilder buffer = new StringBuilder( 256 );
143         if ( factories.isEmpty() )
144         {
145             buffer.append( "No transporter factories registered" );
146         }
147         else
148         {
149             buffer.append( "Cannot access " ).append( repository.getUrl() );
150             buffer.append( " using the registered transporter factories: " );
151             factories.list( buffer );
152         }
153 
154         throw new NoTransporterException( repository, buffer.toString(), errors.size() == 1 ? errors.get( 0 ) : null );
155     }
156 
157 }