1 package org.apache.maven.archiva.consumers.core;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
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;
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;
41 * @plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
42 * role-hint="auto-rename"
43 * instantiation-strategy="per-lookup"
45 public class AutoRenameConsumer
46 extends AbstractMonitoredConsumer
47 implements KnownRepositoryContentConsumer
50 * @plexus.configuration default-value="auto-rename"
55 * @plexus.configuration default-value="Automatically rename common artifact mistakes."
57 private String description;
59 private static final String RENAME_FAILURE = "rename_failure";
61 private File repositoryDir;
63 private List<String> includes = new ArrayList<String>();
65 private Map<String, String> extensionRenameMap = new HashMap<String, String>();
67 public AutoRenameConsumer()
69 includes.add( "**/*.distribution-tgz" );
70 includes.add( "**/*.distribution-zip" );
71 includes.add( "**/*.plugin" );
73 extensionRenameMap.put( ".distribution-tgz", ".tar.gz" );
74 extensionRenameMap.put( ".distribution-zip", ".zip" );
75 extensionRenameMap.put( ".plugin", ".jar" );
83 public String getDescription()
85 return this.description;
88 public boolean isPermanent()
93 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
94 throws ConsumerException
96 this.repositoryDir = new File( repository.getLocation() );
99 public void completeScan()
104 public List<String> getExcludes()
109 public List<String> getIncludes()
114 public void processFile( String path )
115 throws ConsumerException
117 File file = new File( this.repositoryDir, path );
120 Iterator<String> itExtensions = this.extensionRenameMap.keySet().iterator();
121 while ( itExtensions.hasNext() )
123 String extension = (String) itExtensions.next();
124 if ( path.endsWith( extension ) )
126 String fixedExtension = (String) this.extensionRenameMap.get( extension );
127 String correctedPath = path.substring( 0, path.length() - extension.length() ) + fixedExtension;
128 File to = new File( this.repositoryDir, correctedPath );
132 FileUtils.moveFile( file, to );
134 catch ( IOException e )
136 triggerConsumerWarning( RENAME_FAILURE, "Unable to rename " + path + " to " + correctedPath +
137 ": " + e.getMessage() );
142 triggerConsumerInfo( "(Auto) Removing File: " + file.getAbsolutePath() );