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 java.io.File;
23 import java.io.IOException;
24 import java.io.Reader;
25 import java.io.Writer;
26
27 import org.apache.maven.artifact.repository.metadata.Metadata;
28 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
29 import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
30 import org.codehaus.plexus.util.IOUtil;
31 import org.codehaus.plexus.util.ReaderFactory;
32 import org.codehaus.plexus.util.WriterFactory;
33 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
34 import org.sonatype.aether.RepositoryException;
35 import org.sonatype.aether.metadata.MergeableMetadata;
36
37
38
39
40 abstract class MavenMetadata
41 implements MergeableMetadata
42 {
43
44 private final File file;
45
46 protected Metadata metadata;
47
48 private boolean merged;
49
50 protected MavenMetadata( Metadata metadata, File file )
51 {
52 this.metadata = metadata;
53 this.file = file;
54 }
55
56 public String getType()
57 {
58 return "maven-metadata.xml";
59 }
60
61 public File getFile()
62 {
63 return file;
64 }
65
66 public void merge( File existing, File result )
67 throws RepositoryException
68 {
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 {
80 return merged;
81 }
82
83 protected abstract void merge( Metadata recessive );
84
85 private Metadata read( File metadataFile )
86 throws RepositoryException
87 {
88 if ( metadataFile.length() <= 0 )
89 {
90 return new Metadata();
91 }
92
93 Reader reader = null;
94 try
95 {
96 reader = ReaderFactory.newXmlReader( metadataFile );
97 return new MetadataXpp3Reader().read( reader, false );
98 }
99 catch ( IOException e )
100 {
101 throw new RepositoryException( "Could not read metadata " + metadataFile + ": " + e.getMessage(), e );
102 }
103 catch ( XmlPullParserException e )
104 {
105 throw new RepositoryException( "Could not parse metadata " + metadataFile + ": " + e.getMessage(), e );
106 }
107 finally
108 {
109 IOUtil.close( reader );
110 }
111 }
112
113 private void write( File metadataFile, Metadata metadata )
114 throws RepositoryException
115 {
116 Writer writer = null;
117 try
118 {
119 metadataFile.getParentFile().mkdirs();
120 writer = WriterFactory.newXmlWriter( metadataFile );
121 new MetadataXpp3Writer().write( writer, metadata );
122 }
123 catch ( IOException e )
124 {
125 throw new RepositoryException( "Could not write metadata " + metadataFile + ": " + e.getMessage(), e );
126 }
127 finally
128 {
129 IOUtil.close( writer );
130 }
131 }
132
133 @Override
134 public String toString()
135 {
136 StringBuilder buffer = new StringBuilder( 128 );
137 if ( getGroupId().length() > 0 )
138 {
139 buffer.append( getGroupId() );
140 }
141 if ( getArtifactId().length() > 0 )
142 {
143 buffer.append( ':' ).append( getArtifactId() );
144 }
145 if ( getVersion().length() > 0 )
146 {
147 buffer.append( ':' ).append( getVersion() );
148 }
149 buffer.append( '/' ).append( getType() );
150 return buffer.toString();
151 }
152
153 }