source: events/src/main/java/geniusweb/actions/FileLocation.java@ 50

Last change on this file since 50 was 50, checked in by bart, 2 years ago

Fixed small issues in domaineditor.

File size: 3.2 KB
Line 
1package geniusweb.actions;
2
3import java.io.File;
4import java.nio.file.Path;
5import java.nio.file.Paths;
6import java.util.UUID;
7
8import com.fasterxml.jackson.annotation.JsonCreator;
9import com.fasterxml.jackson.annotation.JsonValue;
10
11/**
12 * This contains a "system independent" {@link File}. This is represented by a
13 * {@link UUID}. See {@link #getFile()} to get the file referenced by this. The
14 * file may or may not yet exist (depending on whether your party already wrote
15 * some contents in that file in previous runs), may or may not be empty (your
16 * choice), and can contain any type of data. The original intent of this is
17 * that parties can store learned data in this file, while the name can be kept
18 * for later re-use in later runs (eg negotiation sessions) on the same machine.
19 * <p>
20 * This file is to be used locally on some other machine (typically a
21 * partiesserver). This object itself only contains a file name.
22 * {@link #getFile()} turns it into a system-specific {@link File}. <br>
23 * <h2>WARNING</h2> if used multiple times on different machines, multiple,
24 * separate files will be involved, there is no magical synchronization of
25 * multiple items with the same UUID used on different machines.
26 */
27public class FileLocation {
28
29 @JsonValue
30 private final UUID name;
31
32 private final static String TMP = System.getProperty("java.io.tmpdir");
33 private final static String GENIUSWEB = "geniusweb";
34 private final static Path rootpath = Paths.get(TMP, GENIUSWEB);
35 private final static File rootfolder = rootpath.toFile();
36
37 /**
38 * @param name the name of the file (must be UUID to prevent injection of
39 * bad filenames like /.. or C:) Use {@link #FileLocation()} to
40 * create new FileLocation. That should ensure a new UUID that
41 * does not collide with existing ones.
42 */
43 @JsonCreator
44 public FileLocation(UUID name) {
45 if (!rootfolder.exists()) {
46 rootfolder.mkdir();
47 System.out
48 .println("created FileLocation root dir at " + rootfolder);
49 }
50
51 this.name = name;
52 }
53
54 /**
55 * Creates a new "random" FileLocation.
56 */
57 public FileLocation() {
58 this(UUID.randomUUID());
59 }
60
61 /**
62 * @return the UUID string
63 */
64 public String getUUIDString() {
65 return name.toString();
66 }
67
68 /**
69 *
70 * @return Actual file that can be used for read/write operations. This file
71 * usually resides in the geniusweb folder inside the java.io.tmpdir
72 * system property. This temp dir depends on the run configuration,
73 * eg some directory inside the user's home directory, or a temp
74 * folder inside tomcat.
75 */
76 public File getFile() {
77 return Paths.get(TMP, GENIUSWEB, name.toString()).toFile();
78 }
79
80 @Override
81 public String toString() {
82 return "FileLocation[" + name + "]";
83 }
84
85 @Override
86 public int hashCode() {
87 final int prime = 31;
88 int result = 1;
89 result = prime * result + ((name == null) ? 0 : name.hashCode());
90 return result;
91 }
92
93 @Override
94 public boolean equals(Object obj) {
95 if (this == obj)
96 return true;
97 if (obj == null)
98 return false;
99 if (getClass() != obj.getClass())
100 return false;
101 FileLocation other = (FileLocation) obj;
102 if (name == null) {
103 if (other.name != null)
104 return false;
105 } else if (!name.equals(other.name))
106 return false;
107 return true;
108 }
109}
Note: See TracBrowser for help on using the repository browser.