1 package org.apache.maven.repository.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.artifact.repository.metadata.Metadata;
23 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
24 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
25 import org.codehaus.plexus.util.ReaderFactory;
26 import org.codehaus.plexus.util.WriterFactory;
27 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
28 import org.eclipse.aether.RepositoryException;
29 import org.eclipse.aether.metadata.AbstractMetadata;
30 import org.eclipse.aether.metadata.MergeableMetadata;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.io.Reader;
35 import java.io.Writer;
36 import java.util.Collections;
37 import java.util.Date;
38 import java.util.Map;
39
40
41
42
43 abstract class MavenMetadata
44 extends AbstractMetadata
45 implements MergeableMetadata
46 {
47
48 static final String MAVEN_METADATA_XML = "maven-metadata.xml";
49
50 protected Metadata metadata;
51
52 private final File file;
53
54 protected final Date timestamp;
55
56 private boolean merged;
57
58 protected MavenMetadata( Metadata metadata, File file, Date timestamp )
59 {
60 this.metadata = metadata;
61 this.file = file;
62 this.timestamp = timestamp;
63 }
64
65 public String getType()
66 {
67 return MAVEN_METADATA_XML;
68 }
69
70 public File getFile()
71 {
72 return file;
73 }
74
75 public void merge( File existing, File result )
76 throws RepositoryException
77 {
78 Metadata recessive = read( existing );
79
80 merge( recessive );
81
82 write( result, metadata );
83
84 merged = true;
85 }
86
87 public boolean isMerged()
88 {
89 return merged;
90 }
91
92 protected abstract void merge( Metadata recessive );
93
94 static Metadata read( File metadataFile )
95 throws RepositoryException
96 {
97 if ( metadataFile.length() <= 0 )
98 {
99 return new Metadata();
100 }
101
102 try ( Reader reader = ReaderFactory.newXmlReader( metadataFile ) )
103 {
104 return new MetadataXpp3Reader().read( reader, false );
105 }
106 catch ( IOException e )
107 {
108 throw new RepositoryException( "Could not read metadata " + metadataFile + ": " + e.getMessage(), e );
109 }
110 catch ( XmlPullParserException e )
111 {
112 throw new RepositoryException( "Could not parse metadata " + metadataFile + ": " + e.getMessage(), e );
113 }
114 }
115
116 private void write( File metadataFile, Metadata metadata )
117 throws RepositoryException
118 {
119 metadataFile.getParentFile().mkdirs();
120 try ( Writer writer = WriterFactory.newXmlWriter( metadataFile ) )
121 {
122 new MetadataXpp3Writer().write( writer, metadata );
123 }
124 catch ( IOException e )
125 {
126 throw new RepositoryException( "Could not write metadata " + metadataFile + ": " + e.getMessage(), e );
127 }
128 }
129
130 public Map<String, String> getProperties()
131 {
132 return Collections.emptyMap();
133 }
134
135 @Override
136 public org.eclipse.aether.metadata.Metadata setProperties( Map<String, String> properties )
137 {
138 return this;
139 }
140
141 }