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