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.commons.lang.StringUtils;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
28 import java.util.ArrayList;
29 import java.util.Calendar;
30 import java.util.List;
31 import java.util.Properties;
34 * AbstractUpdatePolicy
38 public abstract class AbstractUpdatePolicy
39 implements PreDownloadPolicy
41 private Logger log = LoggerFactory.getLogger( AbstractUpdatePolicy.class );
44 * The ALWAYS policy setting means that the artifact is always uipdated from the remote repo.
46 public static final String ALWAYS = "always";
49 * The NEVER policy setting means that the artifact is never updated from the remote repo.
51 public static final String NEVER = "never";
55 * The DAILY policy means that the artifact retrieval occurs only if one of
56 * the following conditions are met...
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>
63 public static final String DAILY = "daily";
67 * The HOURLY policy means that the artifact retrieval occurs only if one of
68 * the following conditions are met...
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>
75 public static final String HOURLY = "hourly";
78 * The ONCE policy means that the artifact retrieval occurs only if the
79 * local artifact is not present. This means that the retreival can only
82 public static final String ONCE = "once";
84 private List<String> options = new ArrayList<String>( 5 );
86 public AbstractUpdatePolicy()
88 options.add( ALWAYS );
89 options.add( HOURLY );
95 protected abstract boolean isSnapshotPolicy();
97 protected abstract String getUpdateMode();
99 public List<String> getOptions()
104 public void applyPolicy( String policySetting, Properties request, File localFile )
105 throws PolicyViolationException, PolicyConfigurationException
107 if ( !StringUtils.equals( request.getProperty( "filetype" ), "artifact" ) )
109 // Only process artifact file types.
113 String version = request.getProperty( "version", "" );
114 boolean isSnapshotVersion = false;
116 if ( StringUtils.isNotBlank( version ) )
118 isSnapshotVersion = VersionUtil.isSnapshot( version );
121 if ( !options.contains( policySetting ) )
124 throw new PolicyConfigurationException(
125 "Unknown " + getUpdateMode() + " policy setting [" + policySetting + "], valid settings are ["
126 + StringUtils.join( options.iterator(), "," ) + "]" );
129 if ( ALWAYS.equals( policySetting ) )
131 // Skip means ok to update.
132 log.debug( "OK to update, {} policy set to ALWAYS.", getUpdateMode() );
136 // Test for mismatches.
137 if ( !isSnapshotVersion && isSnapshotPolicy() )
139 log.debug( "OK to update, snapshot policy does not apply for non-snapshot versions." );
143 if ( isSnapshotVersion && !isSnapshotPolicy() )
145 log.debug( "OK to update, release policy does not apply for snapshot versions." );
149 if ( NEVER.equals( policySetting ) )
152 throw new PolicyViolationException( "NO to update, " + getUpdateMode() + " policy set to NEVER." );
155 if ( !localFile.exists() )
157 // No file means it's ok.
158 log.debug( "OK to update {}, local file does not exist.", getUpdateMode() );
162 if ( ONCE.equals( policySetting ) )
164 // File exists, but policy is once.
165 throw new PolicyViolationException(
166 "NO to update " + getUpdateMode() + ", policy is ONCE, and local file exist." );
169 if ( DAILY.equals( policySetting ) )
171 Calendar cal = Calendar.getInstance();
172 cal.add( Calendar.DAY_OF_MONTH, -1 );
173 Calendar fileCal = Calendar.getInstance();
174 fileCal.setTimeInMillis( localFile.lastModified() );
176 if ( cal.after( fileCal ) )
183 throw new PolicyViolationException( "NO to update " + getUpdateMode()
184 + ", policy is DAILY, local file exist, and has been updated within the last day." );
188 if ( HOURLY.equals( policySetting ) )
190 Calendar cal = Calendar.getInstance();
191 cal.add( Calendar.HOUR, -1 );
192 Calendar fileCal = Calendar.getInstance();
193 fileCal.setTimeInMillis( localFile.lastModified() );
195 if ( cal.after( fileCal ) )
202 throw new PolicyViolationException( "NO to update " + getUpdateMode()
203 + ", policy is HOURLY, local file exist, and has been updated within the last hour." );
207 throw new PolicyConfigurationException(
208 "Unable to process " + getUpdateMode() + " policy of [" + policySetting + "], please file a bug report." );