1 | |
package org.apache.maven.archiva.policies; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import java.io.File; |
23 | |
import java.util.ArrayList; |
24 | |
import java.util.List; |
25 | |
import java.util.Properties; |
26 | |
|
27 | |
import org.apache.commons.lang.StringUtils; |
28 | |
import org.apache.maven.archiva.policies.urlcache.UrlFailureCache; |
29 | |
import org.slf4j.Logger; |
30 | |
import org.slf4j.LoggerFactory; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class CachedFailuresPolicy |
40 | |
implements PreDownloadPolicy |
41 | |
{ |
42 | 0 | private Logger log = LoggerFactory.getLogger( CachedFailuresPolicy.class ); |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
public static final String NO = "no"; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
public static final String YES = "yes"; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
private UrlFailureCache urlFailureCache; |
60 | |
|
61 | 0 | private List<String> options = new ArrayList<String>(); |
62 | |
|
63 | |
public CachedFailuresPolicy() |
64 | 0 | { |
65 | 0 | options.add( NO ); |
66 | 0 | options.add( YES ); |
67 | 0 | } |
68 | |
|
69 | |
public void applyPolicy( String policySetting, Properties request, File localFile ) |
70 | |
throws PolicyViolationException, PolicyConfigurationException |
71 | |
{ |
72 | 0 | if ( !options.contains( policySetting ) ) |
73 | |
{ |
74 | |
|
75 | 0 | throw new PolicyConfigurationException( "Unknown cache-failues policy setting [" + policySetting + |
76 | |
"], valid settings are [" + StringUtils.join( options.iterator(), "," ) + "]" ); |
77 | |
} |
78 | |
|
79 | 0 | if ( NO.equals( policySetting ) ) |
80 | |
{ |
81 | |
|
82 | 0 | log.debug( "OK to fetch, check-failures policy set to NO." ); |
83 | 0 | return; |
84 | |
} |
85 | |
|
86 | 0 | String url = request.getProperty( "url" ); |
87 | |
|
88 | 0 | if ( StringUtils.isNotBlank( url ) ) |
89 | |
{ |
90 | 0 | if ( urlFailureCache.hasFailedBefore( url ) ) |
91 | |
{ |
92 | 0 | throw new PolicyViolationException( |
93 | |
"NO to fetch, check-failures detected previous failure on url: " + url ); |
94 | |
} |
95 | |
} |
96 | |
|
97 | 0 | log.debug( "OK to fetch, check-failures detected no issues." ); |
98 | 0 | } |
99 | |
|
100 | |
public String getDefaultOption() |
101 | |
{ |
102 | 0 | return NO; |
103 | |
} |
104 | |
|
105 | |
public String getId() |
106 | |
{ |
107 | 0 | return "cache-failures"; |
108 | |
} |
109 | |
|
110 | |
public String getName() |
111 | |
{ |
112 | 0 | return "Cache failures"; |
113 | |
} |
114 | |
|
115 | |
public List<String> getOptions() |
116 | |
{ |
117 | 0 | return options; |
118 | |
} |
119 | |
} |