From 360e54320f83cc6fbd4a479e2a97ad036fc32f2a Mon Sep 17 00:00:00 2001 From: Valeriy Kucherenko Date: Fri, 24 Jul 2020 22:07:34 +0300 Subject: [PATCH] Fix for #377 and minor fixes found by Sonar lint (#388) --- .../PropertiesPluginDescriptorFinder.java | 22 ++++++++------ .../pf4j/ServiceProviderExtensionFinder.java | 7 ++++- .../org/pf4j/processor/ExtensionStorage.java | 18 +++++------- .../processor/LegacyExtensionStorage.java | 17 +++++------ .../ServiceProviderExtensionStorage.java | 22 +++++++------- .../main/java/org/pf4j/util/FileUtils.java | 29 +++++++++++-------- .../test/java/org/pf4j/plugin/PluginJar.java | 5 ++-- 7 files changed, 65 insertions(+), 55 deletions(-) diff --git a/pf4j/src/main/java/org/pf4j/PropertiesPluginDescriptorFinder.java b/pf4j/src/main/java/org/pf4j/PropertiesPluginDescriptorFinder.java index 6789a5f..303b662 100644 --- a/pf4j/src/main/java/org/pf4j/PropertiesPluginDescriptorFinder.java +++ b/pf4j/src/main/java/org/pf4j/PropertiesPluginDescriptorFinder.java @@ -75,16 +75,20 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder throw new PluginRuntimeException("Cannot find the properties path"); } - log.debug("Lookup plugin descriptor in '{}'", propertiesPath); - if (Files.notExists(propertiesPath)) { - throw new PluginRuntimeException("Cannot find '{}' path", propertiesPath); - } - Properties properties = new Properties(); - try (InputStream input = Files.newInputStream(propertiesPath)) { - properties.load(input); - } catch (IOException e) { - throw new PluginRuntimeException(e); + try { + log.debug("Lookup plugin descriptor in '{}'", propertiesPath); + if (Files.notExists(propertiesPath)) { + throw new PluginRuntimeException("Cannot find '{}' path", propertiesPath); + } + + try (InputStream input = Files.newInputStream(propertiesPath)) { + properties.load(input); + } catch (IOException e) { + throw new PluginRuntimeException(e); + } + } finally { + FileUtils.closePath(propertiesPath); } return properties; diff --git a/pf4j/src/main/java/org/pf4j/ServiceProviderExtensionFinder.java b/pf4j/src/main/java/org/pf4j/ServiceProviderExtensionFinder.java index e20c467..b477192 100644 --- a/pf4j/src/main/java/org/pf4j/ServiceProviderExtensionFinder.java +++ b/pf4j/src/main/java/org/pf4j/ServiceProviderExtensionFinder.java @@ -121,13 +121,18 @@ public class ServiceProviderExtensionFinder extends AbstractExtensionFinder { private void collectExtensions(URL url, Set bucket) throws URISyntaxException, IOException { Path extensionPath; + if (url.toURI().getScheme().equals("jar")) { extensionPath = FileUtils.getPath(url.toURI(), EXTENSIONS_RESOURCE); } else { extensionPath = Paths.get(url.toURI()); } - bucket.addAll(readExtensions(extensionPath)); + try { + bucket.addAll(readExtensions(extensionPath)); + } finally { + FileUtils.closePath(extensionPath); + } } private Set readExtensions(Path extensionPath) throws IOException { diff --git a/pf4j/src/main/java/org/pf4j/processor/ExtensionStorage.java b/pf4j/src/main/java/org/pf4j/processor/ExtensionStorage.java index f3b3bbd..79f565a 100644 --- a/pf4j/src/main/java/org/pf4j/processor/ExtensionStorage.java +++ b/pf4j/src/main/java/org/pf4j/processor/ExtensionStorage.java @@ -82,18 +82,16 @@ public abstract class ExtensionStorage { } public static void read(Reader reader, Set entries) throws IOException { - BufferedReader bufferedReader = new BufferedReader(reader); - - String line; - while ((line = bufferedReader.readLine()) != null) { - line = COMMENT.matcher(line).replaceFirst(""); - line = WHITESPACE.matcher(line).replaceAll(""); - if (line.length() > 0) { - entries.add(line); + try (BufferedReader bufferedReader = new BufferedReader(reader)) { + String line; + while ((line = bufferedReader.readLine()) != null) { + line = COMMENT.matcher(line).replaceFirst(""); + line = WHITESPACE.matcher(line).replaceAll(""); + if (line.length() > 0) { + entries.add(line); + } } } - - bufferedReader.close(); } } diff --git a/pf4j/src/main/java/org/pf4j/processor/LegacyExtensionStorage.java b/pf4j/src/main/java/org/pf4j/processor/LegacyExtensionStorage.java index 67bd2d6..2b91290 100644 --- a/pf4j/src/main/java/org/pf4j/processor/LegacyExtensionStorage.java +++ b/pf4j/src/main/java/org/pf4j/processor/LegacyExtensionStorage.java @@ -65,17 +65,16 @@ public class LegacyExtensionStorage extends ExtensionStorage { public void write(Map> extensions) { try { FileObject file = getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", EXTENSIONS_RESOURCE); - BufferedWriter writer = new BufferedWriter(file.openWriter()); - writer.write("# Generated by PF4J"); // write header - writer.newLine(); - for (Map.Entry> entry : extensions.entrySet()) { - for (String extension : entry.getValue()) { - writer.write(extension); - writer.newLine(); + try (BufferedWriter writer = new BufferedWriter(file.openWriter())) { + writer.write("# Generated by PF4J"); // write header + writer.newLine(); + for (Map.Entry> entry : extensions.entrySet()) { + for (String extension : entry.getValue()) { + writer.write(extension); + writer.newLine(); + } } } - - writer.close(); } catch (FileNotFoundException e) { // it's the first time, create the file } catch (FilerException e) { diff --git a/pf4j/src/main/java/org/pf4j/processor/ServiceProviderExtensionStorage.java b/pf4j/src/main/java/org/pf4j/processor/ServiceProviderExtensionStorage.java index 6a3ec59..c14e7d3 100644 --- a/pf4j/src/main/java/org/pf4j/processor/ServiceProviderExtensionStorage.java +++ b/pf4j/src/main/java/org/pf4j/processor/ServiceProviderExtensionStorage.java @@ -70,19 +70,19 @@ public class ServiceProviderExtensionStorage extends ExtensionStorage { try { FileObject file = getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", EXTENSIONS_RESOURCE + "/" + extensionPoint); - BufferedWriter writer = new BufferedWriter(file.openWriter()); - // write header - writer.write("# Generated by PF4J"); // write header - writer.newLine(); - // write extensions - for (String extension : entry.getValue()) { - writer.write(extension); - if (!isExtensionOld(extensionPoint, extension)) { - writer.write(" # pf4j extension"); - } + try (BufferedWriter writer = new BufferedWriter(file.openWriter())) { + // write header + writer.write("# Generated by PF4J"); // write header writer.newLine(); + // write extensions + for (String extension : entry.getValue()) { + writer.write(extension); + if (!isExtensionOld(extensionPoint, extension)) { + writer.write(" # pf4j extension"); + } + writer.newLine(); + } } - writer.close(); } catch (FileNotFoundException e) { // it's the first time, create the file } catch (FilerException e) { diff --git a/pf4j/src/main/java/org/pf4j/util/FileUtils.java b/pf4j/src/main/java/org/pf4j/util/FileUtils.java index 7baaad4..0edcf97 100644 --- a/pf4j/src/main/java/org/pf4j/util/FileUtils.java +++ b/pf4j/src/main/java/org/pf4j/util/FileUtils.java @@ -42,12 +42,10 @@ import java.util.List; /** * @author Decebal Suiu */ -public class FileUtils { +public final class FileUtils { private static final Logger log = LoggerFactory.getLogger(FileUtils.class); - private static final boolean IS_WINDOWS_OS = System.getProperty("os.name").startsWith("Windows"); - public static List readLines(Path path, boolean ignoreComments) throws IOException { File file = path.toFile(); if (!file.isFile()) { @@ -163,7 +161,9 @@ public class FileUtils { try { Files.delete(path); - } catch (IOException ignored) { } + } catch (IOException ignored) { + // ignored + } } /** @@ -224,7 +224,7 @@ public class FileUtils { // transformation for Windows OS pathString = StringUtils.addStart(pathString.replace("\\", "/"), "/"); // space is replaced with %20 - pathString = pathString.replaceAll(" ","%20"); + pathString = pathString.replace(" ","%20"); uri = URI.create("jar:file:" + pathString); } @@ -232,14 +232,17 @@ public class FileUtils { } public static Path getPath(URI uri, String first, String... more) throws IOException { - FileSystem fileSystem = getFileSystem(uri); - Path path = fileSystem.getPath(first, more); - if (IS_WINDOWS_OS && "jar".equals(uri.getScheme())) { - // it's a ZipFileSystem - fileSystem.close(); - } + return getFileSystem(uri).getPath(first, more); + } - return path; + public static void closePath(Path path) { + if (path != null) { + try { + path.getFileSystem().close(); + } catch (Exception e) { + // close silently + } + } } public static Path findFile(Path directoryPath, String fileName) { @@ -270,4 +273,6 @@ public class FileUtils { } } + private FileUtils() { + } } diff --git a/pf4j/src/test/java/org/pf4j/plugin/PluginJar.java b/pf4j/src/test/java/org/pf4j/plugin/PluginJar.java index a75b68f..4a08e20 100644 --- a/pf4j/src/test/java/org/pf4j/plugin/PluginJar.java +++ b/pf4j/src/test/java/org/pf4j/plugin/PluginJar.java @@ -146,8 +146,8 @@ public class PluginJar { public PluginJar build() throws IOException { Manifest manifest = createManifest(); - try (OutputStream outputStream = new FileOutputStream(path.toFile())) { - JarOutputStream jarOutputStream = new JarOutputStream(outputStream, manifest); + try (OutputStream outputStream = new FileOutputStream(path.toFile()); + JarOutputStream jarOutputStream = new JarOutputStream(outputStream, manifest)) { if (!extensions.isEmpty()) { // add extensions.idx JarEntry jarEntry = new JarEntry("META-INF/extensions.idx"); @@ -163,7 +163,6 @@ public class PluginJar { jarOutputStream.closeEntry(); } } - jarOutputStream.close(); } return new PluginJar(this); -- 2.39.5