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