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. 22
      pf4j/src/main/java/org/pf4j/PropertiesPluginDescriptorFinder.java
  2. 7
      pf4j/src/main/java/org/pf4j/ServiceProviderExtensionFinder.java
  3. 18
      pf4j/src/main/java/org/pf4j/processor/ExtensionStorage.java
  4. 17
      pf4j/src/main/java/org/pf4j/processor/LegacyExtensionStorage.java
  5. 22
      pf4j/src/main/java/org/pf4j/processor/ServiceProviderExtensionStorage.java
  6. 29
      pf4j/src/main/java/org/pf4j/util/FileUtils.java
  7. 5
      pf4j/src/test/java/org/pf4j/plugin/PluginJar.java

22
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"); 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(); Properties properties = new Properties();
try (InputStream input = Files.newInputStream(propertiesPath)) { try {
properties.load(input); log.debug("Lookup plugin descriptor in '{}'", propertiesPath);
} catch (IOException e) { if (Files.notExists(propertiesPath)) {
throw new PluginRuntimeException(e); 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; return properties;

7
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 { private void collectExtensions(URL url, Set<String> bucket) throws URISyntaxException, IOException {
Path extensionPath; Path extensionPath;
if (url.toURI().getScheme().equals("jar")) { if (url.toURI().getScheme().equals("jar")) {
extensionPath = FileUtils.getPath(url.toURI(), EXTENSIONS_RESOURCE); extensionPath = FileUtils.getPath(url.toURI(), EXTENSIONS_RESOURCE);
} else { } else {
extensionPath = Paths.get(url.toURI()); 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 { private Set<String> readExtensions(Path extensionPath) throws IOException {

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

@ -82,18 +82,16 @@ public abstract class ExtensionStorage {
} }
public static void read(Reader reader, Set<String> entries) throws IOException { public static void read(Reader reader, Set<String> entries) throws IOException {
BufferedReader bufferedReader = new BufferedReader(reader); try (BufferedReader bufferedReader = new BufferedReader(reader)) {
String line;
String line; while ((line = bufferedReader.readLine()) != null) {
while ((line = bufferedReader.readLine()) != null) { line = COMMENT.matcher(line).replaceFirst("");
line = COMMENT.matcher(line).replaceFirst(""); line = WHITESPACE.matcher(line).replaceAll("");
line = WHITESPACE.matcher(line).replaceAll(""); if (line.length() > 0) {
if (line.length() > 0) { entries.add(line);
entries.add(line); }
} }
} }
bufferedReader.close();
} }
} }

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

@ -65,17 +65,16 @@ public class LegacyExtensionStorage extends ExtensionStorage {
public void write(Map<String, Set<String>> extensions) { public void write(Map<String, Set<String>> extensions) {
try { try {
FileObject file = getFiler().createResource(StandardLocation.CLASS_OUTPUT, "", EXTENSIONS_RESOURCE); 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.write("# Generated by PF4J"); // write header
writer.newLine(); writer.newLine();
for (Map.Entry<String, Set<String>> entry : extensions.entrySet()) { for (Map.Entry<String, Set<String>> entry : extensions.entrySet()) {
for (String extension : entry.getValue()) { for (String extension : entry.getValue()) {
writer.write(extension); writer.write(extension);
writer.newLine(); writer.newLine();
}
} }
} }
writer.close();
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
// it's the first time, create the file // it's the first time, create the file
} catch (FilerException e) { } catch (FilerException e) {

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

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

29
pf4j/src/main/java/org/pf4j/util/FileUtils.java

@ -42,12 +42,10 @@ import java.util.List;
/** /**
* @author Decebal Suiu * @author Decebal Suiu
*/ */
public class FileUtils { public final class FileUtils {
private static final Logger log = LoggerFactory.getLogger(FileUtils.class); 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 { public static List<String> readLines(Path path, boolean ignoreComments) throws IOException {
File file = path.toFile(); File file = path.toFile();
if (!file.isFile()) { if (!file.isFile()) {
@ -163,7 +161,9 @@ public class FileUtils {
try { try {
Files.delete(path); Files.delete(path);
} catch (IOException ignored) { } } catch (IOException ignored) {
// ignored
}
} }
/** /**
@ -224,7 +224,7 @@ public class FileUtils {
// transformation for Windows OS // transformation for Windows OS
pathString = StringUtils.addStart(pathString.replace("\\", "/"), "/"); pathString = StringUtils.addStart(pathString.replace("\\", "/"), "/");
// space is replaced with %20 // space is replaced with %20
pathString = pathString.replaceAll(" ","%20"); pathString = pathString.replace(" ","%20");
uri = URI.create("jar:file:" + pathString); 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 { public static Path getPath(URI uri, String first, String... more) throws IOException {
FileSystem fileSystem = getFileSystem(uri); return getFileSystem(uri).getPath(first, more);
Path path = fileSystem.getPath(first, more); }
if (IS_WINDOWS_OS && "jar".equals(uri.getScheme())) {
// it's a ZipFileSystem
fileSystem.close();
}
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) { 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 { public PluginJar build() throws IOException {
Manifest manifest = createManifest(); Manifest manifest = createManifest();
try (OutputStream outputStream = new FileOutputStream(path.toFile())) { try (OutputStream outputStream = new FileOutputStream(path.toFile());
JarOutputStream jarOutputStream = new JarOutputStream(outputStream, manifest); JarOutputStream jarOutputStream = new JarOutputStream(outputStream, manifest)) {
if (!extensions.isEmpty()) { if (!extensions.isEmpty()) {
// add extensions.idx // add extensions.idx
JarEntry jarEntry = new JarEntry("META-INF/extensions.idx"); JarEntry jarEntry = new JarEntry("META-INF/extensions.idx");
@ -163,7 +163,6 @@ public class PluginJar {
jarOutputStream.closeEntry(); jarOutputStream.closeEntry();
} }
} }
jarOutputStream.close();
} }
return new PluginJar(this); return new PluginJar(this);

Loading…
Cancel
Save