This is an automated email from the ASF dual-hosted git repository.
orpiske pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 1e67c67 (chores) tooling: fix resource leaks while processing stream
of files/dirs (#6021)
1e67c67 is described below
commit 1e67c67150dea8ca7a6b47a86928a0caa704a5ee
Author: Otavio Rodolfo Piske <[email protected]>
AuthorDate: Wed Sep 1 13:58:39 2021 +0200
(chores) tooling: fix resource leaks while processing stream of files/dirs
(#6021)
---
.../maven/plugins/javadoc/CamelJavadocJar.java | 5 +-
.../apache/maven/plugins/javadoc/StaleHelper.java | 5 +-
.../org/apache/camel/maven/PrepareFatJarMojo.java | 26 +++++------
.../camel/maven/packaging/PackageJandexMojo.java | 5 +-
.../camel/maven/resources/CopyResources.java | 54 +++++++++++++---------
5 files changed, 54 insertions(+), 41 deletions(-)
diff --git
a/tooling/maven/camel-javadoc-plugin/src/main/java/org/apache/maven/plugins/javadoc/CamelJavadocJar.java
b/tooling/maven/camel-javadoc-plugin/src/main/java/org/apache/maven/plugins/javadoc/CamelJavadocJar.java
index 4d60ab3..8867187 100644
---
a/tooling/maven/camel-javadoc-plugin/src/main/java/org/apache/maven/plugins/javadoc/CamelJavadocJar.java
+++
b/tooling/maven/camel-javadoc-plugin/src/main/java/org/apache/maven/plugins/javadoc/CamelJavadocJar.java
@@ -32,6 +32,7 @@ import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;
import java.util.stream.Collectors;
+import java.util.stream.Stream;
import org.apache.maven.archiver.MavenArchiveConfiguration;
import org.apache.maven.archiver.MavenArchiver;
@@ -327,7 +328,9 @@ public class CamelJavadocJar extends AbstractJavadocMojo {
}
TreeSet<Path> getAllRelativeFiles(Path dir) throws IOException {
- return
Files.walk(dir).map(dir::relativize).collect(Collectors.toCollection(TreeSet::new));
+ try (Stream<Path> pathStream = Files.walk(dir)) {
+ return
pathStream.map(dir::relativize).collect(Collectors.toCollection(TreeSet::new));
+ }
}
// ----------------------------------------------------------------------
diff --git
a/tooling/maven/camel-javadoc-plugin/src/main/java/org/apache/maven/plugins/javadoc/StaleHelper.java
b/tooling/maven/camel-javadoc-plugin/src/main/java/org/apache/maven/plugins/javadoc/StaleHelper.java
index b7d75c3..bc44930 100644
---
a/tooling/maven/camel-javadoc-plugin/src/main/java/org/apache/maven/plugins/javadoc/StaleHelper.java
+++
b/tooling/maven/camel-javadoc-plugin/src/main/java/org/apache/maven/plugins/javadoc/StaleHelper.java
@@ -19,6 +19,7 @@ package org.apache.maven.plugins.javadoc;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
+import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
@@ -114,9 +115,9 @@ public final class StaleHelper {
}
private static Collection<Path> walk(Path dir) {
- try {
+ try (DirectoryStream<Path> pathStream = Files.newDirectoryStream(dir))
{
Collection<Path> paths = new ArrayList<>();
- for (Path p : Files.newDirectoryStream(dir)) {
+ for (Path p : pathStream) {
paths.add(p);
}
return paths;
diff --git
a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/PrepareFatJarMojo.java
b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/PrepareFatJarMojo.java
index 8a3d43e..4d8714c 100644
---
a/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/PrepareFatJarMojo.java
+++
b/tooling/maven/camel-maven-plugin/src/main/java/org/apache/camel/maven/PrepareFatJarMojo.java
@@ -121,19 +121,7 @@ public class PrepareFatJarMojo extends AbstractMojo {
URL url = loaderResources.nextElement();
getLog().debug("Loading file " +
META_INF_SERVICES_TYPE_CONVERTER_LOADER
+ " to retrieve list of type converters, from
url: " + url);
- BufferedReader reader = new BufferedReader(new
InputStreamReader(url.openStream(), StandardCharsets.UTF_8));
- String line;
- do {
- line = reader.readLine();
- if (line != null && !line.startsWith("#") &&
!line.isEmpty()) {
- loaders.add(line);
- }
- } while (line != null);
- try {
- reader.close();
- } catch (Exception e) {
- // ignore
- }
+ readTypeConverters(loaders, url);
}
} catch (Exception e) {
getLog().warn("Error finding type converters due to " +
e.getMessage());
@@ -142,6 +130,18 @@ public class PrepareFatJarMojo extends AbstractMojo {
return loaders;
}
+ private void readTypeConverters(Set<String> loaders, URL url) throws
IOException {
+ try (BufferedReader reader = new BufferedReader(new
InputStreamReader(url.openStream(), StandardCharsets.UTF_8))) {
+ String line;
+ do {
+ line = reader.readLine();
+ if (line != null && !line.startsWith("#") && !line.isEmpty()) {
+ loaders.add(line);
+ }
+ } while (line != null);
+ }
+ }
+
protected final DynamicClassLoader getProjectClassLoader() throws
MojoExecutionException {
if (projectClassLoader == null) {
List<URL> urls = new ArrayList<>();
diff --git
a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageJandexMojo.java
b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageJandexMojo.java
index 5e80aec..20a6d12 100644
---
a/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageJandexMojo.java
+++
b/tooling/maven/camel-package-maven-plugin/src/main/java/org/apache/camel/maven/packaging/PackageJandexMojo.java
@@ -25,6 +25,7 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
+import java.util.stream.Stream;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
@@ -72,8 +73,8 @@ public class PackageJandexMojo extends AbstractGeneratorMojo {
if (!classesDirectory.isDirectory()) {
return;
}
- try {
- List<Path> inputs = Files.walk(classesDirectory.toPath()).filter(f
-> f.getFileName().toString().endsWith(".class"))
+ try (Stream<Path> pathStream = Files.walk(classesDirectory.toPath())) {
+ List<Path> inputs = pathStream.filter(f ->
f.getFileName().toString().endsWith(".class"))
.collect(Collectors.toList());
if (index.exists()) {
long lastmod = lastmod(index.toPath());
diff --git
a/tooling/maven/camel-resources-plugin/src/main/java/org/apache/camel/maven/resources/CopyResources.java
b/tooling/maven/camel-resources-plugin/src/main/java/org/apache/camel/maven/resources/CopyResources.java
index fab6c07..f27134a 100644
---
a/tooling/maven/camel-resources-plugin/src/main/java/org/apache/camel/maven/resources/CopyResources.java
+++
b/tooling/maven/camel-resources-plugin/src/main/java/org/apache/camel/maven/resources/CopyResources.java
@@ -26,6 +26,7 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
+import java.util.stream.Stream;
import org.apache.camel.tooling.util.FileUtil;
import org.apache.camel.tooling.util.Strings;
@@ -79,34 +80,41 @@ public class CopyResources extends AbstractMojo {
StringBuilder sb = new StringBuilder(8192);
for (Resource resource : resources) {
Path dir = resource.getDirectory().toPath();
- List<Path> files =
Files.walk(dir).filter(Files::isRegularFile).collect(Collectors.toList());
- for (Path file : files) {
- boolean filtering = isFiltering(resource, file);
- if (filtering) {
- try (Reader reader = Files.newBufferedReader(file,
Charset.forName(encoding))) {
- Reader wrapped = readerFilter.filter(reader, true,
project,
- null, false, session);
- sb.setLength(0);
- while (true) {
- int l = wrapped.read(buf, 0, buf.length);
- if (l >= 0) {
- sb.append(buf, 0, l);
- } else {
- break;
- }
+ processResource(output, buf, sb, resource, dir);
+ }
+ } catch (IOException | MavenFilteringException e) {
+ throw new MojoFailureException("Unable to copy resources", e);
+ }
+ }
+
+ private void processResource(Path output, char[] buf, StringBuilder sb,
Resource resource, Path dir)
+ throws MavenFilteringException, IOException {
+ try (Stream<Path> pathStream = Files.walk(dir)) {
+ List<Path> files =
pathStream.filter(Files::isRegularFile).collect(Collectors.toList());
+ for (Path file : files) {
+ boolean filtering = isFiltering(resource, file);
+ if (filtering) {
+ try (Reader reader = Files.newBufferedReader(file,
Charset.forName(encoding))) {
+ Reader wrapped = readerFilter.filter(reader, true,
project,
+ null, false, session);
+ sb.setLength(0);
+ while (true) {
+ int l = wrapped.read(buf, 0, buf.length);
+ if (l >= 0) {
+ sb.append(buf, 0, l);
+ } else {
+ break;
}
- String content = sb.toString();
-
FileUtil.updateFile(output.resolve(dir.relativize(file)), content);
- } catch (IOException e) {
- throw new IOException("Error processing resource:
" + file, e);
}
- } else {
- FileUtil.updateFile(file,
output.resolve(dir.relativize(file)));
+ String content = sb.toString();
+
FileUtil.updateFile(output.resolve(dir.relativize(file)), content);
+ } catch (IOException e) {
+ throw new IOException("Error processing resource: " +
file, e);
}
+ } else {
+ FileUtil.updateFile(file,
output.resolve(dir.relativize(file)));
}
}
- } catch (IOException | MavenFilteringException e) {
- throw new MojoFailureException("Unable to copy resources", e);
}
}