View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.repository.internal;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.Collection;
24  import java.util.Date;
25  import java.util.LinkedHashMap;
26  import java.util.Map;
27  
28  import org.apache.maven.artifact.repository.metadata.Metadata;
29  import org.apache.maven.artifact.repository.metadata.Snapshot;
30  import org.apache.maven.artifact.repository.metadata.SnapshotVersion;
31  import org.apache.maven.artifact.repository.metadata.Versioning;
32  import org.eclipse.aether.artifact.Artifact;
33  
34  /**
35   * Maven local GAV level metadata.
36   */
37  final class LocalSnapshotMetadata extends MavenMetadata {
38  
39      private final Collection<Artifact> artifacts = new ArrayList<>();
40  
41      LocalSnapshotMetadata(Artifact artifact, Date timestamp) {
42          super(createMetadata(artifact), null, timestamp);
43      }
44  
45      LocalSnapshotMetadata(Metadata metadata, File file, Date timestamp) {
46          super(metadata, file, timestamp);
47      }
48  
49      private static Metadata createMetadata(Artifact artifact) {
50          Snapshot snapshot = new Snapshot();
51          snapshot.setLocalCopy(true);
52          Versioning versioning = new Versioning();
53          versioning.setSnapshot(snapshot);
54  
55          Metadata metadata = new Metadata();
56          metadata.setVersioning(versioning);
57          metadata.setGroupId(artifact.getGroupId());
58          metadata.setArtifactId(artifact.getArtifactId());
59          metadata.setVersion(artifact.getBaseVersion());
60          metadata.setModelVersion("1.1.0");
61          return metadata;
62      }
63  
64      public void bind(Artifact artifact) {
65          artifacts.add(artifact);
66      }
67  
68      @Override
69      public MavenMetadata setFile(File file) {
70          return new LocalSnapshotMetadata(metadata, file, timestamp);
71      }
72  
73      public Object getKey() {
74          return getGroupId() + ':' + getArtifactId() + ':' + getVersion();
75      }
76  
77      public static Object getKey(Artifact artifact) {
78          return artifact.getGroupId() + ':' + artifact.getArtifactId() + ':' + artifact.getBaseVersion();
79      }
80  
81      @Override
82      protected void merge(Metadata recessive) {
83          metadata.getVersioning().setLastUpdatedTimestamp(timestamp);
84  
85          String lastUpdated = metadata.getVersioning().getLastUpdated();
86  
87          Map<String, SnapshotVersion> versions = new LinkedHashMap<>();
88  
89          for (Artifact artifact : artifacts) {
90              SnapshotVersion sv = new SnapshotVersion();
91              sv.setClassifier(artifact.getClassifier());
92              sv.setExtension(artifact.getExtension());
93              sv.setVersion(getVersion());
94              sv.setUpdated(lastUpdated);
95              versions.put(getKey(sv.getClassifier(), sv.getExtension()), sv);
96          }
97  
98          Versioning versioning = recessive.getVersioning();
99          if (versioning != null) {
100             for (SnapshotVersion sv : versioning.getSnapshotVersions()) {
101                 String key = getKey(sv.getClassifier(), sv.getExtension());
102                 if (!versions.containsKey(key)) {
103                     versions.put(key, sv);
104                 }
105             }
106         }
107 
108         metadata.getVersioning().setSnapshotVersions(new ArrayList<>(versions.values()));
109 
110         artifacts.clear();
111     }
112 
113     private String getKey(String classifier, String extension) {
114         return classifier + ':' + extension;
115     }
116 
117     @Override
118     public String getGroupId() {
119         return metadata.getGroupId();
120     }
121 
122     @Override
123     public String getArtifactId() {
124         return metadata.getArtifactId();
125     }
126 
127     @Override
128     public String getVersion() {
129         return metadata.getVersion();
130     }
131 
132     @Override
133     public Nature getNature() {
134         return Nature.SNAPSHOT;
135     }
136 }