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