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.text.DateFormat;
23  import java.text.SimpleDateFormat;
24  import java.util.ArrayList;
25  import java.util.Date;
26  import java.util.GregorianCalendar;
27  import java.util.LinkedHashMap;
28  import java.util.Map;
29  import java.util.TimeZone;
30  
31  import org.apache.maven.artifact.repository.metadata.Metadata;
32  import org.apache.maven.artifact.repository.metadata.Snapshot;
33  import org.apache.maven.artifact.repository.metadata.SnapshotVersion;
34  import org.apache.maven.artifact.repository.metadata.Versioning;
35  import org.eclipse.aether.artifact.Artifact;
36  
37  /**
38   * Maven remote GAV level metadata.
39   */
40  final class RemoteSnapshotMetadata extends MavenSnapshotMetadata {
41      public static final String DEFAULT_SNAPSHOT_TIMESTAMP_FORMAT = "yyyyMMdd.HHmmss";
42  
43      public static final TimeZone DEFAULT_SNAPSHOT_TIME_ZONE = TimeZone.getTimeZone("Etc/UTC");
44  
45      private final Map<String, SnapshotVersion> versions = new LinkedHashMap<>();
46  
47      RemoteSnapshotMetadata(Artifact artifact, boolean legacyFormat, Date timestamp) {
48          super(createRepositoryMetadata(artifact, legacyFormat), null, legacyFormat, timestamp);
49      }
50  
51      private RemoteSnapshotMetadata(Metadata metadata, File file, boolean legacyFormat, Date timestamp) {
52          super(metadata, file, legacyFormat, timestamp);
53      }
54  
55      public MavenMetadata setFile(File file) {
56          return new RemoteSnapshotMetadata(metadata, file, legacyFormat, timestamp);
57      }
58  
59      public String getExpandedVersion(Artifact artifact) {
60          String key = getKey(artifact.getClassifier(), artifact.getExtension());
61          return versions.get(key).getVersion();
62      }
63  
64      @Override
65      protected void merge(Metadata recessive) {
66          Snapshot snapshot;
67          String lastUpdated;
68  
69          if (metadata.getVersioning() == null) {
70              DateFormat utcDateFormatter = new SimpleDateFormat(DEFAULT_SNAPSHOT_TIMESTAMP_FORMAT);
71              utcDateFormatter.setCalendar(new GregorianCalendar());
72              utcDateFormatter.setTimeZone(DEFAULT_SNAPSHOT_TIME_ZONE);
73  
74              snapshot = new Snapshot();
75              snapshot.setBuildNumber(getBuildNumber(recessive) + 1);
76              snapshot.setTimestamp(utcDateFormatter.format(timestamp));
77  
78              Versioning versioning = new Versioning();
79              versioning.setSnapshot(snapshot);
80              versioning.setLastUpdatedTimestamp(timestamp);
81              lastUpdated = versioning.getLastUpdated();
82  
83              metadata.setVersioning(versioning);
84          } else {
85              snapshot = metadata.getVersioning().getSnapshot();
86              lastUpdated = metadata.getVersioning().getLastUpdated();
87          }
88  
89          for (Artifact artifact : artifacts) {
90              String version = artifact.getVersion();
91  
92              if (version.endsWith(SNAPSHOT)) {
93                  String qualifier = snapshot.getTimestamp() + '-' + snapshot.getBuildNumber();
94                  version = version.substring(0, version.length() - SNAPSHOT.length()) + qualifier;
95              }
96  
97              SnapshotVersion sv = new SnapshotVersion();
98              sv.setClassifier(artifact.getClassifier());
99              sv.setExtension(artifact.getExtension());
100             sv.setVersion(version);
101             sv.setUpdated(lastUpdated);
102 
103             versions.put(getKey(sv.getClassifier(), sv.getExtension()), sv);
104         }
105 
106         artifacts.clear();
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         if (!legacyFormat) {
119             metadata.getVersioning().setSnapshotVersions(new ArrayList<>(versions.values()));
120         }
121     }
122 
123     private static int getBuildNumber(Metadata metadata) {
124         int number = 0;
125 
126         Versioning versioning = metadata.getVersioning();
127         if (versioning != null) {
128             Snapshot snapshot = versioning.getSnapshot();
129             if (snapshot != null && snapshot.getBuildNumber() > 0) {
130                 number = snapshot.getBuildNumber();
131             }
132         }
133 
134         return number;
135     }
136 }