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.Map;
38
39
40
41
42 abstract class MavenMetadata
43 extends AbstractMetadata
44 implements MergeableMetadata
45 {
46
47 static final String MAVEN_METADATA_XML = "maven-metadata.xml";
48
49 private final File file;
50
51 protected Metadata metadata;
52
53 private boolean merged;
54
55 protected MavenMetadata( Metadata metadata, File file )
56 {
57 this.metadata = metadata;
58 this.file = file;
59 }
60
61 public String getType()
62 {
63 return MAVEN_METADATA_XML;
64 }
65
66 public File getFile()
67 {
68 return file;
69 }
70
71 public void merge( File existing, File result )
72 throws RepositoryException
73 {
74 Metadata recessive = read( existing );
75
76 merge( recessive );
77
78 write( result, metadata );
79
80 merged = true;
81 }
82
83 public boolean isMerged()
84 {
85 return merged;
86 }
87
88 protected abstract void merge( Metadata recessive );
89
90 static Metadata read( File metadataFile )
91 throws RepositoryException
92 {
93 if ( metadataFile.length() <= 0 )
94 {
95 return new Metadata();
96 }
97
98 try ( Reader reader = ReaderFactory.newXmlReader( metadataFile ) )
99 {
100 return new MetadataXpp3Reader().read( reader, false );
101 }
102 catch ( IOException e )
103 {
104 throw new RepositoryException( "Could not read metadata " + metadataFile + ": " + e.getMessage(), e );
105 }
106 catch ( XmlPullParserException e )
107 {
108 throw new RepositoryException( "Could not parse metadata " + metadataFile + ": " + e.getMessage(), e );
109 }
110 }
111
112 private void write( File metadataFile, Metadata metadata )
113 throws RepositoryException
114 {
115 metadataFile.getParentFile().mkdirs();
116 try ( Writer writer = WriterFactory.newXmlWriter( metadataFile ) )
117 {
118 new MetadataXpp3Writer().write( writer, metadata );
119 }
120 catch ( IOException e )
121 {
122 throw new RepositoryException( "Could not write metadata " + metadataFile + ": " + e.getMessage(), e );
123 }
124 }
125
126 public Map<String, String> getProperties()
127 {
128 return Collections.emptyMap();
129 }
130
131 @Override
132 public org.eclipse.aether.metadata.Metadata setProperties( Map<String, String> properties )
133 {
134 return this;
135 }
136
137 }