You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AbstractUpdatePolicy.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package org.apache.archiva.policies;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import org.apache.archiva.common.utils.VersionUtil;
  21. import org.apache.archiva.repository.storage.StorageAsset;
  22. import org.apache.commons.lang.StringUtils;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;
  25. import java.util.ArrayList;
  26. import java.util.Calendar;
  27. import java.util.List;
  28. import java.util.Properties;
  29. /**
  30. * AbstractUpdatePolicy
  31. *
  32. *
  33. */
  34. public abstract class AbstractUpdatePolicy
  35. implements PreDownloadPolicy
  36. {
  37. private Logger log = LoggerFactory.getLogger( AbstractUpdatePolicy.class );
  38. /**
  39. * The ALWAYS policy setting means that the artifact is always uipdated from the remote repo.
  40. */
  41. public static final String ALWAYS = "always";
  42. /**
  43. * The NEVER policy setting means that the artifact is never updated from the remote repo.
  44. */
  45. public static final String NEVER = "never";
  46. /**
  47. * <p>
  48. * The DAILY policy means that the artifact retrieval occurs only if one of
  49. * the following conditions are met...
  50. * </p>
  51. * <ul>
  52. * <li>The local artifact is not present.</li>
  53. * <li>The local artifact has a last modified timestamp older than (now - 1 day).</li>
  54. * </ul>
  55. */
  56. public static final String DAILY = "daily";
  57. /**
  58. * <p>
  59. * The HOURLY policy means that the artifact retrieval occurs only if one of
  60. * the following conditions are met...
  61. * </p>
  62. * <ul>
  63. * <li>The local artifact is not present.</li>
  64. * <li>The local artifact has a last modified timestamp older than (now - 1 hour).</li>
  65. * </ul>
  66. */
  67. public static final String HOURLY = "hourly";
  68. /**
  69. * The ONCE policy means that the artifact retrieval occurs only if the
  70. * local artifact is not present. This means that the retreival can only
  71. * occur once.
  72. */
  73. public static final String ONCE = "once";
  74. private List<String> options = new ArrayList<>( 5 );
  75. public AbstractUpdatePolicy()
  76. {
  77. options.add( ALWAYS );
  78. options.add( HOURLY );
  79. options.add( DAILY );
  80. options.add( ONCE );
  81. options.add( NEVER );
  82. }
  83. protected abstract boolean isSnapshotPolicy();
  84. protected abstract String getUpdateMode();
  85. @Override
  86. public List<String> getOptions()
  87. {
  88. return options;
  89. }
  90. @Override
  91. public void applyPolicy( String policySetting, Properties request, StorageAsset localFile )
  92. throws PolicyViolationException, PolicyConfigurationException
  93. {
  94. if ( !StringUtils.equals( request.getProperty( "filetype" ), "artifact" ) )
  95. {
  96. // Only process artifact file types.
  97. return;
  98. }
  99. String version = request.getProperty( "version", "" );
  100. boolean isSnapshotVersion = false;
  101. if ( StringUtils.isNotBlank( version ) )
  102. {
  103. isSnapshotVersion = VersionUtil.isSnapshot( version );
  104. }
  105. if ( !options.contains( policySetting ) )
  106. {
  107. // Not a valid code.
  108. throw new PolicyConfigurationException(
  109. "Unknown " + getUpdateMode() + " policy setting [" + policySetting + "], valid settings are ["
  110. + StringUtils.join( options.iterator(), "," ) + "]" );
  111. }
  112. if ( ALWAYS.equals( policySetting ) )
  113. {
  114. // Skip means ok to update.
  115. log.debug( "OK to update, {} policy set to ALWAYS.", getUpdateMode() );
  116. return;
  117. }
  118. // Test for mismatches.
  119. if ( !isSnapshotVersion && isSnapshotPolicy() )
  120. {
  121. log.debug( "OK to update, snapshot policy does not apply for non-snapshot versions." );
  122. return;
  123. }
  124. if ( isSnapshotVersion && !isSnapshotPolicy() )
  125. {
  126. log.debug( "OK to update, release policy does not apply for snapshot versions." );
  127. return;
  128. }
  129. if ( NEVER.equals( policySetting ) )
  130. {
  131. // Reject means no.
  132. throw new PolicyViolationException( "NO to update, " + getUpdateMode() + " policy set to NEVER." );
  133. }
  134. if ( !localFile.exists() )
  135. {
  136. // No file means it's ok.
  137. log.debug( "OK to update {}, local file does not exist.", getUpdateMode() );
  138. return;
  139. }
  140. if ( ONCE.equals( policySetting ) )
  141. {
  142. // File exists, but policy is once.
  143. throw new PolicyViolationException(
  144. "NO to update " + getUpdateMode() + ", policy is ONCE, and local file exist." );
  145. }
  146. if ( DAILY.equals( policySetting ) )
  147. {
  148. Calendar cal = Calendar.getInstance();
  149. cal.add( Calendar.DAY_OF_MONTH, -1 );
  150. Calendar fileCal = Calendar.getInstance();
  151. fileCal.setTimeInMillis( localFile.getModificationTime().toEpochMilli() );
  152. if ( cal.after( fileCal ) )
  153. {
  154. // Its ok.
  155. return;
  156. }
  157. else
  158. {
  159. throw new PolicyViolationException( "NO to update " + getUpdateMode()
  160. + ", policy is DAILY, local file exist, and has been updated within the last day." );
  161. }
  162. }
  163. if ( HOURLY.equals( policySetting ) )
  164. {
  165. Calendar cal = Calendar.getInstance();
  166. cal.add( Calendar.HOUR, -1 );
  167. Calendar fileCal = Calendar.getInstance();
  168. fileCal.setTimeInMillis( localFile.getModificationTime().toEpochMilli());
  169. if ( cal.after( fileCal ) )
  170. {
  171. // Its ok.
  172. return;
  173. }
  174. else
  175. {
  176. throw new PolicyViolationException( "NO to update " + getUpdateMode()
  177. + ", policy is HOURLY, local file exist, and has been updated within the last hour." );
  178. }
  179. }
  180. throw new PolicyConfigurationException(
  181. "Unable to process " + getUpdateMode() + " policy of [" + policySetting + "], please file a bug report." );
  182. }
  183. }