Skip to content

Commit

Permalink
[MSHARED-1424] Code cleanups
Browse files Browse the repository at this point in the history
- don't use deprecated methods
- cleanups warning reported by IDE
  • Loading branch information
slawekjaranowski committed Aug 15, 2024
1 parent 03664cf commit 74497e9
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 53 deletions.
6 changes: 5 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@
<artifactId>maven-shared-utils</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.16.1</version>
</dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
Expand Down Expand Up @@ -96,7 +101,6 @@
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.10.3</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import org.apache.maven.shared.utils.cli.StreamConsumer;
import org.apache.maven.shared.utils.cli.javatool.AbstractJavaTool;
import org.apache.maven.shared.utils.cli.javatool.JavaToolException;
import org.slf4j.LoggerFactory;

/**
* Default implementation of component {@link JarSigner}.
Expand All @@ -35,20 +34,22 @@
@Named
public class DefaultJarSigner extends AbstractJavaTool<JarSignerRequest> implements JarSigner {

/**
* default constructor
*/
public DefaultJarSigner() {
super("jarsigner");
}

@Override
protected Commandline createCommandLine(JarSignerRequest request, String javaToolFile) throws JavaToolException {
JarSignerCommandLineBuilder cliBuilder = new JarSignerCommandLineBuilder();
cliBuilder.setLogger(LoggerFactory.getLogger(this.getClass()));
cliBuilder.setJarSignerFile(javaToolFile);
try {
Commandline cli = cliBuilder.build(request);
if (request.isVerbose()) {
getLogger().info(cli.toString());
} else {
} else if (getLogger().isDebugEnabled()) {
getLogger().debug(cli.toString());
}
return cli;
Expand All @@ -57,24 +58,19 @@ protected Commandline createCommandLine(JarSignerRequest request, String javaToo
}
}

@Override
protected StreamConsumer createSystemOutStreamConsumer(JarSignerRequest request) {
StreamConsumer systemOut = request.getSystemOutStreamConsumer();

if (systemOut == null) {

final boolean verbose = request.isVerbose();

systemOut = new StreamConsumer() {

/**
* {@inheritDoc}
*/
public void consumeLine(final String line) {
if (verbose) {
getLogger().info(line);
} else {
getLogger().debug(line);
}
systemOut = line -> {
if (verbose) {
getLogger().info(line);
} else {
getLogger().debug(line);
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@
package org.apache.maven.shared.jarsigner;

import java.io.File;
import java.io.IOException;

import org.apache.maven.shared.utils.StringUtils;
import org.apache.maven.shared.utils.cli.Arg;
import org.apache.maven.shared.utils.cli.Commandline;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* To build the command line for a given {@link JarSignerRequest}.
Expand All @@ -35,16 +33,11 @@
*/
public class JarSignerCommandLineBuilder {

private Logger logger = LoggerFactory.getLogger(JarSignerCommandLineBuilder.class);

private String jarSignerFile;

public Commandline build(JarSignerRequest request) throws CommandLineConfigurationException {
try {
checkRequiredState();
} catch (IOException e) {
throw new CommandLineConfigurationException(e.getMessage(), e);
}

checkRequiredState();

Commandline cli = new Commandline();

Expand Down Expand Up @@ -126,21 +119,22 @@ public Commandline build(JarSignerRequest request) throws CommandLineConfigurati
return cli;
}

/**
* @deprecated logger is not used by this class
* @param logger a logger
*/
@Deprecated
public void setLogger(Logger logger) {
this.logger = logger;
// not used
}

public void setJarSignerFile(String jarSignerFile) {
this.jarSignerFile = jarSignerFile;
}

protected void checkRequiredState() throws IOException {
if (logger == null) {
throw new IllegalStateException("A logger instance is required.");
}

protected void checkRequiredState() throws CommandLineConfigurationException {
if (jarSignerFile == null) {
throw new IllegalStateException("A jarSigner file is required.");
throw new CommandLineConfigurationException("A jarSigner file is required.");
}
}

Expand Down Expand Up @@ -197,7 +191,7 @@ protected void build(JarSignerSignRequest request, Commandline cli) {
}
}

protected void build(JarSignerVerifyRequest request, Commandline cli) throws CommandLineConfigurationException {
protected void build(JarSignerVerifyRequest request, Commandline cli) {
cli.createArg(true).setValue("-verify");

if (request.isCerts()) {
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/org/apache/maven/shared/jarsigner/JarSignerUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import org.apache.maven.shared.utils.io.FileUtils;
import org.apache.maven.shared.utils.io.IOUtil;
import org.apache.commons.io.IOUtils;

import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;

/**
* Useful methods.
Expand All @@ -56,7 +57,7 @@ private JarSignerUtil() {
public static boolean isZipFile(final File file) {
boolean result = false;

try (ZipInputStream zis = new ZipInputStream(new FileInputStream(file))) {
try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(file.toPath()))) {
result = zis.getNextEntry() != null;
} catch (Exception e) {
// ignore, will fail below
Expand All @@ -74,11 +75,11 @@ public static boolean isZipFile(final File file) {
*/
public static void unsignArchive(File jarFile) throws IOException {

File unsignedFile = new File(jarFile.getAbsolutePath() + ".unsigned");
Path unsignedPath = new File(jarFile.getAbsolutePath() + ".unsigned").toPath();

try (ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(jarFile)));
try (ZipInputStream zis = new ZipInputStream(new BufferedInputStream(Files.newInputStream(jarFile.toPath())));
ZipOutputStream zos =
new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(unsignedFile)))) {
new ZipOutputStream(new BufferedOutputStream(Files.newOutputStream(unsignedPath)))) {
for (ZipEntry ze = zis.getNextEntry(); ze != null; ze = zis.getNextEntry()) {
if (isSignatureFile(ze.getName())) {
continue;
Expand All @@ -97,11 +98,10 @@ public static void unsignArchive(File jarFile) throws IOException {
continue;
}

IOUtil.copy(zis, zos);
IOUtils.copy(zis, zos);
}
}

FileUtils.rename(unsignedFile, jarFile);
Files.move(unsignedPath, jarFile.toPath(), REPLACE_EXISTING);
}

/**
Expand Down Expand Up @@ -152,7 +152,7 @@ public static boolean isArchiveSigned(final File jarFile) throws IOException {
throw new NullPointerException("jarFile");
}

try (ZipInputStream in = new ZipInputStream(new BufferedInputStream(new FileInputStream(jarFile)))) {
try (ZipInputStream in = new ZipInputStream(new BufferedInputStream(Files.newInputStream(jarFile.toPath())))) {
boolean signed = false;

for (ZipEntry ze = in.getNextEntry(); ze != null; ze = in.getNextEntry()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,33 @@

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import org.apache.maven.shared.utils.io.FileUtils;
import org.apache.commons.io.FileUtils;
import org.eclipse.sisu.launch.InjectedTest;

public abstract class AbstractJarSignerTest extends InjectedTest {

protected File prepareTestJar(String filename) throws IOException {
File file = new File("src/test", filename);
File target = new File("target", filename);
Path source = Paths.get("src", "test", filename);
Path target = Paths.get("target", filename);

if (target.exists()) {
FileUtils.forceDelete(target);
if (Files.exists(target)) {
FileUtils.forceDelete(target.toFile());
}

FileUtils.copyFile(file, target);
Files.createDirectories(target.getParent());
Files.copy(
source,
target,
StandardCopyOption.REPLACE_EXISTING,
StandardCopyOption.COPY_ATTRIBUTES,
LinkOption.NOFOLLOW_LINKS);

return target;
return target.toFile();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
* @author Tony Chemit
* @since 1.1
*/
public class JarSignerUtilTest extends AbstractJarSignerTest {
class JarSignerUtilTest extends AbstractJarSignerTest {

@Test
public void testUnsignArchive() throws Exception {
void testUnsignArchive() throws Exception {

File target = prepareTestJar("javax.persistence_2.0.5.v201212031355.jar");

Expand Down

0 comments on commit 74497e9

Please sign in to comment.