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