View Javadoc
1   package org.apache.maven.resolver.examples.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.Map;
25  import java.util.Set;
26  
27  import javax.inject.Named;
28  import javax.inject.Singleton;
29  
30  import org.apache.maven.model.building.DefaultModelBuilderFactory;
31  import org.apache.maven.model.building.ModelBuilder;
32  import org.apache.maven.repository.internal.DefaultArtifactDescriptorReader;
33  import org.apache.maven.repository.internal.DefaultVersionRangeResolver;
34  import org.apache.maven.repository.internal.DefaultVersionResolver;
35  import org.apache.maven.repository.internal.SnapshotMetadataGeneratorFactory;
36  import org.apache.maven.repository.internal.VersionsMetadataGeneratorFactory;
37  import org.eclipse.aether.connector.basic.BasicRepositoryConnectorFactory;
38  import org.eclipse.aether.impl.ArtifactDescriptorReader;
39  import org.eclipse.aether.impl.MetadataGeneratorFactory;
40  import org.eclipse.aether.impl.VersionRangeResolver;
41  import org.eclipse.aether.impl.VersionResolver;
42  import org.eclipse.aether.impl.guice.AetherModule;
43  import org.eclipse.aether.spi.connector.RepositoryConnectorFactory;
44  import org.eclipse.aether.spi.connector.transport.TransporterFactory;
45  import org.eclipse.aether.transport.file.FileTransporterFactory;
46  import org.eclipse.aether.transport.http.ChecksumExtractor;
47  import org.eclipse.aether.transport.http.HttpTransporterFactory;
48  
49  import com.google.inject.AbstractModule;
50  import com.google.inject.Provides;
51  import com.google.inject.name.Names;
52  
53  /**
54   * Guice module for Demo Resolver snippets.
55   *
56   * Here, we assemble "complete" module by using {@link AetherModule} (see it's Javadoc) and adding bits from
57   * Maven itself (binding those components that completes repository system).
58   */
59  class DemoResolverModule
60      extends AbstractModule
61  {
62  
63      @Override
64      protected void configure()
65      {
66          // NOTE: see org.eclipse.aether.impl.guice.AetherModule Javadoc:
67          // AetherModule alone is "ready-made" but incomplete. To have a complete resolver, we
68          // actually need to bind the missing components making module complete.
69          install( new AetherModule() );
70  
71          // make module "complete" by binding things not bound by AetherModule
72          bind( ArtifactDescriptorReader.class ).to( DefaultArtifactDescriptorReader.class ).in( Singleton.class );
73          bind( VersionResolver.class ).to( DefaultVersionResolver.class ).in( Singleton.class );
74          bind( VersionRangeResolver.class ).to( DefaultVersionRangeResolver.class ).in( Singleton.class );
75          bind( MetadataGeneratorFactory.class ).annotatedWith( Names.named( "snapshot" ) )
76                                                .to( SnapshotMetadataGeneratorFactory.class ).in( Singleton.class );
77  
78          bind( MetadataGeneratorFactory.class ).annotatedWith( Names.named( "versions" ) )
79                                                .to( VersionsMetadataGeneratorFactory.class ).in( Singleton.class );
80  
81          bind( RepositoryConnectorFactory.class ).annotatedWith( Names.named( "basic" ) )
82                  .to( BasicRepositoryConnectorFactory.class );
83          bind( TransporterFactory.class ).annotatedWith( Names.named( "file" ) ).to( FileTransporterFactory.class );
84          bind( TransporterFactory.class ).annotatedWith( Names.named( "http" ) ).to( HttpTransporterFactory.class );
85      }
86  
87      /**
88       * Checksum extractors (none).
89       */
90      @Provides
91      @Singleton
92      Map<String, ChecksumExtractor> provideChecksumExtractors()
93      {
94          return Collections.emptyMap();
95      }
96  
97      /**
98       * Repository system connectors (needed for remote transport).
99       */
100     @Provides
101     @Singleton
102     Set<RepositoryConnectorFactory> provideRepositoryConnectorFactories(
103             @Named( "basic" ) RepositoryConnectorFactory basic )
104     {
105         Set<RepositoryConnectorFactory> factories = new HashSet<>();
106         factories.add( basic );
107         return Collections.unmodifiableSet( factories );
108     }
109 
110     /**
111      * Repository system transporters (needed for remote transport).
112      */
113     @Provides
114     @Singleton
115     Set<TransporterFactory> provideTransporterFactories( @Named( "file" ) TransporterFactory file,
116                                                          @Named( "http" ) TransporterFactory http )
117     {
118         Set<TransporterFactory> factories = new HashSet<>();
119         factories.add( file );
120         factories.add( http );
121         return Collections.unmodifiableSet( factories );
122     }
123 
124     /**
125      * Repository metadata generators (needed for remote transport).
126      */
127     @Provides
128     @Singleton
129     Set<MetadataGeneratorFactory> provideMetadataGeneratorFactories(
130             @Named( "snapshot" ) MetadataGeneratorFactory snapshot,
131             @Named( "versions" ) MetadataGeneratorFactory versions )
132     {
133         Set<MetadataGeneratorFactory> factories = new HashSet<>( 2 );
134         factories.add( snapshot );
135         factories.add( versions );
136         return Collections.unmodifiableSet( factories );
137     }
138 
139     /**
140      * Simple instance provider for model builder factory. Note: Maven 3.8.1 {@link ModelBuilder} is annotated
141      * and would require much more.
142      */
143     @Provides
144     ModelBuilder provideModelBuilder()
145     {
146         return new DefaultModelBuilderFactory().newInstance();
147     }
148 }