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 javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.nio.file.Path;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.IdentityHashMap;
31 import java.util.List;
32 import java.util.ListIterator;
33 import java.util.Map;
34
35 import org.eclipse.aether.RepositoryEvent;
36 import org.eclipse.aether.RepositoryEvent.EventType;
37 import org.eclipse.aether.RepositorySystemSession;
38 import org.eclipse.aether.RequestTrace;
39 import org.eclipse.aether.SyncContext;
40 import org.eclipse.aether.artifact.Artifact;
41 import org.eclipse.aether.impl.Installer;
42 import org.eclipse.aether.impl.MetadataGenerator;
43 import org.eclipse.aether.impl.MetadataGeneratorFactory;
44 import org.eclipse.aether.impl.RepositoryEventDispatcher;
45 import org.eclipse.aether.installation.InstallRequest;
46 import org.eclipse.aether.installation.InstallResult;
47 import org.eclipse.aether.installation.InstallationException;
48 import org.eclipse.aether.metadata.MergeableMetadata;
49 import org.eclipse.aether.metadata.Metadata;
50 import org.eclipse.aether.repository.LocalArtifactRegistration;
51 import org.eclipse.aether.repository.LocalMetadataRegistration;
52 import org.eclipse.aether.repository.LocalRepositoryManager;
53 import org.eclipse.aether.spi.artifact.generator.ArtifactGenerator;
54 import org.eclipse.aether.spi.artifact.generator.ArtifactGeneratorFactory;
55 import org.eclipse.aether.spi.io.PathProcessor;
56 import org.eclipse.aether.spi.synccontext.SyncContextFactory;
57 import org.slf4j.Logger;
58 import org.slf4j.LoggerFactory;
59
60 import static java.util.Objects.requireNonNull;
61
62
63
64 @Singleton
65 @Named
66 public class DefaultInstaller implements Installer {
67 private final Logger logger = LoggerFactory.getLogger(getClass());
68
69 private final PathProcessor pathProcessor;
70
71 private final RepositoryEventDispatcher repositoryEventDispatcher;
72
73 private final Map<String, ArtifactGeneratorFactory> artifactFactories;
74
75 private final Map<String, MetadataGeneratorFactory> metadataFactories;
76
77 private final SyncContextFactory syncContextFactory;
78
79 @Inject
80 public DefaultInstaller(
81 PathProcessor pathProcessor,
82 RepositoryEventDispatcher repositoryEventDispatcher,
83 Map<String, ArtifactGeneratorFactory> artifactFactories,
84 Map<String, MetadataGeneratorFactory> metadataFactories,
85 SyncContextFactory syncContextFactory) {
86 this.pathProcessor = requireNonNull(pathProcessor, "path processor cannot be null");
87 this.repositoryEventDispatcher =
88 requireNonNull(repositoryEventDispatcher, "repository event dispatcher cannot be null");
89 this.artifactFactories = Collections.unmodifiableMap(artifactFactories);
90 this.metadataFactories = Collections.unmodifiableMap(metadataFactories);
91 this.syncContextFactory = requireNonNull(syncContextFactory, "sync context factory cannot be null");
92 }
93
94 @Override
95 public InstallResult install(RepositorySystemSession session, InstallRequest request) throws InstallationException {
96 requireNonNull(session, "session cannot be null");
97 requireNonNull(request, "request cannot be null");
98 try (SyncContext syncContext = syncContextFactory.newInstance(session, false)) {
99 return install(syncContext, session, request);
100 }
101 }
102
103 private InstallResult install(SyncContext syncContext, RepositorySystemSession session, InstallRequest request)
104 throws InstallationException {
105 InstallResult result = new InstallResult(request);
106
107 RequestTrace trace = RequestTrace.newChild(request.getTrace(), request);
108
109 List<Artifact> artifacts = new ArrayList<>(request.getArtifacts());
110 List<? extends ArtifactGenerator> artifactGenerators =
111 Utils.getArtifactGenerators(session, artifactFactories, request);
112 try {
113 List<Artifact> generatedArtifacts = new ArrayList<>();
114 for (ArtifactGenerator artifactGenerator : artifactGenerators) {
115 Collection<? extends Artifact> generated = artifactGenerator.generate(generatedArtifacts);
116 for (Artifact generatedArtifact : generated) {
117 Map<String, String> properties = new HashMap<>(generatedArtifact.getProperties());
118 properties.put(
119 ArtifactGeneratorFactory.ARTIFACT_GENERATOR_ID,
120 requireNonNull(artifactGenerator.generatorId(), "generatorId"));
121 Artifact ga = generatedArtifact.setProperties(properties);
122 generatedArtifacts.add(ga);
123 }
124 }
125 artifacts.addAll(generatedArtifacts);
126
127 List<? extends MetadataGenerator> metadataGenerators =
128 Utils.getMetadataGenerators(session, metadataFactories, request);
129
130 IdentityHashMap<Metadata, Object> processedMetadata = new IdentityHashMap<>();
131
132 List<Metadata> metadatas = Utils.prepareMetadata(metadataGenerators, artifacts);
133
134 syncContext.acquire(artifacts, Utils.combine(request.getMetadata(), metadatas));
135
136 for (Metadata metadata : metadatas) {
137 install(session, trace, metadata);
138 processedMetadata.put(metadata, null);
139 result.addMetadata(metadata);
140 }
141
142 for (ListIterator<Artifact> iterator = artifacts.listIterator(); iterator.hasNext(); ) {
143 Artifact artifact = iterator.next();
144
145 for (MetadataGenerator generator : metadataGenerators) {
146 artifact = generator.transformArtifact(artifact);
147 }
148
149 iterator.set(artifact);
150
151 install(session, trace, artifact);
152 if (artifact.getProperty(ArtifactGeneratorFactory.ARTIFACT_GENERATOR_ID, null) == null) {
153 result.addArtifact(artifact);
154 }
155 }
156
157 metadatas = Utils.finishMetadata(metadataGenerators, artifacts);
158
159 syncContext.acquire(null, metadatas);
160
161 for (Metadata metadata : metadatas) {
162 install(session, trace, metadata);
163 processedMetadata.put(metadata, null);
164 result.addMetadata(metadata);
165 }
166
167 for (Metadata metadata : request.getMetadata()) {
168 if (!processedMetadata.containsKey(metadata)) {
169 install(session, trace, metadata);
170 result.addMetadata(metadata);
171 }
172 }
173
174 return result;
175 } finally {
176 for (ArtifactGenerator artifactGenerator : artifactGenerators) {
177 try {
178 artifactGenerator.close();
179 } catch (Exception e) {
180 logger.warn("ArtifactGenerator close failure: {}", artifactGenerator.generatorId(), e);
181 }
182 }
183 }
184 }
185
186 private void install(RepositorySystemSession session, RequestTrace trace, Artifact artifact)
187 throws InstallationException {
188 final LocalRepositoryManager lrm = session.getLocalRepositoryManager();
189 final Path srcPath = artifact.getPath();
190 final Path dstPath = lrm.getRepository().getBasePath().resolve(lrm.getPathForLocalArtifact(artifact));
191
192 artifactInstalling(session, trace, artifact, dstPath);
193
194 Exception exception = null;
195 try {
196 if (dstPath.equals(srcPath)) {
197 throw new IllegalStateException("cannot install " + dstPath + " to same path");
198 }
199
200 pathProcessor.copyWithTimestamp(srcPath, dstPath);
201 lrm.add(session, new LocalArtifactRegistration(artifact));
202 } catch (Exception e) {
203 exception = e;
204 throw new InstallationException("Failed to install artifact " + artifact + ": " + e.getMessage(), e);
205 } finally {
206 artifactInstalled(session, trace, artifact, dstPath, exception);
207 }
208 }
209
210 private void install(RepositorySystemSession session, RequestTrace trace, Metadata metadata)
211 throws InstallationException {
212 LocalRepositoryManager lrm = session.getLocalRepositoryManager();
213
214 Path dstPath = lrm.getRepository().getBasePath().resolve(lrm.getPathForLocalMetadata(metadata));
215
216 metadataInstalling(session, trace, metadata, dstPath);
217
218 Exception exception = null;
219 try {
220 if (metadata instanceof MergeableMetadata) {
221 ((MergeableMetadata) metadata).merge(dstPath, dstPath);
222 } else {
223 if (dstPath.equals(metadata.getPath())) {
224 throw new IllegalStateException("cannot install " + dstPath + " to same path");
225 }
226 pathProcessor.copy(metadata.getPath(), dstPath);
227 }
228
229 lrm.add(session, new LocalMetadataRegistration(metadata));
230 } catch (Exception e) {
231 exception = e;
232 throw new InstallationException("Failed to install metadata " + metadata + ": " + e.getMessage(), e);
233 } finally {
234 metadataInstalled(session, trace, metadata, dstPath, exception);
235 }
236 }
237
238 private void artifactInstalling(
239 RepositorySystemSession session, RequestTrace trace, Artifact artifact, Path dstPath) {
240 RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.ARTIFACT_INSTALLING);
241 event.setTrace(trace);
242 event.setArtifact(artifact);
243 event.setRepository(session.getLocalRepositoryManager().getRepository());
244 event.setPath(dstPath);
245
246 repositoryEventDispatcher.dispatch(event.build());
247 }
248
249 private void artifactInstalled(
250 RepositorySystemSession session, RequestTrace trace, Artifact artifact, Path dstPath, Exception exception) {
251 RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.ARTIFACT_INSTALLED);
252 event.setTrace(trace);
253 event.setArtifact(artifact);
254 event.setRepository(session.getLocalRepositoryManager().getRepository());
255 event.setPath(dstPath);
256 event.setException(exception);
257
258 repositoryEventDispatcher.dispatch(event.build());
259 }
260
261 private void metadataInstalling(
262 RepositorySystemSession session, RequestTrace trace, Metadata metadata, Path dstPath) {
263 RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.METADATA_INSTALLING);
264 event.setTrace(trace);
265 event.setMetadata(metadata);
266 event.setRepository(session.getLocalRepositoryManager().getRepository());
267 event.setPath(dstPath);
268
269 repositoryEventDispatcher.dispatch(event.build());
270 }
271
272 private void metadataInstalled(
273 RepositorySystemSession session, RequestTrace trace, Metadata metadata, Path dstPath, Exception exception) {
274 RepositoryEvent.Builder event = new RepositoryEvent.Builder(session, EventType.METADATA_INSTALLED);
275 event.setTrace(trace);
276 event.setMetadata(metadata);
277 event.setRepository(session.getLocalRepositoryManager().getRepository());
278 event.setPath(dstPath);
279 event.setException(exception);
280
281 repositoryEventDispatcher.dispatch(event.build());
282 }
283 }