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.nio.file.Path;
23  import java.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Date;
26  import java.util.LinkedHashMap;
27  import java.util.Map;
28  
29  import org.apache.maven.artifact.repository.metadata.Metadata;
30  import org.apache.maven.artifact.repository.metadata.Snapshot;
31  import org.apache.maven.artifact.repository.metadata.SnapshotVersion;
32  import org.apache.maven.artifact.repository.metadata.Versioning;
33  import org.eclipse.aether.artifact.Artifact;
34  
35  /**
36   * Maven local GAV level metadata.
37   *
38   * @deprecated since 4.0.0, use {@code maven-api-impl} jar instead
39   */
40  @Deprecated(since = "4.0.0")
41  final class LocalSnapshotMetadata extends MavenMetadata {
42  
43      private final Collection<Artifact> artifacts = new ArrayList<>();
44  
45      LocalSnapshotMetadata(Artifact artifact, Date timestamp) {
46          super(createMetadata(artifact), (Path) null, timestamp);
47      }
48  
49      LocalSnapshotMetadata(Metadata metadata, Path path, Date timestamp) {
50          super(metadata, path, timestamp);
51      }
52  
53      private static Metadata createMetadata(Artifact artifact) {
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          metadata.setModelVersion("1.1.0");
65          return metadata;
66      }
67  
68      public void bind(Artifact artifact) {
69          artifacts.add(artifact);
70      }
71  
72      @Deprecated
73      @Override
74      public MavenMetadata setFile(File file) {
75          return new LocalSnapshotMetadata(metadata, file.toPath(), timestamp);
76      }
77  
78      @Override
79      public MavenMetadata setPath(Path path) {
80          return new LocalSnapshotMetadata(metadata, path, timestamp);
81      }
82  
83      public Object getKey() {
84          return getGroupId() + ':' + getArtifactId() + ':' + getVersion();
85      }
86  
87      public static Object getKey(Artifact artifact) {
88          return artifact.getGroupId() + ':' + artifact.getArtifactId() + ':' + artifact.getBaseVersion();
89      }
90  
91      @Override
92      protected void merge(Metadata recessive) {
93          metadata.getVersioning().setLastUpdatedTimestamp(timestamp);
94  
95          String lastUpdated = metadata.getVersioning().getLastUpdated();
96  
97          Map<String, SnapshotVersion> versions = new LinkedHashMap<>();
98  
99          for (Artifact artifact : artifacts) {
100             SnapshotVersion sv = new SnapshotVersion();
101             sv.setClassifier(artifact.getClassifier());
102             sv.setExtension(artifact.getExtension());
103             sv.setVersion(getVersion());
104             sv.setUpdated(lastUpdated);
105             versions.put(getKey(sv.getClassifier(), sv.getExtension()), sv);
106         }
107 
108         Versioning versioning = recessive.getVersioning();
109         if (versioning != null) {
110             for (SnapshotVersion sv : versioning.getSnapshotVersions()) {
111                 String key = getKey(sv.getClassifier(), sv.getExtension());
112                 if (!versions.containsKey(key)) {
113                     versions.put(key, sv);
114                 }
115             }
116         }
117 
118         metadata.getVersioning().setSnapshotVersions(new ArrayList<>(versions.values()));
119 
120         // just carry-on as-is
121         if (!recessive.getPlugins().isEmpty()) {
122             metadata.setPlugins(new ArrayList<>(recessive.getPlugins()));
123         }
124 
125         artifacts.clear();
126     }
127 
128     private String getKey(String classifier, String extension) {
129         return classifier + ':' + extension;
130     }
131 
132     @Override
133     public String getGroupId() {
134         return metadata.getGroupId();
135     }
136 
137     @Override
138     public String getArtifactId() {
139         return metadata.getArtifactId();
140     }
141 
142     @Override
143     public String getVersion() {
144         return metadata.getVersion();
145     }
146 
147     @Override
148     public Nature getNature() {
149         return Nature.SNAPSHOT;
150     }
151 }