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.util.Collections;
29 import java.util.Date;
30 import java.util.Map;
31
32 import org.apache.maven.artifact.repository.metadata.Metadata;
33 import org.apache.maven.artifact.repository.metadata.io.MetadataStaxReader;
34 import org.apache.maven.artifact.repository.metadata.io.MetadataStaxWriter;
35 import org.eclipse.aether.RepositoryException;
36 import org.eclipse.aether.metadata.AbstractMetadata;
37 import org.eclipse.aether.metadata.MergeableMetadata;
38
39
40
41 abstract class MavenMetadata extends AbstractMetadata implements MergeableMetadata {
42
43 static final String MAVEN_METADATA_XML = "maven-metadata.xml";
44
45 protected Metadata metadata;
46
47 private final File file;
48
49 protected final Date timestamp;
50
51 private boolean merged;
52
53 protected MavenMetadata(Metadata metadata, File file, Date timestamp) {
54 this.metadata = metadata;
55 this.file = file;
56 this.timestamp = timestamp;
57 }
58
59 @Override
60 public String getType() {
61 return MAVEN_METADATA_XML;
62 }
63
64 @Override
65 public File getFile() {
66 return file;
67 }
68
69 @Override
70 public void merge(File existing, File result) throws RepositoryException {
71 Metadata recessive = read(existing);
72
73 merge(recessive);
74
75 write(result, metadata);
76
77 merged = true;
78 }
79
80 @Override
81 public boolean isMerged() {
82 return merged;
83 }
84
85 protected abstract void merge(Metadata recessive);
86
87 static Metadata read(File metadataFile) throws RepositoryException {
88 if (metadataFile.length() <= 0) {
89 return new Metadata();
90 }
91
92 try (InputStream input = Files.newInputStream(metadataFile.toPath())) {
93 return new Metadata(new MetadataStaxReader().read(input, false));
94 } catch (IOException | XMLStreamException e) {
95 throw new RepositoryException("Could not parse metadata " + metadataFile + ": " + e.getMessage(), e);
96 }
97 }
98
99 private void write(File metadataFile, Metadata metadata) throws RepositoryException {
100 metadataFile.getParentFile().mkdirs();
101 try (OutputStream output = Files.newOutputStream(metadataFile.toPath())) {
102 new MetadataStaxWriter().write(output, metadata.getDelegate());
103 } catch (IOException | XMLStreamException e) {
104 throw new RepositoryException("Could not write metadata " + metadataFile + ": " + e.getMessage(), e);
105 }
106 }
107
108 @Override
109 public Map<String, String> getProperties() {
110 return Collections.emptyMap();
111 }
112
113 @Override
114 public org.eclipse.aether.metadata.Metadata setProperties(Map<String, String> properties) {
115 return this;
116 }
117 }