1 package org.eclipse.aether.internal.impl;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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 requireNonNull( session, "session cannot be null" );
96 requireNonNull( repository, "repository cannot be null" );
97 PrioritizedComponents<LocalRepositoryManagerFactory> factories = new PrioritizedComponents<>( session );
98 for ( LocalRepositoryManagerFactory factory : this.managerFactories )
99 {
100 factories.add( factory, factory.getPriority() );
101 }
102
103 List<NoLocalRepositoryManagerException> errors = new ArrayList<>();
104 for ( PrioritizedComponent<LocalRepositoryManagerFactory> factory : factories.getEnabled() )
105 {
106 try
107 {
108 LocalRepositoryManager manager = factory.getComponent().newInstance( session, repository );
109
110 if ( LOGGER.isDebugEnabled() )
111 {
112 StringBuilder buffer = new StringBuilder( 256 );
113 buffer.append( "Using manager " ).append( manager.getClass().getSimpleName() );
114 Utils.appendClassLoader( buffer, manager );
115 buffer.append( " with priority " ).append( factory.getPriority() );
116 buffer.append( " for " ).append( repository.getBasedir() );
117
118 LOGGER.debug( buffer.toString() );
119 }
120
121 return manager;
122 }
123 catch ( NoLocalRepositoryManagerException e )
124 {
125
126 errors.add( e );
127 }
128 }
129 if ( LOGGER.isDebugEnabled() && errors.size() > 1 )
130 {
131 for ( Exception e : errors )
132 {
133 LOGGER.debug( "Could not obtain local repository manager for {}", repository, e );
134 }
135 }
136
137 StringBuilder buffer = new StringBuilder( 256 );
138 if ( factories.isEmpty() )
139 {
140 buffer.append( "No local repository managers registered" );
141 }
142 else
143 {
144 buffer.append( "Cannot access " ).append( repository.getBasedir() );
145 buffer.append( " with type " ).append( repository.getContentType() );
146 buffer.append( " using the available factories " );
147 factories.list( buffer );
148 }
149
150 throw new NoLocalRepositoryManagerException( repository, buffer.toString(), errors.size() == 1 ? errors.get( 0 )
151 : null );
152 }
153
154 }