Fix critical issue with open file handle leak on user guide folder

This commit is contained in:
George Kalampokis 2022-07-28 11:23:39 +03:00
parent a2228a5fb6
commit fa723c07ae
2 changed files with 32 additions and 27 deletions

View File

@ -20,6 +20,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -41,10 +42,13 @@ public class UserGuideController {
@RequestMapping(path = "{lang}", method = RequestMethod.GET ) @RequestMapping(path = "{lang}", method = RequestMethod.GET )
public ResponseEntity getUserGuide(@PathVariable(name = "lang") String lang) throws IOException { public ResponseEntity getUserGuide(@PathVariable(name = "lang") String lang) throws IOException {
long files = Files.list(Paths.get(this.environment.getProperty("userguide.path"))).count(); long files = 0;
try (Stream<Path> paths = Files.list(Paths.get(Objects.requireNonNull(this.environment.getProperty("userguide.path"))))) {
files = paths.count();
}
metricsManager.calculateValue(MetricNames.LANGUAGES, (int) files, null); metricsManager.calculateValue(MetricNames.LANGUAGES, (int) files, null);
Stream<Path> walk = Files.walk(Paths.get(this.environment.getProperty("userguide.path"))); try (Stream<Path> paths = Files.walk(Paths.get(Objects.requireNonNull(this.environment.getProperty("userguide.path"))))) {
List<String> result = walk.filter(Files::isRegularFile) List<String> result = paths.filter(Files::isRegularFile)
.map(Path::toString).collect(Collectors.toList()); .map(Path::toString).collect(Collectors.toList());
String fileName = result.stream().filter(guide -> guide.contains("_" + lang)).findFirst().orElse(null); String fileName = result.stream().filter(guide -> guide.contains("_" + lang)).findFirst().orElse(null);
@ -67,6 +71,7 @@ public class UserGuideController {
is.close(); is.close();
return new ResponseEntity<>(content, responseHeaders, HttpStatus.OK); return new ResponseEntity<>(content, responseHeaders, HttpStatus.OK);
}
} }

View File

@ -17,15 +17,13 @@ import javax.annotation.PostConstruct;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.text.ParseException; import java.text.ParseException;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.ZoneId; import java.time.ZoneId;
import java.util.Collections; import java.util.*;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream; import java.util.stream.Stream;
@ -179,8 +177,10 @@ public class MetricsManager {
calculateValue(MetricNames.USERS, (int) userManager.countActiveUsers().intValue(), MetricNames.LOGGEDIN); calculateValue(MetricNames.USERS, (int) userManager.countActiveUsers().intValue(), MetricNames.LOGGEDIN);
calculateValue(MetricNames.USERS, (int) userManager.countAllUsers().intValue(), MetricNames.TOTAL); calculateValue(MetricNames.USERS, (int) userManager.countAllUsers().intValue(), MetricNames.TOTAL);
long files = Files.list(Paths.get(this.environment.getProperty("userguide.path"))).count(); try (Stream<Path> paths = Files.list(Paths.get(Objects.requireNonNull(this.environment.getProperty("userguide.path"))))) {
long files = paths.count();
calculateValue(MetricNames.LANGUAGES, (int) files, null); calculateValue(MetricNames.LANGUAGES, (int) files, null);
}
calculateValue(MetricNames.INSTALLATIONS, 1, null); calculateValue(MetricNames.INSTALLATIONS, 1, null);
calculateValue(MetricNames.NEXUS + MetricNames.INSTALLATIONS, 1, null); calculateValue(MetricNames.NEXUS + MetricNames.INSTALLATIONS, 1, null);