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  import javax.inject.Singleton;
31  
32  import org.eclipse.aether.RepositorySystemSession;
33  import org.eclipse.aether.repository.RemoteRepository;
34  import org.eclipse.aether.spi.connector.layout.RepositoryLayout;
35  import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory;
36  import org.eclipse.aether.spi.connector.layout.RepositoryLayoutProvider;
37  import org.eclipse.aether.spi.locator.Service;
38  import org.eclipse.aether.spi.locator.ServiceLocator;
39  import org.eclipse.aether.transfer.NoRepositoryLayoutException;
40  import org.slf4j.Logger;
41  import org.slf4j.LoggerFactory;
42  
43  /**
44   */
45  @Singleton
46  @Named
47  public final class DefaultRepositoryLayoutProvider
48      implements RepositoryLayoutProvider, Service
49  {
50  
51      private static final Logger LOGGER = LoggerFactory.getLogger( DefaultRepositoryLayoutProvider.class );
52  
53      private Collection<RepositoryLayoutFactory> factories = new ArrayList<>();
54  
55      public DefaultRepositoryLayoutProvider()
56      {
57          // enables default constructor
58      }
59  
60      @Inject
61      DefaultRepositoryLayoutProvider( Set<RepositoryLayoutFactory> layoutFactories )
62      {
63          setRepositoryLayoutFactories( layoutFactories );
64      }
65  
66      public void initService( ServiceLocator locator )
67      {
68          setRepositoryLayoutFactories( locator.getServices( RepositoryLayoutFactory.class ) );
69      }
70  
71      public DefaultRepositoryLayoutProvider addRepositoryLayoutFactory( RepositoryLayoutFactory factory )
72      {
73          factories.add( requireNonNull( factory, "layout factory cannot be null" ) );
74          return this;
75      }
76  
77      public DefaultRepositoryLayoutProvider setRepositoryLayoutFactories( Collection<RepositoryLayoutFactory> factories )
78      {
79          if ( factories == null )
80          {
81              this.factories = new ArrayList<>();
82          }
83          else
84          {
85              this.factories = factories;
86          }
87          return this;
88      }
89  
90      public RepositoryLayout newRepositoryLayout( RepositorySystemSession session, RemoteRepository repository )
91          throws NoRepositoryLayoutException
92      {
93          requireNonNull( session, "session cannot be null" );
94          requireNonNull( repository, "remote repository cannot be null" );
95  
96          PrioritizedComponents<RepositoryLayoutFactory> factories = new PrioritizedComponents<>( session );
97          for ( RepositoryLayoutFactory factory : this.factories )
98          {
99              factories.add( factory, factory.getPriority() );
100         }
101 
102         List<NoRepositoryLayoutException> errors = new ArrayList<>();
103         for ( PrioritizedComponent<RepositoryLayoutFactory> factory : factories.getEnabled() )
104         {
105             try
106             {
107                 return factory.getComponent().newInstance( session, repository );
108             }
109             catch ( NoRepositoryLayoutException e )
110             {
111                 // continue and try next factory
112                 errors.add( e );
113             }
114         }
115         if ( LOGGER.isDebugEnabled() && errors.size() > 1 )
116         {
117             for ( Exception e : errors )
118             {
119                 LOGGER.debug( "Could not obtain layout factory for {}", repository, 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 }