001 package org.apache.maven.artifact.repository; 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 022 import java.util.Calendar; 023 import java.util.Date; 024 025 /** 026 * Describes a set of policies for a repository to use under certain conditions. 027 * 028 * @author <a href="mailto:brett@apache.org">Brett Porter</a> 029 */ 030 public class ArtifactRepositoryPolicy 031 { 032 public static final String UPDATE_POLICY_NEVER = "never"; 033 034 public static final String UPDATE_POLICY_ALWAYS = "always"; 035 036 public static final String UPDATE_POLICY_DAILY = "daily"; 037 038 public static final String UPDATE_POLICY_INTERVAL = "interval"; 039 040 public static final String CHECKSUM_POLICY_FAIL = "fail"; 041 042 public static final String CHECKSUM_POLICY_WARN = "warn"; 043 044 public static final String CHECKSUM_POLICY_IGNORE = "ignore"; 045 046 private boolean enabled; 047 048 private String updatePolicy; 049 050 private String checksumPolicy; 051 052 public ArtifactRepositoryPolicy() 053 { 054 this( true, null, null ); 055 } 056 057 public ArtifactRepositoryPolicy( ArtifactRepositoryPolicy policy ) 058 { 059 this( policy.isEnabled(), policy.getUpdatePolicy(), policy.getChecksumPolicy() ); 060 } 061 062 public ArtifactRepositoryPolicy( boolean enabled, String updatePolicy, String checksumPolicy ) 063 { 064 this.enabled = enabled; 065 066 if ( updatePolicy == null ) 067 { 068 updatePolicy = UPDATE_POLICY_DAILY; 069 } 070 this.updatePolicy = updatePolicy; 071 072 if ( checksumPolicy == null ) 073 { 074 checksumPolicy = CHECKSUM_POLICY_WARN; 075 } 076 this.checksumPolicy = checksumPolicy; 077 } 078 079 public void setEnabled( boolean enabled ) 080 { 081 this.enabled = enabled; 082 } 083 084 public void setUpdatePolicy( String updatePolicy ) 085 { 086 if ( updatePolicy != null ) 087 { 088 this.updatePolicy = updatePolicy; 089 } 090 } 091 092 public void setChecksumPolicy( String checksumPolicy ) 093 { 094 if ( checksumPolicy != null ) 095 { 096 this.checksumPolicy = checksumPolicy; 097 } 098 } 099 100 public boolean isEnabled() 101 { 102 return enabled; 103 } 104 105 public String getUpdatePolicy() 106 { 107 return updatePolicy; 108 } 109 110 public String getChecksumPolicy() 111 { 112 return checksumPolicy; 113 } 114 115 public boolean checkOutOfDate( Date lastModified ) 116 { 117 boolean checkForUpdates = false; 118 119 if ( UPDATE_POLICY_ALWAYS.equals( updatePolicy ) ) 120 { 121 checkForUpdates = true; 122 } 123 else if ( UPDATE_POLICY_DAILY.equals( updatePolicy ) ) 124 { 125 // Get local midnight boundary 126 Calendar cal = Calendar.getInstance(); 127 128 cal.set( Calendar.HOUR_OF_DAY, 0 ); 129 cal.set( Calendar.MINUTE, 0 ); 130 cal.set( Calendar.SECOND, 0 ); 131 cal.set( Calendar.MILLISECOND, 0 ); 132 133 if ( cal.getTime().after( lastModified ) ) 134 { 135 checkForUpdates = true; 136 } 137 } 138 else if ( updatePolicy.startsWith( UPDATE_POLICY_INTERVAL ) ) 139 { 140 String s = updatePolicy.substring( UPDATE_POLICY_INTERVAL.length() + 1 ); 141 int minutes = Integer.valueOf( s ); 142 Calendar cal = Calendar.getInstance(); 143 cal.add( Calendar.MINUTE, -minutes ); 144 if ( cal.getTime().after( lastModified ) ) 145 { 146 checkForUpdates = true; 147 } 148 } 149 // else assume "never" 150 return checkForUpdates; 151 } 152 153 @Override 154 public String toString() 155 { 156 StringBuilder buffer = new StringBuilder( 64 ); 157 buffer.append( "{enabled=" ); 158 buffer.append( enabled ); 159 buffer.append( ", checksums=" ); 160 buffer.append( checksumPolicy ); 161 buffer.append( ", updates=" ); 162 buffer.append( updatePolicy ); 163 buffer.append( "}" ); 164 return buffer.toString(); 165 } 166 167 public void merge( ArtifactRepositoryPolicy policy ) 168 { 169 if ( policy != null && policy.isEnabled() ) 170 { 171 setEnabled( true ); 172 173 if ( ordinalOfChecksumPolicy( policy.getChecksumPolicy() ) < ordinalOfChecksumPolicy( getChecksumPolicy() ) ) 174 { 175 setChecksumPolicy( policy.getChecksumPolicy() ); 176 } 177 178 if ( ordinalOfUpdatePolicy( policy.getUpdatePolicy() ) < ordinalOfUpdatePolicy( getUpdatePolicy() ) ) 179 { 180 setUpdatePolicy( policy.getUpdatePolicy() ); 181 } 182 } 183 } 184 185 private int ordinalOfChecksumPolicy( String policy ) 186 { 187 if ( ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL.equals( policy ) ) 188 { 189 return 2; 190 } 191 else if ( ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE.equals( policy ) ) 192 { 193 return 0; 194 } 195 else 196 { 197 return 1; 198 } 199 } 200 201 private int ordinalOfUpdatePolicy( String policy ) 202 { 203 if ( ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY.equals( policy ) ) 204 { 205 return 1440; 206 } 207 else if ( ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( policy ) ) 208 { 209 return 0; 210 } 211 else if ( policy != null && policy.startsWith( ArtifactRepositoryPolicy.UPDATE_POLICY_INTERVAL ) ) 212 { 213 String s = policy.substring( UPDATE_POLICY_INTERVAL.length() + 1 ); 214 return Integer.valueOf( s ); 215 } 216 else 217 { 218 return Integer.MAX_VALUE; 219 } 220 } 221 222 }