-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileStore.java
More file actions
66 lines (56 loc) · 2.05 KB
/
FileStore.java
File metadata and controls
66 lines (56 loc) · 2.05 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
package tech.thedumbdev.data;
import tech.thedumbdev.data.exceptions.DirectoryCreationException;
import tech.thedumbdev.data.exceptions.FileStoreException;
import tech.thedumbdev.data.exceptions.LogFileException;
import tech.thedumbdev.pojo.Log;
import java.io.*;
import java.util.Set;
public class FileStore implements DataStore {
private long timestamp;
private File file;
private FileOutputStream fos;
private ObjectOutputStream oos;
public FileStore() throws FileStoreException {
this.timestamp = java.time.Instant.now().getEpochSecond();
String home = System.getProperty("user.home");
if(home == null || home.isEmpty()) {
throw new FileStoreException("Unable to determine home directory");
}
String directoryPath = home + File.separator + "logs";
File directory = new File(directoryPath);
if(!directory.exists() || !directory.isDirectory()) {
boolean result = directory.mkdir();
if(!result) {
throw new DirectoryCreationException("Unable to create directory");
}
}
this.file = new File(directoryPath + File.separator + this.timestamp + ".log");
try {
this.fos = new FileOutputStream(file, true);
this.oos = new ObjectOutputStream(this.fos);
} catch (IOException | SecurityException e) {
throw new LogFileException("Failed to initialize file store", e);
}
}
@Override
public synchronized void appendLog(Set<Log> logs) {
try {
for(Log log: logs) {
System.out.println(log.toString());
this.oos.writeObject(log);
}
} catch (Exception e) {
throw new RuntimeException("Failed to write log", e);
}
}
@Override
public void close() {
try {
this.oos.flush();
this.oos.close();
this.fos.close();
} catch (IOException e) {
throw new RuntimeException("Failed to close file store", e);
}
}
}