-
Notifications
You must be signed in to change notification settings - Fork 38
/
CacheUtils.java
102 lines (90 loc) · 3.51 KB
/
CacheUtils.java
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
/*
* This file is part of FragPipe.
*
* FragPipe is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* FragPipe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with FragPipe. If not, see <https://www.gnu.org/licenses/>.
*/
package com.github.chhh.utils;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.function.Supplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.dmtavt.fragpipe.params.ThisAppProps;
public class CacheUtils {
private static final Logger log = LoggerFactory.getLogger(CacheUtils.class);
public static final String SYS_TEMP_DIR = System.getProperty("java.io.tmpdir");
public static final String XDG_CONFIG_HOME = ((Supplier<String>) () -> {
if (OsUtils.isUnix()) {
if (System.getenv("XDG_CONFIG_HOME") == null || System.getenv("XDG_CONFIG_HOME").isEmpty())
return System.getProperty("user.home") + "/.config";
else
return System.getenv("XDG_CONFIG_HOME");
}
return null;
}).get();
public static final String XDG_CACHE_HOME = ((Supplier<String>) () -> {
if (OsUtils.isUnix()) {
if (System.getenv("XDG_CACHE_HOME") == null || System.getenv("XDG_CACHE_HOME").isEmpty())
return System.getProperty("user.home") + "/.cache";
else
return System.getenv("XDG_CACHE_HOME");
}
return null;
}).get();
private CacheUtils() {}
public static Path getTempDir() {
return getSystemTempDir().resolve(ThisAppProps.APP_TEMP_DIR);
}
private static Path locateTempFile(Path tempDir, String fn) throws FileNotFoundException {
Path file = tempDir.resolve(fn);
if (!Files.exists(file))
throw new FileNotFoundException("File '" + fn + "' not found in: " + tempDir.toString());
if (!Files.isRegularFile(file) || !Files.isReadable(file))
throw new FileNotFoundException("File '" + file.toString() + "' is not a regular readable file.");
return file;
}
/**
* Search for an existing file in known (new and old) locations.
*/
public static Path locateTempFile(String fn) throws FileNotFoundException {
return locateTempFile(getTempDir(), fn);
}
/**
* @param fn File-name for the temp file. Location is predetermined.
*/
public static Path getTempFile(String fn) {
Path p = getTempDir().resolve(fn);
if (!Files.exists(p.getParent())) {
try {
Files.createDirectories(p.getParent());
} catch (IOException e) {
throw new IllegalStateException("Could not create directory structure for a temporary file: " + p.toString());
}
}
return p;
}
/**
* System-wide temporary directory.
* @return
*/
public static Path getSystemTempDir() {
final String dir = OsUtils.isUnix() ? XDG_CONFIG_HOME + "/FragPipe" : SYS_TEMP_DIR;
if (dir == null || dir.isEmpty())
throw new IllegalStateException("Could not locate system-wide temporary directory");
return Paths.get(dir);
}
}