Coverage Report - org.apache.maven.archiva.consumers.core.MetadataUpdaterConsumer
 
Classes in this File Line Coverage Branch Coverage Complexity
MetadataUpdaterConsumer
0%
0/86
0%
0/12
0
 
 1  
 package org.apache.maven.archiva.consumers.core;
 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.util.ArrayList;
 25  
 import java.util.Date;
 26  
 import java.util.List;
 27  
 
 28  
 import org.apache.maven.archiva.configuration.ArchivaConfiguration;
 29  
 import org.apache.maven.archiva.configuration.ConfigurationNames;
 30  
 import org.apache.maven.archiva.configuration.FileTypes;
 31  
 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
 32  
 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
 33  
 import org.apache.maven.archiva.consumers.ConsumerException;
 34  
 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
 35  
 import org.apache.maven.archiva.model.ArtifactReference;
 36  
 import org.apache.maven.archiva.model.ProjectReference;
 37  
 import org.apache.maven.archiva.model.VersionedReference;
 38  
 import org.apache.maven.archiva.repository.ContentNotFoundException;
 39  
 import org.apache.maven.archiva.repository.ManagedRepositoryContent;
 40  
 import org.apache.maven.archiva.repository.RepositoryContentFactory;
 41  
 import org.apache.maven.archiva.repository.RepositoryException;
 42  
 import org.apache.maven.archiva.repository.RepositoryNotFoundException;
 43  
 import org.apache.maven.archiva.repository.layout.LayoutException;
 44  
 import org.apache.maven.archiva.repository.metadata.MetadataTools;
 45  
 import org.apache.maven.archiva.repository.metadata.RepositoryMetadataException;
 46  
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
 47  
 import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
 48  
 import org.codehaus.plexus.registry.Registry;
 49  
 import org.codehaus.plexus.registry.RegistryListener;
 50  
 import org.slf4j.Logger;
 51  
 import org.slf4j.LoggerFactory;
 52  
 
 53  
 /**
 54  
  * MetadataUpdaterConsumer will create and update the metadata present within the repository.
 55  
  *
 56  
  * @version $Id: MetadataUpdaterConsumer.java 1041824 2010-12-03 14:11:05Z brett $
 57  
  * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
 58  
  * role-hint="metadata-updater"
 59  
  * instantiation-strategy="per-lookup"
 60  
  */
 61  0
 public class MetadataUpdaterConsumer
 62  
     extends AbstractMonitoredConsumer
 63  
     implements KnownRepositoryContentConsumer, RegistryListener, Initializable
 64  
 {
 65  0
     private Logger log = LoggerFactory.getLogger( MetadataUpdaterConsumer.class );
 66  
     
 67  
     /**
 68  
      * @plexus.configuration default-value="metadata-updater"
 69  
      */
 70  
     private String id;
 71  
 
 72  
     /**
 73  
      * @plexus.configuration default-value="Update / Create maven-metadata.xml files"
 74  
      */
 75  
     private String description;
 76  
 
 77  
     /**
 78  
      * @plexus.requirement
 79  
      */
 80  
     private RepositoryContentFactory repositoryFactory;
 81  
 
 82  
     /**
 83  
      * @plexus.requirement
 84  
      */
 85  
     private MetadataTools metadataTools;
 86  
 
 87  
     /**
 88  
      * @plexus.requirement
 89  
      */
 90  
     private ArchivaConfiguration configuration;
 91  
 
 92  
     /**
 93  
      * @plexus.requirement
 94  
      */
 95  
     private FileTypes filetypes;
 96  
 
 97  
     private static final String TYPE_METADATA_BAD_INTERNAL_REF = "metadata-bad-internal-ref";
 98  
 
 99  
     private static final String TYPE_METADATA_WRITE_FAILURE = "metadata-write-failure";
 100  
 
 101  
     private static final String TYPE_METADATA_IO = "metadata-io-warning";
 102  
 
 103  
     private ManagedRepositoryContent repository;
 104  
 
 105  
     private File repositoryDir;
 106  
 
 107  0
     private List<String> includes = new ArrayList<String>();
 108  
 
 109  0
     private long scanStartTimestamp = 0;
 110  
 
 111  
     public String getDescription()
 112  
     {
 113  0
         return description;
 114  
     }
 115  
 
 116  
     public String getId()
 117  
     {
 118  0
         return id;
 119  
     }
 120  
 
 121  
     public void setIncludes( List<String> includes )
 122  
     {
 123  0
         this.includes = includes;
 124  0
     }
 125  
 
 126  
     public void beginScan( ManagedRepositoryConfiguration repoConfig, Date whenGathered )
 127  
         throws ConsumerException
 128  
     {
 129  
         try
 130  
         {
 131  0
             this.repository = repositoryFactory.getManagedRepositoryContent( repoConfig.getId() );
 132  0
             this.repositoryDir = new File( repository.getRepoRoot() );
 133  0
             this.scanStartTimestamp = System.currentTimeMillis();
 134  
         }
 135  0
         catch ( RepositoryNotFoundException e )
 136  
         {
 137  0
             throw new ConsumerException( e.getMessage(), e );
 138  
         }
 139  0
         catch ( RepositoryException e )
 140  
         {
 141  0
             throw new ConsumerException( e.getMessage(), e );
 142  0
         }
 143  0
     }
 144  
 
 145  
     public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered, boolean executeOnEntireRepo )
 146  
         throws ConsumerException
 147  
     {
 148  0
         beginScan( repository, whenGathered );
 149  0
     }
 150  
 
 151  
     public void completeScan()
 152  
     {
 153  
         /* do nothing here */
 154  0
     }
 155  
 
 156  
     public void completeScan( boolean executeOnEntireRepo )
 157  
     {
 158  0
         completeScan();
 159  0
     }
 160  
 
 161  
     public List<String> getExcludes()
 162  
     {
 163  0
         return getDefaultArtifactExclusions();
 164  
     }
 165  
 
 166  
     public List<String> getIncludes()
 167  
     {
 168  0
         return this.includes;
 169  
     }
 170  
 
 171  
     public void processFile( String path )
 172  
         throws ConsumerException
 173  
     {
 174  
         // Ignore paths like .indexer etc
 175  0
         if ( !path.startsWith( "." ) )
 176  
         {
 177  
             try
 178  
             {
 179  0
                 ArtifactReference artifact = repository.toArtifactReference( path );
 180  0
                 updateVersionMetadata( artifact, path );
 181  0
                 updateProjectMetadata( artifact, path );
 182  
             }
 183  0
             catch ( LayoutException e )
 184  
             {
 185  0
                 log.info( "Not processing path that is not an artifact: " + path + " (" + e.getMessage() + ")" );
 186  0
             }
 187  
         }
 188  0
     }
 189  
 
 190  
     public void processFile( String path, boolean executeOnEntireRepo )
 191  
         throws Exception
 192  
     {
 193  0
         processFile( path );
 194  0
     }
 195  
 
 196  
     private void updateProjectMetadata( ArtifactReference artifact, String path )
 197  
     {
 198  0
         ProjectReference projectRef = new ProjectReference();
 199  0
         projectRef.setGroupId( artifact.getGroupId() );
 200  0
         projectRef.setArtifactId( artifact.getArtifactId() );
 201  
 
 202  
         try
 203  
         {
 204  0
             String metadataPath = this.metadataTools.toPath( projectRef );
 205  
 
 206  0
             File projectMetadata = new File( this.repositoryDir, metadataPath );
 207  
 
 208  0
             if ( projectMetadata.exists() && ( projectMetadata.lastModified() >= this.scanStartTimestamp ) )
 209  
             {
 210  
                 // This metadata is up to date. skip it.
 211  0
                 log.debug( "Skipping uptodate metadata: " + this.metadataTools.toPath( projectRef ) );
 212  0
                 return;
 213  
             }
 214  
 
 215  0
             metadataTools.updateMetadata( this.repository, projectRef );
 216  0
             log.debug( "Updated metadata: " + this.metadataTools.toPath( projectRef ) );
 217  
         }
 218  0
         catch ( LayoutException e )
 219  
         {
 220  0
             triggerConsumerWarning( TYPE_METADATA_BAD_INTERNAL_REF, "Unable to convert path [" + path
 221  
                 + "] to an internal project reference: " + e.getMessage() );
 222  
         }
 223  0
         catch ( RepositoryMetadataException e )
 224  
         {
 225  0
             triggerConsumerError( TYPE_METADATA_WRITE_FAILURE, "Unable to write project metadata for artifact [" + path
 226  
                 + "]: " + e.getMessage() );
 227  
         }
 228  0
         catch ( IOException e )
 229  
         {
 230  0
             triggerConsumerWarning( TYPE_METADATA_IO, "Project metadata not written due to IO warning: "
 231  
                 + e.getMessage() );
 232  
         }
 233  0
         catch ( ContentNotFoundException e )
 234  
         {
 235  0
             triggerConsumerWarning( TYPE_METADATA_IO,
 236  
                                     "Project metadata not written because no versions were found to update: "
 237  
                                         + e.getMessage() );
 238  0
         }
 239  0
     }
 240  
 
 241  
     private void updateVersionMetadata( ArtifactReference artifact, String path )
 242  
     {
 243  0
         VersionedReference versionRef = new VersionedReference();
 244  0
         versionRef.setGroupId( artifact.getGroupId() );
 245  0
         versionRef.setArtifactId( artifact.getArtifactId() );
 246  0
         versionRef.setVersion( artifact.getVersion() );
 247  
 
 248  
         try
 249  
         {
 250  0
             String metadataPath = this.metadataTools.toPath( versionRef );
 251  
 
 252  0
             File projectMetadata = new File( this.repositoryDir, metadataPath );
 253  
 
 254  0
             if ( projectMetadata.exists() && ( projectMetadata.lastModified() >= this.scanStartTimestamp ) )
 255  
             {
 256  
                 // This metadata is up to date. skip it.
 257  0
                 log.debug( "Skipping uptodate metadata: " + this.metadataTools.toPath( versionRef ) );
 258  0
                 return;
 259  
             }
 260  
 
 261  0
             metadataTools.updateMetadata( this.repository, versionRef );
 262  0
             log.debug( "Updated metadata: " + this.metadataTools.toPath( versionRef ) );
 263  
         }
 264  0
         catch ( LayoutException e )
 265  
         {
 266  0
             triggerConsumerWarning( TYPE_METADATA_BAD_INTERNAL_REF, "Unable to convert path [" + path
 267  
                 + "] to an internal version reference: " + e.getMessage() );
 268  
         }
 269  0
         catch ( RepositoryMetadataException e )
 270  
         {
 271  0
             triggerConsumerError( TYPE_METADATA_WRITE_FAILURE, "Unable to write version metadata for artifact [" + path
 272  
                 + "]: " + e.getMessage() );
 273  
         }
 274  0
         catch ( IOException e )
 275  
         {
 276  0
             triggerConsumerWarning( TYPE_METADATA_IO, "Version metadata not written due to IO warning: "
 277  
                 + e.getMessage() );
 278  
         }
 279  0
         catch ( ContentNotFoundException e )
 280  
         {
 281  0
             triggerConsumerWarning( TYPE_METADATA_IO,
 282  
                                     "Version metadata not written because no versions were found to update: "
 283  
                                         + e.getMessage() );
 284  0
         }
 285  0
     }
 286  
 
 287  
     public boolean isPermanent()
 288  
     {
 289  0
         return false;
 290  
     }
 291  
 
 292  
     public void afterConfigurationChange( Registry registry, String propertyName, Object propertyValue )
 293  
     {
 294  0
         if ( ConfigurationNames.isRepositoryScanning( propertyName ) )
 295  
         {
 296  0
             initIncludes();
 297  
         }
 298  0
     }
 299  
 
 300  
     public void beforeConfigurationChange( Registry registry, String propertyName, Object propertyValue )
 301  
     {
 302  
         /* do nothing here */
 303  0
     }
 304  
 
 305  
     private void initIncludes()
 306  
     {
 307  0
         includes.clear();
 308  
 
 309  0
         includes.addAll( filetypes.getFileTypePatterns( FileTypes.ARTIFACTS ) );
 310  0
     }
 311  
 
 312  
     public void initialize()
 313  
         throws InitializationException
 314  
     {
 315  0
         configuration.addChangeListener( this );
 316  
 
 317  0
         initIncludes();
 318  0
     }
 319  
 }