1package org.apache.archiva.policies;
23/*4 * Licensed to the Apache Software Foundation (ASF) under one5 * or more contributor license agreements. See the NOTICE file6 * distributed with this work for additional information7 * regarding copyright ownership. The ASF licenses this file8 * to you under the Apache License, Version 2.0 (the9 * "License"); you may not use this file except in compliance10 * with the License. You may obtain a copy of the License at11 *12 * http://www.apache.org/licenses/LICENSE-2.013 *14 * Unless required by applicable law or agreed to in writing,15 * software distributed under the License is distributed on an16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY17 * KIND, either express or implied. See the License for the18 * specific language governing permissions and limitations19 * under the License.20 */2122import org.apache.archiva.common.utils.VersionUtil;
23import org.apache.commons.lang.StringUtils;
24import org.slf4j.Logger;
25import org.slf4j.LoggerFactory;
2627import java.io.File;
28import java.util.ArrayList;
29import java.util.Calendar;
30import java.util.List;
31import java.util.Properties;
3233/**34 * AbstractUpdatePolicy35 *36 *37 */38publicabstractclassAbstractUpdatePolicy39implementsPreDownloadPolicy40 {
41private Logger log = LoggerFactory.getLogger( AbstractUpdatePolicy.class );
4243/**44 * The ALWAYS policy setting means that the artifact is always uipdated from the remote repo.45 */46publicstaticfinal String ALWAYS = "always";
4748/**49 * The NEVER policy setting means that the artifact is never updated from the remote repo.50 */51publicstaticfinal String NEVER = "never";
5253/**54 * <p>55 * The DAILY policy means that the artifact retrieval occurs only if one of56 * the following conditions are met...57 * </p>58 * <ul>59 * <li>The local artifact is not present.</li>60 * <li>The local artifact has a last modified timestamp older than (now - 1 day).</li>61 * </ul>62 */63publicstaticfinal String DAILY = "daily";
6465/**66 * <p>67 * The HOURLY policy means that the artifact retrieval occurs only if one of68 * the following conditions are met...69 * </p>70 * <ul>71 * <li>The local artifact is not present.</li>72 * <li>The local artifact has a last modified timestamp older than (now - 1 hour).</li>73 * </ul>74 */75publicstaticfinal String HOURLY = "hourly";
7677/**78 * The ONCE policy means that the artifact retrieval occurs only if the79 * local artifact is not present. This means that the retreival can only80 * occur once.81 */82publicstaticfinal String ONCE = "once";
8384private List<String> options = new ArrayList<>( 5 );
8586publicAbstractUpdatePolicy()
87 {
88 options.add( ALWAYS );
89 options.add( HOURLY );
90 options.add( DAILY );
91 options.add( ONCE );
92 options.add( NEVER );
93 }
9495protectedabstractboolean isSnapshotPolicy();
9697protectedabstract String getUpdateMode();
9899 @Override
100public List<String> getOptions()
101 {
102return options;
103 }
104105 @Override
106publicvoid applyPolicy( String policySetting, Properties request, File localFile )
107throws PolicyViolationException, PolicyConfigurationException108 {
109if ( !StringUtils.equals( request.getProperty( "filetype" ), "artifact" ) )
110 {
111// Only process artifact file types.112return;
113 }
114115 String version = request.getProperty( "version", "" );
116boolean isSnapshotVersion = false;
117118if ( StringUtils.isNotBlank( version ) )
119 {
120 isSnapshotVersion = VersionUtil.isSnapshot( version );
121 }
122123if ( !options.contains( policySetting ) )
124 {
125// Not a valid code. 126thrownewPolicyConfigurationException(
127"Unknown " + getUpdateMode() + " policy setting [" + policySetting + "], valid settings are ["128 + StringUtils.join( options.iterator(), "," ) + "]" );
129 }
130131if ( ALWAYS.equals( policySetting ) )
132 {
133// Skip means ok to update.134 log.debug( "OK to update, {} policy set to ALWAYS.", getUpdateMode() );
135return;
136 }
137138// Test for mismatches.139if ( !isSnapshotVersion && isSnapshotPolicy() )
140 {
141 log.debug( "OK to update, snapshot policy does not apply for non-snapshot versions." );
142return;
143 }
144145if ( isSnapshotVersion && !isSnapshotPolicy() )
146 {
147 log.debug( "OK to update, release policy does not apply for snapshot versions." );
148return;
149 }
150151if ( NEVER.equals( policySetting ) )
152 {
153// Reject means no.154thrownewPolicyViolationException( "NO to update, " + getUpdateMode() + " policy set to NEVER." );
155 }
156157if ( !localFile.exists() )
158 {
159// No file means it's ok.160 log.debug( "OK to update {}, local file does not exist.", getUpdateMode() );
161return;
162 }
163164if ( ONCE.equals( policySetting ) )
165 {
166// File exists, but policy is once.167thrownewPolicyViolationException(
168"NO to update " + getUpdateMode() + ", policy is ONCE, and local file exist." );
169 }
170171if ( DAILY.equals( policySetting ) )
172 {
173 Calendar cal = Calendar.getInstance();
174 cal.add( Calendar.DAY_OF_MONTH, -1 );
175 Calendar fileCal = Calendar.getInstance();
176 fileCal.setTimeInMillis( localFile.lastModified() );
177178if ( cal.after( fileCal ) )
179 {
180// Its ok.181return;
182 }
183else184 {
185thrownewPolicyViolationException( "NO to update " + getUpdateMode()
186 + ", policy is DAILY, local file exist, and has been updated within the last day." );
187 }
188 }
189190if ( HOURLY.equals( policySetting ) )
191 {
192 Calendar cal = Calendar.getInstance();
193 cal.add( Calendar.HOUR, -1 );
194 Calendar fileCal = Calendar.getInstance();
195 fileCal.setTimeInMillis( localFile.lastModified() );
196197if ( cal.after( fileCal ) )
198 {
199// Its ok.200return;
201 }
202else203 {
204thrownewPolicyViolationException( "NO to update " + getUpdateMode()
205 + ", policy is HOURLY, local file exist, and has been updated within the last hour." );
206 }
207 }
208209thrownewPolicyConfigurationException(
210"Unable to process " + getUpdateMode() + " policy of [" + policySetting + "], please file a bug report." );
211 }
212 }