1 package org.apache.maven.artifact.repository;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.Calendar;
23 import java.util.Date;
24 import java.util.TimeZone;
25
26
27
28
29
30
31
32 public class ArtifactRepositoryPolicy
33 {
34 public static final String UPDATE_POLICY_NEVER = "never";
35
36 public static final String UPDATE_POLICY_ALWAYS = "always";
37
38 public static final String UPDATE_POLICY_DAILY = "daily";
39
40 public static final String UPDATE_POLICY_INTERVAL = "interval";
41
42 public static final String CHECKSUM_POLICY_FAIL = "fail";
43
44 public static final String CHECKSUM_POLICY_WARN = "warn";
45
46 public static final String CHECKSUM_POLICY_IGNORE = "ignore";
47
48 private boolean enabled;
49
50 private String updatePolicy;
51
52 private String checksumPolicy;
53
54 public ArtifactRepositoryPolicy()
55 {
56 this( true, null, null );
57 }
58
59 public ArtifactRepositoryPolicy( ArtifactRepositoryPolicy policy )
60 {
61 this( policy.isEnabled(), policy.getUpdatePolicy(), policy.getChecksumPolicy() );
62 }
63
64 public ArtifactRepositoryPolicy( boolean enabled, String updatePolicy, String checksumPolicy )
65 {
66 this.enabled = enabled;
67
68 if ( updatePolicy == null )
69 {
70 updatePolicy = UPDATE_POLICY_DAILY;
71 }
72 this.updatePolicy = updatePolicy;
73
74 if ( checksumPolicy == null )
75 {
76 checksumPolicy = CHECKSUM_POLICY_WARN;
77 }
78 this.checksumPolicy = checksumPolicy;
79 }
80
81 public void setEnabled( boolean enabled )
82 {
83 this.enabled = enabled;
84 }
85
86 public void setUpdatePolicy( String updatePolicy )
87 {
88 if ( updatePolicy != null )
89 {
90 this.updatePolicy = updatePolicy;
91 }
92 }
93
94 public void setChecksumPolicy( String checksumPolicy )
95 {
96 if ( checksumPolicy != null )
97 {
98 this.checksumPolicy = checksumPolicy;
99 }
100 }
101
102 public boolean isEnabled()
103 {
104 return enabled;
105 }
106
107 public String getUpdatePolicy()
108 {
109 return updatePolicy;
110 }
111
112 public String getChecksumPolicy()
113 {
114 return checksumPolicy;
115 }
116
117 public boolean checkOutOfDate( Date lastModified )
118 {
119 boolean checkForUpdates = false;
120
121 if ( UPDATE_POLICY_ALWAYS.equals( updatePolicy ) )
122 {
123 checkForUpdates = true;
124 }
125 else if ( UPDATE_POLICY_DAILY.equals( updatePolicy ) )
126 {
127
128 Calendar cal = Calendar.getInstance();
129
130 cal.set( Calendar.HOUR_OF_DAY, 0 );
131 cal.set( Calendar.MINUTE, 0 );
132 cal.set( Calendar.SECOND, 0 );
133 cal.set( Calendar.MILLISECOND, 0 );
134
135 if ( cal.getTime().after( lastModified ) )
136 {
137 checkForUpdates = true;
138 }
139 }
140 else if ( updatePolicy.startsWith( UPDATE_POLICY_INTERVAL ) )
141 {
142 String s = updatePolicy.substring( UPDATE_POLICY_INTERVAL.length() + 1 );
143 int minutes = Integer.valueOf( s );
144 Calendar cal = Calendar.getInstance();
145 cal.add( Calendar.MINUTE, -minutes );
146 if ( cal.getTime().after( lastModified ) )
147 {
148 checkForUpdates = true;
149 }
150 }
151
152 return checkForUpdates;
153 }
154
155 @Override
156 public String toString()
157 {
158 StringBuilder buffer = new StringBuilder( 64 );
159 buffer.append( "{enabled=" );
160 buffer.append( enabled );
161 buffer.append( ", checksums=" );
162 buffer.append( checksumPolicy );
163 buffer.append( ", updates=" );
164 buffer.append( updatePolicy );
165 buffer.append( "}" );
166 return buffer.toString();
167 }
168
169 public void merge( ArtifactRepositoryPolicy policy )
170 {
171 if ( policy != null && policy.isEnabled() )
172 {
173 setEnabled( true );
174
175 if ( ordinalOfChecksumPolicy( policy.getChecksumPolicy() ) < ordinalOfChecksumPolicy( getChecksumPolicy() ) )
176 {
177 setChecksumPolicy( policy.getChecksumPolicy() );
178 }
179
180 if ( ordinalOfUpdatePolicy( policy.getUpdatePolicy() ) < ordinalOfUpdatePolicy( getUpdatePolicy() ) )
181 {
182 setUpdatePolicy( policy.getUpdatePolicy() );
183 }
184 }
185 }
186
187 private int ordinalOfChecksumPolicy( String policy )
188 {
189 if ( ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL.equals( policy ) )
190 {
191 return 2;
192 }
193 else if ( ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE.equals( policy ) )
194 {
195 return 0;
196 }
197 else
198 {
199 return 1;
200 }
201 }
202
203 private int ordinalOfUpdatePolicy( String policy )
204 {
205 if ( ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY.equals( policy ) )
206 {
207 return 1440;
208 }
209 else if ( ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS.equals( policy ) )
210 {
211 return 0;
212 }
213 else if ( policy != null && policy.startsWith( ArtifactRepositoryPolicy.UPDATE_POLICY_INTERVAL ) )
214 {
215 String s = policy.substring( UPDATE_POLICY_INTERVAL.length() + 1 );
216 return Integer.valueOf( s );
217 }
218 else
219 {
220 return Integer.MAX_VALUE;
221 }
222 }
223
224 }