This project has retired. For details please refer to its Attic page.
AbstractUpdatePolicy xref
View Javadoc
1   package org.apache.archiva.policies;
2   
3   /*
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
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
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
19   * under the License.
20   */
21  
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;
26  
27  import java.io.File;
28  import java.util.ArrayList;
29  import java.util.Calendar;
30  import java.util.List;
31  import java.util.Properties;
32  
33  /**
34   * AbstractUpdatePolicy
35   *
36   *
37   */
38  public abstract class AbstractUpdatePolicy
39      implements PreDownloadPolicy
40  {
41      private Logger log = LoggerFactory.getLogger( AbstractUpdatePolicy.class );
42  
43      /**
44       * The ALWAYS policy setting means that the artifact is always uipdated from the remote repo.
45       */
46      public static final String ALWAYS = "always";
47  
48      /**
49       * The NEVER policy setting means that the artifact is never updated from the remote repo.
50       */
51      public static final String NEVER = "never";
52  
53      /**
54       * <p>
55       * The DAILY policy means that the artifact retrieval occurs only if one of
56       * the following conditions are met...
57       * </p>
58       * <ul>
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>
61       * </ul>
62       */
63      public static final String DAILY = "daily";
64  
65      /**
66       * <p>
67       * The HOURLY policy means that the artifact retrieval occurs only if one of
68       * the following conditions are met...
69       * </p>
70       * <ul>
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>
73       * </ul>
74       */
75      public static final String HOURLY = "hourly";
76  
77      /**
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
80       * occur once.
81       */
82      public static final String ONCE = "once";
83  
84      private List<String> options = new ArrayList<>( 5 );
85  
86      public AbstractUpdatePolicy()
87      {
88          options.add( ALWAYS );
89          options.add( HOURLY );
90          options.add( DAILY );
91          options.add( ONCE );
92          options.add( NEVER );
93      }
94  
95      protected abstract boolean isSnapshotPolicy();
96  
97      protected abstract String getUpdateMode();
98  
99      @Override
100     public List<String> getOptions()
101     {
102         return options;
103     }
104 
105     @Override
106     public void applyPolicy( String policySetting, Properties request, File localFile )
107         throws PolicyViolationException, PolicyConfigurationException
108     {
109         if ( !StringUtils.equals( request.getProperty( "filetype" ), "artifact" ) )
110         {
111             // Only process artifact file types.
112             return;
113         }
114 
115         String version = request.getProperty( "version", "" );
116         boolean isSnapshotVersion = false;
117 
118         if ( StringUtils.isNotBlank( version ) )
119         {
120             isSnapshotVersion = VersionUtil.isSnapshot( version );
121         }
122 
123         if ( !options.contains( policySetting ) )
124         {
125             // Not a valid code. 
126             throw new PolicyConfigurationException(
127                 "Unknown " + getUpdateMode() + " policy setting [" + policySetting + "], valid settings are ["
128                     + StringUtils.join( options.iterator(), "," ) + "]" );
129         }
130 
131         if ( ALWAYS.equals( policySetting ) )
132         {
133             // Skip means ok to update.
134             log.debug( "OK to update, {} policy set to ALWAYS.", getUpdateMode() );
135             return;
136         }
137 
138         // Test for mismatches.
139         if ( !isSnapshotVersion && isSnapshotPolicy() )
140         {
141             log.debug( "OK to update, snapshot policy does not apply for non-snapshot versions." );
142             return;
143         }
144 
145         if ( isSnapshotVersion && !isSnapshotPolicy() )
146         {
147             log.debug( "OK to update, release policy does not apply for snapshot versions." );
148             return;
149         }
150 
151         if ( NEVER.equals( policySetting ) )
152         {
153             // Reject means no.
154             throw new PolicyViolationException( "NO to update, " + getUpdateMode() + " policy set to NEVER." );
155         }
156 
157         if ( !localFile.exists() )
158         {
159             // No file means it's ok.
160             log.debug( "OK to update {}, local file does not exist.", getUpdateMode() );
161             return;
162         }
163 
164         if ( ONCE.equals( policySetting ) )
165         {
166             // File exists, but policy is once.
167             throw new PolicyViolationException(
168                 "NO to update " + getUpdateMode() + ", policy is ONCE, and local file exist." );
169         }
170 
171         if ( DAILY.equals( policySetting ) )
172         {
173             Calendar cal = Calendar.getInstance();
174             cal.add( Calendar.DAY_OF_MONTH, -1 );
175             Calendar fileCal = Calendar.getInstance();
176             fileCal.setTimeInMillis( localFile.lastModified() );
177 
178             if ( cal.after( fileCal ) )
179             {
180                 // Its ok.
181                 return;
182             }
183             else
184             {
185                 throw new PolicyViolationException( "NO to update " + getUpdateMode()
186                                                         + ", policy is DAILY, local file exist, and has been updated within the last day." );
187             }
188         }
189 
190         if ( HOURLY.equals( policySetting ) )
191         {
192             Calendar cal = Calendar.getInstance();
193             cal.add( Calendar.HOUR, -1 );
194             Calendar fileCal = Calendar.getInstance();
195             fileCal.setTimeInMillis( localFile.lastModified() );
196 
197             if ( cal.after( fileCal ) )
198             {
199                 // Its ok.
200                 return;
201             }
202             else
203             {
204                 throw new PolicyViolationException( "NO to update " + getUpdateMode()
205                                                         + ", policy is HOURLY, local file exist, and has been updated within the last hour." );
206             }
207         }
208 
209         throw new PolicyConfigurationException(
210             "Unable to process " + getUpdateMode() + " policy of [" + policySetting + "], please file a bug report." );
211     }
212 }