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.HashSet;
24  import java.util.Set;
25  
26  import javax.inject.Named;
27  import javax.inject.Singleton;
28  
29  import org.eclipse.aether.RepositoryListener;
30  import org.eclipse.aether.RepositorySystem;
31  import org.eclipse.aether.impl.ArtifactResolver;
32  import org.eclipse.aether.impl.DependencyCollector;
33  import org.eclipse.aether.impl.Deployer;
34  import org.eclipse.aether.impl.Installer;
35  import org.eclipse.aether.impl.LocalRepositoryProvider;
36  import org.eclipse.aether.impl.MetadataResolver;
37  import org.eclipse.aether.impl.OfflineController;
38  import org.eclipse.aether.impl.RemoteRepositoryManager;
39  import org.eclipse.aether.impl.RepositoryConnectorProvider;
40  import org.eclipse.aether.impl.RepositoryEventDispatcher;
41  import org.eclipse.aether.impl.SyncContextFactory;
42  import org.eclipse.aether.impl.UpdateCheckManager;
43  import org.eclipse.aether.impl.UpdatePolicyAnalyzer;
44  import org.eclipse.aether.internal.impl.DefaultArtifactResolver;
45  import org.eclipse.aether.internal.impl.DefaultChecksumPolicyProvider;
46  import org.eclipse.aether.internal.impl.DefaultDependencyCollector;
47  import org.eclipse.aether.internal.impl.DefaultDeployer;
48  import org.eclipse.aether.internal.impl.DefaultFileProcessor;
49  import org.eclipse.aether.internal.impl.DefaultInstaller;
50  import org.eclipse.aether.internal.impl.DefaultLocalRepositoryProvider;
51  import org.eclipse.aether.internal.impl.DefaultMetadataResolver;
52  import org.eclipse.aether.internal.impl.DefaultOfflineController;
53  import org.eclipse.aether.internal.impl.DefaultRemoteRepositoryManager;
54  import org.eclipse.aether.internal.impl.DefaultRepositoryConnectorProvider;
55  import org.eclipse.aether.internal.impl.DefaultRepositoryEventDispatcher;
56  import org.eclipse.aether.internal.impl.DefaultRepositoryLayoutProvider;
57  import org.eclipse.aether.internal.impl.DefaultRepositorySystem;
58  import org.eclipse.aether.internal.impl.DefaultSyncContextFactory;
59  import org.eclipse.aether.internal.impl.DefaultTransporterProvider;
60  import org.eclipse.aether.internal.impl.DefaultUpdateCheckManager;
61  import org.eclipse.aether.internal.impl.DefaultUpdatePolicyAnalyzer;
62  import org.eclipse.aether.internal.impl.EnhancedLocalRepositoryManagerFactory;
63  import org.eclipse.aether.internal.impl.Maven2RepositoryLayoutFactory;
64  import org.eclipse.aether.internal.impl.SimpleLocalRepositoryManagerFactory;
65  import org.eclipse.aether.internal.impl.slf4j.Slf4jLoggerFactory;
66  import org.eclipse.aether.spi.connector.checksum.ChecksumPolicyProvider;
67  import org.eclipse.aether.spi.connector.layout.RepositoryLayoutFactory;
68  import org.eclipse.aether.spi.connector.layout.RepositoryLayoutProvider;
69  import org.eclipse.aether.spi.connector.transport.TransporterProvider;
70  import org.eclipse.aether.spi.io.FileProcessor;
71  import org.eclipse.aether.spi.localrepo.LocalRepositoryManagerFactory;
72  import org.eclipse.aether.spi.log.LoggerFactory;
73  import org.eclipse.aether.spi.log.NullLoggerFactory;
74  import org.slf4j.ILoggerFactory;
75  
76  import com.google.inject.AbstractModule;
77  import com.google.inject.Provides;
78  import com.google.inject.name.Names;
79  
80  /**
81   * A ready-made <a href="https://github.com/google/guice" target="_blank">Guice</a> module that sets up bindings
82   * for all components from this library. To acquire a complete repository system, clients need to bind an artifact
83   * descriptor reader, a version resolver, a version range resolver, zero or more metadata generator factories, some
84   * repository connector and transporter factories to access remote repositories.
85   * 
86   * @noextend This class must not be extended by clients and will eventually be marked {@code final} without prior
87   *           notice.
88   */
89  public class AetherModule
90      extends AbstractModule
91  {
92  
93      /**
94       * Creates a new instance of this Guice module, typically for invoking
95       * {@link com.google.inject.Binder#install(com.google.inject.Module)}.
96       */
97      public AetherModule()
98      {
99      }
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 }