This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.policies;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *  http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.archiva.common.utils.VersionUtil;
023import org.apache.archiva.repository.storage.StorageAsset;
024import org.apache.commons.lang3.StringUtils;
025import org.slf4j.Logger;
026import org.slf4j.LoggerFactory;
027
028import java.util.ArrayList;
029import java.util.Calendar;
030import java.util.List;
031import java.util.Properties;
032
033/**
034 * AbstractUpdatePolicy
035 *
036 *
037 */
038public abstract class AbstractUpdatePolicy
039    extends AbstractPolicy implements PreDownloadPolicy
040{
041    private Logger log = LoggerFactory.getLogger( AbstractUpdatePolicy.class );
042
043    /**
044     * The ALWAYS policy setting means that the artifact is always updated from the remote repo.
045     */
046    public static final PolicyOption ALWAYS = UpdateOption.ALWAYS;
047
048    /**
049     * The NEVER policy setting means that the artifact is never updated from the remote repo.
050     */
051    public static final PolicyOption NEVER = UpdateOption.NEVER;
052
053    /**
054     * <p>
055     * The DAILY policy means that the artifact retrieval occurs only if one of
056     * the following conditions are met...
057     * </p>
058     * <ul>
059     * <li>The local artifact is not present.</li>
060     * <li>The local artifact has a last modified timestamp older than (now - 1 day).</li>
061     * </ul>
062     */
063    public static final UpdateOption DAILY = UpdateOption.DAILY;
064
065    /**
066     * <p>
067     * The HOURLY policy means that the artifact retrieval occurs only if one of
068     * the following conditions are met...
069     * </p>
070     * <ul>
071     * <li>The local artifact is not present.</li>
072     * <li>The local artifact has a last modified timestamp older than (now - 1 hour).</li>
073     * </ul>
074     */
075    public static final UpdateOption HOURLY = UpdateOption.HOURLY;
076
077    /**
078     * The ONCE policy means that the artifact retrieval occurs only if the
079     * local artifact is not present.  This means that the retrieval can only
080     * occur once.
081     */
082    public static final UpdateOption ONCE = UpdateOption.ONCE;
083
084    private List<PolicyOption> options = new ArrayList<>( 5 );
085
086    public AbstractUpdatePolicy()
087    {
088        super();
089        super.setOptionPrefix("update.option.");
090        options.add( ALWAYS );
091        options.add( HOURLY );
092        options.add( DAILY );
093        options.add( ONCE );
094        options.add( NEVER );
095    }
096
097    protected abstract boolean isSnapshotPolicy();
098
099    protected abstract String getUpdateMode();
100
101    @Override
102    public List<PolicyOption> getOptions()
103    {
104        return options;
105    }
106
107    @Override
108    public void applyPolicy( PolicyOption policySetting, Properties request, StorageAsset localFile )
109        throws PolicyViolationException, PolicyConfigurationException
110    {
111        if ( !StringUtils.equals( request.getProperty( "filetype" ), "artifact" ) )
112        {
113            // Only process artifact file types.
114            return;
115        }
116
117        String version = request.getProperty( "version", "" );
118        boolean isSnapshotVersion = false;
119
120        if ( StringUtils.isNotBlank( version ) )
121        {
122            isSnapshotVersion = VersionUtil.isSnapshot( version );
123        }
124
125        if ( !options.contains( policySetting ) )
126        {
127            // Not a valid code. 
128            throw new PolicyConfigurationException(
129                "Unknown " + getUpdateMode() + " policy setting [" + policySetting + "], valid settings are ["
130                    + StringUtils.join( options.iterator(), "," ) + "]" );
131        }
132
133        if ( ALWAYS.equals( policySetting ) )
134        {
135            // Skip means ok to update.
136            log.debug( "OK to update, {} policy set to ALWAYS.", getUpdateMode() );
137            return;
138        }
139
140        // Test for mismatches.
141        if ( !isSnapshotVersion && isSnapshotPolicy() )
142        {
143            log.debug( "OK to update, snapshot policy does not apply for non-snapshot versions." );
144            return;
145        }
146
147        if ( isSnapshotVersion && !isSnapshotPolicy() )
148        {
149            log.debug( "OK to update, release policy does not apply for snapshot versions." );
150            return;
151        }
152
153        if ( NEVER.equals( policySetting ) )
154        {
155            // Reject means no.
156            throw new PolicyViolationException( "NO to update, " + getUpdateMode() + " policy set to NEVER." );
157        }
158
159        if ( !localFile.exists() )
160        {
161            // No file means it's ok.
162            log.debug( "OK to update {}, local file does not exist.", getUpdateMode() );
163            return;
164        }
165
166        if ( ONCE.equals( policySetting ) )
167        {
168            // File exists, but policy is once.
169            throw new PolicyViolationException(
170                "NO to update " + getUpdateMode() + ", policy is ONCE, and local file exist." );
171        }
172
173        if ( 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() );
179
180            if ( cal.after( fileCal ) )
181            {
182                // Its ok.
183                return;
184            }
185            else
186            {
187                throw new PolicyViolationException( "NO to update " + getUpdateMode()
188                                                        + ", policy is DAILY, local file exist, and has been updated within the last day." );
189            }
190        }
191
192        if ( 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());
198
199            if ( cal.after( fileCal ) )
200            {
201                // Its ok.
202                return;
203            }
204            else
205            {
206                throw new PolicyViolationException( "NO to update " + getUpdateMode()
207                                                        + ", policy is HOURLY, local file exist, and has been updated within the last hour." );
208            }
209        }
210
211        throw new PolicyConfigurationException(
212            "Unable to process " + getUpdateMode() + " policy of [" + policySetting + "], please file a bug report." );
213    }
214}