package geniusweb.actions; import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; import java.util.UUID; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; /** * This contains a "system independent" {@link File}. This is represented by a * {@link UUID}. See {@link #getFile()} to get the file referenced by this. The * file may or may not yet exist (depending on whether your party already wrote * some contents in that file in previous runs), may or may not be empty (your * choice), and can contain any type of data. The original intent of this is * that parties can store learned data in this file, while the name can be kept * for later re-use in later runs (eg negotiation sessions) on the same machine. *

* This file is to be used locally on some other machine (typically a * partiesserver). This object itself only contains a file name. * {@link #getFile()} turns it into a system-specific {@link File}.
*

WARNING

if used multiple times on different machines, multiple, * separate files will be involved, there is no magical synchronization of * multiple items with the same UUID used on different machines. */ public class FileLocation { @JsonValue private final UUID name; private final static String TMP = System.getProperty("java.io.tmpdir"); private final static String GENIUSWEB = "geniusweb"; private final static Path rootpath = Paths.get(TMP, GENIUSWEB); private final static File rootfolder = rootpath.toFile(); /** * @param name the name of the file (must be UUID to prevent injection of * bad filenames like /.. or C:) Use {@link #FileLocation()} to * create new FileLocation. That should ensure a new UUID that * does not collide with existing ones. */ @JsonCreator public FileLocation(UUID name) { if (!rootfolder.exists()) { rootfolder.mkdir(); System.out .println("created FileLocation root dir at " + rootfolder); } this.name = name; } /** * Creates a new "random" FileLocation. */ public FileLocation() { this(UUID.randomUUID()); } /** * @return the UUID string */ public String getUUIDString() { return name.toString(); } /** * * @return Actual file that can be used for read/write operations. This file * usually resides in the geniusweb folder inside the java.io.tmpdir * system property. This temp dir depends on the run configuration, * eg some directory inside the user's home directory, or a temp * folder inside tomcat. */ public File getFile() { return Paths.get(TMP, GENIUSWEB, name.toString()).toFile(); } @Override public String toString() { return "FileLocation[" + name + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; FileLocation other = (FileLocation) obj; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } }