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   * @deprecated Avoid use of this type, if you need access to local repository use repository system session instead.
28   */
29  @Deprecated
30  public class ArtifactRepositoryPolicy {
31      public static final String UPDATE_POLICY_NEVER = "never";
32  
33      public static final String UPDATE_POLICY_ALWAYS = "always";
34  
35      public static final String UPDATE_POLICY_DAILY = "daily";
36  
37      public static final String UPDATE_POLICY_INTERVAL = "interval";
38  
39      public static final String CHECKSUM_POLICY_FAIL = "fail";
40  
41      public static final String CHECKSUM_POLICY_WARN = "warn";
42  
43      public static final String CHECKSUM_POLICY_IGNORE = "ignore";
44  
45      public static final String DEFAULT_CHECKSUM_POLICY = CHECKSUM_POLICY_FAIL;
46  
47      private boolean enabled;
48  
49      private String updatePolicy;
50  
51      private String checksumPolicy;
52  
53      public ArtifactRepositoryPolicy() {
54          this(true, null, null);
55      }
56  
57      public ArtifactRepositoryPolicy(ArtifactRepositoryPolicy policy) {
58          this(policy.isEnabled(), policy.getUpdatePolicy(), policy.getChecksumPolicy());
59      }
60  
61      public ArtifactRepositoryPolicy(boolean enabled, String updatePolicy, String checksumPolicy) {
62          this.enabled = enabled;
63  
64          if (updatePolicy == null) {
65              updatePolicy = UPDATE_POLICY_DAILY;
66          }
67          this.updatePolicy = updatePolicy;
68  
69          if (checksumPolicy == null) {
70              checksumPolicy = DEFAULT_CHECKSUM_POLICY;
71          }
72          this.checksumPolicy = checksumPolicy;
73      }
74  
75      public void setEnabled(boolean enabled) {
76          this.enabled = enabled;
77      }
78  
79      public void setUpdatePolicy(String updatePolicy) {
80          if (updatePolicy != null) {
81              this.updatePolicy = updatePolicy;
82          }
83      }
84  
85      public void setChecksumPolicy(String checksumPolicy) {
86          if (checksumPolicy != null) {
87              this.checksumPolicy = checksumPolicy;
88          }
89      }
90  
91      public boolean isEnabled() {
92          return enabled;
93      }
94  
95      public String getUpdatePolicy() {
96          return updatePolicy;
97      }
98  
99      public String getChecksumPolicy() {
100         return checksumPolicy;
101     }
102 
103     public boolean checkOutOfDate(Date lastModified) {
104         boolean checkForUpdates = false;
105 
106         if (UPDATE_POLICY_ALWAYS.equals(updatePolicy)) {
107             checkForUpdates = true;
108         } else if (UPDATE_POLICY_DAILY.equals(updatePolicy)) {
109             // Get local midnight boundary
110             Calendar cal = Calendar.getInstance();
111 
112             cal.set(Calendar.HOUR_OF_DAY, 0);
113             cal.set(Calendar.MINUTE, 0);
114             cal.set(Calendar.SECOND, 0);
115             cal.set(Calendar.MILLISECOND, 0);
116 
117             if (cal.getTime().after(lastModified)) {
118                 checkForUpdates = true;
119             }
120         } else if (updatePolicy.startsWith(UPDATE_POLICY_INTERVAL)) {
121             String s = updatePolicy.substring(UPDATE_POLICY_INTERVAL.length() + 1);
122             int minutes = Integer.parseInt(s);
123             Calendar cal = Calendar.getInstance();
124             cal.add(Calendar.MINUTE, -minutes);
125             if (cal.getTime().after(lastModified)) {
126                 checkForUpdates = true;
127             }
128         }
129         // else assume "never"
130         return checkForUpdates;
131     }
132 
133     @Override
134     public String toString() {
135         StringBuilder buffer = new StringBuilder(64);
136         buffer.append("{enabled=");
137         buffer.append(enabled);
138         buffer.append(", checksums=");
139         buffer.append(checksumPolicy);
140         buffer.append(", updates=");
141         buffer.append(updatePolicy);
142         buffer.append('}');
143         return buffer.toString();
144     }
145 
146     public void merge(ArtifactRepositoryPolicy policy) {
147         if (policy != null && policy.isEnabled()) {
148             setEnabled(true);
149 
150             if (ordinalOfCksumPolicy(policy.getChecksumPolicy()) < ordinalOfCksumPolicy(getChecksumPolicy())) {
151                 setChecksumPolicy(policy.getChecksumPolicy());
152             }
153 
154             if (ordinalOfUpdatePolicy(policy.getUpdatePolicy()) < ordinalOfUpdatePolicy(getUpdatePolicy())) {
155                 setUpdatePolicy(policy.getUpdatePolicy());
156             }
157         }
158     }
159 
160     private int ordinalOfCksumPolicy(String policy) {
161         if (ArtifactRepositoryPolicy.CHECKSUM_POLICY_FAIL.equals(policy)) {
162             return 2;
163         } else if (ArtifactRepositoryPolicy.CHECKSUM_POLICY_IGNORE.equals(policy)) {
164             return 0;
165         } else {
166             return 1;
167         }
168     }
169 
170     @SuppressWarnings("checkstyle:magicnumber")
171     private int ordinalOfUpdatePolicy(String policy) {
172         if (ArtifactRepositoryPolicy.UPDATE_POLICY_DAILY.equals(policy)) {
173             return 1440;
174         } else if (ArtifactRepositoryPolicy.UPDATE_POLICY_ALWAYS.equals(policy)) {
175             return 0;
176         } else if (policy != null && policy.startsWith(ArtifactRepositoryPolicy.UPDATE_POLICY_INTERVAL)) {
177             String s = policy.substring(UPDATE_POLICY_INTERVAL.length() + 1);
178             return Integer.parseInt(s);
179         } else {
180             return Integer.MAX_VALUE;
181         }
182     }
183 }