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, Date timestamp) {
48          super(createRepositoryMetadata(artifact), null, timestamp);
49      }
50  
51      private RemoteSnapshotMetadata(Metadata metadata, File file, Date timestamp) {
52          super(metadata, file, timestamp);
53      }
54  
55      @Override
56      public MavenMetadata setFile(File file) {
57          return new RemoteSnapshotMetadata(metadata, file, timestamp);
58      }
59  
60      public String getExpandedVersion(Artifact artifact) {
61          String key = getKey(artifact.getClassifier(), artifact.getExtension());
62          return versions.get(key).getVersion();
63      }
64  
65      @Override
66      protected void merge(Metadata recessive) {
67          Snapshot snapshot;
68          String lastUpdated;
69  
70          if (metadata.getVersioning() == null) {
71              DateFormat utcDateFormatter = new SimpleDateFormat(DEFAULT_SNAPSHOT_TIMESTAMP_FORMAT);
72              utcDateFormatter.setCalendar(new GregorianCalendar());
73              utcDateFormatter.setTimeZone(DEFAULT_SNAPSHOT_TIME_ZONE);
74  
75              snapshot = new Snapshot();
76              snapshot.setBuildNumber(getBuildNumber(recessive) + 1);
77              snapshot.setTimestamp(utcDateFormatter.format(timestamp));
78  
79              Versioning versioning = new Versioning();
80              versioning.setSnapshot(snapshot);
81              versioning.setLastUpdatedTimestamp(timestamp);
82              lastUpdated = versioning.getLastUpdated();
83  
84              metadata.setVersioning(versioning);
85          } else {
86              snapshot = metadata.getVersioning().getSnapshot();
87              lastUpdated = metadata.getVersioning().getLastUpdated();
88          }
89  
90          for (Artifact artifact : artifacts) {
91              String version = artifact.getVersion();
92  
93              if (version.endsWith(SNAPSHOT)) {
94                  String qualifier = snapshot.getTimestamp() + '-' + snapshot.getBuildNumber();
95                  version = version.substring(0, version.length() - SNAPSHOT.length()) + qualifier;
96              }
97  
98              SnapshotVersion sv = new SnapshotVersion();
99              sv.setClassifier(artifact.getClassifier());
100             sv.setExtension(artifact.getExtension());
101             sv.setVersion(version);
102             sv.setUpdated(lastUpdated);
103 
104             versions.put(getKey(sv.getClassifier(), sv.getExtension()), sv);
105         }
106 
107         artifacts.clear();
108 
109         Versioning versioning = recessive.getVersioning();
110         if (versioning != null) {
111             for (SnapshotVersion sv : versioning.getSnapshotVersions()) {
112                 String key = getKey(sv.getClassifier(), sv.getExtension());
113                 if (!versions.containsKey(key)) {
114                     versions.put(key, sv);
115                 }
116             }
117         }
118 
119         metadata.getVersioning().setSnapshotVersions(new ArrayList<>(versions.values()));
120     }
121 
122     private static int getBuildNumber(Metadata metadata) {
123         int number = 0;
124 
125         Versioning versioning = metadata.getVersioning();
126         if (versioning != null) {
127             Snapshot snapshot = versioning.getSnapshot();
128             if (snapshot != null && snapshot.getBuildNumber() > 0) {
129                 number = snapshot.getBuildNumber();
130             }
131         }
132 
133         return number;
134     }
135 }