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;
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 {
}
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();
}
}
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) {
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) {
/**
* @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()) {
try {
Files.delete(path);
- } catch (IOException ignored) { }
+ } catch (IOException ignored) {
+ // ignored
+ }
}
/**
// 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);
}
}
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) {
}
}
+ private FileUtils() {
+ }
}
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");
jarOutputStream.closeEntry();
}
}
- jarOutputStream.close();
}
return new PluginJar(this);