1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.devops.conga.plugins.aem.maven.model;
21
22 import java.io.File;
23 import java.io.IOException;
24 import java.util.Collection;
25 import java.util.LinkedHashSet;
26 import java.util.Set;
27
28 import org.jetbrains.annotations.NotNull;
29
30 import com.google.common.hash.HashCode;
31 import com.google.common.hash.Hashing;
32 import com.google.common.io.Files;
33
34 import io.wcm.devops.conga.generator.util.FileUtil;
35
36
37
38
39 public abstract class AbstractInstallableFile implements InstallableFile {
40
41 private final File file;
42 private final Set<String> variants;
43 private HashCode hashCode;
44
45 protected AbstractInstallableFile(File file, Collection<String> variants) {
46 this.file = file;
47 this.variants = new LinkedHashSet<>(variants);
48 }
49
50 @Override
51 @NotNull
52 public File getFile() {
53 return file;
54 }
55
56 @Override
57 @NotNull
58 public Set<String> getVariants() {
59 return variants;
60 }
61
62 @Override
63 @NotNull
64 public HashCode getHashCode() {
65 if (this.hashCode == null) {
66 this.hashCode = getHashCode(file);
67 }
68 return this.hashCode;
69 }
70
71 @Override
72 public String toString() {
73 return file.toString();
74 }
75
76 private static HashCode getHashCode(File file) {
77 try {
78 return Files.asByteSource(file).hash(Hashing.sha256());
79 }
80 catch (IOException ex) {
81 throw new IllegalArgumentException("Unable to get hashcode for " + FileUtil.getCanonicalPath(file), ex);
82 }
83 }
84
85 }