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 public void merge(File existing, File result) throws RepositoryException {
84 merge(existing != null ? existing.toPath() : null, result != null ? result.toPath() : null);
85 }
86
87 @Override
88 public void merge(Path existing, Path result) throws RepositoryException {
89 Metadata recessive = read(existing);
90
91 merge(recessive);
92
93 write(result, metadata);
94
95 merged = true;
96 }
97
98 @Override
99 public boolean isMerged() {
100 return merged;
101 }
102
103 protected abstract void merge(Metadata recessive);
104
105 static Metadata read(Path metadataPath) throws RepositoryException {
106 if (!Files.exists(metadataPath)) {
107 return new Metadata();
108 }
109
110 try (InputStream input = Files.newInputStream(metadataPath)) {
111 return new Metadata(new MetadataStaxReader().read(input, false));
112 } catch (IOException | XMLStreamException e) {
113 throw new RepositoryException("Could not parse metadata " + metadataPath + ": " + e.getMessage(), e);
114 }
115 }
116
117 private void write(Path metadataPath, Metadata metadata) throws RepositoryException {
118 try {
119 Files.createDirectories(metadataPath.getParent());
120 try (OutputStream output = Files.newOutputStream(metadataPath)) {
121 new MetadataStaxWriter().write(output, metadata.getDelegate());
122 }
123 } catch (IOException | XMLStreamException e) {
124 throw new RepositoryException("Could not write metadata " + metadataPath + ": " + e.getMessage(), e);
125 }
126 }
127
128 @Override
129 public Map<String, String> getProperties() {
130 return Collections.emptyMap();
131 }
132
133 @Override
134 public org.eclipse.aether.metadata.Metadata setProperties(Map<String, String> properties) {
135 return this;
136 }
137 }