Coverage Report - org.apache.maven.artifact.repository.metadata.AbstractRepositoryMetadata
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractRepositoryMetadata
66 %
41/62
71 %
10/14
1,667
 
 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  4
     {
 50  4
         this.metadata = metadata;
 51  4
     }
 52  
 
 53  
     public String getRemoteFilename()
 54  
     {
 55  2
         return "maven-metadata.xml";
 56  
     }
 57  
 
 58  
     public String getLocalFilename( ArtifactRepository repository )
 59  
     {
 60  3
         return "maven-metadata-" + repository.getKey() + ".xml";
 61  
     }
 62  
 
 63  
     public void storeInLocalRepository( ArtifactRepository localRepository, ArtifactRepository remoteRepository )
 64  
         throws RepositoryMetadataStoreException
 65  
     {
 66  
         try
 67  
         {
 68  2
             updateRepositoryMetadata( localRepository, remoteRepository );
 69  
         }
 70  0
         catch ( IOException e )
 71  
         {
 72  0
             throw new RepositoryMetadataStoreException( "Error updating group repository metadata", e );
 73  
         }
 74  0
         catch ( XmlPullParserException e )
 75  
         {
 76  0
             throw new RepositoryMetadataStoreException( "Error updating group repository metadata", e );
 77  2
         }
 78  2
     }
 79  
 
 80  
     protected void updateRepositoryMetadata( ArtifactRepository localRepository, ArtifactRepository remoteRepository )
 81  
         throws IOException, XmlPullParserException
 82  
     {
 83  4
         MetadataXpp3Reader mappingReader = new MetadataXpp3Reader();
 84  
 
 85  4
         Metadata metadata = null;
 86  
 
 87  4
         File metadataFile = new File( localRepository.getBasedir(),
 88  
                                       localRepository.pathOfLocalRepositoryMetadata( this, remoteRepository ) );
 89  
 
 90  4
         if ( metadataFile.exists() )
 91  
         {
 92  2
             Reader reader = null;
 93  
 
 94  
             try
 95  
             {
 96  2
                 reader = ReaderFactory.newXmlReader( metadataFile );
 97  
 
 98  2
                 metadata = mappingReader.read( reader, false );
 99  
             }
 100  
             finally
 101  
             {
 102  2
                 IOUtil.close( reader );
 103  2
             }
 104  
         }
 105  
 
 106  
         boolean changed;
 107  
 
 108  
         // If file could not be found or was not valid, start from scratch
 109  4
         if ( metadata == null )
 110  
         {
 111  2
             metadata = this.metadata;
 112  
 
 113  2
             changed = true;
 114  
         }
 115  
         else
 116  
         {
 117  2
             changed = metadata.merge( this.metadata );
 118  
         }
 119  
 
 120  
         // beware meta-versions!
 121  4
         String version = metadata.getVersion();
 122  4
         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  2
             metadata.setVersion( null );
 126  
         }
 127  
 
 128  4
         if ( changed || !metadataFile.exists() )
 129  
         {
 130  4
             Writer writer = null;
 131  
             try
 132  
             {
 133  4
                 metadataFile.getParentFile().mkdirs();
 134  4
                 writer = WriterFactory.newXmlWriter( metadataFile );
 135  
 
 136  4
                 MetadataXpp3Writer mappingWriter = new MetadataXpp3Writer();
 137  
 
 138  4
                 mappingWriter.write( writer, metadata );
 139  
             }
 140  
             finally
 141  
             {
 142  4
                 IOUtil.close( writer );
 143  4
             }
 144  4
         }
 145  
         else
 146  
         {
 147  0
             metadataFile.setLastModified( System.currentTimeMillis() );
 148  
         }
 149  4
     }
 150  
 
 151  
     public String toString()
 152  
     {
 153  1
         return "repository metadata for: \'" + getKey() + "\'";
 154  
     }
 155  
 
 156  
     protected static Metadata createMetadata( Artifact artifact, Versioning versioning )
 157  
     {
 158  2
         Metadata metadata = new Metadata();
 159  2
         metadata.setGroupId( artifact.getGroupId() );
 160  2
         metadata.setArtifactId( artifact.getArtifactId() );
 161  2
         metadata.setVersion( artifact.getVersion() );
 162  2
         metadata.setVersioning( versioning );
 163  2
         return metadata;
 164  
     }
 165  
 
 166  
     protected static Versioning createVersioning( Snapshot snapshot )
 167  
     {
 168  0
         Versioning versioning = new Versioning();
 169  0
         versioning.setSnapshot( snapshot );
 170  0
         versioning.updateTimestamp();
 171  0
         return versioning;
 172  
     }
 173  
 
 174  
     public void setMetadata( Metadata metadata )
 175  
     {
 176  0
         this.metadata = metadata;
 177  0
     }
 178  
 
 179  
     public Metadata getMetadata()
 180  
     {
 181  0
         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  0
         AbstractRepositoryMetadata repoMetadata = (AbstractRepositoryMetadata) metadata;
 188  0
         this.metadata.merge( repoMetadata.getMetadata() );
 189  0
     }
 190  
 
 191  
     public String extendedToString()
 192  
     {
 193  0
         StringBuffer buffer = new StringBuffer();
 194  
 
 195  0
         buffer.append( "\nRepository Metadata\n--------------------------" );
 196  0
         buffer.append( "\nGroupId: " ).append( getGroupId() );
 197  0
         buffer.append( "\nArtifactId: " ).append( getArtifactId() );
 198  0
         buffer.append( "\nMetadata Type: " ).append( getClass().getName() );
 199  
 
 200  0
         return buffer.toString();
 201  
     }
 202  
 }