org.yaml.snakeyaml.Yaml Scala Examples

The following examples show how to use org.yaml.snakeyaml.Yaml. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: Swagger.scala    From daf-semantics   with Apache License 2.0 5 votes vote down vote up
package controllers

import java.io.File
import java.util

import de.zalando.play.controllers.PlayBodyParsing._
import org.yaml.snakeyaml.Yaml
import play.api.mvc._

import scala.io.Source

class Swagger extends Controller {

  def notSpec = Seq("application.conf", "logback.xml", "routes")

  def listSpecs() = Action {
    val path = "conf"
    val file = new File(path)
    if (file != null && file.list() != null) {
      val files = file.listFiles().filterNot(_.isDirectory).map(_.getName).toSeq
      implicit val arrayMarshaller = anyToWritable[Seq[String]]("application/json")
      val names = files.filterNot(notSpec.contains).filterNot(_.startsWith("."))
      Ok(names)
    } else {
      NotFound("Path could not be found: " + file.getAbsolutePath)
    }
  }

  def swaggerSpec(name: String) = Action {
    implicit val mapMarshaller = anyToWritable[java.util.Map[_, _]]("application/json")
    getSpec(name).map(s => Ok(s)).getOrElse(NotFound(name))
  }

  private def getSpec(yamlPath: String) = {
    val yamlFile = Option(getClass.getClassLoader.getResource(yamlPath))
    // TODO: close the stream
    val yamlStr = yamlFile map { yaml => Source.fromURL(yaml).getLines().mkString("\n") }
    val javaMap = yamlStr map { new Yaml().load(_).asInstanceOf[util.Map[Any, Any]] }
    javaMap
  }
} 
Example 2
Source File: Swagger.scala    From daf-semantics   with Apache License 2.0 5 votes vote down vote up
package controllers

import java.io.File
import java.util

import de.zalando.play.controllers.PlayBodyParsing._
import org.yaml.snakeyaml.Yaml
import play.api.mvc._

import scala.io.Source

class Swagger extends Controller {

  def notSpec = Seq("application.conf", "logback.xml", "routes")

  def listSpecs() = Action {
    val path = "conf"
    val file = new File(path)
    if (file != null && file.list() != null) {
      val files = file.listFiles().filterNot(_.isDirectory).map(_.getName).toSeq
      implicit val arrayMarshaller = anyToWritable[Seq[String]]("application/json")
      val names = files.filterNot(notSpec.contains).filterNot(_.startsWith("."))
      Ok(names)
    } else {
      NotFound("Path could not be found: " + file.getAbsolutePath)
    }
  }

  def swaggerSpec(name: String) = Action {
    implicit val mapMarshaller = anyToWritable[java.util.Map[_, _]]("application/json")
    getSpec(name).map(s => Ok(s)).getOrElse(NotFound(name))
  }

  private def getSpec(yamlPath: String) = {
    val yamlFile = Option(getClass.getClassLoader.getResource(yamlPath))
    // TODO: close the stream
    val yamlStr = yamlFile map { yaml => Source.fromURL(yaml).getLines().mkString("\n") }
    val javaMap = yamlStr map { new Yaml().load(_).asInstanceOf[util.Map[Any, Any]] }
    javaMap
  }
} 
Example 3
Source File: Swagger.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package controllers

import java.io.File
import java.util

import de.zalando.play.controllers.PlayBodyParsing._
import org.yaml.snakeyaml.Yaml
import play.api.mvc._

import scala.io.Source

@SuppressWarnings(Array("org.wartremover.warts.Equals", "org.wartremover.warts.AsInstanceOf", "org.wartremover.warts.ExplicitImplicitTypes"))
class Swagger extends Controller {

  def notSpec = Seq("application.conf", "logback.xml", "routes")

  def listSpecs() = Action {
    val path = "conf"
    val file = new File(path)
    if (file != null && file.list() != null) {
      val files = file.listFiles().filterNot(_.isDirectory).map(_.getName).toSeq
      implicit val arrayMarshaller = anyToWritable[Seq[String]]("application/json")
      val names = files.filterNot(notSpec.contains).filterNot(_.startsWith("."))
      Ok(names)
    } else {
      NotFound("Path could not be found: " + file.getAbsolutePath)
    }
  }

  def swaggerSpec(name: String) = Action {
    implicit val mapMarshaller = anyToWritable[java.util.Map[_, _]]("application/json")
    getSpec(name).map(s => Ok(s)).getOrElse(NotFound(name))
  }

  private def getSpec(yamlPath: String) = {
    val yamlFile = Option(getClass.getClassLoader.getResource(yamlPath))
    val yamlStr = yamlFile map { yaml => Source.fromURL(yaml).getLines().mkString("\n") }
    val javaMap = yamlStr map { new Yaml().load(_).asInstanceOf[util.Map[Any, Any]] }
    javaMap
  }
} 
Example 4
Source File: Swagger.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package controllers

import java.io.File
import java.util

import de.zalando.play.controllers.PlayBodyParsing._
import org.yaml.snakeyaml.Yaml
import play.api.mvc._

import scala.io.Source

@SuppressWarnings(Array("org.wartremover.warts.Equals", "org.wartremover.warts.AsInstanceOf", "org.wartremover.warts.ExplicitImplicitTypes"))
class Swagger extends Controller {

  def notSpec = Seq("application.conf", "logback.xml", "routes")

  def listSpecs() = Action {
    val path = "conf"
    val file = new File(path)
    if (file != null && file.list() != null) {
      val files = file.listFiles().filterNot(_.isDirectory).map(_.getName).toSeq
      implicit val arrayMarshaller = anyToWritable[Seq[String]]("application/json")
      val names = files.filterNot(notSpec.contains).filterNot(_.startsWith("."))
      Ok(names)
    } else {
      NotFound("Path could not be found: " + file.getAbsolutePath)
    }
  }

  def swaggerSpec(name: String) = Action {
    implicit val mapMarshaller = anyToWritable[java.util.Map[_,_]]("application/json")
    getSpec(name).map(s => Ok(s)).getOrElse(NotFound(name))
  }

  private def getSpec(yamlPath: String) = {
    val yamlFile  = Option(getClass.getClassLoader.getResource(yamlPath))
    val yamlStr = yamlFile map { yaml => Source.fromURL(yaml).getLines().mkString("\n") }
    val javaMap = yamlStr map { new Yaml().load(_).asInstanceOf[util.Map[Any, Any]] }
    javaMap
  }
} 
Example 5
Source File: Swagger.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package controllers

import java.io.File
import java.util

import de.zalando.play.controllers.PlayBodyParsing._
import org.yaml.snakeyaml.Yaml
import play.api.mvc._

import scala.io.Source

@SuppressWarnings(Array("org.wartremover.warts.Equals", "org.wartremover.warts.AsInstanceOf", "org.wartremover.warts.ExplicitImplicitTypes"))
class Swagger extends Controller {

  def notSpec = Seq("application.conf", "logback.xml", "routes")

  def listSpecs() = Action {
    val path = "conf"
    val file = new File(path)
    if (file != null && file.list() != null) {
      val files = file.listFiles().filterNot(_.isDirectory).map(_.getName).toSeq
      implicit val arrayMarshaller = anyToWritable[Seq[String]]("application/json")
      val names = files.filterNot(notSpec.contains).filterNot(_.startsWith("."))
      Ok(names)
    } else {
      NotFound("Path could not be found: " + file.getAbsolutePath)
    }
  }

  def swaggerSpec(name: String) = Action {
    implicit val mapMarshaller = anyToWritable[java.util.Map[_, _]]("application/json")
    getSpec(name).map(s => Ok(s)).getOrElse(NotFound(name))
  }

  private def getSpec(yamlPath: String) = {
    val yamlFile = Option(getClass.getClassLoader.getResource(yamlPath))
    val yamlStr = yamlFile map { yaml => Source.fromURL(yaml).getLines().mkString("\n") }
    val javaMap = yamlStr map { new Yaml().load(_).asInstanceOf[util.Map[Any, Any]] }
    javaMap
  }
} 
Example 6
Source File: Swagger.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package controllers

import java.io.File
import java.util

import de.zalando.play.controllers.PlayBodyParsing._
import org.yaml.snakeyaml.Yaml
import play.api.mvc._

import scala.io.Source

@SuppressWarnings(Array("org.wartremover.warts.Equals", "org.wartremover.warts.AsInstanceOf", "org.wartremover.warts.ExplicitImplicitTypes"))
class Swagger extends Controller {

  def notSpec = Seq("application.conf", "logback.xml", "routes")

  def listSpecs() = Action {
    val path = "conf"
    val file = new File(path)
    if (file != null && file.list() != null) {
      val files = file.listFiles().filterNot(_.isDirectory).map(_.getName).toSeq
      implicit val arrayMarshaller = anyToWritable[Seq[String]]("application/json")
      val names = files.filterNot(notSpec.contains).filterNot(_.startsWith("."))
      Ok(names)
    } else {
      NotFound("Path could not be found: " + file.getAbsolutePath)
    }
  }

  def swaggerSpec(name: String) = Action {
    implicit val mapMarshaller = anyToWritable[java.util.Map[_,_]]("application/json")
    getSpec(name).map(s => Ok(s)).getOrElse(NotFound(name))
  }

  private def getSpec(yamlPath: String) = {
    val yamlFile  = Option(getClass.getClassLoader.getResource(yamlPath))
    val yamlStr = yamlFile map { yaml => Source.fromURL(yaml).getLines().mkString("\n") }
    val javaMap = yamlStr map { new Yaml().load(_).asInstanceOf[util.Map[Any, Any]] }
    javaMap
  }
} 
Example 7
Source File: Swagger.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package controllers

import java.io.File
import java.util

import de.zalando.play.controllers.PlayBodyParsing._
import org.yaml.snakeyaml.Yaml
import play.api.mvc._

import scala.io.Source

//@SuppressWarnings(Array("org.wartremover.warts.Equals", "org.wartremover.warts.AsInstanceOf", "org.wartremover.warts.ExplicitImplicitTypes"))
class Swagger extends Controller {

  def notSpec = Seq("application.conf", "logback.xml", "routes")

  def listSpecs() = Action {
    val path = "conf"
    val file = new File(path)
    if (file != null && file.list() != null) {
      val files = file.listFiles().filterNot(_.isDirectory).map(_.getName).toSeq
      implicit val arrayMarshaller = anyToWritable[Seq[String]]("application/json")
      val names = files.filterNot(notSpec.contains).filterNot(_.startsWith("."))
      Ok(names)
    } else {
      NotFound("Path could not be found: " + file.getAbsolutePath)
    }
  }

  def swaggerSpec(name: String) = Action {
    implicit val mapMarshaller = anyToWritable[java.util.Map[_,_]]("application/json")
    getSpec(name).map(s => Ok(s)).getOrElse(NotFound(name))
  }

  private def getSpec(yamlPath: String) = {
    val yamlFile  = Option(getClass.getClassLoader.getResource(yamlPath))
    val yamlStr = yamlFile map { yaml => Source.fromURL(yaml).getLines().mkString("\n") }
    val javaMap = yamlStr map { new Yaml().load(_).asInstanceOf[util.Map[Any, Any]] }
    javaMap
  }
} 
Example 8
Source File: Swagger.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package controllers

import java.io.File
import java.util

import de.zalando.play.controllers.PlayBodyParsing._
import org.yaml.snakeyaml.Yaml
import play.api.mvc._

import scala.io.Source

@SuppressWarnings(Array("org.wartremover.warts.Equals", "org.wartremover.warts.AsInstanceOf", "org.wartremover.warts.ExplicitImplicitTypes"))
class Swagger extends Controller {

  def notSpec = Seq("application.conf", "logback.xml", "routes")

  def listSpecs() = Action {
    val path = "conf"
    val file = new File(path)
    if (file != null && file.list() != null) {
      val files = file.listFiles().filterNot(_.isDirectory).map(_.getName).toSeq
      implicit val arrayMarshaller = anyToWritable[Seq[String]]("application/json")
      val names = files.filterNot(notSpec.contains).filterNot(_.startsWith("."))
      Ok(names)
    } else {
      NotFound("Path could not be found: " + file.getAbsolutePath)
    }
  }

  def swaggerSpec(name: String) = Action {
    implicit val mapMarshaller = anyToWritable[java.util.Map[_,_]]("application/json")
    getSpec(name).map(s => Ok(s)).getOrElse(NotFound(name))
  }

  private def getSpec(yamlPath: String) = {
    val yamlFile  = Option(getClass.getClassLoader.getResource(yamlPath))
    val yamlStr = yamlFile map { yaml => Source.fromURL(yaml).getLines().mkString("\n") }
    val javaMap = yamlStr map { new Yaml().load(_).asInstanceOf[util.Map[Any, Any]] }
    javaMap
  }
} 
Example 9
Source File: Swagger.scala    From daf   with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
package controllers

import java.io.File
import java.util

import de.zalando.play.controllers.PlayBodyParsing._
import org.yaml.snakeyaml.Yaml
import play.api.mvc._

import scala.io.Source

class Swagger extends Controller {

  def notSpec = Seq("application.conf", "logback.xml", "routes")

  def listSpecs() = Action {
    val path = "conf"
    val file = new File(path)
    if (file != null && file.list() != null) {
      val files = file.listFiles().filterNot(_.isDirectory).map(_.getName).toSeq
      implicit val arrayMarshaller = anyToWritable[Seq[String]]("application/json")
      val names = files.filterNot(notSpec.contains).filterNot(_.startsWith("."))
      Ok(names)
    } else {
      NotFound("Path could not be found: " + file.getAbsolutePath)
    }
  }

  def swaggerSpec(name: String) = Action {
    implicit val mapMarshaller = anyToWritable[java.util.Map[_, _]]("application/json")
    getSpec(name).map(s => Ok(s)).getOrElse(NotFound(name))
  }

  private def getSpec(yamlPath: String) = {
    val yamlFile = Option(getClass.getClassLoader.getResource(yamlPath))
    val yamlStr = yamlFile map { yaml => Source.fromURL(yaml).getLines().mkString("\n") }
    val javaMap = yamlStr map { new Yaml().load(_).asInstanceOf[util.Map[Any, Any]] }
    javaMap
  }
} 
Example 10
Source File: JavaKSYParser.scala    From kaitai_struct_compiler   with GNU General Public License v3.0 5 votes vote down vote up
package io.kaitai.struct.formats

import java.io._
import java.nio.charset.Charset
import java.util.{List => JList, Map => JMap}

import io.kaitai.struct.JavaMain.CLIConfig
import io.kaitai.struct.format.{ClassSpec, ClassSpecs}
import io.kaitai.struct.precompile.YAMLParserError
import io.kaitai.struct.{Log, Main}
import org.yaml.snakeyaml.constructor.SafeConstructor
import org.yaml.snakeyaml.error.MarkedYAMLException
import org.yaml.snakeyaml.representer.Representer
import org.yaml.snakeyaml.{DumperOptions, LoaderOptions, Yaml}

import scala.collection.JavaConversions._
import scala.concurrent.Await
import scala.concurrent.duration.Duration

object JavaKSYParser {
  def localFileToSpecs(yamlFilename: String, config: CLIConfig): ClassSpecs = {
    val firstSpec = fileNameToSpec(yamlFilename)
    val yamlDir = Option(new File(yamlFilename).getParent).getOrElse(".")
    val specs = new JavaClassSpecs(yamlDir, config.importPaths, firstSpec)

    Await.result(Main.importAndPrecompile(specs, config.runtime), Duration.Inf)
    specs
  }

  def fileNameToSpec(yamlFilename: String): ClassSpec = {
    Log.fileOps.info(() => s"reading $yamlFilename...")

    // This complex string of classes is due to the fact that Java's
    // default "FileReader" implementation always uses system locale,
    // which screws up encoding on some systems and screws up reading
    // UTF-8 files with BOM
    val fis = new FileInputStream(yamlFilename)
    val isr = new InputStreamReader(fis, Charset.forName("UTF-8"))
    val br = new BufferedReader(isr)
    try {
      val scalaSrc = readerToYaml(br)
      ClassSpec.fromYaml(scalaSrc)
    } catch {
      case marked: MarkedYAMLException =>
        val mark = marked.getProblemMark
        throw YAMLParserError(
          marked.getProblem,
          Some(yamlFilename),
          Some(mark.getLine + 1),
          Some(mark.getColumn + 1)
        )
    }
  }

  def getYamlLoader: Yaml = {
    val loaderOptions = new LoaderOptions
    loaderOptions.setAllowDuplicateKeys(false)
    new Yaml(
      new SafeConstructor,
      new Representer,
      new DumperOptions,
      loaderOptions
    )
  }

  def readerToYaml(reader: Reader): Any = {
    yamlJavaToScala(getYamlLoader.load(reader))
  }

  def stringToYaml(data: String): Any = {
    yamlJavaToScala(getYamlLoader.load(data))
  }

  def yamlJavaToScala(src: Any): Any = {
    src match {
      case jlist: JList[AnyRef] =>
        jlist.toList.map(yamlJavaToScala)
      case jmap: JMap[String, AnyRef] =>
        jmap.toMap.mapValues(yamlJavaToScala)
      case _: String =>
        src
      case _: Double =>
        src
      case _: Boolean =>
        src
      case javaInt: java.lang.Integer =>
        javaInt.intValue
      case javaLong: java.lang.Long =>
        javaLong.longValue
      case _: java.math.BigInteger =>
        src.toString
      case null =>
        // may be not the very best idea, but these nulls
        // should be handled by real parsing code, i.e. where
        // it tracks tree depth, etc.
        null
    }
  }
} 
Example 11
Source File: Parser.scala    From uap-scala   with Do What The F*ck You Want To Public License 5 votes vote down vote up
package org.uaparser.scala

import java.io.InputStream
import java.util.{ List => JList, Map => JMap }
import org.uaparser.scala.Device.DeviceParser
import org.uaparser.scala.OS.OSParser
import org.uaparser.scala.UserAgent.UserAgentParser
import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.constructor.SafeConstructor
import scala.collection.JavaConverters._
import scala.util.Try

case class Parser(userAgentParser: UserAgentParser, osParser: OSParser, deviceParser: DeviceParser)
    extends UserAgentStringParser {
  def parse(agent: String): Client =
    Client(userAgentParser.parse(agent), osParser.parse(agent), deviceParser.parse(agent))
}

object Parser {
  def fromInputStream(source: InputStream): Try[Parser] = Try {
    val yaml = new Yaml(new SafeConstructor)
    val javaConfig = yaml.load(source).asInstanceOf[JMap[String, JList[JMap[String, String]]]]
    val config = javaConfig.asScala.toMap.mapValues(_.asScala.toList.map(_.asScala.toMap.filterNot {
      case (_ , value) => value eq null
    }))
    val userAgentParser = UserAgentParser.fromList(config.getOrElse("user_agent_parsers", Nil))
    val osParser = OSParser.fromList(config.getOrElse("os_parsers", Nil))
    val deviceParser = DeviceParser.fromList(config.getOrElse("device_parsers", Nil))
    Parser(userAgentParser, osParser, deviceParser)
  }
  def default: Parser = fromInputStream(this.getClass.getResourceAsStream("/regexes.yaml")).get
} 
Example 12
Source File: HaProxyAclResolver.scala    From vamp   with Apache License 2.0 5 votes vote down vote up
package io.vamp.gateway_driver.haproxy

import io.vamp.model.parser._
import org.yaml.snakeyaml.Yaml

import scala.io.Source
import scala.util.Try
import scala.collection.JavaConverters._

case class AclNode(value: String) extends Operand

case class HaProxyAcls(acls: List[Acl], condition: Option[String])

trait HaProxyAclResolver extends ConditionDefinitionParser with BooleanFlatter {

  private val userAgents = {
    val reader = Source.fromURL(getClass.getResource("/io/vamp/gateway_driver/haproxy/user-agents.yml")).bufferedReader()
    try {
      new Yaml().load(reader).asInstanceOf[java.util.Map[String, String]].asScala.map { case (k, v) ⇒ k.toUpperCase → v }
    }
    finally {
      reader.close()
    }
  }

  def resolve(value: String): Option[HaProxyAcls] = {
    (load andThen expand andThen flatten)(value) match {
      case False ⇒ None
      case True  ⇒ Option(HaProxyAcls(Nil, None))
      case node  ⇒ acls(node) match { case acls ⇒ Option(HaProxyAcls(operands(acls).distinct, Option(condition(acls)))) }
    }
  }

  private def load: String ⇒ AstNode = value ⇒ Try(parse(value)).getOrElse(Value(value))

  private def acls(node: AstNode): AstNode = node match {
    case Host(value)                 ⇒ AclNode(s"req.hdr(host) $value")
    case Cookie(value)               ⇒ AclNode(s"req.cook($value) -m found")
    case Header(value)               ⇒ AclNode(s"req.hdr_cnt($value) gt 0")
    case UserAgent(value)            ⇒ AclNode(s"req.fhdr(User-Agent) -m sub '$value'")
    case CookieContains(name, value) ⇒ AclNode(s"req.cook_sub($name) $value")
    case HeaderContains(name, value) ⇒ AclNode(s"req.fhdr($name) -m sub '$value'")
    case Negation(Cookie(value))     ⇒ AclNode(s"req.cook_cnt($value) eq 0")
    case Negation(Header(value))     ⇒ AclNode(s"req.hdr_cnt($value) eq 0")
    case Value(value)                ⇒ AclNode(value)
    case Negation(op)                ⇒ Negation(acls(op))
    case Or(op1, op2)                ⇒ Or(acls(op1), acls(op2))
    case And(op1, op2)               ⇒ And(acls(op1), acls(op2))
    case other                       ⇒ unsupported(other)
  }

  private def operands(node: AstNode): List[Acl] = node match {
    case AclNode(value)          ⇒ Acl(value) :: Nil
    case Negation(operand)       ⇒ operands(operand)
    case Or(operand1, operand2)  ⇒ operands(operand1) ++ operands(operand2)
    case And(operand1, operand2) ⇒ operands(operand1) ++ operands(operand2)
    case other                   ⇒ unsupported(other)
  }

  private def condition(node: AstNode): String = node match {
    case AclNode(value)           ⇒ Acl(value).name
    case Negation(AclNode(value)) ⇒ s"!${Acl(value).name}"
    case And(operand1, operand2)  ⇒ s"${condition(operand1)} ${condition(operand2)}"
    case Or(operand1, operand2)   ⇒ s"${condition(operand1)} or ${condition(operand2)}"
    case other                    ⇒ unsupported(other)
  }

  private def expand: AstNode ⇒ AstNode = {
    case UserAgent(value)        ⇒ expandUserAgent(value)
    case Negation(operand)       ⇒ Negation(expand(operand))
    case And(operand1, operand2) ⇒ And(expand(operand1), expand(operand2))
    case Or(operand1, operand2)  ⇒ Or(expand(operand1), expand(operand2))
    case other                   ⇒ other
  }

  private def expandUserAgent: String ⇒ AstNode = { value ⇒
    userAgents.get(value.toUpperCase) match {
      case None             ⇒ UserAgent(value)
      case Some(definition) ⇒ load(definition)
    }
  }

  private def unsupported(node: AstNode) = throw new RuntimeException(s"unsupported ACL node: $node")
} 
Example 13
Source File: YamlUtil.scala    From vamp   with Apache License 2.0 5 votes vote down vote up
package io.vamp.common.util

import org.yaml.snakeyaml.Yaml
import org.yaml.snakeyaml.constructor.Constructor
import org.yaml.snakeyaml.error.YAMLException

import scala.collection.JavaConverters._
import scala.collection.mutable

object YamlUtil {

  def yaml: Yaml = {
    new Yaml(new Constructor() {
      override def getClassForName(name: String): Class[_] = throw new YAMLException("Not supported.")
    })
  }

  def convert(any: Any, preserveOrder: Boolean): Any = any match {
    case source: java.util.Map[_, _] ⇒
      if (preserveOrder) {
        val map = new mutable.LinkedHashMap[String, Any]()
        source.entrySet().asScala.foreach(entry ⇒ map += entry.getKey.toString → convert(entry.getValue, preserveOrder))
        map
      }
      else source.entrySet().asScala.map(entry ⇒ entry.getKey.toString → convert(entry.getValue, preserveOrder)).toMap
    case source: java.util.List[_]     ⇒ source.asScala.map(convert(_, preserveOrder)).toList
    case source: java.lang.Iterable[_] ⇒ source.asScala.map(convert(_, preserveOrder)).toList
    case source                        ⇒ source
  }
} 
Example 14
Source File: MessageResolver.scala    From vamp   with Apache License 2.0 5 votes vote down vote up
package io.vamp.common.notification

import com.typesafe.scalalogging.Logger
import org.slf4j.LoggerFactory
import org.yaml.snakeyaml.Yaml

import scala.collection.JavaConverters._
import scala.collection.mutable
import scala.io.Source
import scala.language.postfixOps

trait MessageResolverProvider {
  val messageResolver: MessageResolver

  trait MessageResolver {
    def resolve(implicit notification: Notification): String
  }
}

trait DefaultPackageMessageResolverProvider extends MessageResolverProvider {
  val messageResolver: MessageResolver = new DefaultPackageMessageResolver()

  private class DefaultPackageMessageResolver extends MessageResolver {

    protected case class Message(parts: Seq[String], args: Seq[String])

    private val logger = Logger(LoggerFactory.getLogger(classOf[Notification]))
    private val messages = new mutable.LinkedHashMap[String, mutable.Map[String, Any]]()

    def resolve(implicit notification: Notification): String = {
      try {
        val name = notification.getClass.getSimpleName
        val messageSource = resolveMessageSource

        messageSource.get(name) match {
          case None ⇒
            logger.warn(s"No mapping for ${notification.getClass}")
            defaultMapping(error = false)
          case Some(value: Message) ⇒ resolveMessageValue(value)
          case Some(value: Any) ⇒
            val message = parseMessage(value.toString)
            messageSource.put(name, message)
            resolveMessageValue(message)
        }
      }
      catch {
        case e: NoSuchMethodException ⇒
          val field = e.getMessage.substring(e.getMessage.lastIndexOf('.') + 1, e.getMessage.length - 2)
          logger.error(s"Message mapping error: field '$field' not defined for ${notification.getClass}")
          defaultMapping()
        case e: Exception ⇒
          logger.error(e.getMessage, e)
          defaultMapping()
      }
    }

    protected def defaultMapping(error: Boolean = true)(implicit notification: Notification): String = if (error) "Error." else "Notification."

    protected def resolveMessageSource(implicit notification: Notification): mutable.Map[String, Any] = {
      val packageName = notification.getClass.getPackage.toString
      messages.get(packageName) match {
        case None ⇒
          val reader = Source.fromURL(notification.getClass.getResource("messages.yml")).bufferedReader()
          try {
            val input = new Yaml().load(reader).asInstanceOf[java.util.Map[String, Any]].asScala
            messages.put(packageName, input)
            input
          }
          finally {
            reader.close()
          }
        case Some(map) ⇒ map
      }
    }

    protected def parseMessage(message: String)(implicit notification: Notification): Message = {
      val pattern = "\\{[^}]+\\}" r
      val parts = pattern split message
      val args = (pattern findAllIn message).map(s ⇒ s.substring(1, s.length - 1)).toList
      Message(parts, args)
    }

    protected def resolveMessageValue(message: Message)(implicit notification: Notification): String = {
      val pi = message.parts.iterator
      val ai = message.args.iterator
      val sb = new StringBuilder()
      while (ai.hasNext) {
        sb append pi.next
        sb append ai.next().split('.').foldLeft(notification.asInstanceOf[AnyRef])((arg1, arg2) ⇒ arg1.getClass.getDeclaredMethod(arg2).invoke(arg1)).toString
      }
      if (pi.hasNext) sb append pi.next
      sb.toString()
    }
  }

} 
Example 15
Source File: DataLoader.scala    From amaterasu   with Apache License 2.0 5 votes vote down vote up
package org.apache.amaterasu.leader.utilities

import java.io.{File, FileInputStream}
import java.nio.file.{Files, Paths}

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import org.apache.amaterasu.common.configuration.ClusterConfig
import org.apache.amaterasu.common.dataobjects.{ActionData, ExecData, TaskData}
import org.apache.amaterasu.common.execution.dependencies.{Dependencies, PythonDependencies}
import org.apache.amaterasu.common.logging.Logging
import org.apache.amaterasu.common.runtime.Environment
import org.apache.mesos.protobuf.ByteString
import org.yaml.snakeyaml.Yaml

import scala.collection.JavaConverters._
import scala.collection.mutable
import scala.io.Source


object DataLoader extends Logging {

  val mapper = new ObjectMapper()
  mapper.registerModule(DefaultScalaModule)

  val ymlMapper = new ObjectMapper(new YAMLFactory())
  ymlMapper.registerModule(DefaultScalaModule)

  def getTaskData(actionData: ActionData, env: String): ByteString = {

    val srcFile = actionData.src
    val src = Source.fromFile(s"repo/src/$srcFile").mkString
    val envValue = Source.fromFile(s"repo/env/$env/job.yml").mkString

    val envData = ymlMapper.readValue(envValue, classOf[Environment])

    val data = mapper.writeValueAsBytes(TaskData(src, envData, actionData.groupId, actionData.typeId, actionData.exports))
    ByteString.copyFrom(data)

  }

  def getExecutorData(env: String, clusterConf: ClusterConfig): ByteString = {

    // loading the job configuration
    val envValue = Source.fromFile(s"repo/env/$env/job.yml").mkString //TODO: change this to YAML
    val envData = ymlMapper.readValue(envValue, classOf[Environment])
    // loading all additional configurations
    val files = new File(s"repo/env/$env/").listFiles().filter(_.isFile).filter(_.getName != "job.yml")
    val config = files.map(yamlToMap).toMap
    // loading the job's dependencies
    var depsData: Dependencies = null
    var pyDepsData: PythonDependencies = null
    if (Files.exists(Paths.get("repo/deps/jars.yml"))) {
      val depsValue = Source.fromFile(s"repo/deps/jars.yml").mkString
      depsData = ymlMapper.readValue(depsValue, classOf[Dependencies])
    }
    if (Files.exists(Paths.get("repo/deps/python.yml"))) {
      val pyDepsValue = Source.fromFile(s"repo/deps/python.yml").mkString
      pyDepsData = ymlMapper.readValue(pyDepsValue, classOf[PythonDependencies])
    }
    val data = mapper.writeValueAsBytes(ExecData(envData, depsData, pyDepsData, config))
    ByteString.copyFrom(data)
  }

  def yamlToMap(file: File): (String, Map[String, Any]) = {

    val yaml = new Yaml()
    val conf = yaml.load(new FileInputStream(file)).asInstanceOf[java.util.Map[String, Any]].asScala.toMap

    (file.getName.replace(".yml",""), conf)
  }

}

class ConfMap[String,  T <: ConfMap[String, T]] extends mutable.ListMap[String, Either[String, T]]