|
| 1 | +/* |
| 2 | + * Copyright 2021 - 2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.openrewrite.maven; |
| 17 | + |
| 18 | +import lombok.Value; |
| 19 | +import org.openrewrite.internal.lang.Nullable; |
| 20 | +import org.openrewrite.maven.cache.MavenPomCache; |
| 21 | +import org.openrewrite.maven.tree.*; |
| 22 | + |
| 23 | +import java.net.URI; |
| 24 | +import java.nio.file.Path; |
| 25 | +import java.util.Optional; |
| 26 | + |
| 27 | +/** |
| 28 | + * A `MavenPomCache` that uses local filesystem, e.g. Maven repository, to store and retrieve pom files. |
| 29 | + * |
| 30 | + * The cache directory is optionally configurable. If not set local Maven repository is used. |
| 31 | + * |
| 32 | + * The `FilesystemPomCache` must be provided through `ExecutionContext`. |
| 33 | + * |
| 34 | + * [source,java] |
| 35 | + * .... |
| 36 | + * ExecutionContext executionContext = ... |
| 37 | + * MavenExecutionContextView.view(executionContext).setPomCache(new LocalMavenPomCache()); |
| 38 | + * .... |
| 39 | + * |
| 40 | + * @author Fabian Krüger |
| 41 | + */ |
| 42 | +public class FilesystemMavenPomCache implements MavenPomCache { |
| 43 | + |
| 44 | + private final Cache<MetadataKey, Optional<MavenMetadata>> mavenMetadataCache; |
| 45 | + private Cache<ResolvedGroupArtifactVersion, Optional<Pom>> pomCache; |
| 46 | + private final Cache<MavenRepository, Optional<MavenRepository>> repositoryCache; |
| 47 | + private final Cache<ResolvedGroupArtifactVersion, ResolvedPom> dependencyCache; |
| 48 | + |
| 49 | + @Value |
| 50 | + public static class MetadataKey { |
| 51 | + URI repository; |
| 52 | + GroupArtifactVersion gav; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates `FileSystemPomCache` using local Maven repository `${user.home}/.m2/repository` as cache dir. |
| 57 | + */ |
| 58 | + public FilesystemMavenPomCache() { |
| 59 | + this(Path.of(System.getProperty("user.home")).resolve(".m2").resolve("repository")); |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + /** |
| 64 | + * Creates a `FileSystemPomCache` using the provided `cacheDir`. |
| 65 | + * |
| 66 | + * @throws IllegalArgumentException if cacheDir does not exist. |
| 67 | + */ |
| 68 | + public FilesystemMavenPomCache(Path cacheDir) { |
| 69 | + if(!cacheDir.toFile().exists()) { |
| 70 | + throw new IllegalArgumentException("The given cache dir '%s' does not exist, Please provide an existing dir.".formatted(cacheDir)); |
| 71 | + } |
| 72 | + pomCache = new FilesystemPomCache(cacheDir); |
| 73 | + mavenMetadataCache = new MavenMetadataCache(); |
| 74 | + repositoryCache = new RepositoryCache(); |
| 75 | + dependencyCache = new DependencyCache(); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public @Nullable ResolvedPom getResolvedDependencyPom(ResolvedGroupArtifactVersion dependency) { |
| 80 | + return dependencyCache.getIfPresent(dependency); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void putResolvedDependencyPom(ResolvedGroupArtifactVersion dependency, ResolvedPom resolved) { |
| 85 | + dependencyCache.put(dependency, resolved.deduplicate()); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public @Nullable Optional<MavenMetadata> getMavenMetadata(URI repo, GroupArtifactVersion gav) { |
| 90 | + return mavenMetadataCache.getIfPresent(new MetadataKey(repo, gav)); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public void putMavenMetadata(URI repo, GroupArtifactVersion gav, @Nullable MavenMetadata metadata) { |
| 95 | + mavenMetadataCache.put(new MetadataKey(repo, gav), Optional.ofNullable(metadata)); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public @Nullable Optional<Pom> getPom(ResolvedGroupArtifactVersion gav) throws MavenDownloadingException { |
| 100 | + return pomCache.getIfPresent(gav); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void putPom(ResolvedGroupArtifactVersion gav, @Nullable Pom pom) { |
| 105 | + pomCache.put(gav, Optional.ofNullable(pom)); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public @Nullable Optional<MavenRepository> getNormalizedRepository(MavenRepository repository) { |
| 110 | + return repositoryCache.getIfPresent(repository); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public void putNormalizedRepository(MavenRepository repository, MavenRepository normalized) { |
| 115 | + repositoryCache.put(repository, Optional.ofNullable(normalized)); |
| 116 | + } |
| 117 | + |
| 118 | + private class MavenMetadataCache implements Cache<MetadataKey, Optional<MavenMetadata>> { |
| 119 | + @Override |
| 120 | + public void put(MetadataKey key, Optional<MavenMetadata> value) { |
| 121 | + |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public Optional<MavenMetadata> getIfPresent(MetadataKey key) { |
| 126 | + return Optional.empty(); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private class RepositoryCache implements Cache<MavenRepository, Optional<MavenRepository>> { |
| 131 | + @Override |
| 132 | + public void put(MavenRepository key, Optional<MavenRepository> value) { |
| 133 | + |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + public Optional<MavenRepository> getIfPresent(MavenRepository key) { |
| 138 | + return Optional.empty(); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private class DependencyCache implements Cache<ResolvedGroupArtifactVersion, ResolvedPom> { |
| 143 | + @Override |
| 144 | + public void put(ResolvedGroupArtifactVersion key, ResolvedPom value) { |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public ResolvedPom getIfPresent(ResolvedGroupArtifactVersion key) { |
| 150 | + return null; |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments