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.archiva.repository.storage.StorageAsset;
24import org.apache.commons.lang3.StringUtils;
25import org.slf4j.Logger;
26import org.slf4j.LoggerFactory;
2728import java.util.ArrayList;
29import java.util.Calendar;
30import java.util.List;
31import java.util.Properties;
3233/**34 * AbstractUpdatePolicy35 *36 *37 */38publicabstractclassAbstractUpdatePolicy39extendsAbstractPolicyimplementsPreDownloadPolicy40 {
41private Logger log = LoggerFactory.getLogger( AbstractUpdatePolicy.class );
4243/**44 * The ALWAYS policy setting means that the artifact is always updated from the remote repo.45 */46publicstaticfinalPolicyOption ALWAYS = UpdateOption.ALWAYS;
4748/**49 * The NEVER policy setting means that the artifact is never updated from the remote repo.50 */51publicstaticfinalPolicyOption NEVER = UpdateOption.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 */63publicstaticfinalUpdateOption DAILY = UpdateOption.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 */75publicstaticfinalUpdateOption HOURLY = UpdateOption.HOURLY;
7677/**78 * The ONCE policy means that the artifact retrieval occurs only if the79 * local artifact is not present. This means that the retrieval can only80 * occur once.81 */82publicstaticfinalUpdateOption ONCE = UpdateOption.ONCE;
8384private List<PolicyOption> options = new ArrayList<>( 5 );
8586publicAbstractUpdatePolicy()
87 {
88super();
89super.setOptionPrefix("update.option.");
90 options.add( ALWAYS );
91 options.add( HOURLY );
92 options.add( DAILY );
93 options.add( ONCE );
94 options.add( NEVER );
95 }
9697protectedabstractboolean isSnapshotPolicy();
9899protectedabstract String getUpdateMode();
100101 @Override
102public List<PolicyOption> getOptions()
103 {
104return options;
105 }
106107 @Override
108publicvoid applyPolicy( PolicyOption policySetting, Properties request, StorageAsset localFile )
109throws PolicyViolationException, PolicyConfigurationException110 {
111if ( !StringUtils.equals( request.getProperty( "filetype" ), "artifact" ) )
112 {
113// Only process artifact file types.114return;
115 }
116117 String version = request.getProperty( "version", "" );
118boolean isSnapshotVersion = false;
119120if ( StringUtils.isNotBlank( version ) )
121 {
122 isSnapshotVersion = VersionUtil.isSnapshot( version );
123 }
124125if ( !options.contains( policySetting ) )
126 {
127// Not a valid code. 128thrownewPolicyConfigurationException(
129"Unknown " + getUpdateMode() + " policy setting [" + policySetting + "], valid settings are ["130 + StringUtils.join( options.iterator(), "," ) + "]" );
131 }
132133if ( ALWAYS.equals( policySetting ) )
134 {
135// Skip means ok to update.136 log.debug( "OK to update, {} policy set to ALWAYS.", getUpdateMode() );
137return;
138 }
139140// Test for mismatches.141if ( !isSnapshotVersion && isSnapshotPolicy() )
142 {
143 log.debug( "OK to update, snapshot policy does not apply for non-snapshot versions." );
144return;
145 }
146147if ( isSnapshotVersion && !isSnapshotPolicy() )
148 {
149 log.debug( "OK to update, release policy does not apply for snapshot versions." );
150return;
151 }
152153if ( NEVER.equals( policySetting ) )
154 {
155// Reject means no.156thrownewPolicyViolationException( "NO to update, " + getUpdateMode() + " policy set to NEVER." );
157 }
158159if ( !localFile.exists() )
160 {
161// No file means it's ok.162 log.debug( "OK to update {}, local file does not exist.", getUpdateMode() );
163return;
164 }
165166if ( ONCE.equals( policySetting ) )
167 {
168// File exists, but policy is once.169thrownewPolicyViolationException(
170"NO to update " + getUpdateMode() + ", policy is ONCE, and local file exist." );
171 }
172173if ( DAILY.equals( policySetting ) )
174 {
175 Calendar cal = Calendar.getInstance();
176 cal.add( Calendar.DAY_OF_MONTH, -1 );
177 Calendar fileCal = Calendar.getInstance();
178 fileCal.setTimeInMillis( localFile.getModificationTime().toEpochMilli() );
179180if ( cal.after( fileCal ) )
181 {
182// Its ok.183return;
184 }
185else186 {
187thrownewPolicyViolationException( "NO to update " + getUpdateMode()
188 + ", policy is DAILY, local file exist, and has been updated within the last day." );
189 }
190 }
191192if ( HOURLY.equals( policySetting ) )
193 {
194 Calendar cal = Calendar.getInstance();
195 cal.add( Calendar.HOUR, -1 );
196 Calendar fileCal = Calendar.getInstance();
197 fileCal.setTimeInMillis( localFile.getModificationTime().toEpochMilli());
198199if ( cal.after( fileCal ) )
200 {
201// Its ok.202return;
203 }
204else205 {
206thrownewPolicyViolationException( "NO to update " + getUpdateMode()
207 + ", policy is HOURLY, local file exist, and has been updated within the last hour." );
208 }
209 }
210211thrownewPolicyConfigurationException(
212"Unable to process " + getUpdateMode() + " policy of [" + policySetting + "], please file a bug report." );
213 }
214 }