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