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.HashSet;
024import java.util.Set;
025
026import javax.inject.Named;
027import javax.inject.Singleton;
028
029import org.eclipse.aether.RepositoryListener;
030import org.eclipse.aether.RepositorySystem;
031import org.eclipse.aether.impl.ArtifactResolver;
032import org.eclipse.aether.impl.DependencyCollector;
033import org.eclipse.aether.impl.Deployer;
034import org.eclipse.aether.impl.Installer;
035import org.eclipse.aether.impl.LocalRepositoryProvider;
036import org.eclipse.aether.impl.MetadataResolver;
037import org.eclipse.aether.impl.OfflineController;
038import org.eclipse.aether.impl.RemoteRepositoryManager;
039import org.eclipse.aether.impl.RepositoryConnectorProvider;
040import org.eclipse.aether.impl.RepositoryEventDispatcher;
041import org.eclipse.aether.impl.SyncContextFactory;
042import org.eclipse.aether.impl.UpdateCheckManager;
043import org.eclipse.aether.impl.UpdatePolicyAnalyzer;
044import org.eclipse.aether.internal.impl.DefaultArtifactResolver;
045import org.eclipse.aether.internal.impl.DefaultChecksumPolicyProvider;
046import org.eclipse.aether.internal.impl.DefaultDependencyCollector;
047import org.eclipse.aether.internal.impl.DefaultDeployer;
048import org.eclipse.aether.internal.impl.DefaultFileProcessor;
049import org.eclipse.aether.internal.impl.DefaultInstaller;
050import org.eclipse.aether.internal.impl.DefaultLocalRepositoryProvider;
051import org.eclipse.aether.internal.impl.DefaultMetadataResolver;
052import org.eclipse.aether.internal.impl.DefaultOfflineController;
053import org.eclipse.aether.internal.impl.DefaultRemoteRepositoryManager;
054import org.eclipse.aether.internal.impl.DefaultRepositoryConnectorProvider;
055import org.eclipse.aether.internal.impl.DefaultRepositoryEventDispatcher;
056import org.eclipse.aether.internal.impl.DefaultRepositoryLayoutProvider;
057import org.eclipse.aether.internal.impl.DefaultRepositorySystem;
058import org.eclipse.aether.internal.impl.DefaultSyncContextFactory;
059import org.eclipse.aether.internal.impl.DefaultTransporterProvider;
060import org.eclipse.aether.internal.impl.DefaultUpdateCheckManager;
061import org.eclipse.aether.internal.impl.DefaultUpdatePolicyAnalyzer;
062import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
063import org.eclipse.aether.internal.impl.Maven2RepositoryLayoutFactory;
064import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
065import org.eclipse.aether.internal.impl.slf4j.Slf4jLoggerFactory;
066import org.eclipse.aether.spi.connector.checksum.ChecksumPolicyProvider;
067import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory;
068import org.eclipse.aether.spi.connector.layout.RepositoryLayoutProvider;
069import org.eclipse.aether.spi.connector.transport.TransporterProvider;
070import org.eclipse.aether.spi.io.FileProcessor;
071import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory;
072import org.eclipse.aether.spi.log.LoggerFactory;
073import org.eclipse.aether.spi.log.NullLoggerFactory;
074import org.slf4j.ILoggerFactory;
075
076import com.google.inject.AbstractModule;
077import com.google.inject.Provides;
078import com.google.inject.name.Names;
079
080/**
081 * A ready-made <a href="https://github.com/google/guice" target="_blank">Guice</a> module that sets up bindings
082 * for all components from this library. To acquire a complete repository system, clients need to bind an artifact
083 * descriptor reader, a version resolver, a version range resolver, zero or more metadata generator factories, some
084 * repository connector and transporter factories to access remote repositories.
085 * 
086 * @noextend This class must not be extended by clients and will eventually be marked {@code final} without prior
087 *           notice.
088 */
089public class AetherModule
090    extends AbstractModule
091{
092
093    /**
094     * Creates a new instance of this Guice module, typically for invoking
095     * {@link com.google.inject.Binder#install(com.google.inject.Module)}.
096     */
097    public AetherModule()
098    {
099    }
100
101    /**
102     * Configures Guice with bindings for Aether components provided by this library.
103     */
104    @Override
105    protected void configure()
106    {
107        bind( RepositorySystem.class ) //
108        .to( DefaultRepositorySystem.class ).in( Singleton.class );
109        bind( ArtifactResolver.class ) //
110        .to( DefaultArtifactResolver.class ).in( Singleton.class );
111        bind( DependencyCollector.class ) //
112        .to( DefaultDependencyCollector.class ).in( Singleton.class );
113        bind( Deployer.class ) //
114        .to( DefaultDeployer.class ).in( Singleton.class );
115        bind( Installer.class ) //
116        .to( DefaultInstaller.class ).in( Singleton.class );
117        bind( MetadataResolver.class ) //
118        .to( DefaultMetadataResolver.class ).in( Singleton.class );
119        bind( RepositoryLayoutProvider.class ) //
120        .to( DefaultRepositoryLayoutProvider.class ).in( Singleton.class );
121        bind( RepositoryLayoutFactory.class ).annotatedWith( Names.named( "maven2" ) ) //
122        .to( Maven2RepositoryLayoutFactory.class ).in( Singleton.class );
123        bind( TransporterProvider.class ) //
124        .to( DefaultTransporterProvider.class ).in( Singleton.class );
125        bind( ChecksumPolicyProvider.class ) //
126        .to( DefaultChecksumPolicyProvider.class ).in( Singleton.class );
127        bind( RepositoryConnectorProvider.class ) //
128        .to( DefaultRepositoryConnectorProvider.class ).in( Singleton.class );
129        bind( RemoteRepositoryManager.class ) //
130        .to( DefaultRemoteRepositoryManager.class ).in( Singleton.class );
131        bind( UpdateCheckManager.class ) //
132        .to( DefaultUpdateCheckManager.class ).in( Singleton.class );
133        bind( UpdatePolicyAnalyzer.class ) //
134        .to( DefaultUpdatePolicyAnalyzer.class ).in( Singleton.class );
135        bind( FileProcessor.class ) //
136        .to( DefaultFileProcessor.class ).in( Singleton.class );
137        bind( SyncContextFactory.class ) //
138        .to( DefaultSyncContextFactory.class ).in( Singleton.class );
139        bind( RepositoryEventDispatcher.class ) //
140        .to( DefaultRepositoryEventDispatcher.class ).in( Singleton.class );
141        bind( OfflineController.class ) //
142        .to( DefaultOfflineController.class ).in( Singleton.class );
143        bind( LocalRepositoryProvider.class ) //
144        .to( DefaultLocalRepositoryProvider.class ).in( Singleton.class );
145        bind( LocalRepositoryManagerFactory.class ).annotatedWith( Names.named( "simple" ) ) //
146        .to( SimpleLocalRepositoryManagerFactory.class ).in( Singleton.class );
147        bind( LocalRepositoryManagerFactory.class ).annotatedWith( Names.named( "enhanced" ) ) //
148        .to( EnhancedLocalRepositoryManagerFactory.class ).in( Singleton.class );
149        if ( Slf4jLoggerFactory.isSlf4jAvailable() )
150        {
151            bindSlf4j();
152        }
153        else
154        {
155            bind( LoggerFactory.class ) //
156            .toInstance( NullLoggerFactory.INSTANCE );
157        }
158
159    }
160
161    private void bindSlf4j()
162    {
163        install( new Slf4jModule() );
164    }
165
166    @Provides
167    @Singleton
168    Set<LocalRepositoryManagerFactory> provideLocalRepositoryManagerFactories( @Named( "simple" ) LocalRepositoryManagerFactory simple,
169                                                                               @Named( "enhanced" ) LocalRepositoryManagerFactory enhanced )
170    {
171        Set<LocalRepositoryManagerFactory> factories = new HashSet<LocalRepositoryManagerFactory>();
172        factories.add( simple );
173        factories.add( enhanced );
174        return Collections.unmodifiableSet( factories );
175    }
176
177    @Provides
178    @Singleton
179    Set<RepositoryLayoutFactory> provideRepositoryLayoutFactories( @Named( "maven2" ) RepositoryLayoutFactory maven2 )
180    {
181        Set<RepositoryLayoutFactory> factories = new HashSet<RepositoryLayoutFactory>();
182        factories.add( maven2 );
183        return Collections.unmodifiableSet( factories );
184    }
185
186    @Provides
187    @Singleton
188    Set<RepositoryListener> providesRepositoryListeners()
189    {
190        return Collections.emptySet();
191    }
192
193    private static class Slf4jModule
194        extends AbstractModule
195    {
196
197        @Override
198        protected void configure()
199        {
200            bind( LoggerFactory.class ) //
201            .to( Slf4jLoggerFactory.class );
202        }
203
204        @Provides
205        @Singleton
206        ILoggerFactory getLoggerFactory()
207        {
208            return org.slf4j.LoggerFactory.getILoggerFactory();
209        }
210
211    }
212
213}