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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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.commons.lang.StringUtils;
  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;
  24. import java.io.File;
  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<String>( 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. public List<String> getOptions()
  86. {
  87. return options;
  88. }
  89. public void applyPolicy( String policySetting, Properties request, File localFile )
  90. throws PolicyViolationException, PolicyConfigurationException
  91. {
  92. if ( !StringUtils.equals( request.getProperty( "filetype" ), "artifact" ) )
  93. {
  94. // Only process artifact file types.
  95. return;
  96. }
  97. String version = request.getProperty( "version", "" );
  98. boolean isSnapshotVersion = false;
  99. if ( StringUtils.isNotBlank( version ) )
  100. {
  101. isSnapshotVersion = VersionUtil.isSnapshot( version );
  102. }
  103. if ( !options.contains( policySetting ) )
  104. {
  105. // Not a valid code.
  106. throw new PolicyConfigurationException(
  107. "Unknown " + getUpdateMode() + " policy setting [" + policySetting + "], valid settings are ["
  108. + StringUtils.join( options.iterator(), "," ) + "]" );
  109. }
  110. if ( ALWAYS.equals( policySetting ) )
  111. {
  112. // Skip means ok to update.
  113. log.debug( "OK to update, {} policy set to ALWAYS.", getUpdateMode() );
  114. return;
  115. }
  116. // Test for mismatches.
  117. if ( !isSnapshotVersion && isSnapshotPolicy() )
  118. {
  119. log.debug( "OK to update, snapshot policy does not apply for non-snapshot versions." );
  120. return;
  121. }
  122. if ( isSnapshotVersion && !isSnapshotPolicy() )
  123. {
  124. log.debug( "OK to update, release policy does not apply for snapshot versions." );
  125. return;
  126. }
  127. if ( NEVER.equals( policySetting ) )
  128. {
  129. // Reject means no.
  130. throw new PolicyViolationException( "NO to update, " + getUpdateMode() + " policy set to NEVER." );
  131. }
  132. if ( !localFile.exists() )
  133. {
  134. // No file means it's ok.
  135. log.debug( "OK to update {}, local file does not exist.", getUpdateMode() );
  136. return;
  137. }
  138. if ( ONCE.equals( policySetting ) )
  139. {
  140. // File exists, but policy is once.
  141. throw new PolicyViolationException(
  142. "NO to update " + getUpdateMode() + ", policy is ONCE, and local file exist." );
  143. }
  144. if ( DAILY.equals( policySetting ) )
  145. {
  146. Calendar cal = Calendar.getInstance();
  147. cal.add( Calendar.DAY_OF_MONTH, -1 );
  148. Calendar fileCal = Calendar.getInstance();
  149. fileCal.setTimeInMillis( localFile.lastModified() );
  150. if ( cal.after( fileCal ) )
  151. {
  152. // Its ok.
  153. return;
  154. }
  155. else
  156. {
  157. throw new PolicyViolationException( "NO to update " + getUpdateMode()
  158. + ", policy is DAILY, local file exist, and has been updated within the last day." );
  159. }
  160. }
  161. if ( HOURLY.equals( policySetting ) )
  162. {
  163. Calendar cal = Calendar.getInstance();
  164. cal.add( Calendar.HOUR, -1 );
  165. Calendar fileCal = Calendar.getInstance();
  166. fileCal.setTimeInMillis( localFile.lastModified() );
  167. if ( cal.after( fileCal ) )
  168. {
  169. // Its ok.
  170. return;
  171. }
  172. else
  173. {
  174. throw new PolicyViolationException( "NO to update " + getUpdateMode()
  175. + ", policy is HOURLY, local file exist, and has been updated within the last hour." );
  176. }
  177. }
  178. throw new PolicyConfigurationException(
  179. "Unable to process " + getUpdateMode() + " policy of [" + policySetting + "], please file a bug report." );
  180. }
  181. }