Skip to content

Commit 2d28a3c

Browse files
committed
Issue #281 : Directory streams etc are now auto closed to prevent systems running out of file handles.
1 parent de13c4d commit 2d28a3c

File tree

2 files changed

+54
-44
lines changed

2 files changed

+54
-44
lines changed

stroom-core-server/src/main/java/stroom/importexport/server/ImportExportSerializerImpl.java

+33-23
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
import java.util.Map.Entry;
6161
import java.util.Set;
6262
import java.util.stream.Collectors;
63+
import java.util.stream.Stream;
6364

6465
@Component
6566
public class ImportExportSerializerImpl implements ImportExportSerializer {
@@ -190,17 +191,22 @@ public <E extends DocumentEntity> void performImport(final Path dir,
190191
try {
191192
// Look for matching files
192193
final String match = "." + entityType + ".";
193-
final List<Path> paths = Files.walk(dir)
194-
.filter(p -> p.getFileName().toString().contains(match))
195-
.map(p -> {
196-
String name = p.getFileName().toString();
197-
name = name.substring(0, name.indexOf(match) + match.length() - 1);
198-
return p.getParent().resolve(name);
199-
})
200-
.collect(Collectors.toSet())
201-
.stream()
202-
.sorted(Comparator.comparingInt(Path::getNameCount))
203-
.collect(Collectors.toList());
194+
List<Path> paths;
195+
try (final Stream<Path> stream = Files.walk(dir)) {
196+
final Set<Path> set = stream
197+
.filter(p -> p.getFileName().toString().contains(match))
198+
.map(p -> {
199+
String name = p.getFileName().toString();
200+
name = name.substring(0, name.indexOf(match) + match.length() - 1);
201+
return p.getParent().resolve(name);
202+
})
203+
.collect(Collectors.toSet());
204+
try (Stream<Path> s = set.stream()) {
205+
paths = s
206+
.sorted(Comparator.comparingInt(Path::getNameCount))
207+
.collect(Collectors.toList());
208+
}
209+
}
204210

205211
paths.forEach(path -> {
206212
ImportState importState = null;
@@ -213,21 +219,25 @@ public <E extends DocumentEntity> void performImport(final Path dir,
213219

214220
// Find all of the files associated with this document config.
215221
final String matchingConfig = relativePath.getFileName().toString();
216-
final List<Path> parts = Files.list(path.getParent())
217-
.filter(p -> p.getFileName().toString().startsWith(matchingConfig))
218-
.collect(Collectors.toList());
219222

220223
// Create a map of all of the data required to import this document.
221224
final Map<String, String> dataMap = new HashMap<>();
222-
parts.forEach(part -> {
223-
try {
224-
final String key = dir.relativize(part).toString();
225-
final String content = new String(Files.readAllBytes(part), Charset.forName("UTF-8"));
226-
dataMap.put(key, content);
227-
} catch (final IOException e) {
228-
LOGGER.error(e.getMessage(), e);
229-
}
230-
});
225+
try (final Stream<Path> stream = Files.list(path.getParent())) {
226+
final List<Path> parts = stream
227+
.filter(p -> p.getFileName().toString().startsWith(matchingConfig))
228+
.collect(Collectors.toList());
229+
230+
parts.forEach(part -> {
231+
try {
232+
final String key = dir.relativize(part).toString();
233+
final String content = new String(Files.readAllBytes(part), Charset.forName("UTF-8"));
234+
dataMap.put(key, content);
235+
} catch (final Throwable e) {
236+
LOGGER.error(e.getMessage(), e);
237+
LOGGER.error("DATA SIZE = " + dataMap.size());
238+
}
239+
});
240+
}
231241

232242
// Find out if this item exists.
233243
// TODO : In v6 the UUID will be part of the file name so that we don't have to read the config to get it.

stroom-util/src/main/java/stroom/util/zip/ZipUtil.java

+21-21
Original file line numberDiff line numberDiff line change
@@ -149,30 +149,30 @@ public static void unzip(final File zipFile, final File dir) throws IOException
149149
}
150150

151151
public static void unzip(final Path zipFile, final Path dir) throws IOException {
152-
final ZipInputStream zip = new ZipInputStream(new BufferedInputStream(Files.newInputStream(zipFile)));
153-
154-
ZipEntry zipEntry = null;
155-
while ((zipEntry = zip.getNextEntry()) != null) {
156-
// Get output file.
157-
final Path file = dir.resolve(zipEntry.getName());
158-
159-
if (zipEntry.isDirectory()) {
160-
// Make sure output directories exist.
161-
Files.createDirectories(file);
162-
} else {
163-
// Make sure output directories exist.
164-
Files.createDirectories(file.getParent());
165-
166-
// Write file.
167-
try (final OutputStream outputStream = Files.newOutputStream(file)) {
168-
StreamUtil.streamToStream(zip, outputStream, false);
152+
try (final ZipInputStream zip = new ZipInputStream(new BufferedInputStream(Files.newInputStream(zipFile)))) {
153+
ZipEntry zipEntry;
154+
while ((zipEntry = zip.getNextEntry()) != null) {
155+
try {
156+
// Get output file.
157+
final Path file = dir.resolve(zipEntry.getName());
158+
159+
if (zipEntry.isDirectory()) {
160+
// Make sure output directories exist.
161+
Files.createDirectories(file);
162+
} else {
163+
// Make sure output directories exist.
164+
Files.createDirectories(file.getParent());
165+
166+
// Write file.
167+
try (final OutputStream outputStream = Files.newOutputStream(file)) {
168+
StreamUtil.streamToStream(zip, outputStream, false);
169+
}
170+
}
171+
} finally {
172+
zip.closeEntry();
169173
}
170174
}
171-
172-
zip.closeEntry();
173175
}
174-
175-
zip.close();
176176
}
177177

178178
public static List<String> pathList(final File zipFile) throws IOException {

0 commit comments

Comments
 (0)