Browse Source

Fix for #377 and minor fixes found by Sonar lint (#388)

pull/397/head
Valeriy Kucherenko 4 years ago committed by GitHub
parent
commit
360e54320f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      pf4j/src/main/java/org/pf4j/PropertiesPluginDescriptorFinder.java
  2. 5
      pf4j/src/main/java/org/pf4j/ServiceProviderExtensionFinder.java
  3. 6
      pf4j/src/main/java/org/pf4j/processor/ExtensionStorage.java
  4. 5
      pf4j/src/main/java/org/pf4j/processor/LegacyExtensionStorage.java
  5. 4
      pf4j/src/main/java/org/pf4j/processor/ServiceProviderExtensionStorage.java
  6. 27
      pf4j/src/main/java/org/pf4j/util/FileUtils.java
  7. 5
      pf4j/src/test/java/org/pf4j/plugin/PluginJar.java

6
pf4j/src/main/java/org/pf4j/PropertiesPluginDescriptorFinder.java

@ -75,17 +75,21 @@ public class PropertiesPluginDescriptorFinder implements PluginDescriptorFinder
throw new PluginRuntimeException("Cannot find the properties path");
}
Properties properties = new Properties();
try {
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);
}
} finally {
FileUtils.closePath(propertiesPath);
}
return properties;
}

5
pf4j/src/main/java/org/pf4j/ServiceProviderExtensionFinder.java

@ -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());
}
try {
bucket.addAll(readExtensions(extensionPath));
} finally {
FileUtils.closePath(extensionPath);
}
}
private Set<String> readExtensions(Path extensionPath) throws IOException {

6
pf4j/src/main/java/org/pf4j/processor/ExtensionStorage.java

@ -82,8 +82,7 @@ public abstract class ExtensionStorage {
}
public static void read(Reader reader, Set<String> entries) throws IOException {
BufferedReader bufferedReader = new BufferedReader(reader);
try (BufferedReader bufferedReader = new BufferedReader(reader)) {
String line;
while ((line = bufferedReader.readLine()) != null) {
line = COMMENT.matcher(line).replaceFirst("");
@ -92,8 +91,7 @@ public abstract class ExtensionStorage {
entries.add(line);
}
}
bufferedReader.close();
}
}
}

5
pf4j/src/main/java/org/pf4j/processor/LegacyExtensionStorage.java

@ -65,7 +65,7 @@ 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());
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()) {
@ -74,8 +74,7 @@ public class LegacyExtensionStorage extends ExtensionStorage {
writer.newLine();
}
}
writer.close();
}
} catch (FileNotFoundException e) {
// it's the first time, create the file
} catch (FilerException e) {

4
pf4j/src/main/java/org/pf4j/processor/ServiceProviderExtensionStorage.java

@ -70,7 +70,7 @@ public class ServiceProviderExtensionStorage extends ExtensionStorage {
try {
FileObject file = getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", EXTENSIONS_RESOURCE
+ "/" + extensionPoint);
BufferedWriter writer = new BufferedWriter(file.openWriter());
try (BufferedWriter writer = new BufferedWriter(file.openWriter())) {
// write header
writer.write("# Generated by PF4J"); // write header
writer.newLine();
@ -82,7 +82,7 @@ public class ServiceProviderExtensionStorage extends ExtensionStorage {
}
writer.newLine();
}
writer.close();
}
} catch (FileNotFoundException e) {
// it's the first time, create the file
} catch (FilerException e) {

27
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<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() {
}
}

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

Loading…
Cancel
Save