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