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.eclipse.aether.internal.impl;
20  
21  import javax.inject.Named;
22  import javax.inject.Singleton;
23  
24  import java.util.Calendar;
25  
26  import org.eclipse.aether.RepositorySystemSession;
27  import org.eclipse.aether.impl.UpdatePolicyAnalyzer;
28  import org.eclipse.aether.repository.RepositoryPolicy;
29  import org.slf4j.Logger;
30  import org.slf4j.LoggerFactory;
31  
32  import static java.util.Objects.requireNonNull;
33  
34  /**
35   */
36  @Singleton
37  @Named
38  public class DefaultUpdatePolicyAnalyzer implements UpdatePolicyAnalyzer {
39  
40      private static final Logger LOGGER = LoggerFactory.getLogger(DefaultUpdatePolicyAnalyzer.class);
41  
42      public DefaultUpdatePolicyAnalyzer() {
43          // enables default constructor
44      }
45  
46      public String getEffectiveUpdatePolicy(RepositorySystemSession session, String policy1, String policy2) {
47          requireNonNull(session, "session cannot be null");
48          return ordinalOfUpdatePolicy(policy1) < ordinalOfUpdatePolicy(policy2) ? policy1 : policy2;
49      }
50  
51      @SuppressWarnings({"checkstyle:magicnumber"})
52      private int ordinalOfUpdatePolicy(String policy) {
53          if (RepositoryPolicy.UPDATE_POLICY_DAILY.equals(policy)) {
54              return 1440;
55          } else if (RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals(policy)) {
56              return 0;
57          } else if (policy != null && policy.startsWith(RepositoryPolicy.UPDATE_POLICY_INTERVAL)) {
58              return getMinutes(policy);
59          } else {
60              // assume "never"
61              return Integer.MAX_VALUE;
62          }
63      }
64  
65      public boolean isUpdatedRequired(RepositorySystemSession session, long lastModified, String policy) {
66          requireNonNull(session, "session cannot be null");
67          boolean checkForUpdates;
68  
69          if (policy == null) {
70              policy = "";
71          }
72  
73          if (RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals(policy)) {
74              checkForUpdates = true;
75          } else if (RepositoryPolicy.UPDATE_POLICY_DAILY.equals(policy)) {
76              Calendar cal = Calendar.getInstance();
77              cal.set(Calendar.HOUR_OF_DAY, 0);
78              cal.set(Calendar.MINUTE, 0);
79              cal.set(Calendar.SECOND, 0);
80              cal.set(Calendar.MILLISECOND, 0);
81  
82              checkForUpdates = cal.getTimeInMillis() > lastModified;
83          } else if (policy.startsWith(RepositoryPolicy.UPDATE_POLICY_INTERVAL)) {
84              int minutes = getMinutes(policy);
85  
86              Calendar cal = Calendar.getInstance();
87              cal.add(Calendar.MINUTE, -minutes);
88  
89              checkForUpdates = cal.getTimeInMillis() > lastModified;
90          } else {
91              // assume "never"
92              checkForUpdates = false;
93  
94              if (!RepositoryPolicy.UPDATE_POLICY_NEVER.equals(policy)) {
95                  LOGGER.warn(
96                          "Unknown repository update policy '{}', assuming '{}'",
97                          policy,
98                          RepositoryPolicy.UPDATE_POLICY_NEVER);
99              }
100         }
101 
102         return checkForUpdates;
103     }
104 
105     @SuppressWarnings({"checkstyle:magicnumber"})
106     private int getMinutes(String policy) {
107         int minutes;
108         try {
109             String s = policy.substring(RepositoryPolicy.UPDATE_POLICY_INTERVAL.length() + 1);
110             minutes = Integer.parseInt(s);
111         } catch (RuntimeException e) {
112             minutes = 24 * 60;
113 
114             LOGGER.warn(
115                     "Non-parseable repository update policy '{}', assuming '{}:1440'",
116                     policy,
117                     RepositoryPolicy.UPDATE_POLICY_INTERVAL);
118         }
119         return minutes;
120     }
121 }