1 package org.apache.archiva.policies;
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17 * KIND, either express or implied. See the License for the
18 * specific language governing permissions and limitations
22 import org.apache.archiva.common.utils.VersionUtil;
23 import org.apache.archiva.repository.content.StorageAsset;
24 import org.apache.commons.lang.StringUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import java.util.ArrayList;
31 import java.util.Calendar;
32 import java.util.Date;
33 import java.util.List;
34 import java.util.Properties;
37 * AbstractUpdatePolicy
41 public abstract class AbstractUpdatePolicy
42 implements PreDownloadPolicy
44 private Logger log = LoggerFactory.getLogger( AbstractUpdatePolicy.class );
47 * The ALWAYS policy setting means that the artifact is always uipdated from the remote repo.
49 public static final String ALWAYS = "always";
52 * The NEVER policy setting means that the artifact is never updated from the remote repo.
54 public static final String NEVER = "never";
58 * The DAILY policy means that the artifact retrieval occurs only if one of
59 * the following conditions are met...
62 * <li>The local artifact is not present.</li>
63 * <li>The local artifact has a last modified timestamp older than (now - 1 day).</li>
66 public static final String DAILY = "daily";
70 * The HOURLY policy means that the artifact retrieval occurs only if one of
71 * the following conditions are met...
74 * <li>The local artifact is not present.</li>
75 * <li>The local artifact has a last modified timestamp older than (now - 1 hour).</li>
78 public static final String HOURLY = "hourly";
81 * The ONCE policy means that the artifact retrieval occurs only if the
82 * local artifact is not present. This means that the retreival can only
85 public static final String ONCE = "once";
87 private List<String> options = new ArrayList<>( 5 );
89 public AbstractUpdatePolicy()
91 options.add( ALWAYS );
92 options.add( HOURLY );
98 protected abstract boolean isSnapshotPolicy();
100 protected abstract String getUpdateMode();
103 public List<String> getOptions()
109 public void applyPolicy( String policySetting, Properties request, StorageAsset localFile )
110 throws PolicyViolationException, PolicyConfigurationException
112 if ( !StringUtils.equals( request.getProperty( "filetype" ), "artifact" ) )
114 // Only process artifact file types.
118 String version = request.getProperty( "version", "" );
119 boolean isSnapshotVersion = false;
121 if ( StringUtils.isNotBlank( version ) )
123 isSnapshotVersion = VersionUtil.isSnapshot( version );
126 if ( !options.contains( policySetting ) )
129 throw new PolicyConfigurationException(
130 "Unknown " + getUpdateMode() + " policy setting [" + policySetting + "], valid settings are ["
131 + StringUtils.join( options.iterator(), "," ) + "]" );
134 if ( ALWAYS.equals( policySetting ) )
136 // Skip means ok to update.
137 log.debug( "OK to update, {} policy set to ALWAYS.", getUpdateMode() );
141 // Test for mismatches.
142 if ( !isSnapshotVersion && isSnapshotPolicy() )
144 log.debug( "OK to update, snapshot policy does not apply for non-snapshot versions." );
148 if ( isSnapshotVersion && !isSnapshotPolicy() )
150 log.debug( "OK to update, release policy does not apply for snapshot versions." );
154 if ( NEVER.equals( policySetting ) )
157 throw new PolicyViolationException( "NO to update, " + getUpdateMode() + " policy set to NEVER." );
160 if ( !localFile.exists() )
162 // No file means it's ok.
163 log.debug( "OK to update {}, local file does not exist.", getUpdateMode() );
167 if ( ONCE.equals( policySetting ) )
169 // File exists, but policy is once.
170 throw new PolicyViolationException(
171 "NO to update " + getUpdateMode() + ", policy is ONCE, and local file exist." );
174 if ( DAILY.equals( policySetting ) )
176 Calendar cal = Calendar.getInstance();
177 cal.add( Calendar.DAY_OF_MONTH, -1 );
178 Calendar fileCal = Calendar.getInstance();
179 fileCal.setTimeInMillis( localFile.getModificationTime().toEpochMilli() );
181 if ( cal.after( fileCal ) )
188 throw new PolicyViolationException( "NO to update " + getUpdateMode()
189 + ", policy is DAILY, local file exist, and has been updated within the last day." );
193 if ( HOURLY.equals( policySetting ) )
195 Calendar cal = Calendar.getInstance();
196 cal.add( Calendar.HOUR, -1 );
197 Calendar fileCal = Calendar.getInstance();
198 fileCal.setTimeInMillis( localFile.getModificationTime().toEpochMilli());
200 if ( cal.after( fileCal ) )
207 throw new PolicyViolationException( "NO to update " + getUpdateMode()
208 + ", policy is HOURLY, local file exist, and has been updated within the last hour." );
212 throw new PolicyConfigurationException(
213 "Unable to process " + getUpdateMode() + " policy of [" + policySetting + "], please file a bug report." );