View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.artifact.repository;
20  
21  import java.util.Calendar;
22  import java.util.Date;
23  
24  /**
25   * Describes a set of policies for a repository to use under certain conditions.
26   *
27   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
28   */
29  public class ArtifactRepositoryPolicy {
30      public static final String UPDATE_POLICY_NEVER = "never";
31  
32      public static final String UPDATE_POLICY_ALWAYS = "always";
33  
34      public static final String UPDATE_POLICY_DAILY = "daily";
35  
36      public static final String UPDATE_POLICY_INTERVAL = "interval";
37  
38      public static final String CHECKSUM_POLICY_FAIL = "fail";
39  
40      public static final String CHECKSUM_POLICY_WARN = "warn";
41  
42      public static final String CHECKSUM_POLICY_IGNORE = "ignore";
43  
44      private boolean enabled;
45  
46      private String updatePolicy;
47  
48      private String checksumPolicy;
49  
50      public ArtifactRepositoryPolicy() {
51          this(true, null, null);
52      }
53  
54      public ArtifactRepositoryPolicy(ArtifactRepositoryPolicy policy) {
55          this(policy.isEnabled(), policy.getUpdatePolicy(), policy.getChecksumPolicy());
56      }
57  
58      public ArtifactRepositoryPolicy(boolean enabled, String updatePolicy, String checksumPolicy) {
59          this.enabled = enabled;
60  
61          if (updatePolicy == null) {
62              updatePolicy = UPDATE_POLICY_DAILY;
63          }
64          this.updatePolicy = updatePolicy;
65  
66          if (checksumPolicy == null) {
67              checksumPolicy = CHECKSUM_POLICY_WARN;
68          }
69          this.checksumPolicy = checksumPolicy;
70      }
71  
72      public void setEnabled(boolean enabled) {
73          this.enabled = enabled;
74      }
75  
76      public void setUpdatePolicy(String updatePolicy) {
77          if (updatePolicy != null) {
78              this.updatePolicy = updatePolicy;
79          }
80      }
81  
82      public void setChecksumPolicy(String checksumPolicy) {
83          if (checksumPolicy != null) {
84              this.checksumPolicy = checksumPolicy;
85          }
86      }
87  
88      public boolean isEnabled() {
89          return enabled;
90      }
91  
92      public String getUpdatePolicy() {
93          return updatePolicy;
94      }
95  
96      public String getChecksumPolicy() {
97          return checksumPolicy;
98      }
99  
100     public boolean checkOutOfDate(Date lastModified) {
101         boolean checkForUpdates = false;
102 
103         if (UPDATE_POLICY_ALWAYS.equals(updatePolicy)) {
104             checkForUpdates = true;
105         } else if (UPDATE_POLICY_DAILY.equals(updatePolicy)) {
106             // Get local midnight boundary
107             Calendar cal = Calendar.getInstance();
108 
109             cal.set(Calendar.HOUR_OF_DAY, 0);
110             cal.set(Calendar.MINUTE, 0);
111             cal.set(Calendar.SECOND, 0);
112             cal.set(Calendar.MILLISECOND, 0);
113 
114             if (cal.getTime().after(lastModified)) {
115                 checkForUpdates = true;
116             }
117         } else if (updatePolicy.startsWith(UPDATE_POLICY_INTERVAL)) {
118             String s = updatePolicy.substring(UPDATE_POLICY_INTERVAL.length() + 1);
119             int minutes = Integer.parseInt(s);
120             Calendar cal = Calendar.getInstance();
121             cal.add(Calendar.MINUTE, -minutes);
122             if (cal.getTime().after(lastModified)) {
123                 checkForUpdates = true;
124             }
125         }
126         // else assume "never"
127         return checkForUpdates;
128     }
129 
130     @Override
131     public String toString() {
132         StringBuilder buffer = new StringBuilder(64);
133         buffer.append("{enabled=");
134         buffer.append(enabled);
135         buffer.append(", checksums=");
136         buffer.append(checksumPolicy);
137         buffer.append(", updates=");
138         buffer.append(updatePolicy);
139         buffer.append('}');
140         return buffer.toString();
141     }
142 
143     public void merge(ArtifactRepositoryPolicy policy) {
144         if (policy != null && policy.isEnabled()) {
145             setEnabled(true);
146 
147             if (ordinalOfCksumPolicy(policy.getChecksumPolicy()) < ordinalOfCksumPolicy(getChecksumPolicy())) {
148                 setChecksumPolicy(policy.getChecksumPolicy());
149             }
150 
151             if (ordinalOfUpdatePolicy(policy.getUpdatePolicy()) < ordinalOfUpdatePolicy(getUpdatePolicy())) {
152                 setUpdatePolicy(policy.getUpdatePolicy());
153             }
154         }
155     }
156 
157     private int ordinalOfCksumPolicy(String policy) {
158         if (ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL.equals(policy)) {
159             return 2;
160         } else if (ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE.equals(policy)) {
161             return 0;
162         } else {
163             return 1;
164         }
165     }
166 
167     @SuppressWarnings("checkstyle:magicnumber")
168     private int ordinalOfUpdatePolicy(String policy) {
169         if (ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY.equals(policy)) {
170             return 1440;
171         } else if (ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS.equals(policy)) {
172             return 0;
173         } else if (policy != null && policy.startsWith(ArtifactRepositoryPolicy.UPDATE_POLICY_INTERVAL)) {
174             String s = policy.substring(UPDATE_POLICY_INTERVAL.length() + 1);
175             return Integer.parseInt(s);
176         } else {
177             return Integer.MAX_VALUE;
178         }
179     }
180 }