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