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