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