Skip to content

Commit f2add5a

Browse files
committed
make log file retriever a singleton
so that it is initialized only once, because until now, it was needlessly init twice (in prod profile). first, in the starter to print the log file, and then in the spring controller.
1 parent 3c7fa30 commit f2add5a

3 files changed

Lines changed: 7 additions & 14 deletions

File tree

src/main/java/de/rwth/idsg/steve/SteveProdStarter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void start() throws Exception {
6262

6363
private void starting() {
6464
String msg = "Log file: "
65-
+ new LogFileRetriever().getLogFilePathOrErrorMessage()
65+
+ LogFileRetriever.INSTANCE.getLogFilePathOrErrorMessage()
6666
+ sep()
6767
+ "Starting";
6868

src/main/java/de/rwth/idsg/steve/utils/LogFileRetriever.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@
2525
* @since 05.11.2015
2626
*/
2727
@Slf4j
28-
public class LogFileRetriever {
28+
public final class LogFileRetriever {
29+
public static final LogFileRetriever INSTANCE = new LogFileRetriever();
2930

3031
private List<Path> logPathList;
3132
private Random random = new Random();
3233

33-
public LogFileRetriever() {
34+
private LogFileRetriever() {
3435
logPathList = getActiveLogFilePaths();
3536
}
3637

src/main/java/de/rwth/idsg/steve/web/controller/LogController.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.springframework.web.bind.annotation.RequestMapping;
88
import org.springframework.web.bind.annotation.RequestMethod;
99

10-
import javax.annotation.PostConstruct;
1110
import javax.servlet.http.HttpServletResponse;
1211
import java.io.IOException;
1312
import java.io.PrintWriter;
@@ -24,32 +23,25 @@
2423
@RequestMapping(value = "/manager")
2524
public class LogController {
2625

27-
private LogFileRetriever logFileRetriever;
28-
29-
@PostConstruct
30-
private void init() {
31-
logFileRetriever = new LogFileRetriever();
32-
}
33-
3426
@RequestMapping(value = "/log", method = RequestMethod.GET)
3527
public void log(HttpServletResponse response) {
3628
response.setContentType("text/plain");
3729

3830
try (PrintWriter writer = response.getWriter()) {
39-
Optional<Path> p = logFileRetriever.getPath();
31+
Optional<Path> p = LogFileRetriever.INSTANCE.getPath();
4032
if (p.isPresent()) {
4133
Files.lines(p.get(), StandardCharsets.UTF_8)
4234
.forEach(writer::println);
4335
} else {
44-
writer.write(logFileRetriever.getErrorMessage());
36+
writer.write(LogFileRetriever.INSTANCE.getErrorMessage());
4537
}
4638
} catch (IOException e) {
4739
log.error("Exception happened", e);
4840
}
4941
}
5042

5143
public String getLogFilePath() {
52-
return logFileRetriever.getLogFilePathOrErrorMessage();
44+
return LogFileRetriever.INSTANCE.getLogFilePathOrErrorMessage();
5345
}
5446

5547
}

0 commit comments

Comments
 (0)