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 java.io.File;
22 import java.io.IOException;
23 import java.io.Reader;
24 import java.io.Writer;
25 import java.util.Collections;
26 import java.util.Date;
27 import java.util.Map;
28
29 import org.apache.maven.artifact.repository.metadata.Metadata;
30 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
31 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
32 import org.codehaus.plexus.util.ReaderFactory;
33 import org.codehaus.plexus.util.WriterFactory;
34 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
35 import org.eclipse.aether.RepositoryException;
36 import org.eclipse.aether.metadata.AbstractMetadata;
37 import org.eclipse.aether.metadata.MergeableMetadata;
38
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 File file;
49
50 protected final Date timestamp;
51
52 private boolean merged;
53
54 protected MavenMetadata(Metadata metadata, File file, Date timestamp) {
55 this.metadata = metadata;
56 this.file = file;
57 this.timestamp = timestamp;
58 }
59
60 public String getType() {
61 return MAVEN_METADATA_XML;
62 }
63
64 public File getFile() {
65 return file;
66 }
67
68 public void merge(File existing, File result) throws RepositoryException {
69 Metadata recessive = read(existing);
70
71 merge(recessive);
72
73 write(result, metadata);
74
75 merged = true;
76 }
77
78 public boolean isMerged() {
79 return merged;
80 }
81
82 protected abstract void merge(Metadata recessive);
83
84 static Metadata read(File metadataFile) throws RepositoryException {
85 if (metadataFile.length() <= 0) {
86 return new Metadata();
87 }
88
89 try (Reader reader = ReaderFactory.newXmlReader(metadataFile)) {
90 return new MetadataXpp3Reader().read(reader, false);
91 } catch (IOException e) {
92 throw new RepositoryException("Could not read metadata " + metadataFile + ": " + e.getMessage(), e);
93 } catch (XmlPullParserException e) {
94 throw new RepositoryException("Could not parse metadata " + metadataFile + ": " + e.getMessage(), e);
95 }
96 }
97
98 private void write(File metadataFile, Metadata metadata) throws RepositoryException {
99 metadataFile.getParentFile().mkdirs();
100 try (Writer writer = WriterFactory.newXmlWriter(metadataFile)) {
101 new MetadataXpp3Writer().write(writer, metadata);
102 } catch (IOException e) {
103 throw new RepositoryException("Could not write metadata " + metadataFile + ": " + e.getMessage(), e);
104 }
105 }
106
107 public Map<String, String> getProperties() {
108 return Collections.emptyMap();
109 }
110
111 @Override
112 public org.eclipse.aether.metadata.Metadata setProperties(Map<String, String> properties) {
113 return this;
114 }
115 }