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.text.DateFormat;
24 import java.text.SimpleDateFormat;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Date;
28 import java.util.LinkedHashMap;
29 import java.util.Map;
30 import java.util.TimeZone;
31
32 import org.apache.maven.artifact.repository.metadata.Metadata;
33 import org.apache.maven.artifact.repository.metadata.Snapshot;
34 import org.apache.maven.artifact.repository.metadata.SnapshotVersion;
35 import org.apache.maven.artifact.repository.metadata.Versioning;
36 import org.sonatype.aether.artifact.Artifact;
37
38
39
40
41 final class RemoteSnapshotMetadata
42 extends MavenMetadata
43 {
44
45 private static final String SNAPSHOT = "SNAPSHOT";
46
47 private final Collection<Artifact> artifacts = new ArrayList<Artifact>();
48
49 private final Map<String, SnapshotVersion> versions = new LinkedHashMap<String, SnapshotVersion>();
50
51 private final boolean legacyFormat;
52
53 public RemoteSnapshotMetadata( Artifact artifact, boolean legacyFormat )
54 {
55 super( createMetadata( artifact, legacyFormat ), null );
56 this.legacyFormat = legacyFormat;
57 }
58
59 private RemoteSnapshotMetadata( Metadata metadata, File file, boolean legacyFormat )
60 {
61 super( metadata, file );
62 this.legacyFormat = legacyFormat;
63 }
64
65 private static Metadata createMetadata( Artifact artifact, boolean legacy )
66 {
67 Metadata metadata = new Metadata();
68 if ( !legacy )
69 {
70 metadata.setModelVersion( "1.1.0" );
71 }
72 metadata.setGroupId( artifact.getGroupId() );
73 metadata.setArtifactId( artifact.getArtifactId() );
74 metadata.setVersion( artifact.getBaseVersion() );
75
76 return metadata;
77 }
78
79 public void bind( Artifact artifact )
80 {
81 artifacts.add( artifact );
82 }
83
84 public MavenMetadata setFile( File file )
85 {
86 return new RemoteSnapshotMetadata( metadata, file, legacyFormat );
87 }
88
89 public Object getKey()
90 {
91 return getGroupId() + ':' + getArtifactId() + ':' + getVersion();
92 }
93
94 public static Object getKey( Artifact artifact )
95 {
96 return artifact.getGroupId() + ':' + artifact.getArtifactId() + ':' + artifact.getBaseVersion();
97 }
98
99 public String getExpandedVersion( Artifact artifact )
100 {
101 String key = getKey( artifact.getClassifier(), artifact.getExtension() );
102 return versions.get( key ).getVersion();
103 }
104
105 @Override
106 protected void merge( Metadata recessive )
107 {
108 Snapshot snapshot;
109 String lastUpdated = "";
110
111 if ( metadata.getVersioning() == null )
112 {
113 DateFormat utcDateFormatter = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
114 utcDateFormatter.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
115
116 snapshot = new Snapshot();
117 snapshot.setBuildNumber( getBuildNumber( recessive ) + 1 );
118 snapshot.setTimestamp( utcDateFormatter.format( new Date() ) );
119
120 Versioning versioning = new Versioning();
121 versioning.setSnapshot( snapshot );
122 versioning.setLastUpdated( snapshot.getTimestamp().replace( ".", "" ) );
123 lastUpdated = versioning.getLastUpdated();
124
125 metadata.setVersioning( versioning );
126 }
127 else
128 {
129 snapshot = metadata.getVersioning().getSnapshot();
130 lastUpdated = metadata.getVersioning().getLastUpdated();
131 }
132
133 for ( Artifact artifact : artifacts )
134 {
135 String version = artifact.getVersion();
136
137 if ( version.endsWith( SNAPSHOT ) )
138 {
139 String qualifier = snapshot.getTimestamp() + "-" + snapshot.getBuildNumber();
140 version = version.substring( 0, version.length() - SNAPSHOT.length() ) + qualifier;
141 }
142
143 SnapshotVersion sv = new SnapshotVersion();
144 sv.setClassifier( artifact.getClassifier() );
145 sv.setExtension( artifact.getExtension() );
146 sv.setVersion( version );
147 sv.setUpdated( lastUpdated );
148 versions.put( getKey( sv.getClassifier(), sv.getExtension() ), sv );
149 }
150
151 artifacts.clear();
152
153 Versioning versioning = recessive.getVersioning();
154 if ( versioning != null )
155 {
156 for ( SnapshotVersion sv : versioning.getSnapshotVersions() )
157 {
158 String key = getKey( sv.getClassifier(), sv.getExtension() );
159 if ( !versions.containsKey( key ) )
160 {
161 versions.put( key, sv );
162 }
163 }
164 }
165
166 if ( !legacyFormat )
167 {
168 metadata.getVersioning().setSnapshotVersions( new ArrayList<SnapshotVersion>( versions.values() ) );
169 }
170 }
171
172 private String getKey( String classifier, String extension )
173 {
174 return classifier + ':' + extension;
175 }
176
177 private static int getBuildNumber( Metadata metadata )
178 {
179 int number = 0;
180
181 Versioning versioning = metadata.getVersioning();
182 if ( versioning != null )
183 {
184 Snapshot snapshot = versioning.getSnapshot();
185 if ( snapshot != null && snapshot.getBuildNumber() > 0 )
186 {
187 number = snapshot.getBuildNumber();
188 }
189 }
190
191 return number;
192 }
193
194 public String getGroupId()
195 {
196 return metadata.getGroupId();
197 }
198
199 public String getArtifactId()
200 {
201 return metadata.getArtifactId();
202 }
203
204 public String getVersion()
205 {
206 return metadata.getVersion();
207 }
208
209 public Nature getNature()
210 {
211 return Nature.SNAPSHOT;
212 }
213
214 }