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