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