|
| 1 | +/* |
| 2 | + * Copyright (c) 2023-2024 Maveniverse Org. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-v20.html |
| 7 | + */ |
| 8 | +package eu.maveniverse.maven.toolbox.shared; |
| 9 | + |
| 10 | +import static java.util.Objects.requireNonNull; |
| 11 | + |
| 12 | +import eu.maveniverse.maven.toolbox.shared.internal.ArtifactMapper; |
| 13 | +import eu.maveniverse.maven.toolbox.shared.internal.ArtifactMatcher; |
| 14 | +import eu.maveniverse.maven.toolbox.shared.internal.ArtifactNameMapper; |
| 15 | +import java.io.IOException; |
| 16 | +import java.io.UncheckedIOException; |
| 17 | +import java.nio.file.Files; |
| 18 | +import java.nio.file.Path; |
| 19 | +import java.nio.file.StandardCopyOption; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.HashSet; |
| 22 | +import java.util.function.Consumer; |
| 23 | +import org.eclipse.aether.artifact.Artifact; |
| 24 | + |
| 25 | +/** |
| 26 | + * Construction to accept collection of artifacts, for example like a filesystem directory. |
| 27 | + */ |
| 28 | +public final class DirectorySink implements Consumer<Collection<Artifact>> { |
| 29 | + |
| 30 | + public static DirectorySink flat(Output output, Path path) throws IOException { |
| 31 | + return new DirectorySink(output, path); |
| 32 | + } |
| 33 | + |
| 34 | + private final Output output; |
| 35 | + private final Path directory; |
| 36 | + private final ArtifactMatcher artifactMatcher; |
| 37 | + private final ArtifactMapper artifactMapper; |
| 38 | + private final ArtifactNameMapper artifactNameMapper; |
| 39 | + private final boolean allowOverwrite; |
| 40 | + private final HashSet<Path> writtenPaths; |
| 41 | + |
| 42 | + private DirectorySink(Output output, Path directory) throws IOException { |
| 43 | + this.output = requireNonNull(output, "output"); |
| 44 | + this.directory = requireNonNull(directory, "directory"); |
| 45 | + if (Files.exists(directory) && !Files.isDirectory(directory)) { |
| 46 | + throw new IllegalArgumentException("directory must not exists, or be a directory"); |
| 47 | + } |
| 48 | + if (!Files.exists(directory)) { |
| 49 | + Files.createDirectories(directory); |
| 50 | + } |
| 51 | + |
| 52 | + this.artifactMatcher = ArtifactMatcher.any(); |
| 53 | + this.artifactMapper = ArtifactMapper.identity(); |
| 54 | + this.artifactNameMapper = ArtifactNameMapper.ACVE(); |
| 55 | + this.allowOverwrite = false; |
| 56 | + this.writtenPaths = new HashSet<>(); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public void accept(Collection<Artifact> artifacts) { |
| 61 | + requireNonNull(artifacts, "artifacts"); |
| 62 | + output.verbose("Copying {} artifacts to directory {}", artifacts.size(), directory); |
| 63 | + try { |
| 64 | + for (Artifact artifact : artifacts) { |
| 65 | + accept(artifact); |
| 66 | + } |
| 67 | + } catch (IOException e) { |
| 68 | + cleanup(artifacts, e); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + private void accept(Artifact artifact) throws IOException { |
| 73 | + output.verbose("Artifact {} processed", artifact); |
| 74 | + if (artifactMatcher.test(artifact)) { |
| 75 | + output.verbose(" matched"); |
| 76 | + String name = artifactNameMapper.map(artifactMapper.map(artifact)); |
| 77 | + output.verbose(" mapped to name {}", name); |
| 78 | + Path target = directory.resolve(name); |
| 79 | + if (!writtenPaths.add(target) && !allowOverwrite) { |
| 80 | + throw new IOException("Overwrite prevented: check mappings"); |
| 81 | + } |
| 82 | + output.verbose(" copied to file {}", target); |
| 83 | + Files.copy( |
| 84 | + artifact.getFile().toPath(), |
| 85 | + target, |
| 86 | + StandardCopyOption.REPLACE_EXISTING, |
| 87 | + StandardCopyOption.COPY_ATTRIBUTES); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private void cleanup(Collection<Artifact> artifacts, IOException e) { |
| 92 | + output.error("IO error happened, cleaning up", e); |
| 93 | + writtenPaths.forEach(p -> { |
| 94 | + try { |
| 95 | + Files.deleteIfExists(p); |
| 96 | + } catch (IOException ex) { |
| 97 | + // ignore |
| 98 | + } |
| 99 | + }); |
| 100 | + throw new UncheckedIOException(e); |
| 101 | + } |
| 102 | +} |
0 commit comments