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.deployment.DeployRequest;
29  import org.eclipse.aether.impl.MetadataGenerator;
30  import org.eclipse.aether.impl.MetadataGeneratorFactory;
31  import org.eclipse.aether.impl.OfflineController;
32  import org.eclipse.aether.installation.InstallRequest;
33  import org.eclipse.aether.metadata.Metadata;
34  import org.eclipse.aether.repository.RemoteRepository;
35  import org.eclipse.aether.resolution.ResolutionErrorPolicy;
36  import org.eclipse.aether.resolution.ResolutionErrorPolicyRequest;
37  import org.eclipse.aether.spi.artifact.decorator.ArtifactDecorator;
38  import org.eclipse.aether.spi.artifact.decorator.ArtifactDecoratorFactory;
39  import org.eclipse.aether.spi.artifact.generator.ArtifactGenerator;
40  import org.eclipse.aether.spi.artifact.generator.ArtifactGeneratorFactory;
41  import org.eclipse.aether.transfer.RepositoryOfflineException;
42  
43  /**
44   * Internal utility methods.
45   */
46  public final class Utils {
47      private Utils() {}
48  
49      private static PrioritizedComponents<ArtifactDecoratorFactory> sortArtifactDecoratorFactories(
50              RepositorySystemSession session, Map<String, ArtifactDecoratorFactory> factories) {
51          return PrioritizedComponents.reuseOrCreate(
52                  session, ArtifactDecoratorFactory.class, factories, ArtifactDecoratorFactory::getPriority);
53      }
54  
55      public static List<? extends ArtifactDecorator> getArtifactDecorators(
56              RepositorySystemSession session, Map<String, ArtifactDecoratorFactory> artifactDecoratorFactories) {
57          PrioritizedComponents<ArtifactDecoratorFactory> factories =
58                  sortArtifactDecoratorFactories(session, artifactDecoratorFactories);
59          List<ArtifactDecorator> decorators = new ArrayList<>();
60          for (PrioritizedComponent<ArtifactDecoratorFactory> factory : factories.getEnabled()) {
61              ArtifactDecorator decorator = factory.getComponent().newInstance(session);
62              if (decorator != null) {
63                  decorators.add(decorator);
64              }
65          }
66          return decorators;
67      }
68  
69      private static PrioritizedComponents<ArtifactGeneratorFactory> sortArtifactGeneratorFactories(
70              RepositorySystemSession session, Map<String, ArtifactGeneratorFactory> factories) {
71          return PrioritizedComponents.reuseOrCreate(
72                  session, ArtifactGeneratorFactory.class, factories, ArtifactGeneratorFactory::getPriority);
73      }
74  
75      private static List<? extends ArtifactGenerator> doGetArtifactGenerators(
76              RepositorySystemSession session, Map<String, ArtifactGeneratorFactory> artifactFactories, Object request) {
77          PrioritizedComponents<ArtifactGeneratorFactory> factories =
78                  sortArtifactGeneratorFactories(session, artifactFactories);
79          List<ArtifactGenerator> generators = new ArrayList<>();
80          for (PrioritizedComponent<ArtifactGeneratorFactory> factory : factories.getEnabled()) {
81              ArtifactGenerator generator;
82              if (request instanceof InstallRequest) {
83                  generator = factory.getComponent().newInstance(session, (InstallRequest) request);
84              } else if (request instanceof DeployRequest) {
85                  generator = factory.getComponent().newInstance(session, (DeployRequest) request);
86              } else {
87                  throw new IllegalArgumentException("Unknown request");
88              }
89              if (generator != null) {
90                  generators.add(generator);
91              }
92          }
93          return generators;
94      }
95  
96      public static List<? extends ArtifactGenerator> getArtifactGenerators(
97              RepositorySystemSession session,
98              Map<String, ArtifactGeneratorFactory> artifactFactories,
99              InstallRequest request) {
100         return doGetArtifactGenerators(session, artifactFactories, request);
101     }
102 
103     public static List<? extends ArtifactGenerator> getArtifactGenerators(
104             RepositorySystemSession session,
105             Map<String, ArtifactGeneratorFactory> artifactFactories,
106             DeployRequest request) {
107         return doGetArtifactGenerators(session, artifactFactories, request);
108     }
109 
110     private static PrioritizedComponents<MetadataGeneratorFactory> sortMetadataGeneratorFactories(
111             RepositorySystemSession session, Map<String, MetadataGeneratorFactory> factories) {
112         return PrioritizedComponents.reuseOrCreate(
113                 session, MetadataGeneratorFactory.class, factories, MetadataGeneratorFactory::getPriority);
114     }
115 
116     private static List<? extends MetadataGenerator> doGetMetadataGenerators(
117             RepositorySystemSession session, Map<String, MetadataGeneratorFactory> metadataFactories, Object request) {
118         PrioritizedComponents<MetadataGeneratorFactory> factories =
119                 sortMetadataGeneratorFactories(session, metadataFactories);
120         List<MetadataGenerator> generators = new ArrayList<>();
121         for (PrioritizedComponent<MetadataGeneratorFactory> factory : factories.getEnabled()) {
122             MetadataGenerator generator;
123             if (request instanceof InstallRequest) {
124                 generator = factory.getComponent().newInstance(session, (InstallRequest) request);
125             } else if (request instanceof DeployRequest) {
126                 generator = factory.getComponent().newInstance(session, (DeployRequest) request);
127             } else {
128                 throw new IllegalArgumentException("Unknown request");
129             }
130             if (generator != null) {
131                 generators.add(generator);
132             }
133         }
134         return generators;
135     }
136 
137     public static List<? extends MetadataGenerator> getMetadataGenerators(
138             RepositorySystemSession session,
139             Map<String, MetadataGeneratorFactory> metadataFactories,
140             InstallRequest request) {
141         return doGetMetadataGenerators(session, metadataFactories, request);
142     }
143 
144     public static List<? extends MetadataGenerator> getMetadataGenerators(
145             RepositorySystemSession session,
146             Map<String, MetadataGeneratorFactory> metadataFactories,
147             DeployRequest request) {
148         return doGetMetadataGenerators(session, metadataFactories, request);
149     }
150 
151     public static List<Metadata> prepareMetadata(
152             List<? extends MetadataGenerator> generators, List<? extends Artifact> artifacts) {
153         List<Metadata> metadatas = new ArrayList<>();
154 
155         for (MetadataGenerator generator : generators) {
156             metadatas.addAll(generator.prepare(artifacts));
157         }
158 
159         return metadatas;
160     }
161 
162     public static List<Metadata> finishMetadata(
163             List<? extends MetadataGenerator> generators, List<? extends Artifact> artifacts) {
164         List<Metadata> metadatas = new ArrayList<>();
165 
166         for (MetadataGenerator generator : generators) {
167             metadatas.addAll(generator.finish(artifacts));
168         }
169 
170         return metadatas;
171     }
172 
173     public static <T> List<T> combine(Collection<? extends T> first, Collection<? extends T> second) {
174         List<T> result = new ArrayList<>(first.size() + second.size());
175         result.addAll(first);
176         result.addAll(second);
177         return result;
178     }
179 
180     public static int getPolicy(RepositorySystemSession session, Artifact artifact, RemoteRepository repository) {
181         ResolutionErrorPolicy rep = session.getResolutionErrorPolicy();
182         if (rep == null) {
183             return ResolutionErrorPolicy.CACHE_DISABLED;
184         }
185         return rep.getArtifactPolicy(session, new ResolutionErrorPolicyRequest<>(artifact, repository));
186     }
187 
188     public static int getPolicy(RepositorySystemSession session, Metadata metadata, RemoteRepository repository) {
189         ResolutionErrorPolicy rep = session.getResolutionErrorPolicy();
190         if (rep == null) {
191             return ResolutionErrorPolicy.CACHE_DISABLED;
192         }
193         return rep.getMetadataPolicy(session, new ResolutionErrorPolicyRequest<>(metadata, repository));
194     }
195 
196     public static void appendClassLoader(StringBuilder buffer, Object component) {
197         ClassLoader loader = component.getClass().getClassLoader();
198         if (loader != null && !loader.equals(Utils.class.getClassLoader())) {
199             buffer.append(" from ").append(loader);
200         }
201     }
202 
203     public static void checkOffline(
204             RepositorySystemSession session, OfflineController offlineController, RemoteRepository repository)
205             throws RepositoryOfflineException {
206         if (session.isOffline()) {
207             offlineController.checkOffline(session, repository);
208         }
209     }
210 }