View Javadoc
1   package org.apache.maven.repository.internal;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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.Date;
27  import java.util.LinkedHashMap;
28  import java.util.Map;
29  import java.util.TimeZone;
30  
31  import org.apache.maven.artifact.repository.metadata.Metadata;
32  import org.apache.maven.artifact.repository.metadata.Snapshot;
33  import org.apache.maven.artifact.repository.metadata.SnapshotVersion;
34  import org.apache.maven.artifact.repository.metadata.Versioning;
35  import org.eclipse.aether.artifact.Artifact;
36  
37  /**
38   * @author Benjamin Bentmann
39   */
40  final class RemoteSnapshotMetadata
41      extends MavenSnapshotMetadata
42  {
43  
44      private final Map<String, SnapshotVersion> versions = new LinkedHashMap<String, SnapshotVersion>();
45  
46      public RemoteSnapshotMetadata( Artifact artifact, boolean legacyFormat )
47      {
48          super( createRepositoryMetadata( artifact, legacyFormat ), null, legacyFormat );
49      }
50  
51      private RemoteSnapshotMetadata( Metadata metadata, File file, boolean legacyFormat )
52      {
53          super( metadata, file, legacyFormat );
54      }
55  
56      public MavenMetadata setFile( File file )
57      {
58          return new RemoteSnapshotMetadata( metadata, file, legacyFormat );
59      }
60  
61      public String getExpandedVersion( Artifact artifact )
62      {
63          String key = getKey( artifact.getClassifier(), artifact.getExtension() );
64          return versions.get( key ).getVersion();
65      }
66  
67      @Override
68      protected void merge( Metadata recessive )
69      {
70          Snapshot snapshot;
71          String lastUpdated;
72  
73          if ( metadata.getVersioning() == null )
74          {
75              DateFormat utcDateFormatter = new SimpleDateFormat( "yyyyMMdd.HHmmss" );
76              utcDateFormatter.setTimeZone( TimeZone.getTimeZone( "UTC" ) );
77  
78              snapshot = new Snapshot();
79              snapshot.setBuildNumber( getBuildNumber( recessive ) + 1 );
80              snapshot.setTimestamp( utcDateFormatter.format( new Date() ) );
81  
82              Versioning versioning = new Versioning();
83              versioning.setSnapshot( snapshot );
84              versioning.setLastUpdated( snapshot.getTimestamp().replace( ".", "" ) );
85              lastUpdated = versioning.getLastUpdated();
86  
87              metadata.setVersioning( versioning );
88          }
89          else
90          {
91              snapshot = metadata.getVersioning().getSnapshot();
92              lastUpdated = metadata.getVersioning().getLastUpdated();
93          }
94  
95          for ( Artifact artifact : artifacts )
96          {
97              String version = artifact.getVersion();
98  
99              if ( version.endsWith( SNAPSHOT ) )
100             {
101                 String qualifier = snapshot.getTimestamp() + '-' + snapshot.getBuildNumber();
102                 version = version.substring( 0, version.length() - SNAPSHOT.length() ) + qualifier;
103             }
104 
105             SnapshotVersion sv = new SnapshotVersion();
106             sv.setClassifier( artifact.getClassifier() );
107             sv.setExtension( artifact.getExtension() );
108             sv.setVersion( version );
109             sv.setUpdated( lastUpdated );
110 
111             versions.put( getKey( sv.getClassifier(), sv.getExtension() ), sv );
112         }
113 
114         artifacts.clear();
115 
116         Versioning versioning = recessive.getVersioning();
117         if ( versioning != null )
118         {
119             for ( SnapshotVersion sv : versioning.getSnapshotVersions() )
120             {
121                 String key = getKey( sv.getClassifier(), sv.getExtension() );
122                 if ( !versions.containsKey( key ) )
123                 {
124                     versions.put( key, sv );
125                 }
126             }
127         }
128 
129         if ( !legacyFormat )
130         {
131             metadata.getVersioning().setSnapshotVersions( new ArrayList<SnapshotVersion>( versions.values() ) );
132         }
133     }
134 
135     private static int getBuildNumber( Metadata metadata )
136     {
137         int number = 0;
138 
139         Versioning versioning = metadata.getVersioning();
140         if ( versioning != null )
141         {
142             Snapshot snapshot = versioning.getSnapshot();
143             if ( snapshot != null && snapshot.getBuildNumber() > 0 )
144             {
145                 number = snapshot.getBuildNumber();
146             }
147         }
148 
149         return number;
150     }
151 
152 }