|
| 1 | +/*- |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2017 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics, University of |
| 8 | + * Konstanz, and KNIME GmbH. |
| 9 | + * %% |
| 10 | + * Redistribution and use in source and binary forms, with or without |
| 11 | + * modification, are permitted provided that the following conditions are met: |
| 12 | + * |
| 13 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 14 | + * this list of conditions and the following disclaimer. |
| 15 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 16 | + * this list of conditions and the following disclaimer in the documentation |
| 17 | + * and/or other materials provided with the distribution. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 20 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 21 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 22 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 23 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 24 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 25 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 29 | + * POSSIBILITY OF SUCH DAMAGE. |
| 30 | + * #L% |
| 31 | + */ |
| 32 | + |
| 33 | +package org.scijava.download; |
| 34 | + |
| 35 | +import java.io.File; |
| 36 | +import java.io.IOException; |
| 37 | + |
| 38 | +import org.scijava.io.location.FileLocation; |
| 39 | +import org.scijava.io.location.Location; |
| 40 | +import org.scijava.util.DigestUtils; |
| 41 | +import org.scijava.util.FileUtils; |
| 42 | + |
| 43 | +/** |
| 44 | + * A file-based implementation of {@link LocationCache}. |
| 45 | + * |
| 46 | + * @author Curtis Rueden |
| 47 | + */ |
| 48 | +public class DiskLocationCache implements LocationCache { |
| 49 | + |
| 50 | + private File baseDir = new File(System.getProperty("user.home") + |
| 51 | + File.separator + ".scijava" + File.separator + "cache" + File.separator); |
| 52 | + |
| 53 | + private boolean cacheFileLocations; |
| 54 | + |
| 55 | + // -- DiskLocationCache methods -- |
| 56 | + |
| 57 | + public File getBaseDirectory() { |
| 58 | + return baseDir; |
| 59 | + } |
| 60 | + |
| 61 | + public void setBaseDirectory(final File baseDir) { |
| 62 | + if (!baseDir.isDirectory()) { |
| 63 | + throw new IllegalArgumentException("Not a directory: " + baseDir); |
| 64 | + } |
| 65 | + this.baseDir = baseDir; |
| 66 | + } |
| 67 | + |
| 68 | + public boolean isFileLocationCachingEnabled() { |
| 69 | + return cacheFileLocations; |
| 70 | + } |
| 71 | + |
| 72 | + public void setFileLocationCachingEnabled(final boolean enabled) { |
| 73 | + // NB: It is possible the input file is stored on a volume which is much |
| 74 | + // slower than the local disk cache, so we make this setting configurable. |
| 75 | + cacheFileLocations = enabled; |
| 76 | + } |
| 77 | + |
| 78 | + // -- LocationCache methods -- |
| 79 | + |
| 80 | + @Override |
| 81 | + public boolean canCache(final Location source) { |
| 82 | + if (source instanceof FileLocation && !isFileLocationCachingEnabled()) { |
| 83 | + // The cache is not configured to cache files to other files. |
| 84 | + return false; |
| 85 | + } |
| 86 | + return source.getURI() != null; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public Location cachedLocation(final Location source) { |
| 91 | + if (!canCache(source)) { |
| 92 | + throw new IllegalArgumentException("Uncacheable source: " + source); |
| 93 | + } |
| 94 | + return new FileLocation(cachedData(source)); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public String loadChecksum(final Location source) throws IOException { |
| 99 | + final File cachedChecksum = cachedChecksum(source); |
| 100 | + if (!cachedChecksum.exists()) return null; |
| 101 | + return DigestUtils.string(FileUtils.readFile(cachedChecksum)); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public void saveChecksum(final Location source, final String checksum) |
| 106 | + throws IOException |
| 107 | + { |
| 108 | + final File cachedChecksum = cachedChecksum(source); |
| 109 | + FileUtils.writeFile(cachedChecksum, DigestUtils.bytes(checksum)); |
| 110 | + } |
| 111 | + |
| 112 | + // -- Helper methods -- |
| 113 | + |
| 114 | + private File cachedData(final Location source) { |
| 115 | + return cachedFile(source, ".data"); |
| 116 | + } |
| 117 | + |
| 118 | + private File cachedChecksum(final Location source) { |
| 119 | + return cachedFile(source, ".checksum"); |
| 120 | + } |
| 121 | + |
| 122 | + private File cachedFile(final Location source, final String suffix) { |
| 123 | + final String hexCode = Integer.toHexString(source.hashCode()); |
| 124 | + return new File(getBaseDirectory(), hexCode + suffix); |
| 125 | + } |
| 126 | +} |
0 commit comments