View Javadoc

1   package org.apache.maven.repository.internal;
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.Collection;
23  import java.util.Collections;
24  import java.util.Iterator;
25  import java.util.LinkedHashMap;
26  import java.util.Map;
27  
28  import org.sonatype.aether.RepositorySystemSession;
29  import org.sonatype.aether.artifact.Artifact;
30  import org.sonatype.aether.deployment.DeployRequest;
31  import org.sonatype.aether.impl.MetadataGenerator;
32  import org.sonatype.aether.installation.InstallRequest;
33  import org.sonatype.aether.metadata.Metadata;
34  
35  /**
36   * @author Benjamin Bentmann
37   */
38  class VersionsMetadataGenerator
39      implements MetadataGenerator
40  {
41  
42      private Map<Object, VersionsMetadata> versions;
43  
44      private Map<Object, VersionsMetadata> processedVersions;
45  
46      public VersionsMetadataGenerator( RepositorySystemSession session, InstallRequest request )
47      {
48          this( session, request.getMetadata() );
49      }
50  
51      public VersionsMetadataGenerator( RepositorySystemSession session, DeployRequest request )
52      {
53          this( session, request.getMetadata() );
54      }
55  
56      private VersionsMetadataGenerator( RepositorySystemSession session, Collection<? extends Metadata> metadatas )
57      {
58          versions = new LinkedHashMap<Object, VersionsMetadata>();
59          processedVersions = new LinkedHashMap<Object, VersionsMetadata>();
60  
61          /*
62           * NOTE: This should be considered a quirk to support interop with Maven's legacy ArtifactDeployer which
63           * processes one artifact at a time and hence cannot associate the artifacts from the same project to use the
64           * same version index. Allowing the caller to pass in metadata from a previous deployment allows to re-establish
65           * the association between the artifacts of the same project.
66           */
67          for ( Iterator<? extends Metadata> it = metadatas.iterator(); it.hasNext(); )
68          {
69              Metadata metadata = it.next();
70              if ( metadata instanceof VersionsMetadata )
71              {
72                  it.remove();
73                  VersionsMetadata versionsMetadata = (VersionsMetadata) metadata;
74                  processedVersions.put( versionsMetadata.getKey(), versionsMetadata );
75              }
76          }
77      }
78  
79      public Collection<? extends Metadata> prepare( Collection<? extends Artifact> artifacts )
80      {
81          return Collections.emptyList();
82      }
83  
84      public Artifact transformArtifact( Artifact artifact )
85      {
86          return artifact;
87      }
88  
89      public Collection<? extends Metadata> finish( Collection<? extends Artifact> artifacts )
90      {
91          for ( Artifact artifact : artifacts )
92          {
93              Object key = VersionsMetadata.getKey( artifact );
94              if ( processedVersions.get( key ) == null )
95              {
96                  VersionsMetadata versionsMetadata = versions.get( key );
97                  if ( versionsMetadata == null )
98                  {
99                      versionsMetadata = new VersionsMetadata( artifact );
100                     versions.put( key, versionsMetadata );
101                 }
102             }
103         }
104 
105         return versions.values();
106     }
107 
108 }