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.collect.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.slf4j.ILoggerFactory;
74  
75  import com.google.inject.AbstractModule;
76  import com.google.inject.Provides;
77  import com.google.inject.name.Names;
78  
79  /**
80   * A ready-made <a href="https://github.com/google/guice" target="_blank">Guice</a> module that sets up bindings
81   * for all components from this library. To acquire a complete repository system, clients need to bind an artifact
82   * descriptor reader, a version resolver, a version range resolver, zero or more metadata generator factories, some
83   * repository connector and transporter factories to access remote repositories.
84   * 
85   * @noextend This class must not be extended by clients and will eventually be marked {@code final} without prior
86   *           notice.
87   */
88  public class AetherModule
89      extends AbstractModule
90  {
91  
92      /**
93       * Creates a new instance of this Guice module, typically for invoking
94       * {@link com.google.inject.Binder#install(com.google.inject.Module)}.
95       */
96      public AetherModule()
97      {
98      }
99  
100     /**
101      * Configures Guice with bindings for Aether components provided by this library.
102      */
103     @Override
104     protected void configure()
105     {
106         bind( RepositorySystem.class ) //
107         .to( DefaultRepositorySystem.class ).in( Singleton.class );
108         bind( ArtifactResolver.class ) //
109         .to( DefaultArtifactResolver.class ).in( Singleton.class );
110         bind( DependencyCollector.class ) //
111         .to( DefaultDependencyCollector.class ).in( Singleton.class );
112         bind( Deployer.class ) //
113         .to( DefaultDeployer.class ).in( Singleton.class );
114         bind( Installer.class ) //
115         .to( DefaultInstaller.class ).in( Singleton.class );
116         bind( MetadataResolver.class ) //
117         .to( DefaultMetadataResolver.class ).in( Singleton.class );
118         bind( RepositoryLayoutProvider.class ) //
119         .to( DefaultRepositoryLayoutProvider.class ).in( Singleton.class );
120         bind( RepositoryLayoutFactory.class ).annotatedWith( Names.named( "maven2" ) ) //
121         .to( Maven2RepositoryLayoutFactory.class ).in( Singleton.class );
122         bind( TransporterProvider.class ) //
123         .to( DefaultTransporterProvider.class ).in( Singleton.class );
124         bind( ChecksumPolicyProvider.class ) //
125         .to( DefaultChecksumPolicyProvider.class ).in( Singleton.class );
126         bind( RepositoryConnectorProvider.class ) //
127         .to( DefaultRepositoryConnectorProvider.class ).in( Singleton.class );
128         bind( RemoteRepositoryManager.class ) //
129         .to( DefaultRemoteRepositoryManager.class ).in( Singleton.class );
130         bind( UpdateCheckManager.class ) //
131         .to( DefaultUpdateCheckManager.class ).in( Singleton.class );
132         bind( UpdatePolicyAnalyzer.class ) //
133         .to( DefaultUpdatePolicyAnalyzer.class ).in( Singleton.class );
134         bind( FileProcessor.class ) //
135         .to( DefaultFileProcessor.class ).in( Singleton.class );
136         bind( SyncContextFactory.class ) //
137         .to( DefaultSyncContextFactory.class ).in( Singleton.class );
138         bind( RepositoryEventDispatcher.class ) //
139         .to( DefaultRepositoryEventDispatcher.class ).in( Singleton.class );
140         bind( OfflineController.class ) //
141         .to( DefaultOfflineController.class ).in( Singleton.class );
142         bind( LocalRepositoryProvider.class ) //
143         .to( DefaultLocalRepositoryProvider.class ).in( Singleton.class );
144         bind( LocalRepositoryManagerFactory.class ).annotatedWith( Names.named( "simple" ) ) //
145         .to( SimpleLocalRepositoryManagerFactory.class ).in( Singleton.class );
146         bind( LocalRepositoryManagerFactory.class ).annotatedWith( Names.named( "enhanced" ) ) //
147         .to( EnhancedLocalRepositoryManagerFactory.class ).in( Singleton.class );
148 
149         install( new Slf4jModule() );
150 
151     }
152 
153     @Provides
154     @Singleton
155     Set<LocalRepositoryManagerFactory> provideLocalRepositoryManagerFactories(
156             @Named( "simple" ) LocalRepositoryManagerFactory simple,
157             @Named( "enhanced" ) LocalRepositoryManagerFactory enhanced )
158     {
159         Set<LocalRepositoryManagerFactory> factories = new HashSet<>();
160         factories.add( simple );
161         factories.add( enhanced );
162         return Collections.unmodifiableSet( factories );
163     }
164 
165     @Provides
166     @Singleton
167     Set<RepositoryLayoutFactory> provideRepositoryLayoutFactories( @Named( "maven2" ) RepositoryLayoutFactory maven2 )
168     {
169         Set<RepositoryLayoutFactory> factories = new HashSet<>();
170         factories.add( maven2 );
171         return Collections.unmodifiableSet( factories );
172     }
173 
174     @Provides
175     @Singleton
176     Set<RepositoryListener> providesRepositoryListeners()
177     {
178         return Collections.emptySet();
179     }
180 
181     private static class Slf4jModule
182         extends AbstractModule
183     {
184 
185         @Override
186         protected void configure()
187         {
188             bind( LoggerFactory.class ) //
189             .to( Slf4jLoggerFactory.class );
190         }
191 
192         @Provides
193         @Singleton
194         ILoggerFactory getLoggerFactory()
195         {
196             return org.slf4j.LoggerFactory.getILoggerFactory();
197         }
198 
199     }
200 
201 }