You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

129 lines
4.2 KiB

package com.sothr.imagetools.engine.util
import java.io.{File, FileOutputStream, PrintStream}
import java.util.Properties
import com.sothr.imagetools.hash.HashSetting
import com.typesafe.config.{Config, ConfigFactory, ConfigRenderOptions}
import grizzled.slf4j.Logging
/*
* Service for loading and interacting with the properties file
*/
object PropertiesService extends Logging {
//OS information
val OS: String = System.getProperty("os.name", "UNKNOWN")
val OS_VERSION: String = System.getProperty("os.version", "UNKNOWN")
val OS_ARCH: String = System.getProperty("os.arch", "UNKNOWN")
private val newUserConf: Properties = new Properties()
private val configRenderOptions = ConfigRenderOptions.concise().setFormatted(true)
//specific highly used properties
var TimingEnabled: Boolean = false
var aHashSettings = new HashSetting("AHash", false, 0, 0, 0.0f)
var dHashSettings = new HashSetting("DHash", false, 0, 0, 0.0f)
var pHashSettings = new HashSetting("PHash", false, 0, 0, 0.0f)
private var defaultConf: Config = _
private var userConf: Config = _
private var version: Version = _
def getVersion: Version = this.version
/*
* Load the properties file from the specified location
*/
def loadProperties(defaultLocation: String, userLocation: String = null) = {
info(s"Attempting to load properties from: $defaultLocation")
defaultConf = ConfigFactory.load(defaultLocation)
if (userLocation != null) {
userConf = ConfigFactory.parseFile(new File(userLocation))
} else {
userConf = ConfigFactory.empty
info("No user properties file exists to load from")
}
version = new Version(get(PropertyEnum.Version.toString))
info(s"Detected Version: $version")
//load special properties
TimingEnabled = get(PropertyEnum.Timed.toString).toBoolean
aHashSettings = new HashSetting(
"AHash",
get(PropertyEnum.UseAhash.toString).toBoolean,
get(PropertyEnum.AhashPrecision.toString).toInt,
get(PropertyEnum.AhashTolerance.toString).toInt,
get(PropertyEnum.AhashWeight.toString).toFloat
)
dHashSettings = new HashSetting(
"DHash",
get(PropertyEnum.UseDhash.toString).toBoolean,
get(PropertyEnum.DhashPrecision.toString).toInt,
get(PropertyEnum.DhashTolerance.toString).toInt,
get(PropertyEnum.DhashWeight.toString).toFloat
)
pHashSettings = new HashSetting(
"PHash",
get(PropertyEnum.UsePhash.toString).toBoolean,
get(PropertyEnum.PhashPrecision.toString).toInt,
get(PropertyEnum.PhashTolerance.toString).toInt,
get(PropertyEnum.PhashWeight.toString).toFloat
)
info("Loaded Special Properties")
}
def get(key: String, defaultValue: String = null): String = {
var result: String = defaultValue
//check the latest properties
if (newUserConf.containsKey(key)) {
result = newUserConf.getProperty(key)
}
//check the loaded user properties
else if (userConf.hasPath(key)) {
result = userConf.getString(key)
}
//check the default properties
else if (defaultConf.hasPath(key)) {
result = defaultConf.getString(key)
}
result
}
def saveConf(location: String) = {
info(s"Saving user properties to $location")
val out: PrintStream = new PrintStream(new FileOutputStream(location, false))
val userConfToSave = getCleanedMergedUserConf
//print to the output stream
out.print(userConfToSave.root().render(configRenderOptions))
out.flush()
out.close()
}
private def getCleanedMergedUserConf: Config = {
ConfigFactory.parseProperties(cleanAndPrepareNewUserProperties()) withFallback userConf
}
private def cleanAndPrepareNewUserProperties(): Properties = {
//insert special keys here
newUserConf.setProperty(PropertyEnum.PreviousVersion.toString, version.parsableToString())
//remove special keys here
newUserConf.remove(PropertyEnum.Version.toString)
newUserConf
}
def has(key: String): Boolean = {
var result = false
if (newUserConf.containsKey(key)
|| userConf.hasPath(key)
|| defaultConf.hasPath(key)) {
result = true
}
result
}
def set(key: String, value: String) = {
newUserConf.setProperty(key, value)
}
}