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