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