View Javadoc
1   package org.eclipse.aether.impl.guice;
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.Collections;
23  import java.util.HashMap;
24  import java.util.HashSet;
25  import java.util.Map;
26  import java.util.Set;
27  
28  import javax.inject.Named;
29  import javax.inject.Singleton;
30  
31  import org.eclipse.aether.RepositoryListener;
32  import org.eclipse.aether.RepositorySystem;
33  import org.eclipse.aether.impl.ArtifactResolver;
34  import org.eclipse.aether.impl.DependencyCollector;
35  import org.eclipse.aether.impl.Deployer;
36  import org.eclipse.aether.impl.Installer;
37  import org.eclipse.aether.impl.LocalRepositoryProvider;
38  import org.eclipse.aether.impl.MetadataResolver;
39  import org.eclipse.aether.impl.OfflineController;
40  import org.eclipse.aether.impl.RemoteRepositoryManager;
41  import org.eclipse.aether.impl.RepositoryConnectorProvider;
42  import org.eclipse.aether.impl.RepositoryEventDispatcher;
43  import org.eclipse.aether.internal.impl.DefaultTrackingFileManager;
44  import org.eclipse.aether.internal.impl.TrackingFileManager;
45  import org.eclipse.aether.internal.impl.synccontext.DefaultSyncContextFactory;
46  import org.eclipse.aether.internal.impl.synccontext.NamedLockFactorySelector;
47  import org.eclipse.aether.internal.impl.synccontext.named.GAVNameMapper;
48  import org.eclipse.aether.internal.impl.synccontext.named.DiscriminatingNameMapper;
49  import org.eclipse.aether.internal.impl.synccontext.named.NameMapper;
50  import org.eclipse.aether.internal.impl.synccontext.named.StaticNameMapper;
51  import org.eclipse.aether.named.NamedLockFactory;
52  import org.eclipse.aether.named.providers.LocalReadWriteLockNamedLockFactory;
53  import org.eclipse.aether.named.providers.LocalSemaphoreNamedLockFactory;
54  import org.eclipse.aether.impl.UpdateCheckManager;
55  import org.eclipse.aether.impl.UpdatePolicyAnalyzer;
56  import org.eclipse.aether.internal.impl.DefaultArtifactResolver;
57  import org.eclipse.aether.internal.impl.DefaultChecksumPolicyProvider;
58  import org.eclipse.aether.internal.impl.collect.DefaultDependencyCollector;
59  import org.eclipse.aether.internal.impl.DefaultDeployer;
60  import org.eclipse.aether.internal.impl.DefaultFileProcessor;
61  import org.eclipse.aether.internal.impl.DefaultInstaller;
62  import org.eclipse.aether.internal.impl.DefaultLocalRepositoryProvider;
63  import org.eclipse.aether.internal.impl.DefaultMetadataResolver;
64  import org.eclipse.aether.internal.impl.DefaultOfflineController;
65  import org.eclipse.aether.internal.impl.DefaultRemoteRepositoryManager;
66  import org.eclipse.aether.internal.impl.DefaultRepositoryConnectorProvider;
67  import org.eclipse.aether.internal.impl.DefaultRepositoryEventDispatcher;
68  import org.eclipse.aether.internal.impl.DefaultRepositoryLayoutProvider;
69  import org.eclipse.aether.internal.impl.DefaultRepositorySystem;
70  import org.eclipse.aether.internal.impl.DefaultTransporterProvider;
71  import org.eclipse.aether.internal.impl.DefaultUpdateCheckManager;
72  import org.eclipse.aether.internal.impl.DefaultUpdatePolicyAnalyzer;
73  import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
74  import org.eclipse.aether.internal.impl.Maven2RepositoryLayoutFactory;
75  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
76  import org.eclipse.aether.internal.impl.slf4j.Slf4jLoggerFactory;
77  import org.eclipse.aether.named.providers.NoopNamedLockFactory;
78  import org.eclipse.aether.spi.connector.checksum.ChecksumPolicyProvider;
79  import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory;
80  import org.eclipse.aether.spi.connector.layout.RepositoryLayoutProvider;
81  import org.eclipse.aether.spi.connector.transport.TransporterProvider;
82  import org.eclipse.aether.spi.io.FileProcessor;
83  import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory;
84  import org.eclipse.aether.spi.log.LoggerFactory;
85  import org.eclipse.aether.spi.synccontext.SyncContextFactory;
86  import org.slf4j.ILoggerFactory;
87  
88  import com.google.inject.AbstractModule;
89  import com.google.inject.Provides;
90  import com.google.inject.name.Names;
91  
92  /**
93   * A ready-made <a href="https://github.com/google/guice" target="_blank">Guice</a> module that sets up bindings
94   * for all components from this library. To acquire a complete repository system, clients need to bind an artifact
95   * descriptor reader, a version resolver, a version range resolver, zero or more metadata generator factories, some
96   * repository connector and transporter factories to access remote repositories.
97   *
98   * @noextend This class must not be extended by clients and will eventually be marked {@code final} without prior
99   *           notice.
100  */
101 public class AetherModule
102     extends AbstractModule
103 {
104 
105     /**
106      * Creates a new instance of this Guice module, typically for invoking
107      * {@link com.google.inject.Binder#install(com.google.inject.Module)}.
108      */
109     public AetherModule()
110     {
111     }
112 
113     /**
114      * Configures Guice with bindings for Aether components provided by this library.
115      */
116     @Override
117     protected void configure()
118     {
119         bind( RepositorySystem.class ) //
120         .to( DefaultRepositorySystem.class ).in( Singleton.class );
121         bind( ArtifactResolver.class ) //
122         .to( DefaultArtifactResolver.class ).in( Singleton.class );
123         bind( DependencyCollector.class ) //
124         .to( DefaultDependencyCollector.class ).in( Singleton.class );
125         bind( Deployer.class ) //
126         .to( DefaultDeployer.class ).in( Singleton.class );
127         bind( Installer.class ) //
128         .to( DefaultInstaller.class ).in( Singleton.class );
129         bind( MetadataResolver.class ) //
130         .to( DefaultMetadataResolver.class ).in( Singleton.class );
131         bind( RepositoryLayoutProvider.class ) //
132         .to( DefaultRepositoryLayoutProvider.class ).in( Singleton.class );
133         bind( RepositoryLayoutFactory.class ).annotatedWith( Names.named( "maven2" ) ) //
134         .to( Maven2RepositoryLayoutFactory.class ).in( Singleton.class );
135         bind( TransporterProvider.class ) //
136         .to( DefaultTransporterProvider.class ).in( Singleton.class );
137         bind( ChecksumPolicyProvider.class ) //
138         .to( DefaultChecksumPolicyProvider.class ).in( Singleton.class );
139         bind( RepositoryConnectorProvider.class ) //
140         .to( DefaultRepositoryConnectorProvider.class ).in( Singleton.class );
141         bind( RemoteRepositoryManager.class ) //
142         .to( DefaultRemoteRepositoryManager.class ).in( Singleton.class );
143         bind( UpdateCheckManager.class ) //
144         .to( DefaultUpdateCheckManager.class ).in( Singleton.class );
145         bind( UpdatePolicyAnalyzer.class ) //
146         .to( DefaultUpdatePolicyAnalyzer.class ).in( Singleton.class );
147         bind( FileProcessor.class ) //
148         .to( DefaultFileProcessor.class ).in( Singleton.class );
149         bind( RepositoryEventDispatcher.class ) //
150         .to( DefaultRepositoryEventDispatcher.class ).in( Singleton.class );
151         bind( OfflineController.class ) //
152         .to( DefaultOfflineController.class ).in( Singleton.class );
153         bind( LocalRepositoryProvider.class ) //
154         .to( DefaultLocalRepositoryProvider.class ).in( Singleton.class );
155         bind( LocalRepositoryManagerFactory.class ).annotatedWith( Names.named( "simple" ) ) //
156         .to( SimpleLocalRepositoryManagerFactory.class ).in( Singleton.class );
157         bind( LocalRepositoryManagerFactory.class ).annotatedWith( Names.named( "enhanced" ) ) //
158         .to( EnhancedLocalRepositoryManagerFactory.class ).in( Singleton.class );
159         bind( TrackingFileManager.class ).to( DefaultTrackingFileManager.class ).in( Singleton.class );
160 
161         bind( NamedLockFactorySelector.class ).in( Singleton.class );
162         bind( SyncContextFactory.class ).to( DefaultSyncContextFactory.class ).in( Singleton.class );
163         bind( org.eclipse.aether.impl.SyncContextFactory.class )
164                 .to( org.eclipse.aether.internal.impl.synccontext.legacy.DefaultSyncContextFactory.class )
165                 .in( Singleton.class );
166 
167         bind( NameMapper.class ).annotatedWith( Names.named( StaticNameMapper.NAME ) )
168             .to( StaticNameMapper.class ).in( Singleton.class );
169         bind( NameMapper.class ).annotatedWith( Names.named( GAVNameMapper.NAME ) )
170             .to( GAVNameMapper.class ).in( Singleton.class );
171         bind( NameMapper.class ).annotatedWith( Names.named( DiscriminatingNameMapper.NAME ) )
172             .to( DiscriminatingNameMapper.class ).in( Singleton.class );
173 
174         bind( NamedLockFactory.class ).annotatedWith( Names.named( NoopNamedLockFactory.NAME ) )
175             .to( NoopNamedLockFactory.class ).in( Singleton.class );
176         bind( NamedLockFactory.class ).annotatedWith( Names.named( LocalReadWriteLockNamedLockFactory.NAME ) )
177             .to( LocalReadWriteLockNamedLockFactory.class ).in( Singleton.class );
178         bind( NamedLockFactory.class ).annotatedWith( Names.named( LocalSemaphoreNamedLockFactory.NAME ) )
179             .to( LocalSemaphoreNamedLockFactory.class ).in( Singleton.class );
180 
181         install( new Slf4jModule() );
182 
183     }
184 
185     @Provides
186     @Singleton
187     Map<String, NameMapper> provideNameMappers(
188         @Named( StaticNameMapper.NAME ) NameMapper staticNameMapper,
189         @Named( GAVNameMapper.NAME ) NameMapper gavNameMapper,
190         @Named( DiscriminatingNameMapper.NAME ) NameMapper discriminatingNameMapper )
191     {
192         Map<String, NameMapper> nameMappers = new HashMap<>();
193         nameMappers.put( StaticNameMapper.NAME, staticNameMapper );
194         nameMappers.put( GAVNameMapper.NAME, gavNameMapper );
195         nameMappers.put( DiscriminatingNameMapper.NAME, discriminatingNameMapper );
196         return Collections.unmodifiableMap( nameMappers );
197     }
198 
199     @Provides
200     @Singleton
201     Map<String, NamedLockFactory> provideNamedLockFactories(
202             @Named( LocalReadWriteLockNamedLockFactory.NAME ) NamedLockFactory localRwLock,
203             @Named( LocalSemaphoreNamedLockFactory.NAME ) NamedLockFactory localSemaphore )
204     {
205         Map<String, NamedLockFactory> factories = new HashMap<>();
206         factories.put( LocalReadWriteLockNamedLockFactory.NAME, localRwLock );
207         factories.put( LocalSemaphoreNamedLockFactory.NAME, localSemaphore );
208         return Collections.unmodifiableMap( factories );
209     }
210 
211     @Provides
212     @Singleton
213     Set<LocalRepositoryManagerFactory> provideLocalRepositoryManagerFactories(
214             @Named( "simple" ) LocalRepositoryManagerFactory simple,
215             @Named( "enhanced" ) LocalRepositoryManagerFactory enhanced )
216     {
217         Set<LocalRepositoryManagerFactory> factories = new HashSet<>();
218         factories.add( simple );
219         factories.add( enhanced );
220         return Collections.unmodifiableSet( factories );
221     }
222 
223     @Provides
224     @Singleton
225     Set<RepositoryLayoutFactory> provideRepositoryLayoutFactories( @Named( "maven2" ) RepositoryLayoutFactory maven2 )
226     {
227         Set<RepositoryLayoutFactory> factories = new HashSet<>();
228         factories.add( maven2 );
229         return Collections.unmodifiableSet( factories );
230     }
231 
232     @Provides
233     @Singleton
234     Set<RepositoryListener> providesRepositoryListeners()
235     {
236         return Collections.emptySet();
237     }
238 
239     private static class Slf4jModule
240         extends AbstractModule
241     {
242 
243         @Override
244         protected void configure()
245         {
246             bind( LoggerFactory.class ) //
247             .to( Slf4jLoggerFactory.class );
248         }
249 
250         @Provides
251         @Singleton
252         ILoggerFactory getLoggerFactory()
253         {
254             return org.slf4j.LoggerFactory.getILoggerFactory();
255         }
256 
257     }
258 
259 }