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