1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.impl.resolver;
20
21 import javax.xml.stream.XMLStreamException;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.time.Instant;
30 import java.time.ZoneOffset;
31 import java.time.format.DateTimeFormatter;
32 import java.util.Collections;
33 import java.util.Map;
34
35 import org.apache.maven.api.metadata.Metadata;
36 import org.apache.maven.metadata.v4.MetadataStaxReader;
37 import org.apache.maven.metadata.v4.MetadataStaxWriter;
38 import org.eclipse.aether.RepositoryException;
39 import org.eclipse.aether.metadata.AbstractMetadata;
40 import org.eclipse.aether.metadata.MergeableMetadata;
41
42
43
44 abstract class MavenMetadata extends AbstractMetadata implements MergeableMetadata {
45
46 static final String MAVEN_METADATA_XML = "maven-metadata.xml";
47
48 static DateTimeFormatter fmt;
49
50 static {
51 fmt = DateTimeFormatter.ofPattern("yyyyMMddHHmmss").withZone(ZoneOffset.UTC);
52 }
53
54 protected Metadata metadata;
55
56 private final Path path;
57
58 protected final Instant timestamp;
59
60 private boolean merged;
61
62 @Deprecated
63 protected MavenMetadata(Metadata metadata, File file, Instant timestamp) {
64 this(metadata, file != null ? file.toPath() : null, timestamp);
65 }
66
67 protected MavenMetadata(Metadata metadata, Path path, Instant timestamp) {
68 this.metadata = metadata;
69 this.path = path;
70 this.timestamp = timestamp;
71 }
72
73 @Override
74 public String getType() {
75 return MAVEN_METADATA_XML;
76 }
77
78 @Deprecated
79 @Override
80 public File getFile() {
81 return path != null ? path.toFile() : null;
82 }
83
84 @Override
85 public Path getPath() {
86 return path;
87 }
88
89 @Deprecated
90 @Override
91 public void merge(File existing, File result) throws RepositoryException {
92 merge(existing != null ? existing.toPath() : null, result != null ? result.toPath() : null);
93 }
94
95 @Override
96 public void merge(Path existing, Path result) throws RepositoryException {
97 Metadata recessive = read(existing);
98
99 merge(recessive);
100
101 write(result, metadata);
102
103 merged = true;
104 }
105
106 @Override
107 public boolean isMerged() {
108 return merged;
109 }
110
111 protected abstract void merge(Metadata recessive);
112
113 static Metadata read(Path metadataPath) throws RepositoryException {
114 if (!Files.exists(metadataPath)) {
115 return Metadata.newInstance();
116 }
117
118 try (InputStream input = Files.newInputStream(metadataPath)) {
119 return new MetadataStaxReader().read(input, false);
120 } catch (IOException | XMLStreamException e) {
121 throw new RepositoryException("Could not parse metadata " + metadataPath + ": " + e.getMessage(), e);
122 }
123 }
124
125 private void write(Path metadataPath, Metadata metadata) throws RepositoryException {
126 try {
127 Files.createDirectories(metadataPath.getParent());
128 try (OutputStream output = Files.newOutputStream(metadataPath)) {
129 new MetadataStaxWriter().write(output, metadata);
130 }
131 } catch (IOException | XMLStreamException e) {
132 throw new RepositoryException("Could not write metadata " + metadataPath + ": " + e.getMessage(), e);
133 }
134 }
135
136 @Override
137 public Map<String, String> getProperties() {
138 return Collections.emptyMap();
139 }
140
141 @Override
142 public org.eclipse.aether.metadata.Metadata setProperties(Map<String, String> properties) {
143 return this;
144 }
145 }