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 @Override
61 public String getType() {
62 return MAVEN_METADATA_XML;
63 }
64
65 @Override
66 public File getFile() {
67 return file;
68 }
69
70 @Override
71 public void merge(File existing, File result) throws RepositoryException {
72 Metadata recessive = read(existing);
73
74 merge(recessive);
75
76 write(result, metadata);
77
78 merged = true;
79 }
80
81 @Override
82 public boolean isMerged() {
83 return merged;
84 }
85
86 protected abstract void merge(Metadata recessive);
87
88 static Metadata read(File metadataFile) throws RepositoryException {
89 if (metadataFile.length() <= 0) {
90 return new Metadata();
91 }
92
93 try (Reader reader = ReaderFactory.newXmlReader(metadataFile)) {
94 return new MetadataXpp3Reader().read(reader, false);
95 } catch (IOException e) {
96 throw new RepositoryException("Could not read metadata " + metadataFile + ": " + e.getMessage(), e);
97 } catch (XmlPullParserException e) {
98 throw new RepositoryException("Could not parse metadata " + metadataFile + ": " + e.getMessage(), e);
99 }
100 }
101
102 private void write(File metadataFile, Metadata metadata) throws RepositoryException {
103 metadataFile.getParentFile().mkdirs();
104 try (Writer writer = WriterFactory.newXmlWriter(metadataFile)) {
105 new MetadataXpp3Writer().write(writer, metadata);
106 } catch (IOException e) {
107 throw new RepositoryException("Could not write metadata " + metadataFile + ": " + e.getMessage(), e);
108 }
109 }
110
111 @Override
112 public Map<String, String> getProperties() {
113 return Collections.emptyMap();
114 }
115
116 @Override
117 public org.eclipse.aether.metadata.Metadata setProperties(Map<String, String> properties) {
118 return this;
119 }
120 }