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