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