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      @Override
43      public String getEffectiveUpdatePolicy(RepositorySystemSession session, String policy1, String policy2) {
44          requireNonNull(session, "session cannot be null");
45          return ordinalOfUpdatePolicy(policy1) < ordinalOfUpdatePolicy(policy2) ? policy1 : policy2;
46      }
47  
48      private int ordinalOfUpdatePolicy(String policy) {
49          if (RepositoryPolicy.UPDATE_POLICY_DAILY.equals(policy)) {
50              return 1440;
51          } else if (RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals(policy)) {
52              return 0;
53          } else if (policy != null && policy.startsWith(RepositoryPolicy.UPDATE_POLICY_INTERVAL)) {
54              return getMinutes(policy);
55          } else {
56              // assume "never"
57              return Integer.MAX_VALUE;
58          }
59      }
60  
61      @Override
62      public boolean isUpdatedRequired(RepositorySystemSession session, long lastModified, String policy) {
63          requireNonNull(session, "session cannot be null");
64          boolean checkForUpdates;
65  
66          if (policy == null) {
67              policy = "";
68          }
69  
70          if (RepositoryPolicy.UPDATE_POLICY_ALWAYS.equals(policy)) {
71              checkForUpdates = true;
72          } else if (RepositoryPolicy.UPDATE_POLICY_DAILY.equals(policy)) {
73              Calendar cal = Calendar.getInstance();
74              cal.set(Calendar.HOUR_OF_DAY, 0);
75              cal.set(Calendar.MINUTE, 0);
76              cal.set(Calendar.SECOND, 0);
77              cal.set(Calendar.MILLISECOND, 0);
78  
79              checkForUpdates = cal.getTimeInMillis() > lastModified;
80          } else if (policy.startsWith(RepositoryPolicy.UPDATE_POLICY_INTERVAL)) {
81              int minutes = getMinutes(policy);
82  
83              Calendar cal = Calendar.getInstance();
84              cal.add(Calendar.MINUTE, -minutes);
85  
86              checkForUpdates = cal.getTimeInMillis() > lastModified;
87          } else {
88              // assume "never"
89              checkForUpdates = false;
90  
91              if (!RepositoryPolicy.UPDATE_POLICY_NEVER.equals(policy)) {
92                  LOGGER.warn(
93                          "Unknown repository update policy '{}', assuming '{}'",
94                          policy,
95                          RepositoryPolicy.UPDATE_POLICY_NEVER);
96              }
97          }
98  
99          return checkForUpdates;
100     }
101 
102     private int getMinutes(String policy) {
103         int minutes;
104         try {
105             String s = policy.substring(RepositoryPolicy.UPDATE_POLICY_INTERVAL.length() + 1);
106             minutes = Integer.parseInt(s);
107         } catch (RuntimeException e) {
108             minutes = 24 * 60;
109 
110             LOGGER.warn(
111                     "Non-parseable repository update policy '{}', assuming '{}:1440'",
112                     policy,
113                     RepositoryPolicy.UPDATE_POLICY_INTERVAL);
114         }
115         return minutes;
116     }
117 }