1 | |
package org.apache.maven.artifact.repository.metadata; |
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.Artifact; |
23 | |
import org.apache.maven.artifact.metadata.ArtifactMetadata; |
24 | |
import org.apache.maven.artifact.repository.ArtifactRepository; |
25 | |
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader; |
26 | |
import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer; |
27 | |
import org.codehaus.plexus.util.IOUtil; |
28 | |
import org.codehaus.plexus.util.ReaderFactory; |
29 | |
import org.codehaus.plexus.util.WriterFactory; |
30 | |
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; |
31 | |
|
32 | |
import java.io.File; |
33 | |
import java.io.IOException; |
34 | |
import java.io.Reader; |
35 | |
import java.io.Writer; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public abstract class AbstractRepositoryMetadata |
44 | |
implements RepositoryMetadata |
45 | |
{ |
46 | |
private Metadata metadata; |
47 | |
|
48 | |
protected AbstractRepositoryMetadata( Metadata metadata ) |
49 | 4 | { |
50 | 4 | this.metadata = metadata; |
51 | 4 | } |
52 | |
|
53 | |
public String getRemoteFilename() |
54 | |
{ |
55 | 2 | return "maven-metadata.xml"; |
56 | |
} |
57 | |
|
58 | |
public String getLocalFilename( ArtifactRepository repository ) |
59 | |
{ |
60 | 3 | return "maven-metadata-" + repository.getKey() + ".xml"; |
61 | |
} |
62 | |
|
63 | |
public void storeInLocalRepository( ArtifactRepository localRepository, ArtifactRepository remoteRepository ) |
64 | |
throws RepositoryMetadataStoreException |
65 | |
{ |
66 | |
try |
67 | |
{ |
68 | 2 | updateRepositoryMetadata( localRepository, remoteRepository ); |
69 | |
} |
70 | 0 | catch ( IOException e ) |
71 | |
{ |
72 | 0 | throw new RepositoryMetadataStoreException( "Error updating group repository metadata", e ); |
73 | |
} |
74 | 0 | catch ( XmlPullParserException e ) |
75 | |
{ |
76 | 0 | throw new RepositoryMetadataStoreException( "Error updating group repository metadata", e ); |
77 | 2 | } |
78 | 2 | } |
79 | |
|
80 | |
protected void updateRepositoryMetadata( ArtifactRepository localRepository, ArtifactRepository remoteRepository ) |
81 | |
throws IOException, XmlPullParserException |
82 | |
{ |
83 | 4 | MetadataXpp3Reader mappingReader = new MetadataXpp3Reader(); |
84 | |
|
85 | 4 | Metadata metadata = null; |
86 | |
|
87 | 4 | File metadataFile = new File( localRepository.getBasedir(), |
88 | |
localRepository.pathOfLocalRepositoryMetadata( this, remoteRepository ) ); |
89 | |
|
90 | 4 | if ( metadataFile.exists() ) |
91 | |
{ |
92 | 2 | Reader reader = null; |
93 | |
|
94 | |
try |
95 | |
{ |
96 | 2 | reader = ReaderFactory.newXmlReader( metadataFile ); |
97 | |
|
98 | 2 | metadata = mappingReader.read( reader, false ); |
99 | |
} |
100 | |
finally |
101 | |
{ |
102 | 2 | IOUtil.close( reader ); |
103 | 2 | } |
104 | |
} |
105 | |
|
106 | |
boolean changed; |
107 | |
|
108 | |
|
109 | 4 | if ( metadata == null ) |
110 | |
{ |
111 | 2 | metadata = this.metadata; |
112 | |
|
113 | 2 | changed = true; |
114 | |
} |
115 | |
else |
116 | |
{ |
117 | 2 | changed = metadata.merge( this.metadata ); |
118 | |
} |
119 | |
|
120 | |
|
121 | 4 | String version = metadata.getVersion(); |
122 | 4 | if ( version != null && ( Artifact.LATEST_VERSION.equals( version ) || Artifact.RELEASE_VERSION.equals( version ) ) ) |
123 | |
{ |
124 | |
|
125 | 2 | metadata.setVersion( null ); |
126 | |
} |
127 | |
|
128 | 4 | if ( changed || !metadataFile.exists() ) |
129 | |
{ |
130 | 4 | Writer writer = null; |
131 | |
try |
132 | |
{ |
133 | 4 | metadataFile.getParentFile().mkdirs(); |
134 | 4 | writer = WriterFactory.newXmlWriter( metadataFile ); |
135 | |
|
136 | 4 | MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer(); |
137 | |
|
138 | 4 | mappingWriter.write( writer, metadata ); |
139 | |
} |
140 | |
finally |
141 | |
{ |
142 | 4 | IOUtil.close( writer ); |
143 | 4 | } |
144 | 4 | } |
145 | |
else |
146 | |
{ |
147 | 0 | metadataFile.setLastModified( System.currentTimeMillis() ); |
148 | |
} |
149 | 4 | } |
150 | |
|
151 | |
public String toString() |
152 | |
{ |
153 | 1 | return "repository metadata for: \'" + getKey() + "\'"; |
154 | |
} |
155 | |
|
156 | |
protected static Metadata createMetadata( Artifact artifact, Versioning versioning ) |
157 | |
{ |
158 | 2 | Metadata metadata = new Metadata(); |
159 | 2 | metadata.setGroupId( artifact.getGroupId() ); |
160 | 2 | metadata.setArtifactId( artifact.getArtifactId() ); |
161 | 2 | metadata.setVersion( artifact.getVersion() ); |
162 | 2 | metadata.setVersioning( versioning ); |
163 | 2 | return metadata; |
164 | |
} |
165 | |
|
166 | |
protected static Versioning createVersioning( Snapshot snapshot ) |
167 | |
{ |
168 | 0 | Versioning versioning = new Versioning(); |
169 | 0 | versioning.setSnapshot( snapshot ); |
170 | 0 | versioning.updateTimestamp(); |
171 | 0 | return versioning; |
172 | |
} |
173 | |
|
174 | |
public void setMetadata( Metadata metadata ) |
175 | |
{ |
176 | 0 | this.metadata = metadata; |
177 | 0 | } |
178 | |
|
179 | |
public Metadata getMetadata() |
180 | |
{ |
181 | 0 | return metadata; |
182 | |
} |
183 | |
|
184 | |
public void merge( ArtifactMetadata metadata ) |
185 | |
{ |
186 | |
|
187 | 0 | AbstractRepositoryMetadata repoMetadata = (AbstractRepositoryMetadata) metadata; |
188 | 0 | this.metadata.merge( repoMetadata.getMetadata() ); |
189 | 0 | } |
190 | |
|
191 | |
public String extendedToString() |
192 | |
{ |
193 | 0 | StringBuffer buffer = new StringBuffer(); |
194 | |
|
195 | 0 | buffer.append( "\nRepository Metadata\n--------------------------" ); |
196 | 0 | buffer.append( "\nGroupId: " ).append( getGroupId() ); |
197 | 0 | buffer.append( "\nArtifactId: " ).append( getArtifactId() ); |
198 | 0 | buffer.append( "\nMetadata Type: " ).append( getClass().getName() ); |
199 | |
|
200 | 0 | return buffer.toString(); |
201 | |
} |
202 | |
} |