123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212 |
- package org.apache.archiva.policies;
-
- /*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
- import org.apache.archiva.common.utils.VersionUtil;
- import org.apache.archiva.repository.storage.StorageAsset;
- import org.apache.commons.lang.StringUtils;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.List;
- import java.util.Properties;
-
- /**
- * AbstractUpdatePolicy
- *
- *
- */
- public abstract class AbstractUpdatePolicy
- implements PreDownloadPolicy
- {
- private Logger log = LoggerFactory.getLogger( AbstractUpdatePolicy.class );
-
- /**
- * The ALWAYS policy setting means that the artifact is always uipdated from the remote repo.
- */
- public static final String ALWAYS = "always";
-
- /**
- * The NEVER policy setting means that the artifact is never updated from the remote repo.
- */
- public static final String NEVER = "never";
-
- /**
- * <p>
- * The DAILY policy means that the artifact retrieval occurs only if one of
- * the following conditions are met...
- * </p>
- * <ul>
- * <li>The local artifact is not present.</li>
- * <li>The local artifact has a last modified timestamp older than (now - 1 day).</li>
- * </ul>
- */
- public static final String DAILY = "daily";
-
- /**
- * <p>
- * The HOURLY policy means that the artifact retrieval occurs only if one of
- * the following conditions are met...
- * </p>
- * <ul>
- * <li>The local artifact is not present.</li>
- * <li>The local artifact has a last modified timestamp older than (now - 1 hour).</li>
- * </ul>
- */
- public static final String HOURLY = "hourly";
-
- /**
- * The ONCE policy means that the artifact retrieval occurs only if the
- * local artifact is not present. This means that the retreival can only
- * occur once.
- */
- public static final String ONCE = "once";
-
- private List<String> options = new ArrayList<>( 5 );
-
- public AbstractUpdatePolicy()
- {
- options.add( ALWAYS );
- options.add( HOURLY );
- options.add( DAILY );
- options.add( ONCE );
- options.add( NEVER );
- }
-
- protected abstract boolean isSnapshotPolicy();
-
- protected abstract String getUpdateMode();
-
- @Override
- public List<String> getOptions()
- {
- return options;
- }
-
- @Override
- public void applyPolicy( String policySetting, Properties request, StorageAsset localFile )
- throws PolicyViolationException, PolicyConfigurationException
- {
- if ( !StringUtils.equals( request.getProperty( "filetype" ), "artifact" ) )
- {
- // Only process artifact file types.
- return;
- }
-
- String version = request.getProperty( "version", "" );
- boolean isSnapshotVersion = false;
-
- if ( StringUtils.isNotBlank( version ) )
- {
- isSnapshotVersion = VersionUtil.isSnapshot( version );
- }
-
- if ( !options.contains( policySetting ) )
- {
- // Not a valid code.
- throw new PolicyConfigurationException(
- "Unknown " + getUpdateMode() + " policy setting [" + policySetting + "], valid settings are ["
- + StringUtils.join( options.iterator(), "," ) + "]" );
- }
-
- if ( ALWAYS.equals( policySetting ) )
- {
- // Skip means ok to update.
- log.debug( "OK to update, {} policy set to ALWAYS.", getUpdateMode() );
- return;
- }
-
- // Test for mismatches.
- if ( !isSnapshotVersion && isSnapshotPolicy() )
- {
- log.debug( "OK to update, snapshot policy does not apply for non-snapshot versions." );
- return;
- }
-
- if ( isSnapshotVersion && !isSnapshotPolicy() )
- {
- log.debug( "OK to update, release policy does not apply for snapshot versions." );
- return;
- }
-
- if ( NEVER.equals( policySetting ) )
- {
- // Reject means no.
- throw new PolicyViolationException( "NO to update, " + getUpdateMode() + " policy set to NEVER." );
- }
-
- if ( !localFile.exists() )
- {
- // No file means it's ok.
- log.debug( "OK to update {}, local file does not exist.", getUpdateMode() );
- return;
- }
-
- if ( ONCE.equals( policySetting ) )
- {
- // File exists, but policy is once.
- throw new PolicyViolationException(
- "NO to update " + getUpdateMode() + ", policy is ONCE, and local file exist." );
- }
-
- if ( DAILY.equals( policySetting ) )
- {
- Calendar cal = Calendar.getInstance();
- cal.add( Calendar.DAY_OF_MONTH, -1 );
- Calendar fileCal = Calendar.getInstance();
- fileCal.setTimeInMillis( localFile.getModificationTime().toEpochMilli() );
-
- if ( cal.after( fileCal ) )
- {
- // Its ok.
- return;
- }
- else
- {
- throw new PolicyViolationException( "NO to update " + getUpdateMode()
- + ", policy is DAILY, local file exist, and has been updated within the last day." );
- }
- }
-
- if ( HOURLY.equals( policySetting ) )
- {
- Calendar cal = Calendar.getInstance();
- cal.add( Calendar.HOUR, -1 );
- Calendar fileCal = Calendar.getInstance();
- fileCal.setTimeInMillis( localFile.getModificationTime().toEpochMilli());
-
- if ( cal.after( fileCal ) )
- {
- // Its ok.
- return;
- }
- else
- {
- throw new PolicyViolationException( "NO to update " + getUpdateMode()
- + ", policy is HOURLY, local file exist, and has been updated within the last hour." );
- }
- }
-
- throw new PolicyConfigurationException(
- "Unable to process " + getUpdateMode() + " policy of [" + policySetting + "], please file a bug report." );
- }
- }
|