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.TrackingFileManager;
045import org.eclipse.aether.internal.impl.synccontext.DefaultSyncContextFactory;
046import org.eclipse.aether.internal.impl.synccontext.NamedLockFactorySelector;
047import org.eclipse.aether.internal.impl.synccontext.named.GAVNameMapper;
048import org.eclipse.aether.internal.impl.synccontext.named.DiscriminatingNameMapper;
049import org.eclipse.aether.internal.impl.synccontext.named.NameMapper;
050import org.eclipse.aether.internal.impl.synccontext.named.StaticNameMapper;
051import org.eclipse.aether.named.NamedLockFactory;
052import org.eclipse.aether.named.providers.LocalReadWriteLockNamedLockFactory;
053import org.eclipse.aether.named.providers.LocalSemaphoreNamedLockFactory;
054import org.eclipse.aether.impl.UpdateCheckManager;
055import org.eclipse.aether.impl.UpdatePolicyAnalyzer;
056import org.eclipse.aether.internal.impl.DefaultArtifactResolver;
057import org.eclipse.aether.internal.impl.DefaultChecksumPolicyProvider;
058import org.eclipse.aether.internal.impl.collect.DefaultDependencyCollector;
059import org.eclipse.aether.internal.impl.DefaultDeployer;
060import org.eclipse.aether.internal.impl.DefaultFileProcessor;
061import org.eclipse.aether.internal.impl.DefaultInstaller;
062import org.eclipse.aether.internal.impl.DefaultLocalRepositoryProvider;
063import org.eclipse.aether.internal.impl.DefaultMetadataResolver;
064import org.eclipse.aether.internal.impl.DefaultOfflineController;
065import org.eclipse.aether.internal.impl.DefaultRemoteRepositoryManager;
066import org.eclipse.aether.internal.impl.DefaultRepositoryConnectorProvider;
067import org.eclipse.aether.internal.impl.DefaultRepositoryEventDispatcher;
068import org.eclipse.aether.internal.impl.DefaultRepositoryLayoutProvider;
069import org.eclipse.aether.internal.impl.DefaultRepositorySystem;
070import org.eclipse.aether.internal.impl.DefaultTransporterProvider;
071import org.eclipse.aether.internal.impl.DefaultUpdateCheckManager;
072import org.eclipse.aether.internal.impl.DefaultUpdatePolicyAnalyzer;
073import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
074import org.eclipse.aether.internal.impl.Maven2RepositoryLayoutFactory;
075import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
076import org.eclipse.aether.internal.impl.slf4j.Slf4jLoggerFactory;
077import org.eclipse.aether.named.providers.NoopNamedLockFactory;
078import org.eclipse.aether.spi.connector.checksum.ChecksumPolicyProvider;
079import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory;
080import org.eclipse.aether.spi.connector.layout.RepositoryLayoutProvider;
081import org.eclipse.aether.spi.connector.transport.TransporterProvider;
082import org.eclipse.aether.spi.io.FileProcessor;
083import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory;
084import org.eclipse.aether.spi.log.LoggerFactory;
085import org.eclipse.aether.spi.synccontext.SyncContextFactory;
086import org.slf4j.ILoggerFactory;
087
088import com.google.inject.AbstractModule;
089import com.google.inject.Provides;
090import com.google.inject.name.Names;
091
092/**
093 * A ready-made <a href="https://github.com/google/guice" target="_blank">Guice</a> module that sets up bindings
094 * for all components from this library. To acquire a complete repository system, clients need to bind an artifact
095 * descriptor reader, a version resolver, a version range resolver, zero or more metadata generator factories, some
096 * repository connector and transporter factories to access remote repositories.
097 *
098 * @noextend This class must not be extended by clients and will eventually be marked {@code final} without prior
099 *           notice.
100 */
101public 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}