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.text.DateFormat;
24  import java.text.SimpleDateFormat;
25  import java.util.ArrayList;
26  import java.util.Date;
27  import java.util.GregorianCalendar;
28  import java.util.LinkedHashMap;
29  import java.util.Map;
30  import java.util.TimeZone;
31  
32  import org.apache.maven.artifact.repository.metadata.Metadata;
33  import org.apache.maven.artifact.repository.metadata.Snapshot;
34  import org.apache.maven.artifact.repository.metadata.SnapshotVersion;
35  import org.apache.maven.artifact.repository.metadata.Versioning;
36  import org.eclipse.aether.artifact.Artifact;
37  
38  /**
39   * Maven remote GAV level metadata.
40   *
41   * @deprecated since 4.0.0, use {@code maven-api-impl} jar instead
42   */
43  @Deprecated(since = "4.0.0")
44  final class RemoteSnapshotMetadata extends MavenSnapshotMetadata {
45      public static final String DEFAULT_SNAPSHOT_TIMESTAMP_FORMAT = "yyyyMMdd.HHmmss";
46  
47      public static final TimeZone DEFAULT_SNAPSHOT_TIME_ZONE = TimeZone.getTimeZone("Etc/UTC");
48  
49      private final Map<String, SnapshotVersion> versions = new LinkedHashMap<>();
50  
51      private final Integer buildNumber;
52  
53      RemoteSnapshotMetadata(Artifact artifact, Date timestamp, Integer buildNumber) {
54          super(createRepositoryMetadata(artifact), null, timestamp);
55          this.buildNumber = buildNumber;
56      }
57  
58      private RemoteSnapshotMetadata(Metadata metadata, Path path, Date timestamp, Integer buildNumber) {
59          super(metadata, path, timestamp);
60          this.buildNumber = buildNumber;
61      }
62  
63      @Deprecated
64      @Override
65      public MavenMetadata setFile(File file) {
66          return new RemoteSnapshotMetadata(metadata, file.toPath(), timestamp, buildNumber);
67      }
68  
69      @Override
70      public MavenMetadata setPath(Path path) {
71          return new RemoteSnapshotMetadata(metadata, path, timestamp, buildNumber);
72      }
73  
74      public String getExpandedVersion(Artifact artifact) {
75          String key = getKey(artifact.getClassifier(), artifact.getExtension());
76          return versions.get(key).getVersion();
77      }
78  
79      @Override
80      protected void merge(Metadata recessive) {
81          Snapshot snapshot;
82          String lastUpdated;
83  
84          if (metadata.getVersioning() == null) {
85              DateFormat utcDateFormatter = new SimpleDateFormat(DEFAULT_SNAPSHOT_TIMESTAMP_FORMAT);
86              utcDateFormatter.setCalendar(new GregorianCalendar());
87              utcDateFormatter.setTimeZone(DEFAULT_SNAPSHOT_TIME_ZONE);
88  
89              snapshot = new Snapshot();
90              snapshot.setBuildNumber(buildNumber != null ? buildNumber : getBuildNumber(recessive) + 1);
91              snapshot.setTimestamp(utcDateFormatter.format(timestamp));
92  
93              Versioning versioning = new Versioning();
94              versioning.setSnapshot(snapshot);
95              versioning.setLastUpdatedTimestamp(timestamp);
96              lastUpdated = versioning.getLastUpdated();
97  
98              metadata.setVersioning(versioning);
99          } else {
100             snapshot = metadata.getVersioning().getSnapshot();
101             lastUpdated = metadata.getVersioning().getLastUpdated();
102         }
103 
104         for (Artifact artifact : artifacts) {
105             String version = artifact.getVersion();
106 
107             if (version.endsWith(SNAPSHOT)) {
108                 String qualifier = snapshot.getTimestamp() + '-' + snapshot.getBuildNumber();
109                 version = version.substring(0, version.length() - SNAPSHOT.length()) + qualifier;
110             }
111 
112             SnapshotVersion sv = new SnapshotVersion();
113             sv.setClassifier(artifact.getClassifier());
114             sv.setExtension(artifact.getExtension());
115             sv.setVersion(version);
116             sv.setUpdated(lastUpdated);
117 
118             versions.put(getKey(sv.getClassifier(), sv.getExtension()), sv);
119         }
120 
121         artifacts.clear();
122 
123         Versioning versioning = recessive.getVersioning();
124         if (versioning != null) {
125             for (SnapshotVersion sv : versioning.getSnapshotVersions()) {
126                 String key = getKey(sv.getClassifier(), sv.getExtension());
127                 if (!versions.containsKey(key)) {
128                     versions.put(key, sv);
129                 }
130             }
131         }
132 
133         metadata.getVersioning().setSnapshotVersions(new ArrayList<>(versions.values()));
134 
135         // just carry-on as-is
136         if (!recessive.getPlugins().isEmpty()) {
137             metadata.setPlugins(new ArrayList<>(recessive.getPlugins()));
138         }
139     }
140 
141     private static int getBuildNumber(Metadata metadata) {
142         int number = 0;
143 
144         Versioning versioning = metadata.getVersioning();
145         if (versioning != null) {
146             Snapshot snapshot = versioning.getSnapshot();
147             if (snapshot != null && snapshot.getBuildNumber() > 0) {
148                 number = snapshot.getBuildNumber();
149             }
150         }
151 
152         return number;
153     }
154 }