[31] | 1 | package geniusweb.actions;
|
---|
| 2 |
|
---|
| 3 | import java.io.File;
|
---|
| 4 | import java.nio.file.Path;
|
---|
| 5 | import java.nio.file.Paths;
|
---|
| 6 | import java.util.UUID;
|
---|
| 7 |
|
---|
| 8 | import com.fasterxml.jackson.annotation.JsonCreator;
|
---|
| 9 | import com.fasterxml.jackson.annotation.JsonValue;
|
---|
| 10 |
|
---|
| 11 | /**
|
---|
| 12 | * This contains a "system independent" {@link File}. This is represented by a
|
---|
| 13 | * {@link UUID}. The file may or may not yet exist (depending on whether your
|
---|
| 14 | * party already wrote some contents in that file in previous runs), may or may
|
---|
| 15 | * not be empty (your choice), and can contain any type of data. The original
|
---|
| 16 | * intent of this is that parties can store learned data in this file, while the
|
---|
| 17 | * name can be kept for later re-use in later runs (eg negotiation sessions) on
|
---|
| 18 | * 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 | */
|
---|
| 27 | public 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 | this.name = name;
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
| 49 | * Default constructor creates a new "random" FileLocation.
|
---|
| 50 | */
|
---|
| 51 | public FileLocation() {
|
---|
| 52 | if (!rootfolder.exists()) {
|
---|
| 53 | rootfolder.mkdir();
|
---|
| 54 | System.out
|
---|
| 55 | .println("created FileLocation root dir at " + rootfolder);
|
---|
| 56 | }
|
---|
| 57 | this.name = UUID.randomUUID();
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | /**
|
---|
| 61 | *
|
---|
| 62 | * @return the UUID string
|
---|
| 63 | */
|
---|
| 64 | public String getUUIDString() {
|
---|
| 65 | return name.toString();
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | public File getFile() {
|
---|
| 69 | return Paths.get(TMP, GENIUSWEB, name.toString()).toFile();
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | @Override
|
---|
| 73 | public String toString() {
|
---|
| 74 | return "FileLocation[" + name + "]";
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | @Override
|
---|
| 78 | public int hashCode() {
|
---|
| 79 | final int prime = 31;
|
---|
| 80 | int result = 1;
|
---|
| 81 | result = prime * result + ((name == null) ? 0 : name.hashCode());
|
---|
| 82 | return result;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | @Override
|
---|
| 86 | public boolean equals(Object obj) {
|
---|
| 87 | if (this == obj)
|
---|
| 88 | return true;
|
---|
| 89 | if (obj == null)
|
---|
| 90 | return false;
|
---|
| 91 | if (getClass() != obj.getClass())
|
---|
| 92 | return false;
|
---|
| 93 | FileLocation other = (FileLocation) obj;
|
---|
| 94 | if (name == null) {
|
---|
| 95 | if (other.name != null)
|
---|
| 96 | return false;
|
---|
| 97 | } else if (!name.equals(other.name))
|
---|
| 98 | return false;
|
---|
| 99 | return true;
|
---|
| 100 | }
|
---|
| 101 | } |
---|