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.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       * Enforce presence of checksums and fail the resolution if they do not match or are absent.
48       */
49      public static final String CHECKSUM_POLICY_FAIL = "fail";
50  
51      /**
52       * Expect presence of checksums and warn if they do not match or are absent.
53       */
54      public static final String CHECKSUM_POLICY_WARN = "warn";
55  
56      /**
57       * Ignore presence and verification of checksums.
58       */
59      public static final String CHECKSUM_POLICY_IGNORE = "ignore";
60  
61      private final boolean enabled;
62  
63      private final String artifactUpdatePolicy;
64  
65      private final String metadataUpdatePolicy;
66  
67      private final String checksumPolicy;
68  
69      /**
70       * Creates a new policy with checksum warnings and daily update checks.
71       */
72      public RepositoryPolicy() {
73          this(true, UPDATE_POLICY_DAILY, UPDATE_POLICY_DAILY, CHECKSUM_POLICY_WARN);
74      }
75  
76      /**
77       * Creates a new policy with the specified settings (uses same update policy for data and metadata, retains old
78       * resolver behaviour).
79       *
80       * @param enabled A flag whether the associated repository should be accessed or not.
81       * @param updatePolicy The update interval after which locally cached data from the repository is considered
82       *            stale and should be re-fetched, may be {@code null}.
83       * @param checksumPolicy The way checksum verification should be handled, may be {@code null}.
84       */
85      public RepositoryPolicy(boolean enabled, String updatePolicy, String checksumPolicy) {
86          this(enabled, updatePolicy, updatePolicy, checksumPolicy);
87      }
88  
89      /**
90       * Creates a new policy with the specified settings.
91       *
92       * @param enabled A flag whether the associated repository should be accessed or not.
93       * @param artifactUpdatePolicy The update interval after which locally cached data from the repository is considered stale
94       *            and should be re-fetched, may be {@code null}.
95       * @param metadataUpdatePolicy The update interval after which locally cached metadata from the repository is considered stale
96       *            and should be re-fetched, may be {@code null}.
97       * @param checksumPolicy The way checksum verification should be handled, may be {@code null}.
98       * @since 2.0.0
99       */
100     public RepositoryPolicy(
101             boolean enabled, String artifactUpdatePolicy, String metadataUpdatePolicy, String checksumPolicy) {
102         this.enabled = enabled;
103         this.artifactUpdatePolicy = (artifactUpdatePolicy != null) ? artifactUpdatePolicy : "";
104         this.metadataUpdatePolicy = (metadataUpdatePolicy != null) ? metadataUpdatePolicy : "";
105         this.checksumPolicy = (checksumPolicy != null) ? checksumPolicy : "";
106     }
107 
108     /**
109      * Indicates whether the associated repository should be contacted or not.
110      *
111      * @return {@code true} if the repository should be contacted, {@code false} otherwise.
112      */
113     public boolean isEnabled() {
114         return enabled;
115     }
116 
117     /**
118      * This method is not used in Resolver, as resolver internally strictly distinguishes between artifact and metadata
119      * update policies.
120      *
121      * @see #getArtifactUpdatePolicy()
122      * @see #getMetadataUpdatePolicy()
123      * @return The update policy for locally cached artifacts, never {@code null}.
124      * @deprecated This method should not be used. Since version 2 Resolver internally distinguishes between artifact
125      * update policy and metadata update policy. This method was left only to preserve binary compatibility, and in
126      * reality invokes {@link #getArtifactUpdatePolicy()}.
127      */
128     @Deprecated
129     public String getUpdatePolicy() {
130         return getArtifactUpdatePolicy();
131     }
132 
133     /**
134      * Gets the update policy for locally cached artifacts from the repository.
135      *
136      * @return The update policy, never {@code null}.
137      * @since 2.0.0
138      */
139     public String getArtifactUpdatePolicy() {
140         return artifactUpdatePolicy;
141     }
142 
143     /**
144      * Gets the update policy for locally cached metadata from the repository.
145      *
146      * @return The update policy, never {@code null}.
147      * @since 2.0.0
148      */
149     public String getMetadataUpdatePolicy() {
150         return metadataUpdatePolicy;
151     }
152 
153     /**
154      * Gets the policy for checksum validation.
155      *
156      * @return The checksum policy, never {@code null}.
157      */
158     public String getChecksumPolicy() {
159         return checksumPolicy;
160     }
161 
162     @Override
163     public String toString() {
164         return "enabled=" + isEnabled()
165                 + ", checksums=" + getChecksumPolicy()
166                 + ", artifactUpdates=" + getArtifactUpdatePolicy()
167                 + ", metadataUpdates=" + getMetadataUpdatePolicy();
168     }
169 
170     @Override
171     public boolean equals(Object obj) {
172         if (this == obj) {
173             return true;
174         }
175 
176         if (obj == null || !getClass().equals(obj.getClass())) {
177             return false;
178         }
179 
180         RepositoryPolicy that = (RepositoryPolicy) obj;
181 
182         return enabled == that.enabled
183                 && artifactUpdatePolicy.equals(that.artifactUpdatePolicy)
184                 && metadataUpdatePolicy.equals(that.metadataUpdatePolicy)
185                 && checksumPolicy.equals(that.checksumPolicy);
186     }
187 
188     @Override
189     public int hashCode() {
190         int hash = 17;
191         hash = hash * 31 + (enabled ? 1 : 0);
192         hash = hash * 31 + artifactUpdatePolicy.hashCode();
193         hash = hash * 31 + metadataUpdatePolicy.hashCode();
194         hash = hash * 31 + checksumPolicy.hashCode();
195         return hash;
196     }
197 }