1 package org.apache.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.archiva.admin.model.beans.ManagedRepository;
23 import org.apache.archiva.consumers.AbstractMonitoredConsumer;
24 import org.apache.archiva.consumers.ConsumerException;
25 import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
26 import org.apache.commons.io.FileUtils;
27 import org.springframework.context.annotation.Scope;
28 import org.springframework.stereotype.Service;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 import java.io.IOException;
34 import java.util.ArrayList;
35 import java.util.Date;
36 import java.util.HashMap;
37 import java.util.Iterator;
38 import java.util.List;
46 @Service( "knownRepositoryContentConsumer#auto-rename" )
48 public class AutoRenameConsumer
49 extends AbstractMonitoredConsumer
50 implements KnownRepositoryContentConsumer
52 private Logger log = LoggerFactory.getLogger( AutoRenameConsumer.class );
54 private String id = "auto-rename";
56 private String description = "Automatically rename common artifact mistakes.";
58 private static final String RENAME_FAILURE = "rename_failure";
60 private File repositoryDir;
62 private List<String> includes = new ArrayList<>( 3 );
64 private Map<String, String> extensionRenameMap = new HashMap<>();
66 public AutoRenameConsumer()
68 includes.add( "**/*.distribution-tgz" );
69 includes.add( "**/*.distribution-zip" );
70 includes.add( "**/*.plugin" );
72 extensionRenameMap.put( ".distribution-tgz", ".tar.gz" );
73 extensionRenameMap.put( ".distribution-zip", ".zip" );
74 extensionRenameMap.put( ".plugin", ".jar" );
84 public String getDescription()
86 return this.description;
90 public boolean isPermanent()
96 public void beginScan( ManagedRepository repository, Date whenGathered )
97 throws ConsumerException
99 this.repositoryDir = new File( repository.getLocation() );
103 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
104 throws ConsumerException
106 beginScan( repository, whenGathered );
110 public void completeScan()
116 public void completeScan( boolean executeOnEntireRepo )
122 public List<String> getExcludes()
128 public List<String> getIncludes()
134 public void processFile( String path )
135 throws ConsumerException
137 File file = new File( this.repositoryDir, path );
140 Iterator<String> itExtensions = this.extensionRenameMap.keySet().iterator();
141 while ( itExtensions.hasNext() )
143 String extension = itExtensions.next();
144 if ( path.endsWith( extension ) )
146 String fixedExtension = this.extensionRenameMap.get( extension );
147 String correctedPath = path.substring( 0, path.length() - extension.length() ) + fixedExtension;
148 File to = new File( this.repositoryDir, correctedPath );
152 FileUtils.moveFile( file, to );
154 catch ( IOException e )
156 log.warn( "Unable to rename {} to {} :", path, correctedPath, e );
157 triggerConsumerWarning( RENAME_FAILURE, "Unable to rename " + path + " to " + correctedPath +
158 ": " + e.getMessage() );
163 log.info( "(Auto) Removing File: {} ", file.getAbsolutePath() );
164 triggerConsumerInfo( "(Auto) Removing File: " + file.getAbsolutePath() );
170 public void processFile( String path, boolean executeOnEntireRepo )
171 throws ConsumerException