|
|
@ -1,8 +1,9 @@ |
|
|
|
package com.sothr.imagetools.util |
|
|
|
|
|
|
|
import java.util.Properties |
|
|
|
import com.typesafe.config.{Config, ConfigFactory} |
|
|
|
import grizzled.slf4j.Logging |
|
|
|
import java.io.{FileInputStream, FileOutputStream, OutputStream} |
|
|
|
import java.io.{File, PrintStream, FileOutputStream} |
|
|
|
import java.util.Properties |
|
|
|
import scala.collection.JavaConversions._ |
|
|
|
|
|
|
|
/* |
|
|
@ -10,107 +11,109 @@ import scala.collection.JavaConversions._ |
|
|
|
*/ |
|
|
|
object PropertiesService extends Logging { |
|
|
|
|
|
|
|
private val properties:Properties = new Properties() |
|
|
|
private var defaultConf:Config = null |
|
|
|
private var userConf:Config = null |
|
|
|
private var newUserConf:Properties = new Properties() |
|
|
|
private var version:Version = null |
|
|
|
def getVersion:Version = this.version |
|
|
|
private val propertiesToClean:Array[String] = Array("version") |
|
|
|
|
|
|
|
//specific highly used properties |
|
|
|
var TimingEnabled:Boolean = false |
|
|
|
|
|
|
|
//ahash |
|
|
|
var aHashPrecision = 0 |
|
|
|
var aHashTolerance = 0 |
|
|
|
var aHashWeight = 0.0f |
|
|
|
var useAhash = false |
|
|
|
//dhash |
|
|
|
var dHashPrecision = 0 |
|
|
|
var dHashTolerance = 0 |
|
|
|
var dHashWeight = 0.0f |
|
|
|
var useDhash = false |
|
|
|
//phash |
|
|
|
var pHashPrecision = 0 |
|
|
|
var pHashTolerance = 0 |
|
|
|
var pHashWeight = 0.0f |
|
|
|
var usePhash = false |
|
|
|
|
|
|
|
/* |
|
|
|
* Load the properties file from the specified location |
|
|
|
*/ |
|
|
|
def loadProperties(defaultLocation:String, userLocation:String = null) = { |
|
|
|
info(s"Attempting to load properties from: $defaultLocation") |
|
|
|
val defaultInputStream = ResourceLoader.get.getResourceStream(defaultLocation) |
|
|
|
properties.load(defaultInputStream) |
|
|
|
defaultConf = ConfigFactory.load(defaultLocation); |
|
|
|
if (userLocation != null) { |
|
|
|
val userInputStream = new FileInputStream(userLocation) |
|
|
|
val userProperties = new Properties(); |
|
|
|
userProperties.loadFromXML(userInputStream) |
|
|
|
|
|
|
|
for (propertyName:String <- userProperties.stringPropertyNames()) { |
|
|
|
properties.setProperty(propertyName, userProperties.getProperty(propertyName)); |
|
|
|
} |
|
|
|
userConf = ConfigFactory.parseFile(new File(userLocation)); |
|
|
|
} else { |
|
|
|
userConf = ConfigFactory.empty |
|
|
|
info("No user properties file exists to load from") |
|
|
|
} |
|
|
|
version = new Version(properties.getProperty("version")); |
|
|
|
version = new Version(get(PropertiesEnum.Version.toString)); |
|
|
|
info(s"Detected Version: $version") |
|
|
|
|
|
|
|
//load special properties |
|
|
|
DebugLogEnabled = get(PropertiesEnum.LogDebug.toString).toBoolean |
|
|
|
InfoLogEnabled = get(PropertiesEnum.LogInfo.toString).toBoolean |
|
|
|
ErrorLogEnabled = get(PropertiesEnum.LogError.toString).toBoolean |
|
|
|
TimingEnabled = get(PropertiesEnum.Timed.toString).toBoolean |
|
|
|
|
|
|
|
//ahash |
|
|
|
aHashPrecision = PropertiesService.get(PropertiesEnum.AhashPrecision.toString).toInt |
|
|
|
aHashTolerance = PropertiesService.get(PropertiesEnum.AhashTolerance.toString).toInt |
|
|
|
aHashWeight = PropertiesService.get(PropertiesEnum.AhashWeight.toString).toFloat |
|
|
|
useAhash = PropertiesService.get(PropertiesEnum.UseAhash.toString).toBoolean |
|
|
|
aHashPrecision = get(PropertiesEnum.AhashPrecision.toString).toInt |
|
|
|
aHashTolerance = get(PropertiesEnum.AhashTolerance.toString).toInt |
|
|
|
aHashWeight = get(PropertiesEnum.AhashWeight.toString).toFloat |
|
|
|
useAhash = get(PropertiesEnum.UseAhash.toString).toBoolean |
|
|
|
//dhash |
|
|
|
dHashPrecision = PropertiesService.get(PropertiesEnum.DhashPrecision.toString).toInt |
|
|
|
dHashTolerance = PropertiesService.get(PropertiesEnum.DhashTolerance.toString).toInt |
|
|
|
dHashWeight = PropertiesService.get(PropertiesEnum.DhashWeight.toString).toFloat |
|
|
|
useDhash = PropertiesService.get(PropertiesEnum.UseDhash.toString).toBoolean |
|
|
|
dHashPrecision = get(PropertiesEnum.DhashPrecision.toString).toInt |
|
|
|
dHashTolerance = get(PropertiesEnum.DhashTolerance.toString).toInt |
|
|
|
dHashWeight = get(PropertiesEnum.DhashWeight.toString).toFloat |
|
|
|
useDhash = get(PropertiesEnum.UseDhash.toString).toBoolean |
|
|
|
//phash |
|
|
|
pHashPrecision = PropertiesService.get(PropertiesEnum.PhashPrecision.toString).toInt |
|
|
|
pHashTolerance = PropertiesService.get(PropertiesEnum.PhashTolerance.toString).toInt |
|
|
|
pHashWeight = PropertiesService.get(PropertiesEnum.PhashWeight.toString).toFloat |
|
|
|
usePhash = PropertiesService.get(PropertiesEnum.UsePhash.toString).toBoolean |
|
|
|
pHashPrecision = get(PropertiesEnum.PhashPrecision.toString).toInt |
|
|
|
pHashTolerance = get(PropertiesEnum.PhashTolerance.toString).toInt |
|
|
|
pHashWeight = get(PropertiesEnum.PhashWeight.toString).toFloat |
|
|
|
usePhash = get(PropertiesEnum.UsePhash.toString).toBoolean |
|
|
|
info("Loaded Special Properties") |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Gets a properties object that is cleaned of things that are expected to change. i.e. version |
|
|
|
*/ |
|
|
|
private def getCleanProperties():Properties = { |
|
|
|
val cleanProperties:Properties = properties.clone().asInstanceOf[Properties] |
|
|
|
//Remove properties to be cleaned |
|
|
|
for (key <- propertiesToClean) { |
|
|
|
cleanProperties.remove(key) |
|
|
|
private def cleanAndPrepareNewUserProperties():Properties = { |
|
|
|
//insert special keys here |
|
|
|
newUserConf.setProperty(PropertiesEnum.PreviousVersion.toString, version.parsableToString()) |
|
|
|
//remove special keys here |
|
|
|
newUserConf.remove(PropertiesEnum.Version.toString) |
|
|
|
newUserConf |
|
|
|
} |
|
|
|
return cleanProperties |
|
|
|
|
|
|
|
private def getCleanedMergedUserConf():Config = { |
|
|
|
|
|
|
|
ConfigFactory.parseProperties(cleanAndPrepareNewUserProperties()) withFallback(userConf) |
|
|
|
} |
|
|
|
|
|
|
|
def saveXMLProperties(location:String) = { |
|
|
|
def saveConf(location:String) = { |
|
|
|
info(s"Saving user properties to $location") |
|
|
|
val out:OutputStream = new FileOutputStream(location, false) |
|
|
|
val cleanProperties = getCleanProperties |
|
|
|
//insert special keys here |
|
|
|
cleanProperties.setProperty("version.previous", version.parsableToString()) |
|
|
|
cleanProperties.storeToXML(out, "User Properties") |
|
|
|
val out:PrintStream = new PrintStream(new FileOutputStream(location, false)) |
|
|
|
val userConfToSave = getCleanedMergedUserConf |
|
|
|
//print to the output stream |
|
|
|
out.print(userConfToSave.root.render) |
|
|
|
out.flush() |
|
|
|
out.close() |
|
|
|
} |
|
|
|
|
|
|
|
def get(key:String):String = { |
|
|
|
return properties.getProperty(key) |
|
|
|
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) |
|
|
|
} |
|
|
|
return result |
|
|
|
} |
|
|
|
|
|
|
|
def set(key:String, value:String) = { |
|
|
|
properties.setProperty(key, value) |
|
|
|
newUserConf.setProperty(key, value) |
|
|
|
} |
|
|
|
|
|
|
|
//specific highly used properties |
|
|
|
var DebugLogEnabled:Boolean = false |
|
|
|
var InfoLogEnabled:Boolean = false |
|
|
|
var ErrorLogEnabled:Boolean = false |
|
|
|
var TimingEnabled:Boolean = false |
|
|
|
|
|
|
|
//ahash |
|
|
|
var aHashPrecision = 0 |
|
|
|
var aHashTolerance = 0 |
|
|
|
var aHashWeight = 0.0f |
|
|
|
var useAhash = false |
|
|
|
//dhash |
|
|
|
var dHashPrecision = 0 |
|
|
|
var dHashTolerance = 0 |
|
|
|
var dHashWeight = 0.0f |
|
|
|
var useDhash = false |
|
|
|
//phash |
|
|
|
var pHashPrecision = 0 |
|
|
|
var pHashTolerance = 0 |
|
|
|
var pHashWeight = 0.0f |
|
|
|
var usePhash = false |
|
|
|
|
|
|
|
} |