package com.itstherules.mediacentre.model;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import com.itstherules.stream.model.AnyItemsModel;
import com.itstherules.stream.model.DirectoriesModel;
public class HostScalar {
private static final String CONFIGURATION = "configuration";
public String value() {
try {
String name = new AnyItemsModel(CONFIGURATION).asList().get(0).getName();
if("".equals(name) || name == null){ throw new NullPointerException();}
return name;
} catch (Exception e) {
try {
return InetAddress.getLocalHost().getCanonicalHostName();
} catch (UnknownHostException e2) {
return "localhost";
}
}
}
public void persist(String hostName) throws IOException {
String itemPath = itemPath();
File directory = new File(itemPath);
for (File file : directory.listFiles()) {
file.delete();
}
File fileToWrite = new File(itemPath + hostName);
fileToWrite.createNewFile();
}
private String itemPath() {
DirectoriesModel directory = new DirectoriesModel(CONFIGURATION);
return directory.getDirectoryPath();
}
}