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.GregorianCalendar;
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.eclipse.aether.artifact.Artifact;
37  
38  /**
39   * @author Benjamin Bentmann
40   */
41  final class RemoteSnapshotMetadata
42      extends MavenSnapshotMetadata
43  {
44      public static final String DEFAULT_SNAPSHOT_TIMESTAMP_FORMAT = "yyyyMMdd.HHmmss";
45  
46      public static final TimeZone DEFAULT_SNAPSHOT_TIME_ZONE = TimeZone.getTimeZone( "Etc/UTC" );
47  
48      private final Map<String, SnapshotVersion> versions = new LinkedHashMap<>();
49  
50      RemoteSnapshotMetadata( Artifact artifact, boolean legacyFormat, Date timestamp )
51      {
52          super( createRepositoryMetadata( artifact, legacyFormat ), null, legacyFormat, timestamp );
53      }
54  
55      private RemoteSnapshotMetadata( Metadata metadata, File file, boolean legacyFormat, Date timestamp )
56      {
57          super( metadata, file, legacyFormat, timestamp );
58      }
59  
60      public MavenMetadata setFile( File file )
61      {
62          return new RemoteSnapshotMetadata( metadata, file, legacyFormat, timestamp );
63      }
64  
65      public String getExpandedVersion( Artifact artifact )
66      {
67          String key = getKey( artifact.getClassifier(), artifact.getExtension() );
68          return versions.get( key ).getVersion();
69      }
70  
71      @Override
72      protected void merge( Metadata recessive )
73      {
74          Snapshot snapshot;
75          String lastUpdated;
76  
77          if ( metadata.getVersioning() == null )
78          {
79              DateFormat utcDateFormatter = new SimpleDateFormat( DEFAULT_SNAPSHOT_TIMESTAMP_FORMAT );
80              utcDateFormatter.setCalendar( new GregorianCalendar() );
81              utcDateFormatter.setTimeZone( DEFAULT_SNAPSHOT_TIME_ZONE );
82  
83              snapshot = new Snapshot();
84              snapshot.setBuildNumber( getBuildNumber( recessive ) + 1 );
85              snapshot.setTimestamp( utcDateFormatter.format( timestamp ) );
86  
87              Versioning versioning = new Versioning();
88              versioning.setSnapshot( snapshot );
89              versioning.setLastUpdatedTimestamp( timestamp );
90              lastUpdated = versioning.getLastUpdated();
91  
92              metadata.setVersioning( versioning );
93          }
94          else
95          {
96              snapshot = metadata.getVersioning().getSnapshot();
97              lastUpdated = metadata.getVersioning().getLastUpdated();
98          }
99  
100         for ( Artifact artifact : artifacts )
101         {
102             String version = artifact.getVersion();
103 
104             if ( version.endsWith( SNAPSHOT ) )
105             {
106                 String qualifier = snapshot.getTimestamp() + '-' + snapshot.getBuildNumber();
107                 version = version.substring( 0, version.length() - SNAPSHOT.length() ) + qualifier;
108             }
109 
110             SnapshotVersion sv = new SnapshotVersion();
111             sv.setClassifier( artifact.getClassifier() );
112             sv.setExtension( artifact.getExtension() );
113             sv.setVersion( version );
114             sv.setUpdated( lastUpdated );
115 
116             versions.put( getKey( sv.getClassifier(), sv.getExtension() ), sv );
117         }
118 
119         artifacts.clear();
120 
121         Versioning versioning = recessive.getVersioning();
122         if ( versioning != null )
123         {
124             for ( SnapshotVersion sv : versioning.getSnapshotVersions() )
125             {
126                 String key = getKey( sv.getClassifier(), sv.getExtension() );
127                 if ( !versions.containsKey( key ) )
128                 {
129                     versions.put( key, sv );
130                 }
131             }
132         }
133 
134         if ( !legacyFormat )
135         {
136             metadata.getVersioning().setSnapshotVersions( new ArrayList<>( versions.values() ) );
137         }
138     }
139 
140     private static int getBuildNumber( Metadata metadata )
141     {
142         int number = 0;
143 
144         Versioning versioning = metadata.getVersioning();
145         if ( versioning != null )
146         {
147             Snapshot snapshot = versioning.getSnapshot();
148             if ( snapshot != null && snapshot.getBuildNumber() > 0 )
149             {
150                 number = snapshot.getBuildNumber();
151             }
152         }
153 
154         return number;
155     }
156 
157 }