All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.twitter.finatra.validation.ValidationMessageResolver.scala Maven / Gradle / Ivy

There is a newer version: 2.1.6
Show newest version
package com.twitter.finatra.validation

import java.lang.annotation.Annotation
import java.util.Properties

class ValidationMessageResolver {

  val validationProperties = load

  //TODO: Use [T <: Annotation : Manifest] instead of clazz
  def resolve(clazz: Class[_ <: Annotation], values: Any*): String = {
    val unresolvedMessage = validationProperties.getProperty(clazz.getName)
    if(unresolvedMessage == null)
      "unable to resolve error message due to unknown validation annotation: " + clazz
    else
      unresolvedMessage.format(values: _*)
  }

  private def load: Properties = {
    val properties = new Properties()
    loadBaseProperties(properties)
    loadPropertiesFromClasspath(properties)
    properties
  }

  private def loadBaseProperties(properties: Properties) {
    properties.load(getClass.getResourceAsStream("/com/twitter/finatra/json/validation.properties"))
  }

  private def loadPropertiesFromClasspath(properties: Properties) {
    val validationPropertiesUrl = getClass.getResource("/validation.properties")
    if(validationPropertiesUrl != null) {
      properties.load(validationPropertiesUrl.openStream())
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy