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