JAL-3130 adapted getdown src. attempt 2. first attempt failed due to cp'ed .git files
[jalview.git] / getdown / src / getdown / core / src / main / java / com / threerings / getdown / cache / ResourceCache.java
1 //
2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
5
6 package com.threerings.getdown.cache;
7
8 import java.io.File;
9 import java.io.IOException;
10
11 import com.threerings.getdown.util.FileUtil;
12
13 /**
14  * Maintains a cache of code resources. The cache allows multiple application instances of different
15  * versions to open at the same time.
16  */
17 public class ResourceCache
18 {
19     public ResourceCache (File _cacheDir) throws IOException
20     {
21         this._cacheDir = _cacheDir;
22         createDirectoryIfNecessary(_cacheDir);
23     }
24
25     private void createDirectoryIfNecessary (File dir) throws IOException
26     {
27         if (!dir.exists() && !dir.mkdirs()) {
28             throw new IOException("unable to create directory: " + dir.getAbsolutePath());
29         }
30     }
31
32     /**
33      * Caches the given file under its {@code digest}.
34      * @param fileToCache file to cache.
35      * @param cacheSubdir the subdirectory of the cache directory in which to store the cached
36      * file. Usually either {@code digest} or a prefix of {@code digest}.
37      * @param digest a crypto digest of the cached files contents.
38      * @return the cached file.
39      */
40     public File cacheFile (File fileToCache, String cacheSubdir, String digest) throws IOException
41     {
42         File cacheLocation = new File(_cacheDir, cacheSubdir);
43         createDirectoryIfNecessary(cacheLocation);
44
45         File cachedFile = new File(cacheLocation, digest + getFileSuffix(fileToCache));
46         File lastAccessedFile = new File(
47                 cacheLocation, cachedFile.getName() + LAST_ACCESSED_FILE_SUFFIX);
48
49         if (!cachedFile.exists()) {
50             createNewFile(cachedFile);
51             FileUtil.copy(fileToCache, cachedFile);
52         }
53
54         if (lastAccessedFile.exists()) {
55             lastAccessedFile.setLastModified(System.currentTimeMillis());
56         } else {
57             createNewFile(lastAccessedFile);
58         }
59
60         return cachedFile;
61     }
62
63     private void createNewFile (File fileToCreate) throws IOException
64     {
65         if (!fileToCreate.exists() && !fileToCreate.createNewFile()) {
66             throw new IOException("unable to create new file: " + fileToCreate.getAbsolutePath());
67         }
68     }
69
70     private String getFileSuffix (File fileToCache) {
71         String fileName = fileToCache.getName();
72         int index = fileName.lastIndexOf(".");
73
74         return index > -1 ? fileName.substring(index) : "";
75     }
76
77     private final File _cacheDir;
78
79     static final String LAST_ACCESSED_FILE_SUFFIX = ".lastAccessed";
80 }