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.Calendar; |
25 | |
import java.util.List; |
26 | |
import java.util.Properties; |
27 | |
|
28 | |
import org.apache.commons.lang.StringUtils; |
29 | |
import org.apache.maven.archiva.common.utils.VersionUtil; |
30 | |
import org.slf4j.Logger; |
31 | |
import org.slf4j.LoggerFactory; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public abstract class AbstractUpdatePolicy |
39 | |
implements PreDownloadPolicy |
40 | |
{ |
41 | 0 | private Logger log = LoggerFactory.getLogger( AbstractUpdatePolicy.class ); |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
public static final String ALWAYS = "always"; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public static final String NEVER = "never"; |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public static final String DAILY = "daily"; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
public static final String HOURLY = "hourly"; |
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public static final String ONCE = "once"; |
83 | |
|
84 | 0 | private List<String> options = new ArrayList<String>(); |
85 | |
|
86 | |
public AbstractUpdatePolicy() |
87 | 0 | { |
88 | 0 | options.add( ALWAYS ); |
89 | 0 | options.add( HOURLY ); |
90 | 0 | options.add( DAILY ); |
91 | 0 | options.add( ONCE ); |
92 | 0 | options.add( NEVER ); |
93 | 0 | } |
94 | |
|
95 | |
protected abstract boolean isSnapshotPolicy(); |
96 | |
|
97 | |
protected abstract String getUpdateMode(); |
98 | |
|
99 | |
public List<String> getOptions() |
100 | |
{ |
101 | 0 | return options; |
102 | |
} |
103 | |
|
104 | |
public void applyPolicy( String policySetting, Properties request, File localFile ) |
105 | |
throws PolicyViolationException, PolicyConfigurationException |
106 | |
{ |
107 | 0 | if ( !StringUtils.equals( request.getProperty( "filetype" ), "artifact" ) ) |
108 | |
{ |
109 | |
|
110 | 0 | return; |
111 | |
} |
112 | |
|
113 | 0 | String version = request.getProperty( "version", "" ); |
114 | 0 | boolean isSnapshotVersion = false; |
115 | |
|
116 | 0 | if ( StringUtils.isNotBlank( version ) ) |
117 | |
{ |
118 | 0 | isSnapshotVersion = VersionUtil.isSnapshot( version ); |
119 | |
} |
120 | |
|
121 | 0 | if ( !options.contains( policySetting ) ) |
122 | |
{ |
123 | |
|
124 | 0 | throw new PolicyConfigurationException( "Unknown " + getUpdateMode() + " policy setting [" + policySetting |
125 | |
+ "], valid settings are [" + StringUtils.join( options.iterator(), "," ) + "]" ); |
126 | |
} |
127 | |
|
128 | 0 | if ( ALWAYS.equals( policySetting ) ) |
129 | |
{ |
130 | |
|
131 | 0 | log.debug( "OK to update, " + getUpdateMode() + " policy set to ALWAYS." ); |
132 | 0 | return; |
133 | |
} |
134 | |
|
135 | |
|
136 | 0 | if ( !isSnapshotVersion && isSnapshotPolicy() ) |
137 | |
{ |
138 | 0 | log.debug( "OK to update, snapshot policy does not apply for non-snapshot versions." ); |
139 | 0 | return; |
140 | |
} |
141 | |
|
142 | 0 | if ( isSnapshotVersion && !isSnapshotPolicy() ) |
143 | |
{ |
144 | 0 | log.debug( "OK to update, release policy does not apply for snapshot versions." ); |
145 | 0 | return; |
146 | |
} |
147 | |
|
148 | 0 | if ( NEVER.equals( policySetting ) ) |
149 | |
{ |
150 | |
|
151 | 0 | throw new PolicyViolationException( "NO to update, " + getUpdateMode() + " policy set to NEVER." ); |
152 | |
} |
153 | |
|
154 | 0 | if ( !localFile.exists() ) |
155 | |
{ |
156 | |
|
157 | 0 | log.debug( "OK to update " + getUpdateMode() + ", local file does not exist." ); |
158 | 0 | return; |
159 | |
} |
160 | |
|
161 | 0 | if ( ONCE.equals( policySetting ) ) |
162 | |
{ |
163 | |
|
164 | 0 | throw new PolicyViolationException( "NO to update " + getUpdateMode() + ", policy is ONCE, and local file exist." ); |
165 | |
} |
166 | |
|
167 | 0 | if ( DAILY.equals( policySetting ) ) |
168 | |
{ |
169 | 0 | Calendar cal = Calendar.getInstance(); |
170 | 0 | cal.add( Calendar.DAY_OF_MONTH, -1 ); |
171 | 0 | Calendar fileCal = Calendar.getInstance(); |
172 | 0 | fileCal.setTimeInMillis( localFile.lastModified() ); |
173 | |
|
174 | 0 | if( cal.after( fileCal ) ) |
175 | |
{ |
176 | |
|
177 | 0 | return; |
178 | |
} |
179 | |
else |
180 | |
{ |
181 | 0 | throw new PolicyViolationException( "NO to update " + getUpdateMode() |
182 | |
+ ", policy is DAILY, local file exist, and has been updated within the last day." ); |
183 | |
} |
184 | |
} |
185 | |
|
186 | 0 | if ( HOURLY.equals( policySetting ) ) |
187 | |
{ |
188 | 0 | Calendar cal = Calendar.getInstance(); |
189 | 0 | cal.add( Calendar.HOUR, -1 ); |
190 | 0 | Calendar fileCal = Calendar.getInstance(); |
191 | 0 | fileCal.setTimeInMillis( localFile.lastModified() ); |
192 | |
|
193 | 0 | if( cal.after( fileCal ) ) |
194 | |
{ |
195 | |
|
196 | 0 | return; |
197 | |
} |
198 | |
else |
199 | |
{ |
200 | 0 | throw new PolicyViolationException( "NO to update " + getUpdateMode() |
201 | |
+ ", policy is HOURLY, local file exist, and has been updated within the last hour." ); |
202 | |
} |
203 | |
} |
204 | |
|
205 | 0 | throw new PolicyConfigurationException( "Unable to process " + getUpdateMode() |
206 | |
+ " policy of [" + policySetting + "], please file a bug report." ); |
207 | |
} |
208 | |
} |