1 package org.apache.archiva.consumers.core;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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;
31
32 import java.io.File;
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;
39 import java.util.Map;
40
41
42
43
44
45
46 @Service( "knownRepositoryContentConsumer#auto-rename" )
47 @Scope( "prototype" )
48 public class AutoRenameConsumer
49 extends AbstractMonitoredConsumer
50 implements KnownRepositoryContentConsumer
51 {
52 private Logger log = LoggerFactory.getLogger( AutoRenameConsumer.class );
53
54 private String id = "auto-rename";
55
56 private String description = "Automatically rename common artifact mistakes.";
57
58 private static final String RENAME_FAILURE = "rename_failure";
59
60 private File repositoryDir;
61
62 private List<String> includes = new ArrayList<>( 3 );
63
64 private Map<String, String> extensionRenameMap = new HashMap<>();
65
66 public AutoRenameConsumer()
67 {
68 includes.add( "**/*.distribution-tgz" );
69 includes.add( "**/*.distribution-zip" );
70 includes.add( "**/*.plugin" );
71
72 extensionRenameMap.put( ".distribution-tgz", ".tar.gz" );
73 extensionRenameMap.put( ".distribution-zip", ".zip" );
74 extensionRenameMap.put( ".plugin", ".jar" );
75 }
76
77 @Override
78 public String getId()
79 {
80 return this.id;
81 }
82
83 @Override
84 public String getDescription()
85 {
86 return this.description;
87 }
88
89 @Override
90 public void beginScan( ManagedRepository repository, Date whenGathered )
91 throws ConsumerException
92 {
93 this.repositoryDir = new File( repository.getLocation() );
94 }
95
96 @Override
97 public void beginScan( ManagedRepository repository, Date whenGathered, boolean executeOnEntireRepo )
98 throws ConsumerException
99 {
100 beginScan( repository, whenGathered );
101 }
102
103 @Override
104 public void completeScan()
105 {
106
107 }
108
109 @Override
110 public void completeScan( boolean executeOnEntireRepo )
111 {
112 completeScan();
113 }
114
115 @Override
116 public List<String> getExcludes()
117 {
118 return null;
119 }
120
121 @Override
122 public List<String> getIncludes()
123 {
124 return includes;
125 }
126
127 @Override
128 public void processFile( String path )
129 throws ConsumerException
130 {
131 File file = new File( this.repositoryDir, path );
132 if ( file.exists() )
133 {
134 Iterator<String> itExtensions = this.extensionRenameMap.keySet().iterator();
135 while ( itExtensions.hasNext() )
136 {
137 String extension = itExtensions.next();
138 if ( path.endsWith( extension ) )
139 {
140 String fixedExtension = this.extensionRenameMap.get( extension );
141 String correctedPath = path.substring( 0, path.length() - extension.length() ) + fixedExtension;
142 File to = new File( this.repositoryDir, correctedPath );
143 try
144 {
145
146 FileUtils.moveFile( file, to );
147 }
148 catch ( IOException e )
149 {
150 log.warn( "Unable to rename {} to {} :", path, correctedPath, e );
151 triggerConsumerWarning( RENAME_FAILURE, "Unable to rename " + path + " to " + correctedPath +
152 ": " + e.getMessage() );
153 }
154 }
155 }
156
157 log.info( "(Auto) Removing File: {} ", file.getAbsolutePath() );
158 triggerConsumerInfo( "(Auto) Removing File: " + file.getAbsolutePath() );
159 file.delete();
160 }
161 }
162
163 @Override
164 public void processFile( String path, boolean executeOnEntireRepo )
165 throws ConsumerException
166 {
167 processFile( path );
168 }
169 }