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.repository;
20
21 /**
22 * A policy controlling access to a repository.
23 */
24 public final class RepositoryPolicy {
25
26 /**
27 * Never update locally cached data.
28 */
29 public static final String UPDATE_POLICY_NEVER = "never";
30
31 /**
32 * Always update locally cached data.
33 */
34 public static final String UPDATE_POLICY_ALWAYS = "always";
35
36 /**
37 * Update locally cached data once a day.
38 */
39 public static final String UPDATE_POLICY_DAILY = "daily";
40
41 /**
42 * Update locally cached data every X minutes as given by "interval:X".
43 */
44 public static final String UPDATE_POLICY_INTERVAL = "interval";
45
46 /**
47 * Verify checksums and fail the resolution if they do not match.
48 */
49 public static final String CHECKSUM_POLICY_FAIL = "fail";
50
51 /**
52 * Verify checksums and warn if they do not match.
53 */
54 public static final String CHECKSUM_POLICY_WARN = "warn";
55
56 /**
57 * Do not verify checksums.
58 */
59 public static final String CHECKSUM_POLICY_IGNORE = "ignore";
60
61 private final boolean enabled;
62
63 private final String updatePolicy;
64
65 private final String checksumPolicy;
66
67 /**
68 * Creates a new policy with checksum warnings and daily update checks.
69 */
70 public RepositoryPolicy() {
71 this(true, UPDATE_POLICY_DAILY, CHECKSUM_POLICY_WARN);
72 }
73
74 /**
75 * Creates a new policy with the specified settings.
76 *
77 * @param enabled A flag whether the associated repository should be accessed or not.
78 * @param updatePolicy The update interval after which locally cached data from the repository is considered stale
79 * and should be refetched, may be {@code null}.
80 * @param checksumPolicy The way checksum verification should be handled, may be {@code null}.
81 */
82 public RepositoryPolicy(boolean enabled, String updatePolicy, String checksumPolicy) {
83 this.enabled = enabled;
84 this.updatePolicy = (updatePolicy != null) ? updatePolicy : "";
85 this.checksumPolicy = (checksumPolicy != null) ? checksumPolicy : "";
86 }
87
88 /**
89 * Indicates whether the associated repository should be contacted or not.
90 *
91 * @return {@code true} if the repository should be contacted, {@code false} otherwise.
92 */
93 public boolean isEnabled() {
94 return enabled;
95 }
96
97 /**
98 * Gets the update policy for locally cached data from the repository.
99 *
100 * @return The update policy, never {@code null}.
101 */
102 public String getUpdatePolicy() {
103 return updatePolicy;
104 }
105
106 /**
107 * Gets the policy for checksum validation.
108 *
109 * @return The checksum policy, never {@code null}.
110 */
111 public String getChecksumPolicy() {
112 return checksumPolicy;
113 }
114
115 @Override
116 public String toString() {
117 StringBuilder buffer = new StringBuilder(256);
118 buffer.append("enabled=").append(isEnabled());
119 buffer.append(", checksums=").append(getChecksumPolicy());
120 buffer.append(", updates=").append(getUpdatePolicy());
121 return buffer.toString();
122 }
123
124 @Override
125 public boolean equals(Object obj) {
126 if (this == obj) {
127 return true;
128 }
129
130 if (obj == null || !getClass().equals(obj.getClass())) {
131 return false;
132 }
133
134 RepositoryPolicy that = (RepositoryPolicy) obj;
135
136 return enabled == that.enabled
137 && updatePolicy.equals(that.updatePolicy)
138 && checksumPolicy.equals(that.checksumPolicy);
139 }
140
141 @Override
142 public int hashCode() {
143 int hash = 17;
144 hash = hash * 31 + (enabled ? 1 : 0);
145 hash = hash * 31 + updatePolicy.hashCode();
146 hash = hash * 31 + checksumPolicy.hashCode();
147 return hash;
148 }
149 }