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;
27 import org.springframework.context.annotation.Scope;
28 import org.springframework.stereotype.Service;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.Date;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
43 * plexus.component role="org.apache.maven.archiva.consumers.KnownRepositoryContentConsumer"
44 * role-hint="auto-rename"
45 * instantiation-strategy="per-lookup"
47 @Service("knownRepositoryContentConsumer#auto-rename")
49 public class AutoRenameConsumer
50 extends AbstractMonitoredConsumer
51 implements KnownRepositoryContentConsumer
54 * plexus.configuration default-value="auto-rename"
56 private String id = "auto-rename";
59 * plexus.configuration default-value="Automatically rename common artifact mistakes."
61 private String description = "Automatically rename common artifact mistakes.";
63 private static final String RENAME_FAILURE = "rename_failure";
65 private File repositoryDir;
67 private List<String> includes = new ArrayList<String>();
69 private Map<String, String> extensionRenameMap = new HashMap<String, String>();
71 public AutoRenameConsumer()
73 includes.add( "**/*.distribution-tgz" );
74 includes.add( "**/*.distribution-zip" );
75 includes.add( "**/*.plugin" );
77 extensionRenameMap.put( ".distribution-tgz", ".tar.gz" );
78 extensionRenameMap.put( ".distribution-zip", ".zip" );
79 extensionRenameMap.put( ".plugin", ".jar" );
87 public String getDescription()
89 return this.description;
92 public boolean isPermanent()
97 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
98 throws ConsumerException
100 this.repositoryDir = new File( repository.getLocation() );
103 public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered, boolean executeOnEntireRepo )
104 throws ConsumerException
106 beginScan( repository, whenGathered );
109 public void completeScan()
114 public void completeScan( boolean executeOnEntireRepo )
119 public List<String> getExcludes()
124 public List<String> getIncludes()
129 public void processFile( String path )
130 throws ConsumerException
132 File file = new File( this.repositoryDir, path );
135 Iterator<String> itExtensions = this.extensionRenameMap.keySet().iterator();
136 while ( itExtensions.hasNext() )
138 String extension = (String) itExtensions.next();
139 if ( path.endsWith( extension ) )
141 String fixedExtension = (String) this.extensionRenameMap.get( extension );
142 String correctedPath = path.substring( 0, path.length() - extension.length() ) + fixedExtension;
143 File to = new File( this.repositoryDir, correctedPath );
147 FileUtils.moveFile( file, to );
149 catch ( IOException e )
151 triggerConsumerWarning( RENAME_FAILURE, "Unable to rename " + path + " to " + correctedPath +
152 ": " + e.getMessage() );
157 triggerConsumerInfo( "(Auto) Removing File: " + file.getAbsolutePath() );
162 public void processFile( String path, boolean executeOnEntireRepo )
163 throws ConsumerException