View Javadoc

1   package org.apache.maven.artifact.repository.metadata;
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.io.IOException;
24  import java.io.Reader;
25  import java.io.Writer;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.artifact.metadata.ArtifactMetadata;
29  import org.apache.maven.artifact.repository.ArtifactRepository;
30  import org.apache.maven.artifact.repository.ArtifactRepositoryPolicy;
31  import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader;
32  import org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Writer;
33  import org.codehaus.plexus.util.IOUtil;
34  import org.codehaus.plexus.util.ReaderFactory;
35  import org.codehaus.plexus.util.WriterFactory;
36  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
37  
38  /**
39   * Shared methods of the repository metadata handling.
40   *
41   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
42   * @version $Id: AbstractRepositoryMetadata.java 932128 2010-04-08 21:29:56Z bentmann $
43   */
44  public abstract class AbstractRepositoryMetadata
45      implements RepositoryMetadata
46  {
47      private Metadata metadata;
48  
49      protected AbstractRepositoryMetadata( Metadata metadata )
50      {
51          this.metadata = metadata;
52      }
53  
54      public String getRemoteFilename()
55      {
56          return "maven-metadata.xml";
57      }
58  
59      public String getLocalFilename( ArtifactRepository repository )
60      {        
61          return "maven-metadata-" + repository.getKey() + ".xml";
62      }
63  
64      public void storeInLocalRepository( ArtifactRepository localRepository,
65                                          ArtifactRepository remoteRepository )
66          throws RepositoryMetadataStoreException
67      {
68          try
69          {
70              updateRepositoryMetadata( localRepository, remoteRepository );
71          }
72          catch ( IOException e )
73          {
74              throw new RepositoryMetadataStoreException( "Error updating group repository metadata", e );
75          }
76          catch ( XmlPullParserException e )
77          {
78              throw new RepositoryMetadataStoreException( "Error updating group repository metadata", e );
79          }
80      }
81  
82      protected void updateRepositoryMetadata( ArtifactRepository localRepository,
83                                               ArtifactRepository remoteRepository )
84          throws IOException, XmlPullParserException
85      {
86          MetadataXpp3Reader mappingReader = new MetadataXpp3Reader();
87  
88          Metadata metadata = null;
89  
90          File metadataFile = new File( localRepository.getBasedir(),
91              localRepository.pathOfLocalRepositoryMetadata( this, remoteRepository ) );
92  
93          if ( metadataFile.length() == 0 )
94          {
95              metadataFile.delete();
96          }
97          else if ( metadataFile.exists() )
98          {
99              Reader reader = null;
100 
101             try
102             {
103                 reader = ReaderFactory.newXmlReader( metadataFile );
104 
105                 metadata = mappingReader.read( reader, false );
106             }
107             finally
108             {
109                 IOUtil.close( reader );
110             }
111         }
112 
113         boolean changed;
114 
115         // If file could not be found or was not valid, start from scratch
116         if ( metadata == null )
117         {
118             metadata = this.metadata;
119 
120             changed = true;
121         }
122         else
123         {
124             changed = metadata.merge( this.metadata );
125         }
126 
127         // beware meta-versions!
128         String version = metadata.getVersion();
129         if ( version != null && ( Artifact.LATEST_VERSION.equals( version ) || Artifact.RELEASE_VERSION.equals(
130             version ) ) )
131         {
132             // meta-versions are not valid <version/> values...don't write them.
133             metadata.setVersion( null );
134         }
135 
136         if ( changed || !metadataFile.exists() )
137         {
138             Writer writer = null;
139             try
140             {
141                 metadataFile.getParentFile().mkdirs();
142                 writer = WriterFactory.newXmlWriter( metadataFile );
143 
144                 MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
145 
146                 mappingWriter.write( writer, metadata );
147             }
148             finally
149             {
150                 IOUtil.close( writer );
151             }
152         }
153         else
154         {
155             metadataFile.setLastModified( System.currentTimeMillis() );
156         }
157     }
158 
159     public String toString()
160     {
161         return "repository metadata for: \'" + getKey() + "\'";
162     }
163 
164     protected static Metadata createMetadata( Artifact artifact,
165                                               Versioning versioning )
166     {
167         Metadata metadata = new Metadata();
168         metadata.setGroupId( artifact.getGroupId() );
169         metadata.setArtifactId( artifact.getArtifactId() );
170         metadata.setVersion( artifact.getVersion() );
171         metadata.setVersioning( versioning );
172         return metadata;
173     }
174 
175     protected static Versioning createVersioning( Snapshot snapshot )
176     {
177         Versioning versioning = new Versioning();
178         versioning.setSnapshot( snapshot );
179         versioning.updateTimestamp();
180         return versioning;
181     }
182 
183     public void setMetadata( Metadata metadata )
184     {
185         this.metadata = metadata;
186     }
187 
188     public Metadata getMetadata()
189     {
190         return metadata;
191     }
192 
193     public void merge( org.apache.maven.repository.legacy.metadata.ArtifactMetadata metadata )
194     {
195         // TODO: not sure that it should assume this, maybe the calls to addMetadata should pre-merge, then artifact replaces?
196         AbstractRepositoryMetadata repoMetadata = (AbstractRepositoryMetadata) metadata;
197         this.metadata.merge( repoMetadata.getMetadata() );
198     }
199     
200     public void merge( ArtifactMetadata metadata )
201     {
202         // TODO: not sure that it should assume this, maybe the calls to addMetadata should pre-merge, then artifact replaces?
203         AbstractRepositoryMetadata repoMetadata = (AbstractRepositoryMetadata) metadata;
204         this.metadata.merge( repoMetadata.getMetadata() );
205     }
206 
207     public String extendedToString()
208     {
209         StringBuilder buffer = new StringBuilder();
210 
211         buffer.append( "\nRepository Metadata\n--------------------------" );
212         buffer.append( "\nGroupId: " ).append( getGroupId() );
213         buffer.append( "\nArtifactId: " ).append( getArtifactId() );
214         buffer.append( "\nMetadata Type: " ).append( getClass().getName() );
215 
216         return buffer.toString();
217     }
218 
219     public int getNature()
220     {
221         return RELEASE;
222     }
223 
224     public ArtifactRepositoryPolicy getPolicy( ArtifactRepository repository )
225     {
226         int nature = getNature();
227         if ( ( nature & RepositoryMetadata.RELEASE_OR_SNAPSHOT ) == RepositoryMetadata.RELEASE_OR_SNAPSHOT )
228         {
229             ArtifactRepositoryPolicy policy = new ArtifactRepositoryPolicy( repository.getReleases() );
230             policy.merge( repository.getSnapshots() );
231             return policy;
232         }
233         else if ( ( nature & RepositoryMetadata.SNAPSHOT ) != 0 )
234         {
235             return repository.getSnapshots();
236         }
237         else
238         {
239             return repository.getReleases();
240         }
241     }
242 
243 }