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  import org.apache.maven.artifact.repository.metadata.Metadata;
28  import org.apache.maven.artifact.repository.metadata.Snapshot;
29  import org.apache.maven.artifact.repository.metadata.SnapshotVersion;
30  import org.apache.maven.artifact.repository.metadata.Versioning;
31  import org.eclipse.aether.artifact.Artifact;
32  
33  /**
34   * Maven local GAV level metadata.
35   */
36  final class LocalSnapshotMetadata extends MavenMetadata {
37  
38      private final Collection<Artifact> artifacts = new ArrayList<>();
39  
40      private final boolean legacyFormat;
41  
42      LocalSnapshotMetadata(Artifact artifact, boolean legacyFormat, Date timestamp) {
43          super(createMetadata(artifact, legacyFormat), null, timestamp);
44          this.legacyFormat = legacyFormat;
45      }
46  
47      LocalSnapshotMetadata(Metadata metadata, File file, boolean legacyFormat, Date timestamp) {
48          super(metadata, file, timestamp);
49          this.legacyFormat = legacyFormat;
50      }
51  
52      private static Metadata createMetadata(Artifact artifact, boolean legacyFormat) {
53          Snapshot snapshot = new Snapshot();
54          snapshot.setLocalCopy(true);
55          Versioning versioning = new Versioning();
56          versioning.setSnapshot(snapshot);
57  
58          Metadata metadata = new Metadata();
59          metadata.setVersioning(versioning);
60          metadata.setGroupId(artifact.getGroupId());
61          metadata.setArtifactId(artifact.getArtifactId());
62          metadata.setVersion(artifact.getBaseVersion());
63  
64          if (!legacyFormat) {
65              metadata.setModelVersion("1.1.0");
66          }
67  
68          return metadata;
69      }
70  
71      public void bind(Artifact artifact) {
72          artifacts.add(artifact);
73      }
74  
75      public MavenMetadata setFile(File file) {
76          return new LocalSnapshotMetadata(metadata, file, legacyFormat, timestamp);
77      }
78  
79      public Object getKey() {
80          return getGroupId() + ':' + getArtifactId() + ':' + getVersion();
81      }
82  
83      public static Object getKey(Artifact artifact) {
84          return artifact.getGroupId() + ':' + artifact.getArtifactId() + ':' + artifact.getBaseVersion();
85      }
86  
87      @Override
88      protected void merge(Metadata recessive) {
89          metadata.getVersioning().setLastUpdatedTimestamp(timestamp);
90  
91          if (!legacyFormat) {
92              String lastUpdated = metadata.getVersioning().getLastUpdated();
93  
94              Map<String, SnapshotVersion> versions = new LinkedHashMap<>();
95  
96              for (Artifact artifact : artifacts) {
97                  SnapshotVersion sv = new SnapshotVersion();
98                  sv.setClassifier(artifact.getClassifier());
99                  sv.setExtension(artifact.getExtension());
100                 sv.setVersion(getVersion());
101                 sv.setUpdated(lastUpdated);
102                 versions.put(getKey(sv.getClassifier(), sv.getExtension()), sv);
103             }
104 
105             Versioning versioning = recessive.getVersioning();
106             if (versioning != null) {
107                 for (SnapshotVersion sv : versioning.getSnapshotVersions()) {
108                     String key = getKey(sv.getClassifier(), sv.getExtension());
109                     if (!versions.containsKey(key)) {
110                         versions.put(key, sv);
111                     }
112                 }
113             }
114 
115             metadata.getVersioning().setSnapshotVersions(new ArrayList<>(versions.values()));
116         }
117 
118         artifacts.clear();
119     }
120 
121     private String getKey(String classifier, String extension) {
122         return classifier + ':' + extension;
123     }
124 
125     public String getGroupId() {
126         return metadata.getGroupId();
127     }
128 
129     public String getArtifactId() {
130         return metadata.getArtifactId();
131     }
132 
133     public String getVersion() {
134         return metadata.getVersion();
135     }
136 
137     public Nature getNature() {
138         return Nature.SNAPSHOT;
139     }
140 }