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