001    package org.apache.archiva.consumers.core;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *  http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import org.apache.archiva.admin.model.beans.ManagedRepository;
023    import org.apache.archiva.consumers.AbstractMonitoredConsumer;
024    import org.apache.archiva.consumers.ConsumerException;
025    import org.apache.archiva.consumers.KnownRepositoryContentConsumer;
026    import org.apache.commons.io.FileUtils;
027    import org.springframework.context.annotation.Scope;
028    import org.springframework.stereotype.Service;
029    
030    import java.io.File;
031    import java.io.IOException;
032    import java.util.ArrayList;
033    import java.util.Date;
034    import java.util.HashMap;
035    import java.util.Iterator;
036    import java.util.List;
037    import java.util.Map;
038    
039    /**
040     * AutoRenameConsumer
041     *
042     *
043     */
044    @Service( "knownRepositoryContentConsumer#auto-rename" )
045    @Scope( "prototype" )
046    public class AutoRenameConsumer
047        extends AbstractMonitoredConsumer
048        implements KnownRepositoryContentConsumer
049    {
050        /**
051         * default-value="auto-rename"
052         */
053        private String id = "auto-rename";
054    
055        /**
056         * default-value="Automatically rename common artifact mistakes."
057         */
058        private String description = "Automatically rename common artifact mistakes.";
059    
060        private static final String RENAME_FAILURE = "rename_failure";
061    
062        private File repositoryDir;
063    
064        private List<String> includes = new ArrayList<String>( 3 );
065    
066        private Map<String, String> extensionRenameMap = new HashMap<String, String>();
067    
068        public AutoRenameConsumer()
069        {
070            includes.add( "**/*.distribution-tgz" );
071            includes.add( "**/*.distribution-zip" );
072            includes.add( "**/*.plugin" );
073    
074            extensionRenameMap.put( ".distribution-tgz", ".tar.gz" );
075            extensionRenameMap.put( ".distribution-zip", ".zip" );
076            extensionRenameMap.put( ".plugin", ".jar" );
077        }
078    
079        public String getId()
080        {
081            return this.id;
082        }
083    
084        public String getDescription()
085        {
086            return this.description;
087        }
088    
089        public boolean isPermanent()
090        {
091            return false;
092        }
093    
094        public void beginScan( ManagedRepository repository, Date whenGathered )
095            throws ConsumerException
096        {
097            this.repositoryDir = new File( repository.getLocation() );
098        }
099    
100        public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
101            throws ConsumerException
102        {
103            beginScan( repository, whenGathered );
104        }
105    
106        public void completeScan()
107        {
108            /* do nothing */
109        }
110    
111        public void completeScan( boolean executeOnEntireRepo )
112        {
113            completeScan();
114        }
115    
116        public List<String> getExcludes()
117        {
118            return null;
119        }
120    
121        public List<String> getIncludes()
122        {
123            return includes;
124        }
125    
126        public void processFile( String path )
127            throws ConsumerException
128        {
129            File file = new File( this.repositoryDir, path );
130            if ( file.exists() )
131            {
132                Iterator<String> itExtensions = this.extensionRenameMap.keySet().iterator();
133                while ( itExtensions.hasNext() )
134                {
135                    String extension = itExtensions.next();
136                    if ( path.endsWith( extension ) )
137                    {
138                        String fixedExtension = this.extensionRenameMap.get( extension );
139                        String correctedPath = path.substring( 0, path.length() - extension.length() ) + fixedExtension;
140                        File to = new File( this.repositoryDir, correctedPath );
141                        try
142                        {
143                            // Rename the file.
144                            FileUtils.moveFile( file, to );
145                        }
146                        catch ( IOException e )
147                        {
148                            triggerConsumerWarning( RENAME_FAILURE, "Unable to rename " + path + " to " + correctedPath +
149                                ": " + e.getMessage() );
150                        }
151                    }
152                }
153    
154                triggerConsumerInfo( "(Auto) Removing File: " + file.getAbsolutePath() );
155                file.delete();
156            }
157        }
158    
159        public void processFile( String path, boolean executeOnEntireRepo )
160            throws ConsumerException
161        {
162            processFile( path );
163        }
164    }