Skip to content

Commit

Permalink
feat: Add GAV support to resolve JARs
Browse files Browse the repository at this point in the history
  • Loading branch information
aalmiray committed Dec 13, 2022
1 parent ff6256e commit 2d777d1
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -49,12 +45,8 @@ public class ShowBytecodeJarProcessor implements JarProcessor<BytecodeVersions>

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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -39,12 +35,8 @@ public class QueryManifestJarProcessor implements JarProcessor<Optional<String>>
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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -35,12 +31,8 @@
public class ShowManifestJarProcessor implements JarProcessor<Manifest> {
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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()));
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,12 +39,8 @@ public class ListServicesJarProcessor implements JarProcessor<Optional<List<Stri
private final JarFileResolver jarFileResolver;
private Integer release;

public ListServicesJarProcessor(Path file) {
this.jarFileResolver = new PathBasedJarFileResolver(file);
}

public ListServicesJarProcessor(Path outputDirectory, URL url) {
this.jarFileResolver = new UrlBasedJarFileResolver(outputDirectory, url);
public ListServicesJarProcessor(JarFileResolver jarFileResolver) {
this.jarFileResolver = jarFileResolver;
}

public Integer getRelease() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,9 @@
import org.apache.commons.io.IOUtils;
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.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
Expand All @@ -49,12 +45,8 @@ public class ShowServicesJarProcessor implements JarProcessor<Optional<List<Stri
private Integer release;
private String serviceName;

public ShowServicesJarProcessor(Path file) {
this.jarFileResolver = new PathBasedJarFileResolver(file);
}

public ShowServicesJarProcessor(Path outputDirectory, URL url) {
this.jarFileResolver = new UrlBasedJarFileResolver(outputDirectory, url);
public ShowServicesJarProcessor(JarFileResolver jarFileResolver) {
this.jarFileResolver = jarFileResolver;
}

public Integer getRelease() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ ERROR_PATH_DOES_NOT_EXIST = [JARVIZ-000] Path does not exist: {}
ERROR_PATH_IS_NOT_A_FILE = [JARVIZ-001] Path is not a file: {}
ERROR_PATH_IS_NOT_READABLE = [JARVIZ-002] Path is not readable: {}
ERROR_PATH_IS_NOT_JAR = [JARVIZ-003] Path is not a JAR file: {}
ERROR_OPENING_JAR = [JARVIZ-004] Error while opening JAR file: {}
ERROR_DOWNLOADING_URL = [JARVIZ-005] Error while downloading URL: {}
ERROR_INVALID_GAV = [JARVIZ-004] Invalid GAV coordinates: {}
ERROR_INVALID_URL = [JARVIZ-005] Invalid URL: {}
ERROR_OPENING_JAR = [JARVIZ-006] Error while opening JAR file: {}
ERROR_DOWNLOADING_URL = [JARVIZ-007] Error while downloading URL: {}
ERROR_READING_JAR_MANIFEST = [JARVIZ-010] Error reading manifest from {}
ERROR_READING_JAR_ENTRIES = [JARVIZ-011] Error reading entries from {}
ERROR_READING_JAR_ENTRY = [JARVIZ-012] Error reading entry {} from {}
ERROR_READING_JAR_ENTRY = [JARVIZ-011] Error reading entry {} from {}
ERROR_CREATE_DIRECTORY = [JARVIZ-900] Could not create directory {}
ERROR_UNEXPECTED_WRITE = [JARVIZ-901] Unexpected error when writing to out

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public static class Exclusive {
@CommandLine.Option(names = {"--file"})
public Path file;

@CommandLine.Option(names = {"--gav"})
public String gav;

@CommandLine.Option(names = {"--url"})
public URL url;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.Set;

import static java.util.stream.Collectors.joining;
import static org.kordamp.jarviz.core.resolvers.JarFileResolvers.createJarFileResolver;

/**
* @author Andres Almiray
Expand All @@ -46,9 +47,8 @@ public class BytecodeShow extends AbstractJarvizSubcommand<Bytecode> {

@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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import java.util.Optional;

import static org.kordamp.jarviz.core.resolvers.JarFileResolvers.createJarFileResolver;

/**
* @author Andres Almiray
* @since 0.1.0
Expand All @@ -37,9 +39,8 @@ public class ManifestQuery extends AbstractJarvizSubcommand<Manifest> {

@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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -34,9 +36,8 @@
public class ManifestShow extends AbstractJarvizSubcommand<Manifest> {
@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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,9 +37,8 @@ public class ServicesList extends AbstractJarvizSubcommand<Services> {

@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<List<String>> services = processor.getResult();
if (services.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,9 +40,8 @@ public class ServicesShow extends AbstractJarvizSubcommand<Services> {

@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<List<String>> services = processor.getResult();
Expand Down
Loading

0 comments on commit 2d777d1

Please sign in to comment.