]> source.dussan.org Git - pf4j.git/commitdiff
Fix for #377 and minor fixes found by Sonar lint (#388)
authorValeriy Kucherenko <valeriy.kucherenko@gmail.com>
Fri, 24 Jul 2020 19:07:34 +0000 (22:07 +0300)
committerGitHub <noreply@github.com>
Fri, 24 Jul 2020 19:07:34 +0000 (22:07 +0300)
pf4j/src/main/java/org/pf4j/PropertiesPluginDescriptorFinder.java
pf4j/src/main/java/org/pf4j/ServiceProviderExtensionFinder.java
pf4j/src/main/java/org/pf4j/processor/ExtensionStorage.java
pf4j/src/main/java/org/pf4j/processor/LegacyExtensionStorage.java
pf4j/src/main/java/org/pf4j/processor/ServiceProviderExtensionStorage.java
pf4j/src/main/java/org/pf4j/util/FileUtils.java
pf4j/src/test/java/org/pf4j/plugin/PluginJar.java

index 6789a5fe2c52a6db4199f9229d530585964c79e6..303b662c7c577d366c1c8b48055249e61131447a 100644 (file)
@@ -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;
index e20c46714c30edff994a344f6e7b769b3210d073..b477192a15c5fc877157800480bbf113b32cd9a9 100644 (file)
@@ -121,13 +121,18 @@ public class ServiceProviderExtensionFinder extends AbstractExtensionFinder {
 
     private void collectExtensions(URL url, Set<String> 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<String> readExtensions(Path extensionPath) throws IOException {
index f3b3bbdada01ec22e717e4423995b91d4540479d..79f565a8f664c5fbbd2cb6da126cc57279eb6943 100644 (file)
@@ -82,18 +82,16 @@ public abstract class ExtensionStorage {
     }
 
     public static void read(Reader reader, Set<String> 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();
     }
 
 }
index 67bd2d6ea179afd83dcaabe9dfab8f20230e09d2..2b912903bc5953d9e7347ff8314af9aaf6a217f7 100644 (file)
@@ -65,17 +65,16 @@ public class LegacyExtensionStorage extends ExtensionStorage {
     public void write(Map<String, Set<String>> 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<String, Set<String>> 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<String, Set<String>> 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) {
index 6a3ec59a6782341a16a3c80075f6cccdeeb879a9..c14e7d39ff5f96c4caa985f9fbdeb982b95947f3 100644 (file)
@@ -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) {
index 7baaad4827fe22f4073b4d7069fca2ec8b2e7366..0edcf97a71162a87c9f60685b950556eb6c2c7af 100644 (file)
@@ -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<String> 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() {
+    }
 }
index a75b68fa21841df4708fbe1d30144fc1c4b10943..4a08e209cf895ebd16fd29d15d3581dd7450f03c 100644 (file)
@@ -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);