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  final class LocalSnapshotMetadata extends MavenMetadata {
39  
40      private final Collection<Artifact> artifacts = new ArrayList<>();
41  
42      LocalSnapshotMetadata(Artifact artifact, Date timestamp) {
43          super(createMetadata(artifact), (Path) null, timestamp);
44      }
45  
46      LocalSnapshotMetadata(Metadata metadata, Path path, Date timestamp) {
47          super(metadata, path, timestamp);
48      }
49  
50      private static Metadata createMetadata(Artifact artifact) {
51          Snapshot snapshot = new Snapshot();
52          snapshot.setLocalCopy(true);
53          Versioning versioning = new Versioning();
54          versioning.setSnapshot(snapshot);
55  
56          Metadata metadata = new Metadata();
57          metadata.setVersioning(versioning);
58          metadata.setGroupId(artifact.getGroupId());
59          metadata.setArtifactId(artifact.getArtifactId());
60          metadata.setVersion(artifact.getBaseVersion());
61          metadata.setModelVersion("1.1.0");
62          return metadata;
63      }
64  
65      public void bind(Artifact artifact) {
66          artifacts.add(artifact);
67      }
68  
69      @Deprecated
70      @Override
71      public MavenMetadata setFile(File file) {
72          return new LocalSnapshotMetadata(metadata, file.toPath(), timestamp);
73      }
74  
75      @Override
76      public MavenMetadata setPath(Path path) {
77          return new LocalSnapshotMetadata(metadata, path, timestamp);
78      }
79  
80      public Object getKey() {
81          return getGroupId() + ':' + getArtifactId() + ':' + getVersion();
82      }
83  
84      public static Object getKey(Artifact artifact) {
85          return artifact.getGroupId() + ':' + artifact.getArtifactId() + ':' + artifact.getBaseVersion();
86      }
87  
88      @Override
89      protected void merge(Metadata recessive) {
90          metadata.getVersioning().setLastUpdatedTimestamp(timestamp);
91  
92          String lastUpdated = metadata.getVersioning().getLastUpdated();
93  
94          Map<String, SnapshotVersion> versions = new LinkedHashMap<>();
95  
96          for (Artifact artifact : artifacts) {
97              SnapshotVersion sv = new SnapshotVersion();
98              sv.setClassifier(artifact.getClassifier());
99              sv.setExtension(artifact.getExtension());
100             sv.setVersion(getVersion());
101             sv.setUpdated(lastUpdated);
102             versions.put(getKey(sv.getClassifier(), sv.getExtension()), sv);
103         }
104 
105         Versioning versioning = recessive.getVersioning();
106         if (versioning != null) {
107             for (SnapshotVersion sv : versioning.getSnapshotVersions()) {
108                 String key = getKey(sv.getClassifier(), sv.getExtension());
109                 if (!versions.containsKey(key)) {
110                     versions.put(key, sv);
111                 }
112             }
113         }
114 
115         metadata.getVersioning().setSnapshotVersions(new ArrayList<>(versions.values()));
116 
117         // just carry-on as-is
118         if (!recessive.getPlugins().isEmpty()) {
119             metadata.setPlugins(new ArrayList<>(recessive.getPlugins()));
120         }
121 
122         artifacts.clear();
123     }
124 
125     private String getKey(String classifier, String extension) {
126         return classifier + ':' + extension;
127     }
128 
129     @Override
130     public String getGroupId() {
131         return metadata.getGroupId();
132     }
133 
134     @Override
135     public String getArtifactId() {
136         return metadata.getArtifactId();
137     }
138 
139     @Override
140     public String getVersion() {
141         return metadata.getVersion();
142     }
143 
144     @Override
145     public Nature getNature() {
146         return Nature.SNAPSHOT;
147     }
148 }