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