-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystemLogFileDao.java
More file actions
136 lines (121 loc) · 4.67 KB
/
Copy pathFileSystemLogFileDao.java
File metadata and controls
136 lines (121 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.itasocialacademy.oitassist.logfile.dao;
import com.itasocialacademy.oitassist.logfile.exceptions.LogFileListingException;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.LinkOption;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;
@Slf4j
@Repository
public class FileSystemLogFileDao implements LogFileDao {
private final Path logDirectory;
public FileSystemLogFileDao(
@Value("${logging.file.name}") String configuredLogFile) {
this.logDirectory =
resolveLogDirectory(configuredLogFile);
}
@Override
public List<LogFileMetadata> findAll() {
return findFiles(path -> true);
}
@Override
public List<LogFileMetadata> findByNameContainingIgnoreCase(String name) {
String normalizedName = name.toLowerCase(Locale.ROOT);
return findFiles(path -> path.getFileName()
.toString()
.toLowerCase(Locale.ROOT)
.contains(normalizedName));
}
private List<LogFileMetadata> findFiles(Predicate<Path> filter) {
validateLogDirectory();
try (Stream<Path> paths = Files.list(logDirectory)) {
return paths
.filter(filter)
.map(this::readMetadata)
.flatMap(Optional::stream)
.toList();
} catch (UncheckedIOException exception) {
log.error(
"Failed to read metadata from log directory: {}",
logDirectory,
exception.getCause());
throw new LogFileListingException();
} catch (IOException | SecurityException exception) {
log.error("Failed to access log directory: {}", logDirectory, exception);
throw new LogFileListingException();
}
}
private void validateLogDirectory() {
if (!Files.exists(
logDirectory,
LinkOption.NOFOLLOW_LINKS)) {
log.error(
"Configured log directory does not exist: {}",
logDirectory);
throw new LogFileListingException();
}
if (!Files.isDirectory(
logDirectory,
LinkOption.NOFOLLOW_LINKS)) {
log.error(
"Configured log path is not a directory: {}",
logDirectory);
throw new LogFileListingException();
}
if (!Files.isReadable(logDirectory)) {
log.error("Configured log directory is not readable: {}", logDirectory);
throw new LogFileListingException();
}
}
private static Path resolveLogDirectory(String configuredLogFile) {
if (configuredLogFile == null || configuredLogFile.isBlank()) {
throw new IllegalStateException("Property logging.file.name must be configured");
}
try {
Path logFilePath = Path.of(configuredLogFile)
.toAbsolutePath()
.normalize();
Path parentDirectory = logFilePath.getParent();
if (parentDirectory == null) {
throw new IllegalStateException("Unable to determine the log directory");
}
return parentDirectory;
} catch (InvalidPathException exception) {
throw new IllegalStateException("Property logging.file.name contains an invalid path", exception);
}
}
private Optional<LogFileMetadata> readMetadata(Path path) {
try {
BasicFileAttributes attributes = Files.readAttributes(
path,
BasicFileAttributes.class,
LinkOption.NOFOLLOW_LINKS);
if (!attributes.isRegularFile()) {
return Optional.empty();
}
return Optional.of(new LogFileMetadata(
path.getFileName().toString(),
attributes.size(),
attributes
.lastModifiedTime()
.toInstant()));
} catch (NoSuchFileException exception) {
log.debug("Log file disappeared during directory scan: {}", path.getFileName());
return Optional.empty();
} catch (IOException exception) {
throw new UncheckedIOException("Failed to read log file attributes",
exception);
}
}
}