]> source.dussan.org Git - archiva.git/blob
33a6568bf9d7d1d2fa91f28dfebaaddd8ddb11a5
[archiva.git] /
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 import org.springframework.context.annotation.Scope;
28 import org.springframework.stereotype.Service;
29
30 import java.io.File;
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;
37 import java.util.Map;
38
39 /**
40  * AutoRenameConsumer
41  *
42  * @version $Id$
43  */
44 @Service("knownRepositoryContentConsumer#auto-rename")
45 @Scope("prototype")
46 public class AutoRenameConsumer
47     extends AbstractMonitoredConsumer
48     implements KnownRepositoryContentConsumer
49 {
50     /**
51      * default-value="auto-rename"
52      */
53     private String id = "auto-rename";
54
55     /**
56      * default-value="Automatically rename common artifact mistakes."
57      */
58     private String description = "Automatically rename common artifact mistakes.";
59
60     private static final String RENAME_FAILURE = "rename_failure";
61
62     private File repositoryDir;
63
64     private List<String> includes = new ArrayList<String>();
65
66     private Map<String, String> extensionRenameMap = new HashMap<String, String>();
67
68     public AutoRenameConsumer()
69     {
70         includes.add( "**/*.distribution-tgz" );
71         includes.add( "**/*.distribution-zip" );
72         includes.add( "**/*.plugin" );
73
74         extensionRenameMap.put( ".distribution-tgz", ".tar.gz" );
75         extensionRenameMap.put( ".distribution-zip", ".zip" );
76         extensionRenameMap.put( ".plugin", ".jar" );
77     }
78
79     public String getId()
80     {
81         return this.id;
82     }
83
84     public String getDescription()
85     {
86         return this.description;
87     }
88
89     public boolean isPermanent()
90     {
91         return false;
92     }
93
94     public void beginScan( ManagedRepositoryConfiguration repository, Date whenGathered )
95         throws ConsumerException
96     {
97         this.repositoryDir = new File( repository.getLocation() );
98     }
99
100     public void beginScan( ManagedRepositoryConfiguration 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 = (String) itExtensions.next();
136                 if ( path.endsWith( extension ) )
137                 {
138                     String fixedExtension = (String) 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 }