Files
sheepit-shadow-nabber/src/com/sheepit/client/SettingsLoader.java

261 lines
6.0 KiB
Java
Raw Normal View History

2015-04-08 20:45:54 +01:00
package com.sheepit.client;
2015-03-31 00:29:58 +01:00
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.HashSet;
2015-03-31 00:29:58 +01:00
import java.util.Properties;
import java.util.Set;
2015-03-31 00:29:58 +01:00
import com.sheepit.client.Configuration;
import com.sheepit.client.Configuration.ComputeType;
import com.sheepit.client.hardware.gpu.GPU;
import com.sheepit.client.hardware.gpu.GPUDevice;
public class SettingsLoader {
private String path;
private String login;
private String password;
private String computeMethod;
private String gpu;
private String cores;
2015-03-31 00:29:58 +01:00
private String cacheDir;
private String autoSignIn;
2015-04-08 20:37:53 +01:00
private String ui;
2015-03-31 00:29:58 +01:00
public SettingsLoader() {
2015-04-07 20:07:53 +01:00
path = getDefaultFilePath();
}
public SettingsLoader(String path_) {
path = path_;
2015-03-31 00:29:58 +01:00
}
public SettingsLoader(String login_, String password_, ComputeType computeMethod_, GPUDevice gpu_, int cores_, String cacheDir_, boolean autoSignIn_, String ui_) {
2015-04-07 20:07:53 +01:00
path = getDefaultFilePath();
2015-03-31 00:29:58 +01:00
login = login_;
password = password_;
cacheDir = cacheDir_;
autoSignIn = String.valueOf(autoSignIn_);
2015-04-08 20:37:53 +01:00
ui = ui_;
if (cores_ > 0) {
cores = String.valueOf(cores_);
}
2015-03-31 00:29:58 +01:00
if (computeMethod_ != null) {
try {
computeMethod = computeMethod_.name();
}
catch (IllegalArgumentException e) {
}
}
if (gpu_ != null) {
gpu = gpu_.getCudaName();
}
}
2015-04-07 20:07:53 +01:00
public static String getDefaultFilePath() {
return System.getProperty("user.home") + File.separator + ".sheepit.conf";
2015-03-31 00:29:58 +01:00
}
public String getFilePath() {
return path;
}
2015-03-31 00:29:58 +01:00
public void saveFile() {
Properties prop = new Properties();
OutputStream output = null;
try {
output = new FileOutputStream(path);
if (cacheDir != null) {
prop.setProperty("cache-dir", cacheDir);
}
if (computeMethod != null) {
prop.setProperty("compute-method", computeMethod);
}
if (gpu != null) {
prop.setProperty("compute-gpu", gpu);
}
if (cores != null) {
prop.setProperty("cpu-cores", cores);
}
2015-03-31 00:29:58 +01:00
if (login != null) {
prop.setProperty("login", login);
}
if (password != null) {
prop.setProperty("password", password);
}
if (autoSignIn != null) {
prop.setProperty("auto-signin", autoSignIn);
}
2015-04-08 20:37:53 +01:00
if (ui != null) {
prop.setProperty("ui", ui);
}
2015-03-31 00:29:58 +01:00
prop.store(output, null);
}
catch (IOException io) {
io.printStackTrace();
}
finally {
if (output != null) {
try {
output.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
// Set Owner read/write
Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
perms.add(PosixFilePermission.OWNER_READ);
perms.add(PosixFilePermission.OWNER_WRITE);
try {
Files.setPosixFilePermissions(Paths.get(path), perms);
}
catch (UnsupportedOperationException e) {
// most likely because it's MS Windows
}
catch (IOException e) {
e.printStackTrace();
}
2015-03-31 00:29:58 +01:00
}
public void loadFile() {
this.login = null;
this.password = null;
this.computeMethod = null;
this.gpu = null;
this.cacheDir = null;
this.autoSignIn = null;
2015-04-08 20:37:53 +01:00
this.ui = null;
2015-03-31 00:29:58 +01:00
if (new File(path).exists() == false) {
return;
}
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream(path);
prop.load(input);
if (prop.containsKey("cache-dir")) {
this.cacheDir = prop.getProperty("cache-dir");
}
if (prop.containsKey("compute-method")) {
this.computeMethod = prop.getProperty("compute-method");
}
if (prop.containsKey("compute-gpu")) {
this.gpu = prop.getProperty("compute-gpu");
}
if (prop.containsKey("cpu-cores")) {
this.cores = prop.getProperty("cpu-cores");
}
2015-03-31 00:29:58 +01:00
if (prop.containsKey("login")) {
this.login = prop.getProperty("login");
}
if (prop.containsKey("password")) {
this.password = prop.getProperty("password");
}
if (prop.containsKey("auto-signin")) {
this.autoSignIn = prop.getProperty("auto-signin");
}
2015-04-08 20:37:53 +01:00
if (prop.containsKey("ui")) {
this.ui = prop.getProperty("ui");
}
2015-03-31 00:29:58 +01:00
}
catch (IOException io) {
io.printStackTrace();
}
finally {
if (input != null) {
try {
input.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Merge the Settings file with the Configuration.
* The Configuration will have high priority.
*/
2015-03-31 00:29:58 +01:00
public void merge(Configuration config) {
if (config == null) {
System.out.println("SettingsLoader::merge config is null");
}
loadFile();
if (config.login().isEmpty() && login != null) {
2015-03-31 00:29:58 +01:00
config.setLogin(login);
}
if (config.password().isEmpty() && password != null) {
2015-03-31 00:29:58 +01:00
config.setPassword(password);
}
2015-04-07 20:37:47 +01:00
try {
if ((config.getComputeMethod() == null && computeMethod != null) || (computeMethod != null && config.getComputeMethod() != ComputeType.valueOf(computeMethod))) {
config.setComputeMethod(ComputeType.valueOf(computeMethod));
}
}
catch (IllegalArgumentException e) {
System.err.println("SettingsLoader::merge failed to handle compute method (raw value: '" + computeMethod + "')");
computeMethod = null;
2015-03-31 00:29:58 +01:00
}
if (config.getGPUDevice() == null && gpu != null) {
2015-03-31 00:29:58 +01:00
GPUDevice device = GPU.getGPUDevice(gpu);
if (device != null) {
config.setUseGPU(device);
}
}
if (config.getNbCores() == -1 && cores != null) {
config.setUseNbCores(Integer.valueOf(cores));
}
if (config.getUserSpecifiedACacheDir() == false && cacheDir != null && new File(cacheDir).exists()) {
2015-03-31 00:29:58 +01:00
config.setCacheDir(new File(cacheDir));
}
2015-04-08 20:37:53 +01:00
if (config.getUIType() == null && ui != null) {
config.setUIType(ui);
}
config.setAutoSignIn(Boolean.valueOf(autoSignIn));
2015-03-31 00:29:58 +01:00
}
@Override
public String toString() {
return "ConfigurationLoader [path=" + path + ", login=" + login + ", password=" + password + ", computeMethod=" + computeMethod + ", gpu=" + gpu + ", cacheDir=" + cacheDir + "]";
}
}