66 lines
1.4 KiB
Java
66 lines
1.4 KiB
Java
package org.gcube.smartgears.persistence;
|
|
|
|
|
|
import static org.gcube.smartgears.utils.Utils.fileAt;
|
|
import static org.gcube.smartgears.utils.Utils.notNull;
|
|
|
|
import java.io.File;
|
|
|
|
import org.gcube.common.validator.annotations.NotEmpty;
|
|
import org.gcube.common.validator.annotations.NotNull;
|
|
|
|
public class LocalPersistence implements PersistenceWriter {
|
|
|
|
@NotNull @NotEmpty
|
|
private String location;
|
|
|
|
protected LocalPersistence() {}
|
|
|
|
public LocalPersistence(String location) {
|
|
|
|
notNull("persistence location",location);
|
|
|
|
this.location=location;
|
|
|
|
validate();
|
|
}
|
|
|
|
public String getLocation() {
|
|
return location;
|
|
}
|
|
|
|
public void setLocation(String location) {
|
|
this.location = location;
|
|
}
|
|
|
|
|
|
@Override
|
|
public File writefile(String path) {
|
|
|
|
notNull("relative path", path);
|
|
|
|
return fileAt(new File(location, path).getAbsolutePath()).toWrite();
|
|
}
|
|
|
|
@Override
|
|
public File file(String path) {
|
|
|
|
notNull("relative path", path);
|
|
|
|
return fileAt(new File(location, path).getAbsolutePath()).toRead();
|
|
}
|
|
|
|
void initialize(){
|
|
|
|
}
|
|
|
|
public void validate() {
|
|
|
|
File locationDir = new File(location);
|
|
if (!(locationDir.exists() && locationDir.isDirectory() && locationDir.canRead() && locationDir.canWrite()))
|
|
throw new IllegalStateException("invalid node configuration: home "+location+" does not exist or is not a directory or cannot be accessed in read/write mode");
|
|
|
|
}
|
|
|
|
}
|