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 java.util.ArrayList;
22  import java.util.Collection;
23  import java.util.List;
24  import java.util.Map;
25  
26  import org.eclipse.aether.RepositorySystemSession;
27  import org.eclipse.aether.artifact.Artifact;
28  import org.eclipse.aether.impl.MetadataGenerator;
29  import org.eclipse.aether.impl.MetadataGeneratorFactory;
30  import org.eclipse.aether.impl.OfflineController;
31  import org.eclipse.aether.metadata.Metadata;
32  import org.eclipse.aether.repository.RemoteRepository;
33  import org.eclipse.aether.resolution.ResolutionErrorPolicy;
34  import org.eclipse.aether.resolution.ResolutionErrorPolicyRequest;
35  import org.eclipse.aether.transfer.RepositoryOfflineException;
36  
37  /**
38   * Internal utility methods.
39   */
40  final class Utils {
41  
42      public static PrioritizedComponents<MetadataGeneratorFactory> sortMetadataGeneratorFactories(
43              RepositorySystemSession session, Map<String, MetadataGeneratorFactory> factories) {
44          return PrioritizedComponents.reuseOrCreate(session, factories, MetadataGeneratorFactory::getPriority);
45      }
46  
47      public static List<Metadata> prepareMetadata(
48              List<? extends MetadataGenerator> generators, List<? extends Artifact> artifacts) {
49          List<Metadata> metadatas = new ArrayList<>();
50  
51          for (MetadataGenerator generator : generators) {
52              metadatas.addAll(generator.prepare(artifacts));
53          }
54  
55          return metadatas;
56      }
57  
58      public static List<Metadata> finishMetadata(
59              List<? extends MetadataGenerator> generators, List<? extends Artifact> artifacts) {
60          List<Metadata> metadatas = new ArrayList<>();
61  
62          for (MetadataGenerator generator : generators) {
63              metadatas.addAll(generator.finish(artifacts));
64          }
65  
66          return metadatas;
67      }
68  
69      public static <T> List<T> combine(Collection<? extends T> first, Collection<? extends T> second) {
70          List<T> result = new ArrayList<>(first.size() + second.size());
71          result.addAll(first);
72          result.addAll(second);
73          return result;
74      }
75  
76      public static int getPolicy(RepositorySystemSession session, Artifact artifact, RemoteRepository repository) {
77          ResolutionErrorPolicy rep = session.getResolutionErrorPolicy();
78          if (rep == null) {
79              return ResolutionErrorPolicy.CACHE_DISABLED;
80          }
81          return rep.getArtifactPolicy(session, new ResolutionErrorPolicyRequest<>(artifact, repository));
82      }
83  
84      public static int getPolicy(RepositorySystemSession session, Metadata metadata, RemoteRepository repository) {
85          ResolutionErrorPolicy rep = session.getResolutionErrorPolicy();
86          if (rep == null) {
87              return ResolutionErrorPolicy.CACHE_DISABLED;
88          }
89          return rep.getMetadataPolicy(session, new ResolutionErrorPolicyRequest<>(metadata, repository));
90      }
91  
92      public static void appendClassLoader(StringBuilder buffer, Object component) {
93          ClassLoader loader = component.getClass().getClassLoader();
94          if (loader != null && !loader.equals(Utils.class.getClassLoader())) {
95              buffer.append(" from ").append(loader);
96          }
97      }
98  
99      public static void checkOffline(
100             RepositorySystemSession session, OfflineController offlineController, RemoteRepository repository)
101             throws RepositoryOfflineException {
102         if (session.isOffline()) {
103             offlineController.checkOffline(session, repository);
104         }
105     }
106 }