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