1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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 }