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.named.NamedLockFactorySelector;
047import org.eclipse.aether.internal.impl.synccontext.named.SimpleNamedLockFactorySelector;
048import org.eclipse.aether.internal.impl.synccontext.named.GAVNameMapper;
049import org.eclipse.aether.internal.impl.synccontext.named.DiscriminatingNameMapper;
050import org.eclipse.aether.internal.impl.synccontext.named.NameMapper;
051import org.eclipse.aether.internal.impl.synccontext.named.StaticNameMapper;
052import org.eclipse.aether.internal.impl.synccontext.named.FileGAVNameMapper;
053import org.eclipse.aether.named.NamedLockFactory;
054import org.eclipse.aether.named.providers.FileLockNamedLockFactory;
055import org.eclipse.aether.named.providers.LocalReadWriteLockNamedLockFactory;
056import org.eclipse.aether.named.providers.LocalSemaphoreNamedLockFactory;
057import org.eclipse.aether.impl.UpdateCheckManager;
058import org.eclipse.aether.impl.UpdatePolicyAnalyzer;
059import org.eclipse.aether.internal.impl.DefaultArtifactResolver;
060import org.eclipse.aether.internal.impl.DefaultChecksumPolicyProvider;
061import org.eclipse.aether.internal.impl.collect.DefaultDependencyCollector;
062import org.eclipse.aether.internal.impl.DefaultDeployer;
063import org.eclipse.aether.internal.impl.DefaultFileProcessor;
064import org.eclipse.aether.internal.impl.DefaultInstaller;
065import org.eclipse.aether.internal.impl.DefaultLocalRepositoryProvider;
066import org.eclipse.aether.internal.impl.DefaultMetadataResolver;
067import org.eclipse.aether.internal.impl.DefaultOfflineController;
068import org.eclipse.aether.internal.impl.DefaultRemoteRepositoryManager;
069import org.eclipse.aether.internal.impl.DefaultRepositoryConnectorProvider;
070import org.eclipse.aether.internal.impl.DefaultRepositoryEventDispatcher;
071import org.eclipse.aether.internal.impl.DefaultRepositoryLayoutProvider;
072import org.eclipse.aether.internal.impl.DefaultRepositorySystem;
073import org.eclipse.aether.internal.impl.DefaultTransporterProvider;
074import org.eclipse.aether.internal.impl.DefaultUpdateCheckManager;
075import org.eclipse.aether.internal.impl.DefaultUpdatePolicyAnalyzer;
076import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
077import org.eclipse.aether.internal.impl.Maven2RepositoryLayoutFactory;
078import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
079import org.eclipse.aether.internal.impl.slf4j.Slf4jLoggerFactory;
080import org.eclipse.aether.named.providers.NoopNamedLockFactory;
081import org.eclipse.aether.spi.connector.checksum.ChecksumPolicyProvider;
082import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory;
083import org.eclipse.aether.spi.connector.layout.RepositoryLayoutProvider;
084import org.eclipse.aether.spi.connector.transport.TransporterProvider;
085import org.eclipse.aether.spi.io.FileProcessor;
086import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory;
087import org.eclipse.aether.spi.log.LoggerFactory;
088import org.eclipse.aether.spi.synccontext.SyncContextFactory;
089import org.slf4j.ILoggerFactory;
090
091import com.google.inject.AbstractModule;
092import com.google.inject.Provides;
093import com.google.inject.name.Names;
094
095/**
096 * A ready-made <a href="https://github.com/google/guice" target="_blank">Guice</a> module that sets up bindings
097 * for all components from this library. To acquire a complete repository system, clients need to bind an artifact
098 * descriptor reader, a version resolver, a version range resolver, zero or more metadata generator factories, some
099 * repository connector and transporter factories to access remote repositories.
100 *
101 * @noextend This class must not be extended by clients and will eventually be marked {@code final} without prior
102 *           notice.
103 */
104public class AetherModule
105    extends AbstractModule
106{
107
108    /**
109     * Creates a new instance of this Guice module, typically for invoking
110     * {@link com.google.inject.Binder#install(com.google.inject.Module)}.
111     */
112    public AetherModule()
113    {
114    }
115
116    /**
117     * Configures Guice with bindings for Aether components provided by this library.
118     */
119    @Override
120    protected void configure()
121    {
122        bind( RepositorySystem.class ) //
123        .to( DefaultRepositorySystem.class ).in( Singleton.class );
124        bind( ArtifactResolver.class ) //
125        .to( DefaultArtifactResolver.class ).in( Singleton.class );
126        bind( DependencyCollector.class ) //
127        .to( DefaultDependencyCollector.class ).in( Singleton.class );
128        bind( Deployer.class ) //
129        .to( DefaultDeployer.class ).in( Singleton.class );
130        bind( Installer.class ) //
131        .to( DefaultInstaller.class ).in( Singleton.class );
132        bind( MetadataResolver.class ) //
133        .to( DefaultMetadataResolver.class ).in( Singleton.class );
134        bind( RepositoryLayoutProvider.class ) //
135        .to( DefaultRepositoryLayoutProvider.class ).in( Singleton.class );
136        bind( RepositoryLayoutFactory.class ).annotatedWith( Names.named( "maven2" ) ) //
137        .to( Maven2RepositoryLayoutFactory.class ).in( Singleton.class );
138        bind( TransporterProvider.class ) //
139        .to( DefaultTransporterProvider.class ).in( Singleton.class );
140        bind( ChecksumPolicyProvider.class ) //
141        .to( DefaultChecksumPolicyProvider.class ).in( Singleton.class );
142        bind( RepositoryConnectorProvider.class ) //
143        .to( DefaultRepositoryConnectorProvider.class ).in( Singleton.class );
144        bind( RemoteRepositoryManager.class ) //
145        .to( DefaultRemoteRepositoryManager.class ).in( Singleton.class );
146        bind( UpdateCheckManager.class ) //
147        .to( DefaultUpdateCheckManager.class ).in( Singleton.class );
148        bind( UpdatePolicyAnalyzer.class ) //
149        .to( DefaultUpdatePolicyAnalyzer.class ).in( Singleton.class );
150        bind( FileProcessor.class ) //
151        .to( DefaultFileProcessor.class ).in( Singleton.class );
152        bind( RepositoryEventDispatcher.class ) //
153        .to( DefaultRepositoryEventDispatcher.class ).in( Singleton.class );
154        bind( OfflineController.class ) //
155        .to( DefaultOfflineController.class ).in( Singleton.class );
156        bind( LocalRepositoryProvider.class ) //
157        .to( DefaultLocalRepositoryProvider.class ).in( Singleton.class );
158        bind( LocalRepositoryManagerFactory.class ).annotatedWith( Names.named( "simple" ) ) //
159        .to( SimpleLocalRepositoryManagerFactory.class ).in( Singleton.class );
160        bind( LocalRepositoryManagerFactory.class ).annotatedWith( Names.named( "enhanced" ) ) //
161        .to( EnhancedLocalRepositoryManagerFactory.class ).in( Singleton.class );
162        bind( TrackingFileManager.class ).to( DefaultTrackingFileManager.class ).in( Singleton.class );
163
164        bind( NamedLockFactorySelector.class ).to( SimpleNamedLockFactorySelector.class ).in( Singleton.class );
165        bind( SyncContextFactory.class ).to( DefaultSyncContextFactory.class ).in( Singleton.class );
166        bind( org.eclipse.aether.impl.SyncContextFactory.class )
167                .to( org.eclipse.aether.internal.impl.synccontext.legacy.DefaultSyncContextFactory.class )
168                .in( Singleton.class );
169
170        bind( NameMapper.class ).annotatedWith( Names.named( StaticNameMapper.NAME ) )
171            .to( StaticNameMapper.class ).in( Singleton.class );
172        bind( NameMapper.class ).annotatedWith( Names.named( GAVNameMapper.NAME ) )
173            .to( GAVNameMapper.class ).in( Singleton.class );
174        bind( NameMapper.class ).annotatedWith( Names.named( DiscriminatingNameMapper.NAME ) )
175            .to( DiscriminatingNameMapper.class ).in( Singleton.class );
176        bind( NameMapper.class ).annotatedWith( Names.named( FileGAVNameMapper.NAME ) )
177            .to( FileGAVNameMapper.class ).in( Singleton.class );
178
179        bind( NamedLockFactory.class ).annotatedWith( Names.named( NoopNamedLockFactory.NAME ) )
180            .to( NoopNamedLockFactory.class ).in( Singleton.class );
181        bind( NamedLockFactory.class ).annotatedWith( Names.named( LocalReadWriteLockNamedLockFactory.NAME ) )
182            .to( LocalReadWriteLockNamedLockFactory.class ).in( Singleton.class );
183        bind( NamedLockFactory.class ).annotatedWith( Names.named( LocalSemaphoreNamedLockFactory.NAME ) )
184            .to( LocalSemaphoreNamedLockFactory.class ).in( Singleton.class );
185        bind( NamedLockFactory.class ).annotatedWith( Names.named( FileLockNamedLockFactory.NAME ) )
186            .to( FileLockNamedLockFactory.class ).in( Singleton.class );
187
188        install( new Slf4jModule() );
189
190    }
191
192    @Provides
193    @Singleton
194    Map<String, NameMapper> provideNameMappers(
195        @Named( StaticNameMapper.NAME ) NameMapper staticNameMapper,
196        @Named( GAVNameMapper.NAME ) NameMapper gavNameMapper,
197        @Named( DiscriminatingNameMapper.NAME ) NameMapper discriminatingNameMapper,
198        @Named( FileGAVNameMapper.NAME ) NameMapper fileGavNameMapper )
199    {
200        Map<String, NameMapper> nameMappers = new HashMap<>();
201        nameMappers.put( StaticNameMapper.NAME, staticNameMapper );
202        nameMappers.put( GAVNameMapper.NAME, gavNameMapper );
203        nameMappers.put( DiscriminatingNameMapper.NAME, discriminatingNameMapper );
204        nameMappers.put( FileGAVNameMapper.NAME, fileGavNameMapper );
205        return Collections.unmodifiableMap( nameMappers );
206    }
207
208    @Provides
209    @Singleton
210    Map<String, NamedLockFactory> provideNamedLockFactories(
211            @Named( LocalReadWriteLockNamedLockFactory.NAME ) NamedLockFactory localRwLock,
212            @Named( LocalSemaphoreNamedLockFactory.NAME ) NamedLockFactory localSemaphore,
213            @Named( FileLockNamedLockFactory.NAME ) NamedLockFactory fileLockFactory )
214    {
215        Map<String, NamedLockFactory> factories = new HashMap<>();
216        factories.put( LocalReadWriteLockNamedLockFactory.NAME, localRwLock );
217        factories.put( LocalSemaphoreNamedLockFactory.NAME, localSemaphore );
218        factories.put( FileLockNamedLockFactory.NAME, fileLockFactory );
219        return Collections.unmodifiableMap( factories );
220    }
221
222    @Provides
223    @Singleton
224    Set<LocalRepositoryManagerFactory> provideLocalRepositoryManagerFactories(
225            @Named( "simple" ) LocalRepositoryManagerFactory simple,
226            @Named( "enhanced" ) LocalRepositoryManagerFactory enhanced )
227    {
228        Set<LocalRepositoryManagerFactory> factories = new HashSet<>();
229        factories.add( simple );
230        factories.add( enhanced );
231        return Collections.unmodifiableSet( factories );
232    }
233
234    @Provides
235    @Singleton
236    Set<RepositoryLayoutFactory> provideRepositoryLayoutFactories( @Named( "maven2" ) RepositoryLayoutFactory maven2 )
237    {
238        Set<RepositoryLayoutFactory> factories = new HashSet<>();
239        factories.add( maven2 );
240        return Collections.unmodifiableSet( factories );
241    }
242
243    @Provides
244    @Singleton
245    Set<RepositoryListener> providesRepositoryListeners()
246    {
247        return Collections.emptySet();
248    }
249
250    private static class Slf4jModule
251        extends AbstractModule
252    {
253
254        @Override
255        protected void configure()
256        {
257            bind( LoggerFactory.class ) //
258            .to( Slf4jLoggerFactory.class );
259        }
260
261        @Provides
262        @Singleton
263        ILoggerFactory getLoggerFactory()
264        {
265            return org.slf4j.LoggerFactory.getILoggerFactory();
266        }
267
268    }
269
270}