From 2d777d1784eae5cc782a8492f04c9a4377c6b19a Mon Sep 17 00:00:00 2001 From: Andres Almiray Date: Tue, 13 Dec 2022 15:40:50 +0100 Subject: [PATCH] feat: Add GAV support to resolve JARs --- .../bytecode/ShowBytecodeJarProcessor.java | 12 +-- .../processors/QueryManifestJarProcessor.java | 12 +-- .../processors/ShowManifestJarProcessor.java | 12 +-- .../resolvers/GavBasedJarFileResolver.java | 84 +++++++++++++++++++ .../core/resolvers/JarFileResolvers.java | 37 ++++++++ .../services/ListServicesJarProcessor.java | 12 +-- .../services/ShowServicesJarProcessor.java | 12 +-- .../kordamp/jarviz/bundle/Messages.properties | 9 +- .../jarviz/cli/AbstractJarvizSubcommand.java | 3 + .../jarviz/cli/bytecode/BytecodeShow.java | 6 +- .../jarviz/cli/manifest/ManifestQuery.java | 7 +- .../jarviz/cli/manifest/ManifestShow.java | 7 +- .../jarviz/cli/services/ServicesList.java | 7 +- .../jarviz/cli/services/ServicesShow.java | 7 +- .../kordamp/jarviz/cli/Messages.properties | 1 + 15 files changed, 159 insertions(+), 69 deletions(-) create mode 100644 core/jarviz-core/src/main/java/org/kordamp/jarviz/core/resolvers/GavBasedJarFileResolver.java create mode 100644 core/jarviz-core/src/main/java/org/kordamp/jarviz/core/resolvers/JarFileResolvers.java diff --git a/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/bytecode/ShowBytecodeJarProcessor.java b/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/bytecode/ShowBytecodeJarProcessor.java index c1cfba1..53c52e3 100644 --- a/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/bytecode/ShowBytecodeJarProcessor.java +++ b/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/bytecode/ShowBytecodeJarProcessor.java @@ -21,12 +21,8 @@ import org.kordamp.jarviz.core.JarProcessor; import org.kordamp.jarviz.core.analyzers.QueryJarManifestAnalyzer; import org.kordamp.jarviz.core.model.BytecodeVersions; -import org.kordamp.jarviz.core.resolvers.PathBasedJarFileResolver; -import org.kordamp.jarviz.core.resolvers.UrlBasedJarFileResolver; import org.kordamp.jarviz.util.JarUtils; -import java.net.URL; -import java.nio.file.Path; import java.util.Enumeration; import java.util.Set; import java.util.TreeSet; @@ -49,12 +45,8 @@ public class ShowBytecodeJarProcessor implements JarProcessor private final JarFileResolver jarFileResolver; - public ShowBytecodeJarProcessor(Path file) { - this.jarFileResolver = new PathBasedJarFileResolver(file); - } - - public ShowBytecodeJarProcessor(Path outputDirectory, URL url) { - this.jarFileResolver = new UrlBasedJarFileResolver(outputDirectory, url); + public ShowBytecodeJarProcessor(JarFileResolver jarFileResolver) { + this.jarFileResolver = jarFileResolver; } @Override diff --git a/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/processors/QueryManifestJarProcessor.java b/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/processors/QueryManifestJarProcessor.java index 5834847..b83a523 100644 --- a/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/processors/QueryManifestJarProcessor.java +++ b/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/processors/QueryManifestJarProcessor.java @@ -20,12 +20,8 @@ import org.kordamp.jarviz.core.JarFileResolver; import org.kordamp.jarviz.core.JarProcessor; import org.kordamp.jarviz.core.analyzers.QueryJarManifestAnalyzer; -import org.kordamp.jarviz.core.resolvers.PathBasedJarFileResolver; -import org.kordamp.jarviz.core.resolvers.UrlBasedJarFileResolver; import org.kordamp.jarviz.util.JarUtils; -import java.net.URL; -import java.nio.file.Path; import java.util.Optional; import java.util.jar.JarFile; import java.util.jar.Manifest; @@ -39,12 +35,8 @@ public class QueryManifestJarProcessor implements JarProcessor> private String attributeName; private String sectionName; - public QueryManifestJarProcessor(Path file) { - this.jarFileResolver = new PathBasedJarFileResolver(file); - } - - public QueryManifestJarProcessor(Path outputDirectory, URL url) { - this.jarFileResolver = new UrlBasedJarFileResolver(outputDirectory, url); + public QueryManifestJarProcessor(JarFileResolver jarFileResolver) { + this.jarFileResolver = jarFileResolver; } public String getAttributeName() { diff --git a/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/processors/ShowManifestJarProcessor.java b/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/processors/ShowManifestJarProcessor.java index 15dcf6e..bed9264 100644 --- a/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/processors/ShowManifestJarProcessor.java +++ b/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/processors/ShowManifestJarProcessor.java @@ -19,12 +19,8 @@ import org.kordamp.jarviz.core.JarFileResolver; import org.kordamp.jarviz.core.JarProcessor; -import org.kordamp.jarviz.core.resolvers.PathBasedJarFileResolver; -import org.kordamp.jarviz.core.resolvers.UrlBasedJarFileResolver; import org.kordamp.jarviz.util.JarUtils; -import java.net.URL; -import java.nio.file.Path; import java.util.jar.JarFile; import java.util.jar.Manifest; @@ -35,12 +31,8 @@ public class ShowManifestJarProcessor implements JarProcessor { private final JarFileResolver jarFileResolver; - public ShowManifestJarProcessor(Path file) { - this.jarFileResolver = new PathBasedJarFileResolver(file); - } - - public ShowManifestJarProcessor(Path outputDirectory, URL url) { - this.jarFileResolver = new UrlBasedJarFileResolver(outputDirectory, url); + public ShowManifestJarProcessor(JarFileResolver jarFileResolver) { + this.jarFileResolver = jarFileResolver; } @Override diff --git a/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/resolvers/GavBasedJarFileResolver.java b/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/resolvers/GavBasedJarFileResolver.java new file mode 100644 index 0000000..b6dcab3 --- /dev/null +++ b/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/resolvers/GavBasedJarFileResolver.java @@ -0,0 +1,84 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright 2022 The Jarviz authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.kordamp.jarviz.core.resolvers; + +import org.kordamp.jarviz.bundle.RB; +import org.kordamp.jarviz.core.JarFileResolver; +import org.kordamp.jarviz.core.JarvizException; + +import java.io.IOException; +import java.io.InputStream; +import java.net.MalformedURLException; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.jar.JarFile; + +import static java.nio.file.StandardCopyOption.REPLACE_EXISTING; + +/** + * @author Andres Almiray + * @since 0.1.0 + */ +public class GavBasedJarFileResolver implements JarFileResolver { + private final String groupId; + private final String artifactId; + private final String version; + private final Path outputDirectory; + + public GavBasedJarFileResolver(Path outputDirectory, String gav) { + this.outputDirectory = outputDirectory; + + String[] parts = gav.split(":"); + if (parts.length == 3) { + this.groupId = parts[0].trim().replace(".", "/"); + this.artifactId = parts[1].trim(); + this.version = parts[2].trim(); + } else { + throw new JarvizException(RB.$("ERROR_INVALID_GAV", gav)); + } + } + + @Override + public JarFile resolveJarFile() { + String str = "https://repo1.maven.org/maven2/" + groupId + "/" + artifactId + "/" + version + "/" + artifactId + "-" + version + ".jar"; + + URL url = null; + try { + url = new URL(str); + } catch (MalformedURLException e) { + throw new JarvizException(RB.$("ERROR_INVALID_URL", str)); + } + + Path path = Paths.get(url.getPath()).getFileName(); + Path file = outputDirectory.resolve(path); + + try (InputStream stream = url.openStream()) { + Files.copy(stream, file, REPLACE_EXISTING); + } catch (IOException e) { + throw new JarvizException(RB.$("ERROR_DOWNLOADING_URL", url), e); + } + + try { + return new JarFile(file.toFile()); + } catch (IOException e) { + throw new JarvizException(RB.$("ERROR_OPENING_JAR", file.toAbsolutePath())); + } + } +} diff --git a/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/resolvers/JarFileResolvers.java b/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/resolvers/JarFileResolvers.java new file mode 100644 index 0000000..689f63b --- /dev/null +++ b/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/resolvers/JarFileResolvers.java @@ -0,0 +1,37 @@ +/* + * SPDX-License-Identifier: Apache-2.0 + * + * Copyright 2022 The Jarviz authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.kordamp.jarviz.core.resolvers; + +import org.kordamp.jarviz.core.JarFileResolver; + +import java.net.URL; +import java.nio.file.Path; + +import static org.kordamp.jarviz.util.StringUtils.isNotBlank; + +/** + * @author Andres Almiray + * @since 0.1.0 + */ +public class JarFileResolvers { + public static JarFileResolver createJarFileResolver(Path file, String gav, URL url, Path outputDirectory) { + if (file != null) return new PathBasedJarFileResolver(file); + if (isNotBlank(gav)) return new GavBasedJarFileResolver(outputDirectory, gav); + return new UrlBasedJarFileResolver(outputDirectory, url); + } +} \ No newline at end of file diff --git a/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/services/ListServicesJarProcessor.java b/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/services/ListServicesJarProcessor.java index a9a43f7..a837b65 100644 --- a/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/services/ListServicesJarProcessor.java +++ b/core/jarviz-core/src/main/java/org/kordamp/jarviz/core/services/ListServicesJarProcessor.java @@ -19,12 +19,8 @@ import org.kordamp.jarviz.core.JarFileResolver; import org.kordamp.jarviz.core.JarProcessor; -import org.kordamp.jarviz.core.resolvers.PathBasedJarFileResolver; -import org.kordamp.jarviz.core.resolvers.UrlBasedJarFileResolver; import java.io.IOException; -import java.net.URL; -import java.nio.file.Path; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; @@ -43,12 +39,8 @@ public class ListServicesJarProcessor implements JarProcessor { @Override protected int execute() { - ShowBytecodeJarProcessor processor = null != exclusive.file ? - new ShowBytecodeJarProcessor(exclusive.file) : - new ShowBytecodeJarProcessor(resolveOutputDirectory(), exclusive.url); + ShowBytecodeJarProcessor processor = new ShowBytecodeJarProcessor(createJarFileResolver( + exclusive.file, exclusive.gav, exclusive.url, resolveOutputDirectory())); BytecodeVersions bytecodeVersions = processor.getResult(); diff --git a/plugins/jarviz-cli/src/main/java/org/kordamp/jarviz/cli/manifest/ManifestQuery.java b/plugins/jarviz-cli/src/main/java/org/kordamp/jarviz/cli/manifest/ManifestQuery.java index e5f3c5c..81e6d95 100644 --- a/plugins/jarviz-cli/src/main/java/org/kordamp/jarviz/cli/manifest/ManifestQuery.java +++ b/plugins/jarviz-cli/src/main/java/org/kordamp/jarviz/cli/manifest/ManifestQuery.java @@ -23,6 +23,8 @@ import java.util.Optional; +import static org.kordamp.jarviz.core.resolvers.JarFileResolvers.createJarFileResolver; + /** * @author Andres Almiray * @since 0.1.0 @@ -37,9 +39,8 @@ public class ManifestQuery extends AbstractJarvizSubcommand { @Override protected int execute() { - QueryManifestJarProcessor processor = null != exclusive.file ? - new QueryManifestJarProcessor(exclusive.file) : - new QueryManifestJarProcessor(resolveOutputDirectory(), exclusive.url); + QueryManifestJarProcessor processor = new QueryManifestJarProcessor(createJarFileResolver( + exclusive.file, exclusive.gav, exclusive.url, resolveOutputDirectory())); processor.setAttributeName(attributeName); processor.setSectionName(sectionName); diff --git a/plugins/jarviz-cli/src/main/java/org/kordamp/jarviz/cli/manifest/ManifestShow.java b/plugins/jarviz-cli/src/main/java/org/kordamp/jarviz/cli/manifest/ManifestShow.java index 8f226f6..58d4fee 100644 --- a/plugins/jarviz-cli/src/main/java/org/kordamp/jarviz/cli/manifest/ManifestShow.java +++ b/plugins/jarviz-cli/src/main/java/org/kordamp/jarviz/cli/manifest/ManifestShow.java @@ -26,6 +26,8 @@ import java.io.ByteArrayOutputStream; import java.io.IOException; +import static org.kordamp.jarviz.core.resolvers.JarFileResolvers.createJarFileResolver; + /** * @author Andres Almiray * @since 0.1.0 @@ -34,9 +36,8 @@ public class ManifestShow extends AbstractJarvizSubcommand { @Override protected int execute() { - ShowManifestJarProcessor processor = null != exclusive.file ? - new ShowManifestJarProcessor(exclusive.file) : - new ShowManifestJarProcessor(resolveOutputDirectory(), exclusive.url); + ShowManifestJarProcessor processor = new ShowManifestJarProcessor(createJarFileResolver( + exclusive.file, exclusive.gav, exclusive.url, resolveOutputDirectory())); java.util.jar.Manifest manifest = processor.getResult(); if (null == manifest) return 1; diff --git a/plugins/jarviz-cli/src/main/java/org/kordamp/jarviz/cli/services/ServicesList.java b/plugins/jarviz-cli/src/main/java/org/kordamp/jarviz/cli/services/ServicesList.java index d410470..9a4bebe 100644 --- a/plugins/jarviz-cli/src/main/java/org/kordamp/jarviz/cli/services/ServicesList.java +++ b/plugins/jarviz-cli/src/main/java/org/kordamp/jarviz/cli/services/ServicesList.java @@ -24,6 +24,8 @@ import java.util.List; import java.util.Optional; +import static org.kordamp.jarviz.core.resolvers.JarFileResolvers.createJarFileResolver; + /** * @author Andres Almiray * @since 0.1.0 @@ -35,9 +37,8 @@ public class ServicesList extends AbstractJarvizSubcommand { @Override protected int execute() { - ListServicesJarProcessor processor = null != exclusive.file ? - new ListServicesJarProcessor(exclusive.file) : - new ListServicesJarProcessor(resolveOutputDirectory(), exclusive.url); + ListServicesJarProcessor processor = new ListServicesJarProcessor(createJarFileResolver( + exclusive.file, exclusive.gav, exclusive.url, resolveOutputDirectory())); Optional> services = processor.getResult(); if (services.isPresent()) { diff --git a/plugins/jarviz-cli/src/main/java/org/kordamp/jarviz/cli/services/ServicesShow.java b/plugins/jarviz-cli/src/main/java/org/kordamp/jarviz/cli/services/ServicesShow.java index 1ab56cf..690f4c7 100644 --- a/plugins/jarviz-cli/src/main/java/org/kordamp/jarviz/cli/services/ServicesShow.java +++ b/plugins/jarviz-cli/src/main/java/org/kordamp/jarviz/cli/services/ServicesShow.java @@ -24,6 +24,8 @@ import java.util.List; import java.util.Optional; +import static org.kordamp.jarviz.core.resolvers.JarFileResolvers.createJarFileResolver; + /** * @author Andres Almiray * @since 0.1.0 @@ -38,9 +40,8 @@ public class ServicesShow extends AbstractJarvizSubcommand { @Override protected int execute() { - ShowServicesJarProcessor processor = null != exclusive.file ? - new ShowServicesJarProcessor(exclusive.file) : - new ShowServicesJarProcessor(resolveOutputDirectory(), exclusive.url); + ShowServicesJarProcessor processor = new ShowServicesJarProcessor(createJarFileResolver( + exclusive.file, exclusive.gav, exclusive.url, resolveOutputDirectory())); processor.setServiceName(serviceName); Optional> services = processor.getResult(); diff --git a/plugins/jarviz-cli/src/main/resources/org/kordamp/jarviz/cli/Messages.properties b/plugins/jarviz-cli/src/main/resources/org/kordamp/jarviz/cli/Messages.properties index 23af61e..d518425 100644 --- a/plugins/jarviz-cli/src/main/resources/org/kordamp/jarviz/cli/Messages.properties +++ b/plugins/jarviz-cli/src/main/resources/org/kordamp/jarviz/cli/Messages.properties @@ -30,6 +30,7 @@ help = Show this help message and exit. version = Print version information and exit. output-directory = Output directory. file = JAR file to be analyzed (local). +gav = JAR file to be analyzed (GAV coordinates). url = JAR file to be analyzed (remote). release = Specifies the target Java SE release. attribute-name = Name of a manifest attribute.