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.

131 lines
4.3 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. package com.sothr.imagetools.engine.util
  2. import java.io.{File, FileOutputStream, PrintStream}
  3. import java.util.Properties
  4. import com.typesafe.config.{Config, ConfigFactory, ConfigRenderOptions}
  5. import grizzled.slf4j.Logging
  6. /*
  7. * Service for loading and interacting with the properties file
  8. */
  9. object PropertiesService extends Logging {
  10. //OS information
  11. val OS = System.getProperty("os.name", "UNKNOWN")
  12. val OS_VERSION = System.getProperty("os.version", "UNKNOWN")
  13. val OS_ARCH = System.getProperty("os.arch", "UNKNOWN")
  14. private val newUserConf: Properties = new Properties()
  15. private val configRenderOptions = ConfigRenderOptions.concise().setFormatted(true)
  16. //specific highly used properties
  17. var TimingEnabled: Boolean = false
  18. //ahash
  19. var aHashPrecision = 0
  20. var aHashTolerance = 0
  21. var aHashWeight = 0.0f
  22. var useAhash = false
  23. //dhash
  24. var dHashPrecision = 0
  25. var dHashTolerance = 0
  26. var dHashWeight = 0.0f
  27. var useDhash = false
  28. //phash
  29. var pHashPrecision = 0
  30. var pHashTolerance = 0
  31. var pHashWeight = 0.0f
  32. var usePhash = false
  33. private var defaultConf: Config = null
  34. private var userConf: Config = null
  35. private var version: Version = null
  36. def getVersion: Version = this.version
  37. /*
  38. * Load the properties file from the specified location
  39. */
  40. def loadProperties(defaultLocation: String, userLocation: String = null) = {
  41. info(s"Attempting to load properties from: $defaultLocation")
  42. defaultConf = ConfigFactory.load(defaultLocation)
  43. if (userLocation != null) {
  44. userConf = ConfigFactory.parseFile(new File(userLocation))
  45. } else {
  46. userConf = ConfigFactory.empty
  47. info("No user properties file exists to load from")
  48. }
  49. version = new Version(get(PropertyEnum.Version.toString))
  50. info(s"Detected Version: $version")
  51. //load special properties
  52. TimingEnabled = get(PropertyEnum.Timed.toString).toBoolean
  53. //ahash
  54. aHashPrecision = get(PropertyEnum.AhashPrecision.toString).toInt
  55. aHashTolerance = get(PropertyEnum.AhashTolerance.toString).toInt
  56. aHashWeight = get(PropertyEnum.AhashWeight.toString).toFloat
  57. useAhash = get(PropertyEnum.UseAhash.toString).toBoolean
  58. //dhash
  59. dHashPrecision = get(PropertyEnum.DhashPrecision.toString).toInt
  60. dHashTolerance = get(PropertyEnum.DhashTolerance.toString).toInt
  61. dHashWeight = get(PropertyEnum.DhashWeight.toString).toFloat
  62. useDhash = get(PropertyEnum.UseDhash.toString).toBoolean
  63. //phash
  64. pHashPrecision = get(PropertyEnum.PhashPrecision.toString).toInt
  65. pHashTolerance = get(PropertyEnum.PhashTolerance.toString).toInt
  66. pHashWeight = get(PropertyEnum.PhashWeight.toString).toFloat
  67. usePhash = get(PropertyEnum.UsePhash.toString).toBoolean
  68. info("Loaded Special Properties")
  69. }
  70. def get(key: String, defaultValue: String = null): String = {
  71. var result: String = defaultValue
  72. //check the latest properties
  73. if (newUserConf.containsKey(key)) {
  74. result = newUserConf.getProperty(key)
  75. }
  76. //check the loaded user properties
  77. else if (userConf.hasPath(key)) {
  78. result = userConf.getString(key)
  79. }
  80. //check the default properties
  81. else if (defaultConf.hasPath(key)) {
  82. result = defaultConf.getString(key)
  83. }
  84. result
  85. }
  86. def saveConf(location: String) = {
  87. info(s"Saving user properties to $location")
  88. val out: PrintStream = new PrintStream(new FileOutputStream(location, false))
  89. val userConfToSave = getCleanedMergedUserConf
  90. //print to the output stream
  91. out.print(userConfToSave.root().render(configRenderOptions))
  92. out.flush()
  93. out.close()
  94. }
  95. private def getCleanedMergedUserConf: Config = {
  96. ConfigFactory.parseProperties(cleanAndPrepareNewUserProperties()) withFallback userConf
  97. }
  98. private def cleanAndPrepareNewUserProperties(): Properties = {
  99. //insert special keys here
  100. newUserConf.setProperty(PropertyEnum.PreviousVersion.toString, version.parsableToString())
  101. //remove special keys here
  102. newUserConf.remove(PropertyEnum.Version.toString)
  103. newUserConf
  104. }
  105. def has(key: String): Boolean = {
  106. var result = false
  107. if (newUserConf.containsKey(key)
  108. || userConf.hasPath(key)
  109. || defaultConf.hasPath(key)) {
  110. result = true
  111. }
  112. result
  113. }
  114. def set(key: String, value: String) = {
  115. newUserConf.setProperty(key, value)
  116. }
  117. }