Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close the streams returned by Files.walk properly #3954

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.camel.quarkus.core.deployment;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -197,14 +198,18 @@ List<NativeImageResourceBuildItem> camelRuntimeCatalog(
continue;
}

List<String> items = CamelSupport.safeWalk(resourcePath)
.filter(Files::isRegularFile)
.map(root::relativize)
.map(Path::toString)
.collect(Collectors.toList());
try (Stream<Path> files = Files.walk(resourcePath)) {
List<String> items = files
.filter(Files::isRegularFile)
.map(root::relativize)
.map(Path::toString)
.collect(Collectors.toList());
LOGGER.debug("Register catalog json: {}", items);
resources.add(new NativeImageResourceBuildItem(items));
} catch (IOException e) {
throw new RuntimeException("Could not walk " + resourcePath, e);
}

LOGGER.debug("Register catalog json: {}", items);
resources.add(new NativeImageResourceBuildItem(items));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
package org.apache.camel.quarkus.core.deployment.util;

import java.io.IOError;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Modifier;
Expand Down Expand Up @@ -52,14 +51,6 @@ public static boolean isPublic(ClassInfo ci) {
return (ci.flags() & Modifier.PUBLIC) != 0;
}

public static Stream<Path> safeWalk(Path p) {
try {
return Files.walk(p);
} catch (IOException e) {
throw new IOError(e);
}
}

public static Stream<CamelServiceBuildItem> services(ApplicationArchivesBuildItem archives, PathFilter pathFilter) {
final Set<CamelServiceBuildItem> answer = new HashSet<>();
final Predicate<Path> filter = pathFilter.asPathPredicate();
Expand All @@ -72,19 +63,23 @@ public static Stream<CamelServiceBuildItem> services(ApplicationArchivesBuildIte
continue;
}

safeWalk(resourcePath).filter(Files::isRegularFile).forEach(file -> {
// the root archive may point to a jar file or the absolute path of
// a project's build output so we need to relativize to make the
// FastFactoryFinder work as expected
Path key = root.relativize(file);

if (filter.test(key)) {
String clazz = readProperties(file).getProperty("class");
if (clazz != null) {
answer.add(new CamelServiceBuildItem(key, clazz));
try (Stream<Path> files = Files.walk(resourcePath)) {
files.filter(Files::isRegularFile).forEach(file -> {
// the root archive may point to a jar file or the absolute path of
// a project's build output so we need to relativize to make the
// FastFactoryFinder work as expected
Path key = root.relativize(file);

if (filter.test(key)) {
String clazz = readProperties(file).getProperty("class");
if (clazz != null) {
answer.add(new CamelServiceBuildItem(key, clazz));
}
}
}
});
});
} catch (IOException e) {
throw new RuntimeException("Could not walk " + resourcePath, e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.camel.quarkus.core.deployment.util;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
Expand Down Expand Up @@ -93,8 +94,13 @@ public Predicate<Path> asPathPredicate() {

public String[] scanClassNames(Stream<Path> archiveRootDirs) {
final Set<String> selectedPaths = new TreeSet<>();
archiveRootDirs.forEach(rootDir -> scanClassNames(rootDir, CamelSupport.safeWalk(rootDir),
Files::isRegularFile, selectedPaths::add));
archiveRootDirs.forEach(rootDir -> {
try (Stream<Path> files = Files.walk(rootDir)) {
scanClassNames(rootDir, files, Files::isRegularFile, selectedPaths::add);
} catch (IOException e) {
throw new RuntimeException("Could not walk " + rootDir, e);
}
});
/* Let's add the paths without wildcards even if they did not match any Jandex class
* A workaround for https://github.com/apache/camel-quarkus/issues/2969 */
addNonPatternPaths(selectedPaths);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
Expand Down Expand Up @@ -119,28 +120,32 @@ void xsltResources(
}
}

Files.walk(destination)
.sorted(Comparator.reverseOrder())
.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(".class"))
.forEach(path -> {
try {
final Path rel = destination.relativize(path);
final String fqcn = StringUtils.removeEnd(rel.toString(), ".class").replace(File.separatorChar,
'.');
final byte[] data = Files.readAllBytes(path);

generatedClasses.produce(new GeneratedClassBuildItem(false, fqcn, data));
generatedNames.produce(new XsltGeneratedClassBuildItem(fqcn));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
try (Stream<Path> files = Files.walk(destination)) {
files
.sorted(Comparator.reverseOrder())
.filter(Files::isRegularFile)
.filter(path -> path.toString().endsWith(".class"))
.forEach(path -> {
try {
final Path rel = destination.relativize(path);
final String fqcn = StringUtils.removeEnd(rel.toString(), ".class").replace(File.separatorChar,
'.');
final byte[] data = Files.readAllBytes(path);

generatedClasses.produce(new GeneratedClassBuildItem(false, fqcn, data));
generatedNames.produce(new XsltGeneratedClassBuildItem(fqcn));
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
} finally {
Files.walk(destination)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
try (Stream<Path> files = Files.walk(destination)) {
files
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import org.apache.camel.quarkus.test.AvailablePortFinder;
Expand Down Expand Up @@ -120,10 +121,12 @@ public void stop() {

try {
if (ftpRoot != null) {
Files.walk(ftpRoot)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
try (Stream<Path> files = Files.walk(ftpRoot)) {
files
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}
}
} catch (Exception e) {
LOGGER.warn("Failed delete ftp root: {}, {}", ftpRoot, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.stream.Stream;

import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;
import org.apache.camel.quarkus.test.AvailablePortFinder;
Expand Down Expand Up @@ -89,10 +90,12 @@ public void stop() {

try {
if (sftpHome != null) {
Files.walk(sftpHome)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
try (Stream<Path> files = Files.walk(sftpHome)) {
files
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(File::delete);
}
}
} catch (Exception e) {
LOGGER.warn("Failed delete sftp home: {}, {}", sftpHome, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.stream.Stream;

import io.quarkus.test.QuarkusDevModeTest;
import io.restassured.RestAssured;
Expand Down Expand Up @@ -75,10 +76,12 @@ public static void cleanUp() throws IOException {
deleteFile = File::deleteOnExit;
}

Files.walk(BASE)
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(deleteFile);
try (Stream<Path> files = Files.walk(BASE)) {
files
.sorted(Comparator.reverseOrder())
.map(Path::toFile)
.forEach(deleteFile);
}
}

public static Asset applicationProperties() {
Expand Down