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.util.ArrayList;
24  import java.util.Collection;
25  import java.util.Date;
26  import java.util.LinkedHashMap;
27  import java.util.Map;
28  
29  import org.apache.maven.artifact.repository.metadata.Metadata;
30  import org.apache.maven.artifact.repository.metadata.Snapshot;
31  import org.apache.maven.artifact.repository.metadata.SnapshotVersion;
32  import org.apache.maven.artifact.repository.metadata.Versioning;
33  import org.eclipse.aether.artifact.Artifact;
34  
35  /**
36   * @author Benjamin Bentmann
37   */
38  final class LocalSnapshotMetadata
39      extends MavenMetadata
40  {
41  
42      private final Collection<Artifact> artifacts = new ArrayList<>();
43  
44      private final boolean legacyFormat;
45  
46      LocalSnapshotMetadata( Artifact artifact, boolean legacyFormat, Date timestamp )
47      {
48          super( createMetadata( artifact, legacyFormat ), null, timestamp );
49          this.legacyFormat = legacyFormat;
50      }
51  
52      LocalSnapshotMetadata( Metadata metadata, File file, boolean legacyFormat, Date timestamp )
53      {
54          super( metadata, file, timestamp );
55          this.legacyFormat = legacyFormat;
56      }
57  
58      private static Metadata createMetadata( Artifact artifact, boolean legacyFormat )
59      {
60          Snapshot snapshot = new Snapshot();
61          snapshot.setLocalCopy( true );
62          Versioning versioning = new Versioning();
63          versioning.setSnapshot( snapshot );
64  
65          Metadata metadata = new Metadata();
66          metadata.setVersioning( versioning );
67          metadata.setGroupId( artifact.getGroupId() );
68          metadata.setArtifactId( artifact.getArtifactId() );
69          metadata.setVersion( artifact.getBaseVersion() );
70  
71          if ( !legacyFormat )
72          {
73              metadata.setModelVersion( "1.1.0" );
74          }
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 LocalSnapshotMetadata( metadata, file, legacyFormat, timestamp );
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      @Override
100     protected void merge( Metadata recessive )
101     {
102         metadata.getVersioning().setLastUpdatedTimestamp( timestamp );
103 
104         if ( !legacyFormat )
105         {
106             String lastUpdated = metadata.getVersioning().getLastUpdated();
107 
108             Map<String, SnapshotVersion> versions = new LinkedHashMap<>();
109 
110             for ( Artifact artifact : artifacts )
111             {
112                 SnapshotVersion sv = new SnapshotVersion();
113                 sv.setClassifier( artifact.getClassifier() );
114                 sv.setExtension( artifact.getExtension() );
115                 sv.setVersion( getVersion() );
116                 sv.setUpdated( lastUpdated );
117                 versions.put( getKey( sv.getClassifier(), sv.getExtension() ), sv );
118             }
119 
120             Versioning versioning = recessive.getVersioning();
121             if ( versioning != null )
122             {
123                 for ( SnapshotVersion sv : versioning.getSnapshotVersions() )
124                 {
125                     String key = getKey( sv.getClassifier(), sv.getExtension() );
126                     if ( !versions.containsKey( key ) )
127                     {
128                         versions.put( key, sv );
129                     }
130                 }
131             }
132 
133             metadata.getVersioning().setSnapshotVersions( new ArrayList<>( versions.values() ) );
134         }
135 
136         artifacts.clear();
137     }
138 
139     private String getKey( String classifier, String extension )
140     {
141         return classifier + ':' + extension;
142     }
143 
144     public String getGroupId()
145     {
146         return metadata.getGroupId();
147     }
148 
149     public String getArtifactId()
150     {
151         return metadata.getArtifactId();
152     }
153 
154     public String getVersion()
155     {
156         return metadata.getVersion();
157     }
158 
159     public Nature getNature()
160     {
161         return Nature.SNAPSHOT;
162     }
163 
164 }