Coverage Report - org.apache.maven.archiva.consumers.core.AutoRenameConsumer
 
Classes in this File Line Coverage Branch Coverage Complexity
AutoRenameConsumer
0%
0/41
0%
0/6
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 org.apache.commons.io.FileUtils;
 23  
 import org.apache.maven.archiva.configuration.ManagedRepositoryConfiguration;
 24  
 import org.apache.maven.archiva.consumers.AbstractMonitoredConsumer;
 25  
 import org.apache.maven.archiva.consumers.ConsumerException;
 26  
 import org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer;
 27  
 
 28  
 import java.io.File;
 29  
 import java.io.IOException;
 30  
 import java.util.ArrayList;
 31  
 import java.util.Date;
 32  
 import java.util.HashMap;
 33  
 import java.util.Iterator;
 34  
 import java.util.List;
 35  
 import java.util.Map;
 36  
 
 37  
 /**
 38  
  * AutoRenameConsumer
 39  
  *
 40  
  * @version $Id: AutoRenameConsumer.java 1041824 2010-12-03 14:11:05Z brett $
 41  
  * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
 42  
  * role-hint="auto-rename"
 43  
  * instantiation-strategy="per-lookup"
 44  
  */
 45  
 public class AutoRenameConsumer
 46  
     extends AbstractMonitoredConsumer
 47  
     implements KnownRepositoryContentConsumer
 48  
 {
 49  
     /**
 50  
      * @plexus.configuration default-value="auto-rename"
 51  
      */
 52  
     private String id;
 53  
 
 54  
     /**
 55  
      * @plexus.configuration default-value="Automatically rename common artifact mistakes."
 56  
      */
 57  
     private String description;
 58  
 
 59  
     private static final String RENAME_FAILURE = "rename_failure";
 60  
 
 61  
     private File repositoryDir;
 62  
 
 63  0
     private List<String> includes = new ArrayList<String>();
 64  
 
 65  0
     private Map<String, String> extensionRenameMap = new HashMap<String, String>();
 66  
 
 67  
     public AutoRenameConsumer()
 68  0
     {
 69  0
         includes.add( "**/*.distribution-tgz" );
 70  0
         includes.add( "**/*.distribution-zip" );
 71  0
         includes.add( "**/*.plugin" );
 72  
 
 73  0
         extensionRenameMap.put( ".distribution-tgz", ".tar.gz" );
 74  0
         extensionRenameMap.put( ".distribution-zip", ".zip" );
 75  0
         extensionRenameMap.put( ".plugin", ".jar" );
 76  0
     }
 77  
 
 78  
     public String getId()
 79  
     {
 80  0
         return this.id;
 81  
     }
 82  
 
 83  
     public String getDescription()
 84  
     {
 85  0
         return this.description;
 86  
     }
 87  
 
 88  
     public boolean isPermanent()
 89  
     {
 90  0
         return false;
 91  
     }
 92  
 
 93  
     public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
 94  
         throws ConsumerException
 95  
     {
 96  0
         this.repositoryDir = new File( repository.getLocation() );
 97  0
     }
 98  
 
 99  
     public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered, boolean executeOnEntireRepo )
 100  
         throws ConsumerException
 101  
     {
 102  0
         beginScan( repository, whenGathered );
 103  0
     }
 104  
 
 105  
     public void completeScan()
 106  
     {
 107  
         /* do nothing */
 108  0
     }
 109  
 
 110  
     public void completeScan( boolean executeOnEntireRepo )
 111  
     {
 112  0
         completeScan();
 113  0
     }
 114  
 
 115  
     public List<String> getExcludes()
 116  
     {
 117  0
         return null;
 118  
     }
 119  
 
 120  
     public List<String> getIncludes()
 121  
     {
 122  0
         return includes;
 123  
     }
 124  
 
 125  
     public void processFile( String path )
 126  
         throws ConsumerException
 127  
     {
 128  0
         File file = new File( this.repositoryDir, path );
 129  0
         if ( file.exists() )
 130  
         {
 131  0
             Iterator<String> itExtensions = this.extensionRenameMap.keySet().iterator();
 132  0
             while ( itExtensions.hasNext() )
 133  
             {
 134  0
                 String extension = (String) itExtensions.next();
 135  0
                 if ( path.endsWith( extension ) )
 136  
                 {
 137  0
                     String fixedExtension = (String) this.extensionRenameMap.get( extension );
 138  0
                     String correctedPath = path.substring( 0, path.length() - extension.length() ) + fixedExtension;
 139  0
                     File to = new File( this.repositoryDir, correctedPath );
 140  
                     try
 141  
                     {
 142  
                         // Rename the file.
 143  0
                         FileUtils.moveFile( file, to );
 144  
                     }
 145  0
                     catch ( IOException e )
 146  
                     {
 147  0
                         triggerConsumerWarning( RENAME_FAILURE, "Unable to rename " + path + " to " + correctedPath +
 148  
                             ": " + e.getMessage() );
 149  0
                     }
 150  
                 }
 151  0
             }
 152  
 
 153  0
             triggerConsumerInfo( "(Auto) Removing File: " + file.getAbsolutePath() );
 154  0
             file.delete();
 155  
         }
 156  0
     }
 157  
 
 158  
     public void processFile( String path, boolean executeOnEntireRepo )
 159  
         throws ConsumerException
 160  
     {
 161  0
         processFile( path );        
 162  0
     }
 163  
 }