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.util.repository;
20  
21  import org.eclipse.aether.RepositorySystemSession;
22  import org.eclipse.aether.artifact.Artifact;
23  import org.eclipse.aether.metadata.Metadata;
24  import org.eclipse.aether.resolution.ResolutionErrorPolicy;
25  import org.eclipse.aether.resolution.ResolutionErrorPolicyRequest;
26  
27  import static java.util.Objects.requireNonNull;
28  
29  /**
30   * A resolution error policy that allows to control caching for artifacts and metadata at a global level.
31   */
32  public final class SimpleResolutionErrorPolicy implements ResolutionErrorPolicy {
33  
34      private final int artifactPolicy;
35  
36      private final int metadataPolicy;
37  
38      /**
39       * Creates a new error policy with the specified behavior for both artifacts and metadata.
40       *
41       * @param cacheNotFound {@code true} to enable caching of missing items, {@code false} to disable it.
42       * @param cacheTransferErrors {@code true} to enable chaching of transfer errors, {@code false} to disable it.
43       */
44      public SimpleResolutionErrorPolicy(boolean cacheNotFound, boolean cacheTransferErrors) {
45          this((cacheNotFound ? CACHE_NOT_FOUND : 0) | (cacheTransferErrors ? CACHE_TRANSFER_ERROR : 0));
46      }
47  
48      /**
49       * Creates a new error policy with the specified bit mask for both artifacts and metadata.
50       *
51       * @param policy The bit mask describing the policy for artifacts and metadata.
52       */
53      public SimpleResolutionErrorPolicy(int policy) {
54          this(policy, policy);
55      }
56  
57      /**
58       * Creates a new error policy with the specified bit masks for artifacts and metadata.
59       *
60       * @param artifactPolicy The bit mask describing the policy for artifacts.
61       * @param metadataPolicy The bit mask describing the policy for metadata.
62       */
63      public SimpleResolutionErrorPolicy(int artifactPolicy, int metadataPolicy) {
64          this.artifactPolicy = artifactPolicy;
65          this.metadataPolicy = metadataPolicy;
66      }
67  
68      public int getArtifactPolicy(RepositorySystemSession session, ResolutionErrorPolicyRequest<Artifact> request) {
69          requireNonNull(session, "session cannot be null");
70          requireNonNull(request, "request cannot be null");
71          return artifactPolicy;
72      }
73  
74      public int getMetadataPolicy(RepositorySystemSession session, ResolutionErrorPolicyRequest<Metadata> request) {
75          requireNonNull(session, "session cannot be null");
76          requireNonNull(request, "request cannot be null");
77          return metadataPolicy;
78      }
79  }