1 package org.apache.maven.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.commons.lang.StringUtils;
23 import org.apache.maven.archiva.common.utils.VersionUtil;
24 import org.codehaus.plexus.logging.AbstractLogEnabled;
27 import java.util.ArrayList;
28 import java.util.Calendar;
29 import java.util.List;
30 import java.util.Properties;
33 * AbstractUpdatePolicy
35 * @author <a href="mailto:joakime@apache.org">Joakim Erdfelt</a>
38 public abstract class AbstractUpdatePolicy
39 extends AbstractLogEnabled
40 implements PreDownloadPolicy
43 * The DISABLED policy means that the artifact retrieval isn't even attempted,
44 * let alone updated locally.
46 public static final String DISABLED = "disabled";
50 * The DAILY policy means that the artifact retrieval occurs only if one of
51 * the following conditions are met...
54 * <li>The local artifact is not present.</li>
55 * <li>The local artifact has a last modified timestamp older than (now - 1 day).</li>
58 public static final String DAILY = "daily";
62 * The HOURLY policy means that the artifact retrieval occurs only if one of
63 * the following conditions are met...
66 * <li>The local artifact is not present.</li>
67 * <li>The local artifact has a last modified timestamp older than (now - 1 hour).</li>
70 public static final String HOURLY = "hourly";
73 * The ONCE policy means that the artifact retrieval occurs only if the
74 * local artifact is not present. This means that the retreival can only
77 public static final String ONCE = "once";
79 private List options = new ArrayList();
81 public AbstractUpdatePolicy()
83 options.add( IGNORED );
84 options.add( DISABLED );
86 options.add( HOURLY );
90 protected abstract boolean isSnapshotPolicy();
92 protected abstract String getUpdateMode();
94 public List getOptions()
99 public boolean applyPolicy( String policySetting, Properties request, File localFile )
101 String version = request.getProperty( "version", "" );
102 boolean isSnapshotVersion = false;
104 if ( StringUtils.isNotBlank( version ) )
106 isSnapshotVersion = VersionUtil.isSnapshot( version );
109 if ( !options.contains( policySetting ) )
111 // No valid code? false it is then.
112 getLogger().error( "Unknown artifact-update policyCode [" + policySetting + "]" );
116 if ( IGNORED.equals( policySetting ) )
118 // Ignored means ok to update.
119 getLogger().debug( "OK to update, " + getUpdateMode() + " policy set to IGNORED." );
123 // Test for mismatches.
124 if ( !isSnapshotVersion && isSnapshotPolicy() )
126 getLogger().debug( "OK to update, snapshot policy does not apply for non-snapshot versions." );
130 if ( isSnapshotVersion && !isSnapshotPolicy() )
132 getLogger().debug( "OK to update, release policy does not apply for snapshot versions." );
136 if ( DISABLED.equals( policySetting ) )
138 // Disabled means no.
139 getLogger().debug( "NO to update, " + getUpdateMode() + " policy set to DISABLED." );
143 if ( !localFile.exists() )
145 // No file means it's ok.
146 getLogger().debug( "OK to update " + getUpdateMode() + ", local file does not exist." );
150 if ( ONCE.equals( policySetting ) )
152 // File exists, but policy is once.
153 getLogger().debug( "NO to update" + getUpdateMode() + ", local file exist (and policy is ONCE)." );
157 if ( DAILY.equals( policySetting ) )
159 Calendar cal = Calendar.getInstance();
160 cal.add( Calendar.DAY_OF_MONTH, -1 );
161 Calendar fileCal = Calendar.getInstance();
162 fileCal.setTimeInMillis( localFile.lastModified() );
164 return cal.after( fileCal );
167 if ( HOURLY.equals( policySetting ) )
169 Calendar cal = Calendar.getInstance();
170 cal.add( Calendar.HOUR, -1 );
171 Calendar fileCal = Calendar.getInstance();
172 fileCal.setTimeInMillis( localFile.lastModified() );
174 return cal.after( fileCal );
177 getLogger().error( "Unhandled policyCode [" + policySetting + "]" );