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<RepositoryLayoutFactory>();
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<RepositoryLayoutFactory>();
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 =
94              new PrioritizedComponents<RepositoryLayoutFactory>( session );
95          for ( RepositoryLayoutFactory factory : this.factories )
96          {
97              factories.add( factory, factory.getPriority() );
98          }
99  
100         List<NoRepositoryLayoutException> errors = new ArrayList<NoRepositoryLayoutException>();
101         for ( PrioritizedComponent<RepositoryLayoutFactory> factory : factories.getEnabled() )
102         {
103             try
104             {
105                 RepositoryLayout layout = factory.getComponent().newInstance( session, repository );
106                 return layout;
107             }
108             catch ( NoRepositoryLayoutException e )
109             {
110                 // continue and try next factory
111                 errors.add( e );
112             }
113         }
114         if ( LOGGER.isDebugEnabled() && errors.size() > 1 )
115         {
116             String msg = "Could not obtain layout factory for " + repository;
117             for ( Exception e : errors )
118             {
119                 LOGGER.debug( msg, 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 }