001package org.eclipse.aether.impl.guice;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.util.Collections;
023import java.util.HashMap;
024import java.util.HashSet;
025import java.util.Map;
026import java.util.Set;
027
028import javax.inject.Named;
029import javax.inject.Singleton;
030
031import org.eclipse.aether.RepositoryListener;
032import org.eclipse.aether.RepositorySystem;
033import org.eclipse.aether.impl.ArtifactResolver;
034import org.eclipse.aether.impl.DependencyCollector;
035import org.eclipse.aether.impl.Deployer;
036import org.eclipse.aether.impl.Installer;
037import org.eclipse.aether.impl.LocalRepositoryProvider;
038import org.eclipse.aether.impl.MetadataResolver;
039import org.eclipse.aether.impl.OfflineController;
040import org.eclipse.aether.impl.RemoteRepositoryManager;
041import org.eclipse.aether.impl.RepositoryConnectorProvider;
042import org.eclipse.aether.impl.RepositoryEventDispatcher;
043import org.eclipse.aether.internal.impl.DefaultTrackingFileManager;
044import org.eclipse.aether.internal.impl.FileProvidedChecksumsSource;
045import org.eclipse.aether.internal.impl.TrackingFileManager;
046import org.eclipse.aether.internal.impl.checksum.Md5ChecksumAlgorithmFactory;
047import org.eclipse.aether.internal.impl.checksum.Sha1ChecksumAlgorithmFactory;
048import org.eclipse.aether.internal.impl.checksum.Sha256ChecksumAlgorithmFactory;
049import org.eclipse.aether.internal.impl.checksum.Sha512ChecksumAlgorithmFactory;
050import org.eclipse.aether.internal.impl.checksum.DefaultChecksumAlgorithmFactorySelector;
051import org.eclipse.aether.internal.impl.collect.DependencyCollectorDelegate;
052import org.eclipse.aether.internal.impl.collect.bf.BfDependencyCollector;
053import org.eclipse.aether.internal.impl.collect.df.DfDependencyCollector;
054import org.eclipse.aether.internal.impl.synccontext.DefaultSyncContextFactory;
055import org.eclipse.aether.internal.impl.synccontext.named.NamedLockFactorySelector;
056import org.eclipse.aether.internal.impl.synccontext.named.SimpleNamedLockFactorySelector;
057import org.eclipse.aether.internal.impl.synccontext.named.GAVNameMapper;
058import org.eclipse.aether.internal.impl.synccontext.named.DiscriminatingNameMapper;
059import org.eclipse.aether.internal.impl.synccontext.named.NameMapper;
060import org.eclipse.aether.internal.impl.synccontext.named.StaticNameMapper;
061import org.eclipse.aether.internal.impl.synccontext.named.FileGAVNameMapper;
062import org.eclipse.aether.named.NamedLockFactory;
063import org.eclipse.aether.named.providers.FileLockNamedLockFactory;
064import org.eclipse.aether.named.providers.LocalReadWriteLockNamedLockFactory;
065import org.eclipse.aether.named.providers.LocalSemaphoreNamedLockFactory;
066import org.eclipse.aether.impl.UpdateCheckManager;
067import org.eclipse.aether.impl.UpdatePolicyAnalyzer;
068import org.eclipse.aether.internal.impl.DefaultArtifactResolver;
069import org.eclipse.aether.internal.impl.DefaultChecksumPolicyProvider;
070import org.eclipse.aether.internal.impl.collect.DefaultDependencyCollector;
071import org.eclipse.aether.internal.impl.DefaultDeployer;
072import org.eclipse.aether.internal.impl.DefaultFileProcessor;
073import org.eclipse.aether.internal.impl.DefaultInstaller;
074import org.eclipse.aether.internal.impl.DefaultLocalRepositoryProvider;
075import org.eclipse.aether.internal.impl.DefaultMetadataResolver;
076import org.eclipse.aether.internal.impl.DefaultOfflineController;
077import org.eclipse.aether.internal.impl.DefaultRemoteRepositoryManager;
078import org.eclipse.aether.internal.impl.DefaultRepositoryConnectorProvider;
079import org.eclipse.aether.internal.impl.DefaultRepositoryEventDispatcher;
080import org.eclipse.aether.internal.impl.DefaultRepositoryLayoutProvider;
081import org.eclipse.aether.internal.impl.DefaultRepositorySystem;
082import org.eclipse.aether.internal.impl.DefaultTransporterProvider;
083import org.eclipse.aether.internal.impl.DefaultUpdateCheckManager;
084import org.eclipse.aether.internal.impl.DefaultUpdatePolicyAnalyzer;
085import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
086import org.eclipse.aether.internal.impl.Maven2RepositoryLayoutFactory;
087import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
088import org.eclipse.aether.internal.impl.slf4j.Slf4jLoggerFactory;
089import org.eclipse.aether.named.providers.NoopNamedLockFactory;
090import org.eclipse.aether.spi.connector.checksum.ProvidedChecksumsSource;
091import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactorySelector;
092import org.eclipse.aether.spi.connector.checksum.ChecksumPolicyProvider;
093import org.eclipse.aether.spi.connector.checksum.ChecksumAlgorithmFactory;
094import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory;
095import org.eclipse.aether.spi.connector.layout.RepositoryLayoutProvider;
096import org.eclipse.aether.spi.connector.transport.TransporterProvider;
097import org.eclipse.aether.spi.io.FileProcessor;
098import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory;
099import org.eclipse.aether.spi.log.LoggerFactory;
100import org.eclipse.aether.spi.synccontext.SyncContextFactory;
101import org.slf4j.ILoggerFactory;
102
103import com.google.inject.AbstractModule;
104import com.google.inject.Provides;
105import com.google.inject.name.Names;
106
107/**
108 * A ready-made <a href="https://github.com/google/guice" target="_blank">Guice</a> module that sets up bindings
109 * for all components from this library. To acquire a complete repository system, clients need to bind an artifact
110 * descriptor reader, a version resolver, a version range resolver, zero or more metadata generator factories, some
111 * repository connector and transporter factories to access remote repositories.
112 *
113 * @noextend This class must not be extended by clients and will eventually be marked {@code final} without prior
114 * notice.
115 */
116public class AetherModule
117        extends AbstractModule
118{
119
120    /**
121     * Creates a new instance of this Guice module, typically for invoking
122     * {@link com.google.inject.Binder#install(com.google.inject.Module)}.
123     */
124    public AetherModule()
125    {
126    }
127
128    /**
129     * Configures Guice with bindings for Aether components provided by this library.
130     */
131    @Override
132    protected void configure()
133    {
134        bind( RepositorySystem.class ) //
135                .to( DefaultRepositorySystem.class ).in( Singleton.class );
136        bind( ArtifactResolver.class ) //
137                .to( DefaultArtifactResolver.class ).in( Singleton.class );
138
139        bind( DependencyCollector.class ) //
140                .to( DefaultDependencyCollector.class ).in( Singleton.class );
141        bind( DependencyCollectorDelegate.class ).annotatedWith( Names.named( BfDependencyCollector.NAME ) )
142                .to( BfDependencyCollector.class ).in( Singleton.class );
143        bind( DependencyCollectorDelegate.class ).annotatedWith( Names.named( DfDependencyCollector.NAME ) )
144                .to( DfDependencyCollector.class ).in( Singleton.class );
145
146        bind( Deployer.class ) //
147                .to( DefaultDeployer.class ).in( Singleton.class );
148        bind( Installer.class ) //
149                .to( DefaultInstaller.class ).in( Singleton.class );
150        bind( MetadataResolver.class ) //
151                .to( DefaultMetadataResolver.class ).in( Singleton.class );
152        bind( RepositoryLayoutProvider.class ) //
153                .to( DefaultRepositoryLayoutProvider.class ).in( Singleton.class );
154        bind( RepositoryLayoutFactory.class ).annotatedWith( Names.named( "maven2" ) ) //
155                .to( Maven2RepositoryLayoutFactory.class ).in( Singleton.class );
156        bind( TransporterProvider.class ) //
157                .to( DefaultTransporterProvider.class ).in( Singleton.class );
158        bind( ChecksumPolicyProvider.class ) //
159                .to( DefaultChecksumPolicyProvider.class ).in( Singleton.class );
160        bind( RepositoryConnectorProvider.class ) //
161                .to( DefaultRepositoryConnectorProvider.class ).in( Singleton.class );
162        bind( RemoteRepositoryManager.class ) //
163                .to( DefaultRemoteRepositoryManager.class ).in( Singleton.class );
164        bind( UpdateCheckManager.class ) //
165                .to( DefaultUpdateCheckManager.class ).in( Singleton.class );
166        bind( UpdatePolicyAnalyzer.class ) //
167                .to( DefaultUpdatePolicyAnalyzer.class ).in( Singleton.class );
168        bind( FileProcessor.class ) //
169                .to( DefaultFileProcessor.class ).in( Singleton.class );
170        bind( RepositoryEventDispatcher.class ) //
171                .to( DefaultRepositoryEventDispatcher.class ).in( Singleton.class );
172        bind( OfflineController.class ) //
173                .to( DefaultOfflineController.class ).in( Singleton.class );
174        bind( LocalRepositoryProvider.class ) //
175                .to( DefaultLocalRepositoryProvider.class ).in( Singleton.class );
176        bind( LocalRepositoryManagerFactory.class ).annotatedWith( Names.named( "simple" ) ) //
177                .to( SimpleLocalRepositoryManagerFactory.class ).in( Singleton.class );
178        bind( LocalRepositoryManagerFactory.class ).annotatedWith( Names.named( "enhanced" ) ) //
179                .to( EnhancedLocalRepositoryManagerFactory.class ).in( Singleton.class );
180        bind( TrackingFileManager.class ).to( DefaultTrackingFileManager.class ).in( Singleton.class );
181
182        bind( ProvidedChecksumsSource.class ).annotatedWith( Names.named( FileProvidedChecksumsSource.NAME ) ) //
183            .to( FileProvidedChecksumsSource.class ).in( Singleton.class );
184
185        bind( ChecksumAlgorithmFactory.class ).annotatedWith( Names.named( Md5ChecksumAlgorithmFactory.NAME ) )
186                .to( Md5ChecksumAlgorithmFactory.class );
187        bind( ChecksumAlgorithmFactory.class ).annotatedWith( Names.named( Sha1ChecksumAlgorithmFactory.NAME ) )
188                .to( Sha1ChecksumAlgorithmFactory.class );
189        bind( ChecksumAlgorithmFactory.class ).annotatedWith( Names.named( Sha256ChecksumAlgorithmFactory.NAME ) )
190                .to( Sha256ChecksumAlgorithmFactory.class );
191        bind( ChecksumAlgorithmFactory.class ).annotatedWith( Names.named( Sha512ChecksumAlgorithmFactory.NAME ) )
192                .to( Sha512ChecksumAlgorithmFactory.class );
193        bind( ChecksumAlgorithmFactorySelector.class )
194                .to( DefaultChecksumAlgorithmFactorySelector.class ).in ( Singleton.class );
195
196        bind( NamedLockFactorySelector.class ).to( SimpleNamedLockFactorySelector.class ).in( Singleton.class );
197        bind( SyncContextFactory.class ).to( DefaultSyncContextFactory.class ).in( Singleton.class );
198        bind( org.eclipse.aether.impl.SyncContextFactory.class )
199                .to( org.eclipse.aether.internal.impl.synccontext.legacy.DefaultSyncContextFactory.class )
200                .in( Singleton.class );
201
202        bind( NameMapper.class ).annotatedWith( Names.named( StaticNameMapper.NAME ) )
203                .to( StaticNameMapper.class ).in( Singleton.class );
204        bind( NameMapper.class ).annotatedWith( Names.named( GAVNameMapper.NAME ) )
205                .to( GAVNameMapper.class ).in( Singleton.class );
206        bind( NameMapper.class ).annotatedWith( Names.named( DiscriminatingNameMapper.NAME ) )
207                .to( DiscriminatingNameMapper.class ).in( Singleton.class );
208        bind( NameMapper.class ).annotatedWith( Names.named( FileGAVNameMapper.NAME ) )
209                .to( FileGAVNameMapper.class ).in( Singleton.class );
210
211        bind( NamedLockFactory.class ).annotatedWith( Names.named( NoopNamedLockFactory.NAME ) )
212                .to( NoopNamedLockFactory.class ).in( Singleton.class );
213        bind( NamedLockFactory.class ).annotatedWith( Names.named( LocalReadWriteLockNamedLockFactory.NAME ) )
214                .to( LocalReadWriteLockNamedLockFactory.class ).in( Singleton.class );
215        bind( NamedLockFactory.class ).annotatedWith( Names.named( LocalSemaphoreNamedLockFactory.NAME ) )
216                .to( LocalSemaphoreNamedLockFactory.class ).in( Singleton.class );
217        bind( NamedLockFactory.class ).annotatedWith( Names.named( FileLockNamedLockFactory.NAME ) )
218                .to( FileLockNamedLockFactory.class ).in( Singleton.class );
219
220        install( new Slf4jModule() );
221
222    }
223
224    @Provides
225    @Singleton
226    Map<String, DependencyCollectorDelegate> dependencyCollectorDelegates(
227            @Named( BfDependencyCollector.NAME ) DependencyCollectorDelegate bf,
228            @Named( DfDependencyCollector.NAME ) DependencyCollectorDelegate df
229    )
230    {
231        Map<String, DependencyCollectorDelegate> dependencyCollectorDelegates = new HashMap<>();
232        dependencyCollectorDelegates.put( BfDependencyCollector.NAME, bf );
233        dependencyCollectorDelegates.put( DfDependencyCollector.NAME, df );
234        return dependencyCollectorDelegates;
235    }
236
237    @Provides
238    @Singleton
239    Map<String, ProvidedChecksumsSource> provideChecksumSources(
240        @Named( FileProvidedChecksumsSource.NAME ) ProvidedChecksumsSource fileProvidedChecksumSource
241    )
242    {
243        Map<String, ProvidedChecksumsSource> providedChecksumsSource = new HashMap<>();
244        providedChecksumsSource.put( FileProvidedChecksumsSource.NAME, fileProvidedChecksumSource );
245        return providedChecksumsSource;
246    }
247
248    @Provides
249    @Singleton
250    Map<String, ChecksumAlgorithmFactory> provideChecksumTypes(
251            @Named( Sha512ChecksumAlgorithmFactory.NAME ) ChecksumAlgorithmFactory sha512,
252            @Named( Sha256ChecksumAlgorithmFactory.NAME ) ChecksumAlgorithmFactory sha256,
253            @Named( Sha1ChecksumAlgorithmFactory.NAME ) ChecksumAlgorithmFactory sha1,
254            @Named( Md5ChecksumAlgorithmFactory.NAME ) ChecksumAlgorithmFactory md5 )
255    {
256        Map<String, ChecksumAlgorithmFactory> checksumTypes = new HashMap<>();
257        checksumTypes.put( Sha512ChecksumAlgorithmFactory.NAME, sha512 );
258        checksumTypes.put( Sha256ChecksumAlgorithmFactory.NAME, sha256 );
259        checksumTypes.put( Sha1ChecksumAlgorithmFactory.NAME, sha1 );
260        checksumTypes.put( Md5ChecksumAlgorithmFactory.NAME, md5 );
261        return Collections.unmodifiableMap( checksumTypes );
262    }
263
264    @Provides
265    @Singleton
266    Map<String, NameMapper> provideNameMappers(
267            @Named( StaticNameMapper.NAME ) NameMapper staticNameMapper,
268            @Named( GAVNameMapper.NAME ) NameMapper gavNameMapper,
269            @Named( DiscriminatingNameMapper.NAME ) NameMapper discriminatingNameMapper,
270            @Named( FileGAVNameMapper.NAME ) NameMapper fileGavNameMapper )
271    {
272        Map<String, NameMapper> nameMappers = new HashMap<>();
273        nameMappers.put( StaticNameMapper.NAME, staticNameMapper );
274        nameMappers.put( GAVNameMapper.NAME, gavNameMapper );
275        nameMappers.put( DiscriminatingNameMapper.NAME, discriminatingNameMapper );
276        nameMappers.put( FileGAVNameMapper.NAME, fileGavNameMapper );
277        return Collections.unmodifiableMap( nameMappers );
278    }
279
280    @Provides
281    @Singleton
282    Map<String, NamedLockFactory> provideNamedLockFactories(
283            @Named( LocalReadWriteLockNamedLockFactory.NAME ) NamedLockFactory localRwLock,
284            @Named( LocalSemaphoreNamedLockFactory.NAME ) NamedLockFactory localSemaphore,
285            @Named( FileLockNamedLockFactory.NAME ) NamedLockFactory fileLockFactory )
286    {
287        Map<String, NamedLockFactory> factories = new HashMap<>();
288        factories.put( LocalReadWriteLockNamedLockFactory.NAME, localRwLock );
289        factories.put( LocalSemaphoreNamedLockFactory.NAME, localSemaphore );
290        factories.put( FileLockNamedLockFactory.NAME, fileLockFactory );
291        return Collections.unmodifiableMap( factories );
292    }
293
294    @Provides
295    @Singleton
296    Set<LocalRepositoryManagerFactory> provideLocalRepositoryManagerFactories(
297            @Named( "simple" ) LocalRepositoryManagerFactory simple,
298            @Named( "enhanced" ) LocalRepositoryManagerFactory enhanced )
299    {
300        Set<LocalRepositoryManagerFactory> factories = new HashSet<>();
301        factories.add( simple );
302        factories.add( enhanced );
303        return Collections.unmodifiableSet( factories );
304    }
305
306    @Provides
307    @Singleton
308    Set<RepositoryLayoutFactory> provideRepositoryLayoutFactories( @Named( "maven2" ) RepositoryLayoutFactory maven2 )
309    {
310        Set<RepositoryLayoutFactory> factories = new HashSet<>();
311        factories.add( maven2 );
312        return Collections.unmodifiableSet( factories );
313    }
314
315    @Provides
316    @Singleton
317    Set<RepositoryListener> providesRepositoryListeners()
318    {
319        return Collections.emptySet();
320    }
321
322    private static class Slf4jModule
323            extends AbstractModule
324    {
325
326        @Override
327        protected void configure()
328        {
329            bind( LoggerFactory.class ) //
330                    .to( Slf4jLoggerFactory.class );
331        }
332
333        @Provides
334        @Singleton
335        ILoggerFactory getLoggerFactory()
336        {
337            return org.slf4j.LoggerFactory.getILoggerFactory();
338        }
339
340    }
341
342}