scala.beans.BeanProperty Scala Examples

The following examples show how to use scala.beans.BeanProperty. 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: OrganizationModel.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.service.jdbc

import com.ecfront.common.Resp
import com.ecfront.ez.framework.core.EZ

import scala.beans.BeanProperty


trait OrganizationModel extends BaseModel {

  @Index
  @Desc("Organization Code", 200, 0)
  @BeanProperty var organization_code: String = _

}

object OrganizationModel

trait OrganizationStorage[M <: OrganizationModel] extends BaseStorage[M] {

  override def filterByModel(model: M): Resp[M] = {
    if (EZ.context.optAccCode.nonEmpty && !EZ.context.optInfo.get.roleCodes.contains(BaseModel.SPLIT + BaseModel.SYSTEM_ROLE_FLAG)) {
      // 操作信息存在且是非系统管理员
      if (model.id == null || model.id.isEmpty) {
        // 新建,只能创建自己组织的信息
        model.organization_code = EZ.context.optOrgCode
        Resp.success(model)
      } else {
        val m = doGetById(model.id)
        if (m && m.body != null && m.body.organization_code == EZ.context.optOrgCode) {
          // 更新的是自己组织下的信息
          Resp.success(model)
        } else {
          // 不存在此id或不在自己组织下
          Resp.notFound("Account not exist")
        }
      }
    } else {
      Resp.success(model)
    }
  }

  override def filterById(id: Any): Resp[(String, List[Any])] = {
    filterByCond(s"""${_entityInfo.idFieldName} = ? """, List(id))
  }

  override def filterByUUID(uuid: String): Resp[(String, List[Any])] = {
    filterByCond(s"""${_entityInfo.uuidFieldName} = ? """, List(uuid))
  }

  override def filterByCond(condition: String, parameters: List[Any]): Resp[(String, List[Any])] = {
    val result = if (EZ.context.optAccCode.isEmpty || EZ.context.optInfo.get.roleCodes.contains(BaseModel.SPLIT + BaseModel.SYSTEM_ROLE_FLAG)) {
      // 没有登录(内部逻辑处理) 或 登录为sysadmin 时不需要过滤
      if (condition != null && condition.nonEmpty) {
        (condition, parameters)
      } else {
        ("", parameters)
      }
    } else {
      if (condition != null && condition.nonEmpty) {
        (s" organization_code = '${EZ.context.optOrgCode}' AND " + condition, parameters)
      } else {
        (s" organization_code = '${EZ.context.optOrgCode}'", parameters)
      }
    }
    Resp.success(result)
  }

} 
Example 2
Source File: PMMLModelExport.scala    From multi-tenancy-spark   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.mllib.pmml.export

import java.text.SimpleDateFormat
import java.util.{Date, Locale}

import scala.beans.BeanProperty

import org.dmg.pmml.{Application, Header, PMML, Timestamp}

private[mllib] trait PMMLModelExport {

  
  @BeanProperty
  val pmml: PMML = {
    val version = getClass.getPackage.getImplementationVersion
    val app = new Application("Apache Spark MLlib").setVersion(version)
    val timestamp = new Timestamp()
      .addContent(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US).format(new Date()))
    val header = new Header()
      .setApplication(app)
      .setTimestamp(timestamp)
    new PMML("4.2", header, null)
  }
} 
Example 3
Source File: BoostingStrategy.scala    From iolap   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.mllib.tree.configuration

import scala.beans.BeanProperty

import org.apache.spark.annotation.Experimental
import org.apache.spark.mllib.tree.configuration.Algo._
import org.apache.spark.mllib.tree.loss.{LogLoss, SquaredError, Loss}


  def defaultParams(algo: Algo): BoostingStrategy = {
    val treeStrategy = Strategy.defaultStategy(algo)
    treeStrategy.maxDepth = 3
    algo match {
      case Algo.Classification =>
        treeStrategy.numClasses = 2
        new BoostingStrategy(treeStrategy, LogLoss)
      case Algo.Regression =>
        new BoostingStrategy(treeStrategy, SquaredError)
      case _ =>
        throw new IllegalArgumentException(s"$algo is not supported by boosting.")
    }
  }
} 
Example 4
Source File: PMMLModelExport.scala    From iolap   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.mllib.pmml.export

import java.text.SimpleDateFormat
import java.util.Date

import scala.beans.BeanProperty

import org.dmg.pmml.{Application, Header, PMML, Timestamp}

private[mllib] trait PMMLModelExport {

  
  @BeanProperty
  val pmml: PMML = new PMML

  setHeader(pmml)

  private def setHeader(pmml: PMML): Unit = {
    val version = getClass.getPackage.getImplementationVersion
    val app = new Application().withName("Apache Spark MLlib").withVersion(version)
    val timestamp = new Timestamp()
      .withContent(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date()))
    val header = new Header()
      .withApplication(app)
      .withTimestamp(timestamp)
    pmml.setHeader(header)
  }
} 
Example 5
Source File: BeanDemo.scala    From spark1.52   with Apache License 2.0 5 votes vote down vote up
package scalaDemo


class PersonThree(val name: String, private var age: Int) {
  def description = name + " is " + age + " years old"
  def birthday() { age += 1 }
}
class Network(val name: String) { outer =>
  class Member(val name: String) {
    val contacts = new ArrayBuffer[Member]
    def description = name + " inside " + outer.name
  }

  private val members = new ArrayBuffer[Member]

  def join(name: String) = {
    val m = new Member(name)
    members += m
    m
  }
}
object BeanDemo {
  def main(args: Array[String]):Unit= {
    val fred = new Person
    fred.setName("Fred")
    println("==="+fred.getName)
    val p1 = new PersonBean // Primary constructor
    val p2 = new PersonBean("Fred") // First auxiliary constructor
    val p3 = new PersonBean("Fred", 42) // Second auxiliary constructor

    p1.description
    p2.description
    p3.description

    println("==p1:=="+p1.description+"==p2:=="+p2.description+"==p3:=="+p3.description)


    val p4 = new PersonTwo
    val p5 = new PersonTwo("Fred")
    val p6 = new PersonTwo("Fred", 42)
    p4.description
    p5.description
    p6.description
    println("==p4:=="+p4.description+"==p5:=="+p5.description+"==p6:=="+p6.description)


    val p7 = new PersonThree("Fred", 42)
    p7.name
   // p7.age // Error--it's private
    p7.birthday()
    p7.description
    println("==p7name:=="+p7.name+"==birthday:=="+ p7.birthday()+"==description:=="+  p7.description)


    val chatter = new Network("Chatter")
    val myFace = new Network("MyFace")

    val fredb = chatter.join("Fred")
    println(fredb.description);
    val barney = myFace.join("Barney")
    println(barney.description);
  }
} 
Example 6
Source File: AnnotationDemo.scala    From spark1.52   with Apache License 2.0 5 votes vote down vote up
package scalaDemo


import javax.persistence.{Entity, Id}
import javax.validation.constraints.NotNull

import scala.annotation.target.beanGetter
import scala.beans.BeanProperty
import scala.reflect.BeanProperty

// Annotation for class
@Entity class Credentials(@NotNull @BeanProperty var username: String) {
  // @NotNull is only applied to the constructor parameter, not to
  // the getters/setters
  def check(@NotNull password: String) {}
  // @NotNull is applied to the method parameter
  @BeanProperty @deprecated("Use check instead", "1.5") var pwd = ""
  // @deprecated is applied to the Scala and bean getters/setters
  @(Id @beanGetter) @BeanProperty var id = 0
  // @Id is only applied to the bean getter
}

import scala.reflect.BeanProperty
import javax.persistence.Entity
import javax.persistence.Id

@Entity class Credentialsb {
  @Id @BeanProperty var username : String = _
  @BeanProperty var password : String = _
}


object AnnotationDemo extends App {

  val creds = new Credentials("Fred")
  creds.pwd = "secret" // Deprecation warning for Scala setter
  println(creds.getPwd()) // Deprecation warning for bean getter
} 
Example 7
Source File: BoostingStrategy.scala    From spark1.52   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.mllib.tree.configuration

import scala.beans.BeanProperty

import org.apache.spark.annotation.{Experimental, Since}
import org.apache.spark.mllib.tree.configuration.Algo._
import org.apache.spark.mllib.tree.loss.{LogLoss, SquaredError, Loss}


  @Since("1.3.0")
  def defaultParams(algo: Algo): BoostingStrategy = {
    val treeStrategy = Strategy.defaultStrategy(algo)
    treeStrategy.maxDepth = 3
    algo match {
      case Algo.Classification =>
        treeStrategy.numClasses = 2
        new BoostingStrategy(treeStrategy, LogLoss)
      case Algo.Regression =>
        new BoostingStrategy(treeStrategy, SquaredError)
      case _ =>
        throw new IllegalArgumentException(s"$algo is not supported by boosting.")
    }
  }
} 
Example 8
Source File: PMMLModelExport.scala    From spark1.52   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.mllib.pmml.export

import java.text.SimpleDateFormat
import java.util.Date

import scala.beans.BeanProperty

import org.dmg.pmml.{Application, Header, PMML, Timestamp}

private[mllib] trait PMMLModelExport {

  
  @BeanProperty
  val pmml: PMML = new PMML

  setHeader(pmml)

  private def setHeader(pmml: PMML): Unit = {
    val version = getClass.getPackage.getImplementationVersion
    val app = new Application().withName("Apache Spark MLlib").withVersion(version)
    val timestamp = new Timestamp()
      .withContent(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date()))
    val header = new Header()
      .withApplication(app)
      .withTimestamp(timestamp)
    pmml.setHeader(header)
  }
} 
Example 9
Source File: Stage.scala    From hazelcast-scala   with Apache License 2.0 5 votes vote down vote up
package com.hazelcast.Scala.actress

import scala.concurrent.Future

import com.hazelcast.Scala._
import com.hazelcast.core.{ HazelcastInstance, IMap }
import java.util.Map.Entry
import com.hazelcast.core.HazelcastInstanceAware
import scala.beans.BeanProperty
import com.hazelcast.instance.HazelcastInstanceImpl
import com.hazelcast.instance.HazelcastInstanceProxy

class Stage(private val actressMap: IMap[String, Array[Byte]]) {

  def this(name: String, hz: HazelcastInstance) = this(hz.getMap(name))

  def actressOf[A <: AnyRef](name: String, create: => A): ActressRef[A] =
    new ActressImpl(name, actressMap, create)

}

private class ActressImpl[A <: AnyRef](
  val name: String,
  imap: IMap[String, Array[Byte]],
  create: => A)
    extends ActressRef[A] {

  def apply[R](thunk: (HazelcastInstance, A) => R): Future[R] = {
    val ep = new SingleEntryCallbackUpdater[String, Array[Byte], R] with HazelcastInstanceAware {
      val newActress = create _
      @BeanProperty @transient
      var hazelcastInstance: HazelcastInstance = _
      var newState: Array[Byte] = _
      def onEntry(entry: Entry[String, Array[Byte]]): R = {
        val serializationService = hazelcastInstance match {
          case hz: HazelcastInstanceImpl => hz.getSerializationService
          case hz: HazelcastInstanceProxy => hz.getSerializationService
        }
        val actress = entry.value match {
          case null => newActress()
          case bytes =>
            val inp = serializationService.createObjectDataInput(bytes)
            serializationService.readObject(inp)
        }
        val result = thunk(hazelcastInstance, actress)
        newState = {
          val out = serializationService.createObjectDataOutput()
          serializationService.writeObject(out, actress)
          out.toByteArray()
        }
        entry.value = newState
        result
      }
      override def processBackup(entry: Entry[String, Array[Byte]]): Unit = {
        entry.value = newState
      }
    }
    val callback = ep.newCallback()
    imap.submitToKey(name, ep, callback)
    callback.future
  }
} 
Example 10
Source File: PMMLModelExport.scala    From Spark-2.3.1   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.mllib.pmml.export

import java.text.SimpleDateFormat
import java.util.{Date, Locale}

import scala.beans.BeanProperty

import org.dmg.pmml.{Application, Header, PMML, Timestamp}

private[mllib] trait PMMLModelExport {

  
  @BeanProperty
  val pmml: PMML = {
    val version = getClass.getPackage.getImplementationVersion
    val app = new Application("Apache Spark MLlib").setVersion(version)
    val timestamp = new Timestamp()
      .addContent(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US).format(new Date()))
    val header = new Header()
      .setApplication(app)
      .setTimestamp(timestamp)
    new PMML("4.2", header, null)
  }
} 
Example 11
Source File: PMMLModelExport.scala    From BigDatalog   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.mllib.pmml.export

import java.text.SimpleDateFormat
import java.util.Date

import scala.beans.BeanProperty

import org.dmg.pmml.{Application, Header, PMML, Timestamp}

private[mllib] trait PMMLModelExport {

  
  @BeanProperty
  val pmml: PMML = new PMML

  pmml.setVersion("4.2")
  setHeader(pmml)

  private def setHeader(pmml: PMML): Unit = {
    val version = getClass.getPackage.getImplementationVersion
    val app = new Application().withName("Apache Spark MLlib").withVersion(version)
    val timestamp = new Timestamp()
      .withContent(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date()))
    val header = new Header()
      .withApplication(app)
      .withTimestamp(timestamp)
    pmml.setHeader(header)
  }
} 
Example 12
Source File: Environment.scala    From squbs   with Apache License 2.0 5 votes vote down vote up
package org.squbs.env

import java.beans.ConstructorProperties
import java.lang.management.ManagementFactory
import java.util
import javax.management.{ObjectName, MXBean}

import akka.actor._
import com.typesafe.scalalogging.LazyLogging

import scala.beans.BeanProperty

abstract class Environment {
  def name: String

  def lowercaseName: String = name.toLowerCase
}

case object Default extends Environment {
  override val name: String = "DEFAULT"

  
  val value: Environment = this
}

case class RawEnv(name: String) extends Environment

trait EnvironmentResolver {
  def name: String

  def resolve: Environment
}

class EnvironmentResolverRegistryExtension(system: ExtendedActorSystem) extends Extension with LazyLogging {
  private[env] var environmentResolvers = List.empty[EnvironmentResolver]

  def register(resolver: EnvironmentResolver): Unit = {
    environmentResolvers.find(_.name == resolver.name) match {
      case None => environmentResolvers = resolver :: environmentResolvers
      case Some(oldResolver) =>
        logger.warn("Env Resolver:" + oldResolver.name + " already registered, skipped!")
    }
  }

  def unregister(name: String): Unit = {
    val originalLength = environmentResolvers.length
    environmentResolvers = environmentResolvers.filterNot(_.name == name)
    if(environmentResolvers.length == originalLength)
      logger.warn("Env Resolver:" + name + " cannot be found, skipping unregister!")
  }

  def resolve: Environment = {
    val resolvedEnv = environmentResolvers.view.map(_.resolve).collectFirst {
      case env if env != Default => env
    } getOrElse Default

    logger.debug(s"The environment is: " + resolvedEnv.lowercaseName)
    resolvedEnv
  }
}

object EnvironmentResolverRegistry extends ExtensionId[EnvironmentResolverRegistryExtension] with ExtensionIdProvider {

  override def lookup() = EnvironmentResolverRegistry

  override def createExtension(system: ExtendedActorSystem): EnvironmentResolverRegistryExtension = {
    val mBeanServer = ManagementFactory.getPlatformMBeanServer
    val beanName = new ObjectName(s"org.squbs.configuration.${system.name}:type=EnvironmentResolverRegistry")
    if (!mBeanServer.isRegistered(beanName))
      mBeanServer.registerMBean(EnvironmentResolverRegistryMXBeanImpl(system), beanName)
    new EnvironmentResolverRegistryExtension(system)
  }

  override def get(system: ActorSystem): EnvironmentResolverRegistryExtension = super.get(system)
}

// $COVERAGE-OFF$
case class EnvironmentResolverInfo @ConstructorProperties(
  Array("position", "name", "className"))(@BeanProperty position: Int,
                                          @BeanProperty name: String,
                                          @BeanProperty className: String)

// $COVERAGE-ON$

@MXBean
trait EnvironmentResolverRegistryMXBean {
  def getEnvironmentResolverInfo: java.util.List[EnvironmentResolverInfo]
}

case class EnvironmentResolverRegistryMXBeanImpl(system: ActorSystem) extends EnvironmentResolverRegistryMXBean {

  override def getEnvironmentResolverInfo: util.List[EnvironmentResolverInfo] = {
    import scala.collection.JavaConverters._
    EnvironmentResolverRegistry(system).environmentResolvers.zipWithIndex.map { case(resolver, position) =>
      EnvironmentResolverInfo(position, resolver.name, resolver.getClass.getName)
    }.asJava
  }
} 
Example 13
Source File: HttpExecutionTest.scala    From maze   with Apache License 2.0 5 votes vote down vote up
package fr.vsct.dt.maze.helpers

import com.typesafe.scalalogging.StrictLogging
import fr.vsct.dt.maze.core.Commands.expectThat
import fr.vsct.dt.maze.core.Predef._
import fr.vsct.dt.maze.core.{Predicate, Result}
import org.apache.http._
import org.apache.http.client.methods.{CloseableHttpResponse, HttpGet}
import org.apache.http.entity.{ContentType, StringEntity}
import org.apache.http.impl.client.CloseableHttpClient
import org.apache.http.message.{BasicHttpResponse, BasicStatusLine}
import org.apache.http.protocol.HttpContext
import org.scalatest.FlatSpec

import scala.beans.BeanProperty


class HttpExecutionTest extends FlatSpec {

  
  class MockHttpClient(val response: String) extends CloseableHttpClient with StrictLogging {
    var init = false

    override def doExecute(target: HttpHost, request: HttpRequest, context: HttpContext): CloseableHttpResponse = {
      if (!init) throw new IllegalStateException("Client is not initialized")
      logger.info("Doing actual http call")
      val r = if(request.getRequestLine.getUri == "http://some-url.com") {
        val t = new BasicCloseableHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "OK"))
        t.setEntity(new StringEntity(response, "UTF-8"))
        t
      } else {
        val t = new BasicCloseableHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_BAD_REQUEST, "KO"))
        t.setEntity(new StringEntity("""{"status": "ko"}""", ContentType.APPLICATION_JSON))
        t
      }
      r
    }

    @Deprecated
    override def getConnectionManager = null

    @Deprecated
    override def getParams = null

    override def close(): Unit = {}

    class BasicCloseableHttpResponse(statusLine: StatusLine) extends BasicHttpResponse(statusLine) with CloseableHttpResponse {
      override def close(): Unit = {}
    }

  }

  "a http check" should "not do an effective call until apply is effectively called" in {

    Http.client = new MockHttpClient("Youppy !")

    val requestOk = new HttpGet("http://some-url.com")

    val check1: Predicate = Http.execute(requestOk).status is 200
    val check2: Predicate = Http.execute(requestOk).response is "Youppy !"

    val check3 = check1 || check2

    val check4 = !check3

    Http.client.asInstanceOf[MockHttpClient].init = true

    assert(check1.get() == Result.success)
    assert(check2.get() == Result.success)
    assert(check3.get() == Result.success)
    assert(check4.get() == Result.failure(s"Expected ${check3.label} to be false"))
    expectThat(Http.get("http://some-error-url.com").status is 400)
    expectThat(Http.get("http://some-url.com").isOk)
    expectThat(!Http.get("http://some-error-url.com").isOk)
    expectThat(Http.get("http://some-error-url.com").responseAs(classOf[Stupid]) is Stupid(status = "ko"))

  }

}

case class Stupid(@BeanProperty status: String) 
Example 14
Source File: SchemaData.scala    From pulsar-spark   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.sql.pulsar

import java.sql.Timestamp
import java.util
import java.util.Calendar

import scala.beans.BeanProperty
import scala.collection.JavaConverters._

object SchemaData {

  val booleanSeq = Seq(true, false, true, true, false)
  val bytesSeq = 1.to(5).map(_.toString.getBytes)

  val cal = Calendar.getInstance()
  cal.clear()
  val dateSeq = (1 to 5).map { i =>
    cal.set(2019, 0, i)
    cal.getTime
  }

  cal.clear()
  val timestampSeq = (1 to 5).map { i =>
    cal.set(2019, 0, i, 20, 35, 40)
    new Timestamp(cal.getTimeInMillis)
  }

  val stringSeq = 1.to(5).map(_.toString)
  val int8Seq = 1.to(5).map(_.toByte)
  val doubleSeq = 1.to(5).map(_.toDouble)
  val floatSeq = 1.to(5).map(_.toFloat)
  val int32Seq = 1.to(5)
  val int64Seq = 1.to(5).map(_.toLong)
  val int16Seq = 1.to(5).map(_.toShort)

  case class Foo(@BeanProperty i: Int, @BeanProperty f: Float, @BeanProperty bar: Bar)
  case class Bar(@BeanProperty b: Boolean, @BeanProperty s: String)

  case class F1(@BeanProperty baz: Baz)

  case class Baz(
      @BeanProperty f: Float,
      @BeanProperty d: Double,
      @BeanProperty mp: util.Map[String, Bar],
      @BeanProperty arr: Array[Bar])

  val fooSeq: Seq[Foo] =
    Foo(1, 1.0.toFloat, Bar(true, "a")) :: Foo(2, 2.0.toFloat, Bar(false, "b")) :: Foo(3, 0, null) :: Nil

  val f1Seq: Seq[F1] =
    F1(
      Baz(
        Float.NaN,
        Double.NaN,
        Map("1" -> Bar(true, "1"), "2" -> Bar(false, "2")).asJava,
        Array(Bar(true, "1"), Bar(true, "2")))) ::
    F1(
      Baz(
        Float.NegativeInfinity,
        Double.NegativeInfinity,
        Map("" -> Bar(true, "1")).asJava,
        null)) ::
    F1(Baz(Float.PositiveInfinity, Double.PositiveInfinity, null, null)) ::
    F1(Baz(1.0.toFloat, 2.0, null, null)) :: Nil

  val f1Results = f1Seq.map(f1 =>
    (f1.baz.f, f1.baz.d, if (f1.baz.mp == null) null else f1.baz.mp.asScala, f1.baz.arr))
} 
Example 15
Source File: EZ_Message_Log.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.service.message.entity

import java.util.Date

import com.ecfront.common.Resp
import com.ecfront.ez.framework.core.helper.TimeHelper
import com.ecfront.ez.framework.service.jdbc.{BaseModel, BaseStorage, Desc, Entity}

import scala.beans.BeanProperty

@Entity("消息日志")
case class EZ_Message_Log() extends BaseModel {

  @Desc("消息Id", 200, 0)
  @BeanProperty var message_id: String = _
  @Desc("阅读账号", 200, 0)
  // 用于角色和公共消息
  @BeanProperty var read_account_code: String = _
  @Desc("阅读时间,yyyyMMddHHmmss", 0, 0)
  @BeanProperty var read_time: Long = _

}

object EZ_Message_Log extends BaseStorage[EZ_Message_Log] {

  def apply(messageId: String, accountCode: String): EZ_Message_Log = {
    val log = EZ_Message_Log()
    log.message_id = messageId
    log.read_account_code = accountCode
    log
  }

  override def preSave(model: EZ_Message_Log): Resp[EZ_Message_Log] = {
    model.read_time = TimeHelper.sf.format(new Date()).toLong
    super.preSave(model)
  }

  override def preSaveOrUpdate(model: EZ_Message_Log): Resp[EZ_Message_Log] = {
    model.read_time = TimeHelper.sf.format(new Date()).toLong
    super.preSaveOrUpdate(model)
  }

} 
Example 16
Source File: TemplateEngineHelper.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.service.message.helper

import com.ecfront.common.{JsonHelper, Resp}
import com.ecfront.ez.framework.core.EZ
import com.ecfront.ez.framework.core.logger.Logging

import scala.beans.BeanProperty
import scala.io.Source


  def generateMessageByTemplate(templateCode: String, variable: Map[String, String]): Resp[(String, String)] = {
    if (!messageTemplatesCache.contains(templateCode)) {
      // 缓存中不包含,重新获取一次
      messageTemplatesCache = getMessageTemplates
    }
    if (!messageTemplatesCache.contains(templateCode)) {
      // 重建缓存后还找不到视为错误code
      logger.error(s"Message Template Not exist : $templateCode")
      Resp.notFound(s"Message Template Not exist : $templateCode")
    } else {
      val template = messageTemplatesCache(templateCode)
      val content = TemplateEngineHelper.render(template.content, variable)
      val title = TemplateEngineHelper.render(template.title, variable)
      Resp.success((content, title))
    }
  }


  private def getMessageTemplates: Map[String, MessageTemplateVO] = {
    // TODO  get by DB
    JsonHelper.toObject[List[MessageTemplateVO]](Source.fromFile(EZ.Info.confPath + "message_template.json", "UTF-8").mkString).map {
      item =>
        item.templateCode -> item
    }.toMap
  }

  class MessageTemplateVO {
    @BeanProperty var templateCode: String = _
    @BeanProperty var templateName: String = _
    @BeanProperty var title: String = _
    @BeanProperty var content: String = _
  }

} 
Example 17
Source File: RestProxyResource.scala    From scrapy4s   with GNU Lesser General Public License v3.0 5 votes vote down vote up
package com.scrapy4s.http.proxy

import com.alibaba.fastjson.JSON
import com.scrapy4s.http.{Request, Response}
import org.slf4j.LoggerFactory

import scala.beans.BeanProperty
import scala.collection.JavaConverters._


  private def acquireProxy(): Unit = {
    try {
      logger.info(s"Refresh proxy with $url")
      paser(Request(url).execute()).foreach { proxy =>
        threadLocalProxyStack.get().push(proxy)
      }
    } catch {
      case e: Exception => logger.warn("proxy api error", e)
    }
  }

  override def get: ProxyModel = {
    var proxy: ProxyModel = null
    while (proxy == null) {
      // 建立本地线程代理栈
      if (threadLocalProxyStack.get() == null) {
        threadLocalProxyStack.set(new java.util.Stack[ProxyModel])
      } else if (threadLocalProxyStack.get().empty()) {
        // 判断上次刷新时间
        val timeWait = threadLocalRefreshTime.get + minRefreshTime - System.currentTimeMillis()
        if (timeWait > 0) {
          logger.info(s"Refresh time si too early......, wait ${timeWait}ms for sleep.....")
          Thread.sleep(timeWait)
        }
        threadLocalRefreshTime.set(System.currentTimeMillis())
        // 刷新代理
        acquireProxy()
      } else {
        // 直接返回代理
        proxy = threadLocalProxyStack.get().pop()
      }
    }
    proxy
  }

  override def returnProxy(proxy: ProxyModel): Unit = {
    threadLocalProxyStack.get().push(proxy)
  }
}

object RestProxyResource {

  def apply(
             url: String,
             minRefreshTime: Long = 60 * 1000,
             paser: Response => Seq[ProxyModel] = RestProxyResource.defaultPaser
           ): RestProxyResource = new RestProxyResource(url, minRefreshTime, paser)

  def defaultPaser(response: Response): Seq[ProxyModel] = {
    val proxyString: String = response.body
    JSON.parseObject(proxyString)
      .getJSONObject("result")
      .getJSONArray("proxy_list")
      .toJavaList(classOf[DefaultProxyModel])
      .asScala
      .map(p => {
        ProxyModel(p.proxy.split(":")(0), p.proxy.split(":")(1).toInt)
      })
  }

  class DefaultProxyModel {
    @BeanProperty
    var proxy: String = _
    @BeanProperty
    var userAgent: String = _
  }

} 
Example 18
Source File: Loan_app.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.service.jdbc

import java.util.Date

import scala.beans.BeanProperty

@Entity("贷款申请")
class Loan_app extends SecureModel with StatusModel {
  @UUID
  @Desc("申请编号: 英文简称_INT(11位)", 32, 0)
  @BeanProperty var loan_app_uuid: String = _
  @Desc("申请编号: 英文简称_INT(11位)", 32, 0)
  @Unique
  @BeanProperty var app_number: String = _
  @Desc("贷款商品uuid:车型", 32, 0)
  @BeanProperty var loan_goods_uuid: String = _
  @Desc("贷款产品uuid", 32, 0)
  @BeanProperty var loan_product_uuid: String = _
  @Desc("借款人uuid", 32, 0)
  @BeanProperty var customer_uuid: String = _
  @Desc("销售专员uuid", 32, 0)
  @BeanProperty var sales_uuid: String = _
  @Desc("金融专员uuid", 32, 0)
  @BeanProperty var fin_special_uuid: String = _
  @Desc("4s店/经销商uuid", 32, 0)
  @BeanProperty var supplier_uuid: String = _
  @Desc("开票价", 14, 2)
  @BeanProperty var goods_price_inv: BigDecimal = _
  @Desc("保险金额", 14, 2)
  @BeanProperty var goods_ins_amount: BigDecimal = _
  @Desc("购置税", 14, 2)
  @BeanProperty var goods_tax_amount: BigDecimal = _
  @Desc("首付金额(当贷款产品为反租时填写)", 14, 2)
  @BeanProperty var down_payment_amount: BigDecimal = _
  @Desc("尾款金额(当贷款产品为反租时填写)", 14, 2)
  @BeanProperty var final_payment_amount: BigDecimal = _
  @Desc("保证金额(当贷款产品为正租时填写)", 14, 2)
  @BeanProperty var deposit_payment_amount: BigDecimal = _
  @Desc("标的总额(项目金额=开票价+购置税(融资情况下)+保险金额(融资情况下))", 14, 2)
  @BeanProperty var project_amount: BigDecimal = _
  @Desc("申请金额(标的总额-首付金额)", 14, 2)
  @BeanProperty var loan_amount: BigDecimal = _
  @Desc("还款期数", 11, 0)
  @BeanProperty var repayment_periods: Int = _
  @Desc("每期还款(月供)", 14, 2)
  @BeanProperty var repayment_per_periods: BigDecimal = _
  @Desc("还款总额", 14, 2)
  @BeanProperty var repayment_total_amount: BigDecimal = _
  @Desc("还利总额", 14, 2)
  @BeanProperty var repayment_total_interest: BigDecimal = _
  @Desc("申请日期", 0, 0)
  @BeanProperty var apply_date: Date = _
  @Desc("资金状态", 48, 0)
  @BeanProperty var finance_status: String = _
}

object Loan_app extends SecureStorage[Loan_app] with StatusStorage[Loan_app]{
  override lazy val tableName: String = "biz_app"
} 
Example 19
Source File: TPSIVO.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.service.tpsi

import java.util.Date

import com.ecfront.ez.framework.core.rpc.{Label, Require}

import scala.beans.BeanProperty

class TPSIStartVO() {

  @Label("服务名称")
  @Require
  @BeanProperty var service_code: String = _
  @Label("供应商名称")
  @Require
  @BeanProperty var supplier_code: String = _
  @Label("调用主体")
  @Require
  @BeanProperty var invoke_main_body: String = _

}

class TPSIFinishVO() {

  @Label("开始的日志ID")
  @Require
  @BeanProperty var log_id: Long = _
  @Label("是否成功")
  @Require
  @BeanProperty var success: Boolean = _
  @Label("结果描述")
  @Require
  @BeanProperty var message: String = _

}

class TPSIFullInvokeVO() {

  @Label("服务名称")
  @Require
  @BeanProperty var service_code: String = _
  @Label("供应商名称")
  @Require
  @BeanProperty var supplier_code: String = _
  @Label("调用主体")
  @Require
  @BeanProperty var invoke_main_body: String = _
  @Label("开始时间")
  @Require
  @BeanProperty var start_time: Date = _
  @Label("结束时间")
  @Require
  @BeanProperty var end_time: Date = _
  @Label("是否成功")
  @Require
  @BeanProperty var success: Boolean = _
  @Label("结果描述")
  @Require
  @BeanProperty var message: String = _

} 
Example 20
Source File: EZ_TPSI_Log.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.service.tpsi

import java.util.Date

import com.ecfront.common.Resp
import com.ecfront.ez.framework.core.EZ
import com.ecfront.ez.framework.core.helper.TimeHelper
import com.ecfront.ez.framework.service.jdbc._

import scala.beans.BeanProperty

@Entity("执行日志")
case class EZ_TPSI_Log() extends BaseModel {

  @Index
  @Desc("服务名称", 200, 0)
  @BeanProperty var service_code: String = _
  @Index
  @Desc("供应商名称", 200, 0)
  @BeanProperty var supplier_code: String = _
  @Index
  @Desc("调用主体", 200, 0)
  @BeanProperty var invoke_main_body: String = _
  @Desc("开始年份", 0, 0)
  @BeanProperty var start_year: Long = _
  @Desc("开始月份", 0, 0)
  @BeanProperty var start_month: Long = _
  @Desc("开始时间", 0, 0)
  @BeanProperty var start_time: Long = _
  @Desc("结束时间", 0, 0)
  @BeanProperty var end_time: Long = _
  @Desc("执行时间", 0, 0)
  @BeanProperty var use_time: Long = _
  @Desc("是否成功", 0, 0)
  @BeanProperty var success: Boolean = _
  @Desc("结果描述", 500, 0)
  @BeanProperty var message: String = _

}

object EZ_TPSI_Log extends BaseStorage[EZ_TPSI_Log] {

  def add(serviceCode: String, supplierCode: String, invokeMainBody: String,
          success: Boolean, message: String, startTime: Date, endTime: Date): Unit = {
    val log = new EZ_TPSI_Log
    log.service_code = serviceCode
    log.supplier_code = supplierCode
    log.invoke_main_body = invokeMainBody
    log.start_year = TimeHelper.yf.format(startTime).toLong
    log.start_month = TimeHelper.Mf.format(startTime).toLong
    log.start_time = TimeHelper.msf.format(startTime).toLong
    log.end_time = TimeHelper.msf.format(endTime).toLong
    log.use_time = log.end_time - log.start_time
    log.success = success
    log.message = message
    addEvent(EZ_TPSI_Log.save(log).body)
  }

  def start(serviceCode: String, supplierCode: String, invokeMainBody: String = ""): EZ_TPSI_Log = {
    val now = new Date()
    val log = new EZ_TPSI_Log
    log.service_code = serviceCode
    log.supplier_code = supplierCode
    log.invoke_main_body = invokeMainBody
    log.start_year = TimeHelper.yf.format(now).toLong
    log.start_month = TimeHelper.Mf.format(now).toLong
    log.start_time = TimeHelper.msf.format(now).toLong
    EZ_TPSI_Log.save(log).body
  }

  def finishByLogId(success: Boolean, message: String, logId: Long, endTime: Date = new Date()): Unit = {
    val log = EZ_TPSI_Log.getById(logId).body
    if (log != null) {
      log.end_time = TimeHelper.msf.format(endTime).toLong
      log.use_time = log.end_time - log.start_time
      log.success = success
      log.message = message
      addEvent(EZ_TPSI_Log.update(log).body)
    }
  }

  def finish(success: Boolean, message: String, log: EZ_TPSI_Log, endTime: Date = new Date()): Unit = {
    log.end_time = TimeHelper.msf.format(endTime).toLong
    log.use_time = log.end_time - log.start_time
    log.success = success
    log.message = message
    addEvent(EZ_TPSI_Log.update(log).body)
  }

  def pageByCode(serviceCode: String, supplierCode: String, pageNumber: Long, pageSize: Int): Resp[Page[EZ_TPSI_Log]] = {
    page("service_code = ? and supplier_code = ? order by desc", List(serviceCode, supplierCode), pageNumber, pageSize)
  }

  def addEvent(log: EZ_TPSI_Log): Unit = {
    EZ.eb.pubReq(ServiceAdapter.TPSI_ADD_FLAG, log)
  }

} 
Example 21
Source File: TPSITestService.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.service.tpsi

import java.util.concurrent.CountDownLatch

import com.ecfront.common.Resp
import com.ecfront.ez.framework.core.rpc.{REPLY, RESP, RPC, SUB}
import com.fasterxml.jackson.databind.JsonNode

import scala.beans.BeanProperty

@RPC("/tpsi/","","")
object TPSITestService extends TPSIService {

  val counter = new CountDownLatch(3)

  @REPLY("reply/","","","","")
  def reply(parameter: Map[String, String], body: TPSITestObj): Resp[TPSITestObj] = {
    assert(parameter("id") == "1")
    assert(body.t == "测试")
    assert(body.d == 2.2)
    exec(parameter("id"), "reply", body)
  }

  @SUB("sub/","","","","")
  def sub(parameter: Map[String, String], body: TPSITestObj): Resp[TPSITestObj] = {
    assert(parameter("id") == "1")
    assert(body.t == "测试")
    assert(body.d == 2.2)
    exec(parameter("id"), "sub", body)
  }

  @RESP("resp/","","","","")
  def resp(parameter: Map[String, String], body: TPSITestObj): Resp[TPSITestObj] = {
    assert(parameter("id") == "1")
    assert(body.t == "测试")
    assert(body.d == 2.2)
    exec(parameter("id"), "resp", body)
  }

  override protected def init(args: JsonNode): Unit = {
    assert(args.get("tt").asText() == "字段")
  }

  def exec(id: String, funName: String, body: TPSITestObj): Resp[TPSITestObj] = {
    execute(id, funName, {
      Thread.sleep(100)
      body
    }, {
      body =>
        counter.countDown()
        Resp.success(body.asInstanceOf[TPSITestObj])
    })
  }


}

class TPSITestObj {
  @BeanProperty
  var t: String = _
  @BeanProperty
  var d: BigDecimal = _
}

object TPSITestObj {
  def apply(t: String, d: BigDecimal): TPSITestObj = {
    val obj = new TPSITestObj()
    obj.t = t
    obj.d = d
    obj
  }
} 
Example 22
Source File: MapSpec.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.cluster.redis

import java.util.concurrent.CountDownLatch
import java.util.{Timer, TimerTask}

import com.ecfront.ez.framework.core.EZ
import com.ecfront.ez.framework.test.MockStartupSpec

import scala.beans.BeanProperty


class MapSpec extends MockStartupSpec {

  test("Map Test") {

    val mapObj = EZ.dist.map[TestMapObj]("test_obj_map")
    mapObj.clear()
    val obj = new TestMapObj
    obj.a = "测试"
    assert(mapObj.put("a", obj).get("a").a == "测试")


    val map = EZ.dist.map[Long]("test_map")
    map.clear()

    val timer = new Timer()
    timer.schedule(new TimerTask {
      override def run(): Unit = {
        map.put("a", System.currentTimeMillis())
      }
    }, 0, 1000)
    timer.schedule(new TimerTask {
      override def run(): Unit = {
        map.foreach({
          (k, v) =>
            println(">>a:" + v)
        })
      }
    }, 0, 10000)
    new CountDownLatch(1).await()
  }

}

class TestMapObj extends Serializable {
  @BeanProperty
  var a: String = _
} 
Example 23
Source File: EZAPIContext.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.service.gateway.interceptor

import com.ecfront.ez.framework.core.rpc.OptInfo

import scala.beans.BeanProperty
import scala.language.implicitConversions


class EZAPIContext {

  // 请求方法
  @BeanProperty var method: String = _
  // 请求对应的模块URI(可能带通配符)
  @BeanProperty var templateUri: String = _
  // 请求的真实URI
  @BeanProperty var realUri: String = _
  // 请求URL中的参数
  @BeanProperty var parameters: Map[String, String] = _
  // 远程IP
  @BeanProperty var remoteIP: String = _
  // 请求的Accept
  @BeanProperty var accept: String = _
  // 请求的ContentType
  @BeanProperty var contentType: String = _
  // Token
  @BeanProperty var token: Option[String] = None
  // 认证信息
  @BeanProperty var optInfo: Option[OptInfo] = None
  // 处理结果
  @BeanProperty var executeResult: Any = _

} 
Example 24
Source File: EZ_Test.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.service.other

import com.ecfront.ez.framework.service.jdbc._

import scala.beans.BeanProperty

@Entity("ez_test")
case class EZ_Test() extends BaseModel with SecureModel with StatusModel {

  @UUID
  @BeanProperty var bus_uuid: String = _
  @Require
  @Unique
  @Desc("code", 200, 0)
  @BeanProperty var code: String = _
  @Require
  @Desc("name", 200, 0)
  @BeanProperty var name: String = _

}

object EZ_Test extends BaseStorage[EZ_Test] with SecureStorage[EZ_Test] with StatusStorage[EZ_Test] {

  def apply(code: String, name: String, enable: Boolean = true): EZ_Test = {
    val res = EZ_Test()
    res.code = code
    res.name = name
    res.enable = enable
    res
  }

} 
Example 25
Source File: EZ_Scheduler.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.service.scheduler

import com.ecfront.common.{Ignore, Resp}
import com.ecfront.ez.framework.service.jdbc._

import scala.beans.BeanProperty

@Entity("调度任务")
case class EZ_Scheduler() extends SecureModel with StatusModel {

  @Unique
  @Desc("调度名称", 200, 0)
  @BeanProperty var name: String = _
  @Desc("调度周期", 50, 0)
  @BeanProperty var cron: String = _
  @Desc("回调执行的类", 500, 0)
  @BeanProperty var clazz: String = _
  // 任务参数
  @BeanProperty
  @Ignore var parameters: Map[String, Any] = _
  @Desc("任务参数", 0, 0)
  @BeanProperty
  var parameterstr: String = _
  @Desc("使用的模块", 200, 0)
  @BeanProperty var module: String = _
  @Desc("是否单例", 0, 0)
  @BeanProperty var exec_one_node: Boolean = _

}

object EZ_Scheduler extends SecureStorage[EZ_Scheduler] with StatusStorage[EZ_Scheduler] {

  def findByModule(module: String): Resp[List[EZ_Scheduler]] = {
    findEnabled(s"""module = ?""", List(module))
  }

  def existByModule(module: String): Resp[Boolean] = {
    existEnabledByCond(s"""module = ?""", List(module))
  }

  def deleteByModule(module: String): Resp[Void] = {
    deleteByCond(s"""module = ?""", List(module))
  }

  def enableByModule(module: String): Resp[Void] = {
    updateByCond(s"""enable = true """,s""" module = ? """, List(module))
  }

  def disableByModule(module: String): Resp[Void] = {
    updateByCond(s"""enable = false """,s""" module = ? """, List(module))
  }

  def enableByName(name: String): Resp[Void] = {
    updateByCond(s"""enable = true """,s""" name = ? """, List(name))
  }

  def disableByName(name: String): Resp[Void] = {
    updateByCond(s"""enable = false """,s""" name = ? """, List(name))
  }

  def getByName(name: String): Resp[EZ_Scheduler] = {
    getByCond(s""" name = ? """, List(name))
  }

  def deleteByName(name: String): Resp[Void] = {
    deleteByCond(s""" name = ? """, List(name))
  }

} 
Example 26
Source File: EZ_Scheduler_Log.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.service.scheduler

import com.ecfront.common.Resp
import com.ecfront.ez.framework.service.jdbc._

import scala.beans.BeanProperty

@Entity("调度任务日志")
case class EZ_Scheduler_Log() extends BaseModel {

  @Index
  @Desc("调度名称",200,0)
  @BeanProperty var scheduler_name: String = _
  @Desc("开始时间",0,0)
  @BeanProperty var start_time: Long = _
  @Desc("结束时间",0,0)
  @BeanProperty var end_time: Long = _
  @Desc("是否成功",0,0)
  @BeanProperty var success: Boolean = _
  @Desc("结果描述",500,0)
  @BeanProperty var message: String = _

}

object EZ_Scheduler_Log extends BaseStorage[EZ_Scheduler_Log] {

  def pageByName(name: String, pageNumber: Long, pageSize: Int): Resp[Page[EZ_Scheduler_Log]] = {
    page("scheduler_name = ?", List(name), pageNumber, pageSize)
  }

} 
Example 27
Source File: EZContext.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.core

import java.util.Date

import com.ecfront.common.JsonHelper
import com.ecfront.ez.framework.core.helper.TimeHelper
import com.ecfront.ez.framework.core.rpc.{OptInfo, RPCProcessor}

import scala.beans.BeanProperty
import scala.language.implicitConversions

class EZContext {
  @BeanProperty var id: String = _
  @BeanProperty var sourceIP: String = _
  @BeanProperty var startTime: Long = _
  @BeanProperty var sourceRPCPath: String = _
  @BeanProperty var token: String = _
  @BeanProperty var optAccCode: String = _
  @BeanProperty var optOrgCode: String = _
  @BeanProperty var trace: collection.mutable.Map[String, String] = _

  lazy val optInfo: Option[OptInfo] = {
    if (token != null && token.nonEmpty) {
      val result = EZ.cache.get(RPCProcessor.TOKEN_INFO_FLAG + token)
      if (result != null && result.nonEmpty) {
        Some(JsonHelper.toObject[OptInfo](result))
      } else {
        None
      }
    } else {
      None
    }
  }
}

object EZContext {

  val _context = new ThreadLocal[EZContext]

  def getContext: EZContext = {
    var cxt = _context.get()
    if (cxt == null) {
      cxt = new EZContext
      cxt.id = EZ.createUUID
      cxt.startTime = TimeHelper.msf.format(new Date).toLong
      cxt.sourceIP = EZ.Info.projectIp
      cxt.sourceRPCPath = ""
      cxt.token = ""
      cxt.optAccCode = ""
      cxt.optOrgCode = ""
      setContext(cxt)
    }
    cxt
  }

  def setContext(context: EZContext): Unit = {
    if (context.token == null) {
      context.token = ""
    }
    if (context.optAccCode == null) {
      context.optAccCode = ""
    }
    if (context.optOrgCode == null) {
      context.optOrgCode = ""
    }
    _context.set(context)
  }

} 
Example 28
Source File: RPCTest1.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.core.rpc

import com.ecfront.common.Resp

import scala.beans.BeanProperty

@RPC("/test1/","测试","")
class RPCTest1 {

  @GET("","获取xx","","")
  def t1(args: Map[String, String]): Resp[Void] = {
    Resp.success(null)
  }

  @POST("post","提交xx","","||String|uri|true","||String|url")
  def t2(args: Map[String, String], body: String): Resp[String] = {
    Resp.success(body)
  }

  @PUT("/put","更新xx",
    """
      NOTE: 补充说明
    ""","|a|String|modify|true","")
  def t3(args: Map[String, String], body: BodyTest): Resp[BodyTest] = {
    Resp.success(body)
  }

  @DELETE(":id/","删除xx","","")
  def t4(args: Map[String, String]): Resp[String] = {
    Resp.success(args("id"))
  }

  @WS("/ws/:id/:id2/","WSxx","",
    """
      |aa|String|附加1|false
      |bb|String|附加2|false
    ""","||Boolean|成功或失败")
  def t5(args: Map[String, String], body: Map[String, Any]): Resp[Boolean] = {
    assert(args("id") == "11" && args("id2") == "222")
    Resp.success(body("a").asInstanceOf[Boolean])
  }

  @PUT("/put2","更新xx",
    """
      NOTE: 补充说明
    ""","|a|String|modify|true","")
  def t13(args: Map[String, String], body: BodyTest): Resp[List[BodyTest]] = {
    Resp.success(List())
  }

}

class BodyTest {
  @Label("aaa")
  @BeanProperty @Require
  var a: String = _
}

object BodyTest {
  def apply(a: String): BodyTest = {
    val b = new BodyTest()
    b.a = a
    b
  }
} 
Example 29
Source File: EZ_Test.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.component.perf.test2

import com.ecfront.ez.framework.service.jdbc._

import scala.beans.BeanProperty

@Entity("ez_test")
case class EZ_Test() extends BaseModel with SecureModel with StatusModel {

  @UUID
  @BeanProperty var bus_uuid: String = _
  @Require @Unique
  @Desc("code",100,0)
  @BeanProperty var code: String = _
  @Require
  @Desc("name",100,0)
  @BeanProperty var name: String = _

}

object EZ_Test extends BaseStorage[EZ_Test] with SecureStorage[EZ_Test] with StatusStorage[EZ_Test] {

  def apply(code: String, name: String,enable:Boolean=true): EZ_Test = {
    val res = EZ_Test()
    res.code = code
    res.name = name
    res.enable = enable
    res
  }

} 
Example 30
Source File: EZ_Test.scala    From ez-framework   with Apache License 2.0 5 votes vote down vote up
package com.ecfront.ez.framework.component.perf.test1

import com.ecfront.ez.framework.service.jdbc._

import scala.beans.BeanProperty

@Entity("ez_test")
case class EZ_Test() extends BaseModel with SecureModel with StatusModel {

  @UUID
  @BeanProperty var bus_uuid: String = _
  @Require @Unique
  @Desc("code",100,0)
  @BeanProperty var code: String = _
  @Require
  @Desc("name",100,0)
  @BeanProperty var name: String = _

}

object EZ_Test extends BaseStorage[EZ_Test] with SecureStorage[EZ_Test] with StatusStorage[EZ_Test] {

  def apply(code: String, name: String,enable:Boolean=true): EZ_Test = {
    val res = EZ_Test()
    res.code = code
    res.name = name
    res.enable = enable
    res
  }

} 
Example 31
Source File: JavaClassSpec.scala    From firebase4s   with MIT License 5 votes vote down vote up
package com.firebase4s.test.database

import org.scalatest._
import com.firebase4s.database.DatabaseReference

import scala.beans.BeanProperty

class Name() {
  @BeanProperty var first: String = _
  @BeanProperty var last: String = _
  @BeanProperty var middle: String = _
}


class User() {
  @BeanProperty var name: Name = _
  @BeanProperty var email: String = _
}



class JavaClassSpec extends AsyncWordSpecLike with Matchers {

  import com.firebase4s.test.Test.db

  val userData = new User()
  userData.email = "[email protected]"
  val name = new Name()
  name.first = "tim"
  name.last =  "pike"
  name.middle = "d"
  userData.name = name

  val classRef: DatabaseReference = db.ref(s"test/${userData.name.first}")

  "DatabaseReference" should {
    "successfully set a value at its location" when {
      "provided with an instance of a JavaBean class" in {
        classRef
          .set(userData)
          .map(user => {
            assert(user.name.first == userData.name.first)
            assert(user.name.last == userData.name.last)
            assert(user.name.middle == userData.name.middle)
            assert(user.email == userData.email)
          })
      }
    }

    "return a snapshot containing a class instance" when {
      "provided with the class corresponding to the data at its location" in {
        classRef
          .get()
          .map(snapshot => snapshot.getValue(classOf[User]))
            .map(maybeUser => {
              assert(maybeUser.nonEmpty)
              val user = maybeUser.get
              assert(user.getClass == classOf[User])
              assert(user.name.getClass == classOf[Name])
              assert(user.name.first == userData.name.first)
              assert(user.name.last == userData.name.last)
              assert(user.name.middle == userData.name.middle)
              assert(user.email == userData.email)
            })
      }
    }
  }
} 
Example 32
Source File: ModuleListener.scala    From Linkis   with Apache License 2.0 5 votes vote down vote up
package com.webank.wedatasphere.linkis.resourcemanager.listener

import java.util
import java.util.concurrent.ConcurrentHashMap
import java.util.{Comparator, Map => JMap}

import com.webank.wedatasphere.linkis.common.utils.Logging
import com.webank.wedatasphere.linkis.resourcemanager.event.notify.{ModuleRegisterEvent, ModuleUnregisterEvent, NotifyRMEvent, NotifyRMEventListener}

import scala.beans.BeanProperty


case class EMModule(@BeanProperty moduleName: String,
                    @BeanProperty instance: String,
                    @BeanProperty createTime: Long,
                    @BeanProperty endTime: Long,
                    @BeanProperty id: String,
                    @BeanProperty state: String,
                    @BeanProperty totalResource: JMap[String, Any],
                    @BeanProperty usedResource: JMap[String, Any],
                    @BeanProperty running: Boolean,
                    @BeanProperty completed: Boolean
                   )


object ModuleListener {

  val moduleInfoCollect: JMap[String, ModuleRegisterEvent] = new ConcurrentHashMap[String, ModuleRegisterEvent]()

  def parseResource(r: String): JMap[String, Any] = {
    val resource: JMap[String, Any] = new util.HashMap

    val r0 = r.split(",|:")
    var i = 0
    while (i < r0.length) {
      if (i == 0) {
        resource.put("type", r0(i))
        i = i + 1
      } else {
        resource.put(r0(i), r0(i + 1))
        i = i + 2
      }
    }
    resource
  }
} 
Example 33
Source File: BoostingStrategy.scala    From drizzle-spark   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.mllib.tree.configuration

import scala.beans.BeanProperty

import org.apache.spark.annotation.Since
import org.apache.spark.mllib.tree.configuration.Algo._
import org.apache.spark.mllib.tree.loss.{LogLoss, Loss, SquaredError}


  @Since("1.3.0")
  def defaultParams(algo: Algo): BoostingStrategy = {
    val treeStrategy = Strategy.defaultStrategy(algo)
    treeStrategy.maxDepth = 3
    algo match {
      case Algo.Classification =>
        treeStrategy.numClasses = 2
        new BoostingStrategy(treeStrategy, LogLoss)
      case Algo.Regression =>
        new BoostingStrategy(treeStrategy, SquaredError)
      case _ =>
        throw new IllegalArgumentException(s"$algo is not supported by boosting.")
    }
  }
} 
Example 34
Source File: PMMLModelExport.scala    From drizzle-spark   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.mllib.pmml.export

import java.text.SimpleDateFormat
import java.util.Date

import scala.beans.BeanProperty

import org.dmg.pmml.{Application, Header, PMML, Timestamp}

private[mllib] trait PMMLModelExport {

  
  @BeanProperty
  val pmml: PMML = {
    val version = getClass.getPackage.getImplementationVersion
    val app = new Application("Apache Spark MLlib").setVersion(version)
    val timestamp = new Timestamp()
      .addContent(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date()))
    val header = new Header()
      .setApplication(app)
      .setTimestamp(timestamp)
    new PMML("4.2", header, null)
  }
} 
Example 35
Source File: SelectHiveQL.scala    From piflow   with BSD 2-Clause "Simplified" License 5 votes vote down vote up
package cn.piflow.bundle.hive

import cn.piflow._
import cn.piflow.conf._
import cn.piflow.conf.bean.PropertyDescriptor
import cn.piflow.conf.util.{ImageUtil, MapUtil}
import org.apache.spark.sql.SparkSession

import scala.beans.BeanProperty



class SelectHiveQL extends ConfigurableStop {

  val authorEmail: String = "[email protected]"
  val description: String = "Execute select clause of hiveQL"
  val inportList: List[String] = List(Port.DefaultPort)
  val outportList: List[String] = List(Port.DefaultPort)

  var hiveQL:String = _

  def perform(in: JobInputStream, out: JobOutputStream, pec: JobContext): Unit = {
    val spark = pec.get[SparkSession]()

    import spark.sql
    val df = sql(hiveQL)

    out.write(df)
  }

  def initialize(ctx: ProcessContext): Unit = {

  }

  def setProperties(map : Map[String, Any]): Unit = {
    hiveQL = MapUtil.get(map,"hiveQL").asInstanceOf[String]
  }

  override def getPropertyDescriptor(): List[PropertyDescriptor] = {
    var descriptor : List[PropertyDescriptor] = List()
    val hiveQL = new PropertyDescriptor()
      .name("hiveQL")
      .displayName("HiveQL")
      .defaultValue("")
      .allowableValues(Set(""))
      .description("Execute select clause of hiveQL")
      .required(true)
      .example("select * from test.user1")
    descriptor = hiveQL :: descriptor
    descriptor
  }

  override def getIcon(): Array[Byte] = {
    ImageUtil.getImage("icon/hive/SelectHiveQL.png")
  }

  override def getGroup(): List[String] = {
    List(StopGroup.HiveGroup)
  }


} 
Example 36
Source File: PutHiveStreaming.scala    From piflow   with BSD 2-Clause "Simplified" License 5 votes vote down vote up
package cn.piflow.bundle.hive

import cn.piflow._
import cn.piflow.conf._
import cn.piflow.conf.bean.PropertyDescriptor
import cn.piflow.conf.util.{ImageUtil, MapUtil}
import org.apache.spark.sql.SparkSession

import scala.beans.BeanProperty

class PutHiveStreaming extends ConfigurableStop {

  val authorEmail: String = "[email protected]"
  val description: String = "Save data to hive"
  val inportList: List[String] = List(Port.DefaultPort)
  val outportList: List[String] = List(Port.DefaultPort)

  var database:String = _
  var table:String = _

  def perform(in: JobInputStream, out: JobOutputStream, pec: JobContext): Unit = {
    val spark = pec.get[SparkSession]()
    val inDF = in.read()

    val dfTempTable = table + "_temp"
    inDF.createOrReplaceTempView(dfTempTable)
    spark.sql("insert into " + database + "." + table +  " select * from " + dfTempTable)

  }

  def initialize(ctx: ProcessContext): Unit = {

  }

  def setProperties(map : Map[String, Any]) = {
    database = MapUtil.get(map,"database").asInstanceOf[String]
    table = MapUtil.get(map,"table").asInstanceOf[String]
  }

  override def getPropertyDescriptor(): List[PropertyDescriptor] = {
    var descriptor : List[PropertyDescriptor] = List()
    val database=new PropertyDescriptor()
      .name("database")
      .displayName("DataBase")
      .description("The database name")
      .defaultValue("")
      .required(true)
      .example("test")
    descriptor = database :: descriptor

    val table = new PropertyDescriptor()
      .name("table")
      .displayName("Table")
      .description("The table name")
      .defaultValue("")
      .required(true)
      .example("stream")
    descriptor = table :: descriptor

    descriptor
  }

  override def getIcon(): Array[Byte] = {
    ImageUtil.getImage("icon/hive/PutHiveStreaming.png")
  }

  override def getGroup(): List[String] = {
    List(StopGroup.HiveGroup.toString)
  }


} 
Example 37
Source File: DataFrameRowParser.scala    From piflow   with BSD 2-Clause "Simplified" License 5 votes vote down vote up
package cn.piflow.bundle.script

import cn.piflow.conf.bean.PropertyDescriptor
import cn.piflow.conf.util.{ImageUtil, MapUtil}
import cn.piflow.conf._
import cn.piflow.{JobContext, JobInputStream, JobOutputStream, ProcessContext}
import org.apache.spark.sql.types.{StringType, StructField, StructType}
import org.apache.spark.sql.{Row, SparkSession}

import scala.beans.BeanProperty

class DataFrameRowParser extends ConfigurableStop{

  val authorEmail: String = "[email protected]"
  val description: String = "Create dataframe by schema"
  val inportList: List[String] = List(Port.DefaultPort.toString)
  val outportList: List[String] = List(Port.DefaultPort.toString)

  var schema: String = _
  var separator: String = _

  override def setProperties(map: Map[String, Any]): Unit = {
    schema = MapUtil.get(map,"schema").asInstanceOf[String]
    separator = MapUtil.get(map,"separator").asInstanceOf[String]
  }

  override def getPropertyDescriptor(): List[PropertyDescriptor] = {
    var descriptor : List[PropertyDescriptor] = List()
    val schema = new PropertyDescriptor().name("schema").displayName("schema").description("The schema of dataframe").defaultValue("").required(true)
    val separator = new PropertyDescriptor().name("separator").displayName("separator").description("The separator of schema").defaultValue("").required(true)
    descriptor = schema :: descriptor
    descriptor = separator :: descriptor
    descriptor
  }

  override def getIcon(): Array[Byte] = {
    ImageUtil.getImage("icon/script/DataFrameRowParser.png")
  }

  override def getGroup(): List[String] = {
    List(StopGroup.ScriptGroup.toString)
  }


  override def initialize(ctx: ProcessContext): Unit = {}

  override def perform(in: JobInputStream, out: JobOutputStream, pec: JobContext): Unit = {
    val spark = pec.get[SparkSession]()
    val inDF = in.read()

    //parse RDD
    val rdd = inDF.rdd.map(row => {
      val fieldArray = row.get(0).asInstanceOf[String].split(",")
      Row.fromSeq(fieldArray.toSeq)
    })

    //parse schema
    val field = schema.split(separator)
    val structFieldArray : Array[StructField] = new Array[StructField](field.size)
    for(i <- 0 to field.size - 1){
      structFieldArray(i) = new StructField(field(i),StringType, nullable = true)
    }
    val schemaStructType = StructType(structFieldArray)

    //create DataFrame
    val df = spark.createDataFrame(rdd,schemaStructType)
    //df.show()
    out.write(df)
  }

} 
Example 38
Source File: MysqlWrite.scala    From piflow   with BSD 2-Clause "Simplified" License 5 votes vote down vote up
package cn.piflow.bundle.jdbc

import java.util.Properties

import cn.piflow._
import cn.piflow.conf._
import cn.piflow.conf.bean.PropertyDescriptor
import cn.piflow.conf.util.{ImageUtil, MapUtil}
import org.apache.spark.sql.{SaveMode, SparkSession}

import scala.beans.BeanProperty

class MysqlWrite extends ConfigurableStop{

  val authorEmail: String = "[email protected]"
  val description: String = "Write data to mysql database with jdbc"
  val inportList: List[String] = List(Port.DefaultPort)
  val outportList: List[String] = List(Port.DefaultPort)

  var url:String = _
  var user:String = _
  var password:String = _
  var dbtable:String = _

  def perform(in: JobInputStream, out: JobOutputStream, pec: JobContext): Unit = {
    val spark = pec.get[SparkSession]()
    val jdbcDF = in.read()
    val properties = new Properties()
    properties.put("user", user)
    properties.put("password", password)
    jdbcDF.write.mode(SaveMode.Append).jdbc(url,dbtable,properties)
    out.write(jdbcDF)
  }

  def initialize(ctx: ProcessContext): Unit = {

  }

  override def setProperties(map: Map[String, Any]): Unit = {
    url = MapUtil.get(map,"url").asInstanceOf[String]
    user = MapUtil.get(map,"user").asInstanceOf[String]
    password = MapUtil.get(map,"password").asInstanceOf[String]
    dbtable = MapUtil.get(map,"dbtable").asInstanceOf[String]
  }

  override def getPropertyDescriptor(): List[PropertyDescriptor] = {
    var descriptor : List[PropertyDescriptor] = List()

    val url=new PropertyDescriptor()
      .name("url")
      .displayName("Url")
      .description("The Url, for example jdbc:mysql://127.0.0.1/dbname")
      .defaultValue("")
      .required(true)
      .example("jdbc:mysql://127.0.0.1/dbname")
    descriptor = url :: descriptor

    val user=new PropertyDescriptor()
      .name("user")
      .displayName("User")
      .description("The user name of database")
      .defaultValue("")
      .required(true)
      .example("root")
    descriptor = user :: descriptor

    val password=new PropertyDescriptor()
      .name("password")
      .displayName("Password")
      .description("The password of database")
      .defaultValue("")
      .required(true)
      .example("123456")
      .sensitive(true)
    descriptor = password :: descriptor

    val dbtable=new PropertyDescriptor()
      .name("dbtable")
      .displayName("DBTable")
      .description("The table you want to write")
      .defaultValue("")
      .required(true)
      .example("test")
    descriptor = dbtable :: descriptor

    descriptor
  }

  override def getIcon(): Array[Byte] = {
    ImageUtil.getImage("icon/jdbc/MysqlWrite.png")
  }

  override def getGroup(): List[String] = {
    List(StopGroup.JdbcGroup)
  }


} 
Example 39
Source File: JsonSave.scala    From piflow   with BSD 2-Clause "Simplified" License 5 votes vote down vote up
package cn.piflow.bundle.json

import cn.piflow._
import cn.piflow.conf._
import cn.piflow.conf.bean.PropertyDescriptor
import cn.piflow.conf.util.{ImageUtil, MapUtil}
import org.apache.spark.sql.SaveMode

import scala.beans.BeanProperty

class JsonSave extends ConfigurableStop{

  val authorEmail: String = "[email protected]"
  val description: String = "Save data into json file"
  val inportList: List[String] = List(Port.DefaultPort)
  val outportList: List[String] = List(Port.DefaultPort)

  var jsonSavePath: String = _

  def perform(in: JobInputStream, out: JobOutputStream, pec: JobContext): Unit = {

    val jsonDF = in.read()
    jsonDF.write.format("json").mode(SaveMode.Overwrite).save(jsonSavePath)
  }

  def initialize(ctx: ProcessContext): Unit = {

  }

  override def setProperties(map: Map[String, Any]): Unit = {
    jsonSavePath = MapUtil.get(map,"jsonSavePath").asInstanceOf[String]
  }

  override def getPropertyDescriptor(): List[PropertyDescriptor] = {
    var descriptor : List[PropertyDescriptor] = List()
    val jsonSavePath = new PropertyDescriptor()
      .name("jsonSavePath")
      .displayName("JsonSavePath")
      .description("The save path of the json file")
      .defaultValue("")
      .required(true)
        .example("hdfs://192.168.3.138:8020/work/testJson/test/")

    descriptor = jsonSavePath :: descriptor
    descriptor
  }

  override def getIcon(): Array[Byte] = {
    ImageUtil.getImage("icon/json/JsonSave.png")
  }

  override def getGroup(): List[String] = {
    List(StopGroup.JsonGroup)
  }


} 
Example 40
Source File: XmlParser.scala    From piflow   with BSD 2-Clause "Simplified" License 5 votes vote down vote up
package cn.piflow.bundle.xml

import cn.piflow._
import cn.piflow.conf._
import cn.piflow.conf.bean.PropertyDescriptor
import cn.piflow.conf.util.{ImageUtil, MapUtil}
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.types.StructType

import scala.beans.BeanProperty

class XmlParser extends ConfigurableStop {

  val authorEmail: String = "[email protected]"
  val description: String = "Parse xml file"
  val inportList: List[String] = List(Port.DefaultPort)
  val outportList: List[String] = List(Port.DefaultPort)

  var xmlpath:String = _
  var rowTag:String = _

  def perform(in: JobInputStream, out: JobOutputStream, pec: JobContext): Unit = {

    val spark = pec.get[SparkSession]()

    val xmlDF = spark.read.format("com.databricks.spark.xml")
      .option("rowTag",rowTag)
      .option("treatEmptyValuesAsNulls",true)
      .load(xmlpath)

    out.write(xmlDF)
  }

  def initialize(ctx: ProcessContext): Unit = {

  }

  def setProperties(map : Map[String, Any]) = {
    xmlpath = MapUtil.get(map,"xmlpath").asInstanceOf[String]
    rowTag = MapUtil.get(map,"rowTag").asInstanceOf[String]
  }

  override def getPropertyDescriptor(): List[PropertyDescriptor] = {
    var descriptor : List[PropertyDescriptor] = List()
    val xmlpath = new PropertyDescriptor()
      .name("xmlpath")
      .displayName("xmlpath")
      .description("the path of xml file")
      .defaultValue("")
      .required(true)
      .example("hdfs://192.168.3.138:8020/work/test/testxml.xml")

    val rowTag = new PropertyDescriptor()
      .name("rowTag")
      .displayName("rowTag")
      .description("the tag you want to parse in xml file")
      .defaultValue("")
      .required(true)
        .example("name")

    descriptor = xmlpath :: descriptor
    descriptor = rowTag :: descriptor
    descriptor
  }

  override def getIcon(): Array[Byte] = {
    ImageUtil.getImage("icon/xml/XmlParser.png")
  }

  override def getGroup(): List[String] = {
    List(StopGroup.XmlGroup)
  }

} 
Example 41
Source File: XmlSave.scala    From piflow   with BSD 2-Clause "Simplified" License 5 votes vote down vote up
package cn.piflow.bundle.xml

import cn.piflow._
import cn.piflow.conf._
import cn.piflow.conf.bean.PropertyDescriptor
import cn.piflow.conf.util.{ImageUtil, MapUtil}
import org.codehaus.jackson.map.ext.CoreXMLSerializers.XMLGregorianCalendarSerializer

import scala.beans.BeanProperty

class XmlSave extends ConfigurableStop{

  val authorEmail: String = "[email protected]"
  val description: String = "Save data to xml file"
  val inportList: List[String] = List(Port.DefaultPort)
  val outportList: List[String] = List(Port.DefaultPort)

  var xmlSavePath:String = _

  def perform(in: JobInputStream, out: JobOutputStream, pec: JobContext): Unit = {
    val xmlDF = in.read()

    xmlDF.write.format("xml").save(xmlSavePath)
  }

  def initialize(ctx: ProcessContext): Unit = {

  }

  override def setProperties(map: Map[String, Any]): Unit = {
    xmlSavePath = MapUtil.get(map,"xmlSavePath").asInstanceOf[String]
  }

  override def getPropertyDescriptor(): List[PropertyDescriptor] = {
    var descriptor : List[PropertyDescriptor] = List()
    val xmlSavePath = new PropertyDescriptor()
      .name("xmlSavePath")
      .displayName("XmlSavePath")
      .description("xml file save path")
      .defaultValue("")
      .required(true)
        .example("hdfs://192.168.3.138:8020/work/test/test.xml")
    descriptor = xmlSavePath :: descriptor
    descriptor
  }

  override def getIcon(): Array[Byte] = {
    ImageUtil.getImage("icon/xml/XmlSave.png")
  }

  override def getGroup(): List[String] = {
    List(StopGroup.XmlGroup)
  }

} 
Example 42
Source File: SelectField.scala    From piflow   with BSD 2-Clause "Simplified" License 5 votes vote down vote up
package cn.piflow.bundle.common

import cn.piflow._
import cn.piflow.conf._
import cn.piflow.conf.bean.PropertyDescriptor
import cn.piflow.conf.util.{ImageUtil, MapUtil}
import org.apache.spark.sql.{Column, DataFrame}

import scala.beans.BeanProperty


class SelectField extends ConfigurableStop {

  val authorEmail: String = "[email protected]"
  val description: String = "Select data column"
  val inportList: List[String] = List(Port.DefaultPort)
  val outportList: List[String] = List(Port.DefaultPort)

  var columnNames:String = _

  def perform(in: JobInputStream, out: JobOutputStream, pec: JobContext): Unit = {
    val df = in.read()

    val field = columnNames.split(",").map(x => x.trim)
    val columnArray : Array[Column] = new Array[Column](field.size)
    for(i <- 0 to field.size - 1){
      columnArray(i) = new Column(field(i))
    }

    var finalFieldDF : DataFrame = df.select(columnArray:_*)
    out.write(finalFieldDF)
  }

  def initialize(ctx: ProcessContext): Unit = {

  }

  def setProperties(map : Map[String, Any]): Unit = {
    columnNames = MapUtil.get(map,"columnNames").asInstanceOf[String]
  }

  override def getPropertyDescriptor(): List[PropertyDescriptor] = {
    var descriptor : List[PropertyDescriptor] = List()
    val inports = new PropertyDescriptor()
      .name("columnNames")
      .displayName("ColumnNames")
      .description("Select the column you want,multiple columns separated by commas")
      .defaultValue("")
      .required(true)
      .example("id,name")
    descriptor = inports :: descriptor
    descriptor
  }

  override def getIcon(): Array[Byte] = {
    ImageUtil.getImage("icon/common/SelectField.png")
  }

  override def getGroup(): List[String] = {
    List(StopGroup.CommonGroup)
  }

} 
Example 43
Source File: ModelAdapter.scala    From daf-semantics   with Apache License 2.0 5 votes vote down vote up
package it.almawave.linkeddata.kb.utils

import java.net.URI
import scala.reflect._
import scala.reflect.runtime.universe._
import scala.collection.JavaConversions._
import scala.collection.JavaConverters._
import scala.beans.BeanProperty


  def fromMap[T: TypeTag: ClassTag](m: Map[String, _]) = {

    val rm = runtimeMirror(classTag[T].runtimeClass.getClassLoader)
    val classTest = typeOf[T].typeSymbol.asClass
    val classMirror = rm.reflectClass(classTest)
    val constructor = typeOf[T].decl(termNames.CONSTRUCTOR).asMethod
    val constructorMirror = classMirror.reflectConstructor(constructor)

    val constructorArgs = constructor.paramLists.flatten.map((param: Symbol) => {
      val paramName = param.name.toString
      // if a parameter is optional (it can be omitted!)
      if (param.typeSignature <:< typeOf[Option[Any]]) m.get(paramName)
      // else take the required parameter, or throw an exception!
      else m.get(paramName)
        .getOrElse(throw new IllegalArgumentException(s"Map is missing required parameter ${paramName}!"))
    })

    constructorMirror(constructorArgs: _*).asInstanceOf[T]
  }

} 
Example 44
Source File: StreamConf.scala    From HadoopLearning   with MIT License 5 votes vote down vote up
package com.utils

import scala.beans.BeanProperty

/**
  * Spark Streaming 配置
  * @author dtinone
  * 加米谷大数据学院
  * http://www.dtinone.com/
  * 加米谷版权所有,仅供加米谷大数据学院内部使用,禁止其他机构使用,违者必究,追究法律责任。
  */
class StreamConf {

  @BeanProperty
  var topics: String = "dtinone.*"

  @BeanProperty
  var brokers: String = ""

  @BeanProperty
  var groupId: String = ""

  @BeanProperty
  var zkUrl: String = ""

} 
Example 45
Source File: ResultSetResult.scala    From Linkis   with Apache License 2.0 5 votes vote down vote up
@DWSHttpMessageResult("/api/rest_j/v\\d+/filesystem/openFile")
class ResultSetResult extends DWSResult with UserAction {

  private var `type`: String = _

  def setType(`type`: String) = this.`type` = `type`
  def getType = `type`
  @BeanProperty
  var metadata: Object = _
  @BeanProperty
  var page: Int = _
  @BeanProperty
  var totalLine: Int = _
  @BeanProperty
  var totalPage: Int = _
  @BeanProperty var fileContent: Object = _
} 
Example 46
Source File: UserListener.scala    From Linkis   with Apache License 2.0 5 votes vote down vote up
package com.webank.wedatasphere.linkis.resourcemanager.listener

import java.util
import java.util.concurrent.ConcurrentHashMap
import java.util.{Comparator, List => JList, Map => JMap}

import com.webank.wedatasphere.linkis.common.listener.Event
import com.webank.wedatasphere.linkis.common.utils.Logging
import com.webank.wedatasphere.linkis.resourcemanager.event.metric.{MetricRMEvent, MetricRMEventListener, UserSessionEndEvent, UserSessionStartEvent}

import scala.beans.BeanProperty


class UserListener extends MetricRMEventListener with Logging {

  import UserListener._

  override def onMetricRMEvent(e: MetricRMEvent): Unit = e match {
    case e: UserSessionStartEvent => {
      info("[Add a startup event(添加启动事件)]user:" + e.user + ",UserSessionStartEvent: " + e)

      userSessionCollect.put(e.userUsedResource.moduleInstance.getInstance, e)

    }
    case e: UserSessionEndEvent => {
      info("[Cleanup event(清理结束事件)]user:" + e.user + ",UserSessionEndEvent: " + e)
      userSessionCollect.remove(e.userReleasedResource.moduleInstance)
    }
    case _ => {
      warn("not found : " + e)
    }
  }

  def onEventError(event: Event, t: scala.Throwable): scala.Unit = {}
}

class userEventByUserName extends Comparator[UserSessionStartEvent] {
  override def compare(o1: UserSessionStartEvent, o2: UserSessionStartEvent): Int = {
    o1.user.compareTo(o2.user)
  }
}

case class UserSessionInfo(@BeanProperty userName: String,
                           @BeanProperty id: String,
                           @BeanProperty state: String,
                           @BeanProperty running: Boolean,
                           @BeanProperty completed: Boolean,
                           @BeanProperty moduleName: String,
                           @BeanProperty instance: String,
                           @BeanProperty resource: JMap[String, Any],
                           @BeanProperty ticketId: String
                          )

object UserListener {
  val userSessionCollect: JMap[String, UserSessionStartEvent] = new ConcurrentHashMap[String, UserSessionStartEvent]()

  def parseResource(r: String): JMap[String, Any] = {
    val resource: JMap[String, Any] = new util.HashMap
    if (r == null) return resource;
    val r0 = r.split(",|:")
    var i = 0
    while (i < r0.length) {
      if (i == 0) {
        resource.put("type", r0(i))
        i = i + 1
      } else {
        resource.put(r0(i), r0(i + 1))
        i = i + 2
      }
    }
    resource
  }

  def getUserEvent(userName: String): JList[UserSessionInfo] = {
    val eventIterator = userSessionCollect.values().iterator()
    val userList: JList[UserSessionInfo] = new util.ArrayList[UserSessionInfo]();
    while (eventIterator.hasNext) {
      val e = eventIterator.next()
      if (e.user.equals(userName)) {
        val usi = UserSessionInfo(e.user, e.getId, e.getState.toString, e.isRunning, e.isCompleted, e.userUsedResource.moduleInstance.getApplicationName
          , e.userUsedResource.moduleInstance.getInstance, parseResource(e.userUsedResource.resource.toString), e.userUsedResource.ticketId)
        userList.add(usi)
      }
    }
    userList
  }
} 
Example 47
Source File: LogCollector.scala    From daml   with Apache License 2.0 5 votes vote down vote up
// Copyright (c) 2020 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package com.daml.platform.testing

import ch.qos.logback.classic.Level
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.AppenderBase

import scala.beans.BeanProperty
import scala.collection.concurrent.TrieMap
import scala.collection.mutable
import scala.reflect.ClassTag

object LogCollector {

  private val log =
    TrieMap
      .empty[String, TrieMap[String, mutable.Builder[(Level, String), Vector[(Level, String)]]]]

  def read[Test, Logger](
      implicit test: ClassTag[Test],
      logger: ClassTag[Logger]): IndexedSeq[(Level, String)] =
    log
      .get(test.runtimeClass.getName)
      .flatMap(_.get(logger.runtimeClass.getName))
      .fold(IndexedSeq.empty[(Level, String)])(_.result())

  def clear[Test](implicit test: ClassTag[Test]): Unit = {
    log.remove(test.runtimeClass.getName)
    ()
  }

}

final class LogCollector extends AppenderBase[ILoggingEvent] {

  @BeanProperty
  var test: String = _

  override def append(e: ILoggingEvent): Unit = {
    if (test == null) {
      addError("Test identifier undefined, skipping logging")
    } else {
      val log = LogCollector.log
        .getOrElseUpdate(test, TrieMap.empty)
        .getOrElseUpdate(e.getLoggerName, Vector.newBuilder)
      val _ = log.synchronized { log += e.getLevel -> e.getMessage }
    }
  }
} 
Example 48
Source File: ParamParser.scala    From automl   with Apache License 2.0 5 votes vote down vote up
package com.tencent.angel.spark.automl.tuner.parameter

import com.tencent.angel.spark.automl.utils.AutoMLException

import scala.beans.BeanProperty


  def parseOneParam(input: String): ParamConfig = {
    val configs = input.split(INNER_PARAM_SEP)
    println(s"configs: ${configs.mkString(",")}")
    assert(configs.size == 4 || configs.size == 5, helper)
    val paramName = getParamName(configs)
    val paramType = getParamType(configs)
    val valueType = getValueType(configs, paramType)
    val paramRange = getParamRange(configs, paramType)
    val options = getOptions(configs)
    new ParamConfig(paramName, paramType, valueType, paramRange, options)
  }

  def getParamName(configs: Array[String]): String = configs(0)

  def getParamType(configs: Array[String]): String = {
    val paramType = configs(1).toUpperCase
    paramType match {
      case "D" => "discrete"
      case "C" => "continuous"
      case "CA" => "categorical"
      case _ => throw new AutoMLException(helper_param_type)
    }
  }

  def getValueType(configs: Array[String], paramType: String): String = {
    val valueType = configs(2).toLowerCase
    paramType match {
      case "discrete" =>
        assert(Array("float", "double", "int", "long").contains(valueType), helper_value_type)
        valueType
      case "continuous" =>
        "double"
      case "categorical" =>
        valueType
    }
  }

  def getParamRange(configs: Array[String], paramType: String): String = {
    paramType match {
      case "discrete" => configs(3).mkString("{", "", "}")
      case "continuous" => configs(3).mkString("[", "", "]")
      // TODO: use categorical specific format
      case "categorical" => configs(3)
    }
  }


  def getOptions(configs: Array[String]): Option[String] = {
    if (configs.size == 4)
      None
    else
      Some(configs(4))
  }

}

class ParamConfig(@BeanProperty var paramName: String,
                  @BeanProperty var paramType: String,
                  @BeanProperty var valueType: String,
                  @BeanProperty var paramRange: String,
                  @BeanProperty var option: Option[String]) 
Example 49
Source File: ExceptionHandler.scala    From maha   with Apache License 2.0 5 votes vote down vote up
// Copyright 2017, Yahoo Holdings Inc.
// Licensed under the terms of the Apache License 2.0. Please see LICENSE file in project root for terms.
package com.yahoo.maha.api.jersey

import javax.ws.rs.core.{MediaType, Response}
import javax.ws.rs.ext.{ExceptionMapper, Provider}

import com.yahoo.maha.service.error.{MahaServiceExecutionException, MahaServiceBadRequestException}
import grizzled.slf4j.Logging

import scala.beans.BeanProperty

@Provider
class GenericExceptionMapper extends ExceptionMapper[Throwable] with Logging {

  override def toResponse(e: Throwable): Response = {

    val response: Response = {
      e match {
        case iae: IllegalArgumentException => Response.status(Response.Status.BAD_REQUEST).entity(Error(iae.getMessage)).`type`(MediaType.APPLICATION_JSON).build()
        case NotFoundException(error) => Response.status(Response.Status.BAD_REQUEST).entity(error).`type`(MediaType.APPLICATION_JSON).build()
        case MahaServiceBadRequestException(message, source) => Response.status(Response.Status.BAD_REQUEST).entity(Error(message)).`type`(MediaType.APPLICATION_JSON).build()
        case MahaServiceExecutionException(message, source) =>
          source match {
            case Some(e) if e.isInstanceOf[IllegalArgumentException] =>
              Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage).`type`(MediaType.APPLICATION_JSON).build()
            case _ =>
              Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(Error(message)).`type`(MediaType.APPLICATION_JSON).build()
          }
        case _ => Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(Error(s"$e")).`type`(MediaType.APPLICATION_JSON).build()
      }
    }

    error(s"response status: ${response.getStatus} , response entity: ${response.getEntity}")
    response
  }

}

case class Error(@BeanProperty errorMsg: String)

case class NotFoundException(error: Error) extends Exception 
Example 50
Source File: Note.scala    From spring-cloud-demo   with Apache License 2.0 5 votes vote down vote up
package com.jesperdj.example.service.whiteboard.domain

import java.lang.{Long => JavaLong}
import java.util.Date
import javax.persistence.{Column, Entity, GeneratedValue, Id}

import scala.beans.BeanProperty

@Entity
class Note {

  @Id
  @GeneratedValue
  @BeanProperty
  var id: JavaLong = _

  @Column(nullable = false)
  @BeanProperty
  var createdDateTime: Date = _

  @Column(nullable = false, length = 40)
  @BeanProperty
  var authorName: String = _

  @Column(nullable = false, length = 1000)
  @BeanProperty
  var content: String = _
} 
Example 51
Source File: Application.scala    From spring-scala-examples   with Apache License 2.0 5 votes vote down vote up
package hello

import org.springframework.boot.{CommandLineRunner, SpringApplication}
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.jms.core.JmsTemplate
import org.springframework.jms.core.MessageCreator
import org.springframework.util.FileSystemUtils
import javax.jms.{Message, Session, TextMessage}
import java.io.File

import org.springframework.beans.factory.annotation.Autowired

import scala.beans.BeanProperty

@SpringBootApplication
class Application extends CommandLineRunner {

  @BeanProperty
  @Autowired
  val jmsTemplate : JmsTemplate = null

  def run(args: String*) : Unit = {
    // Clean out any ActiveMQ data from a previous run
    FileSystemUtils.deleteRecursively(new File("activemq-data"))

    // create message
    val messageCreator : MessageCreator = new MessageCreator {
      override def createMessage(session: Session): Message = session.createTextMessage("ping!")
    }

    val receiver: Runnable = new Runnable {
      def run(): Unit ={
        // synchronous reception
        System.out.println("====> Waiting for new message.")
        jmsTemplate.setReceiveTimeout(100)
        jmsTemplate.receive("default") match {
          case m:TextMessage => println("====> Synchronous reception: " + m.getText)
          case _ => println("====> Synchronous reception: No message received")
        }
      }
    }

    new Thread(receiver).start()

    // Send a message
    System.out.println("====> Sending a new message.")
    jmsTemplate.send("default", messageCreator)
  }
}

object Application extends App {
  SpringApplication.run(classOf[Application], args:_*)
} 
Example 52
Source File: ItemCardIdentification.scala    From Electrodynamics   with GNU Lesser General Public License v3.0 5 votes vote down vote up
package com.calclavia.edx.optics.security.card

import com.calclavia.edx.core.EDX
import com.resonant.core.access.{AbstractAccess, AccessUser, Permissions}
import nova.core.entity.component.Player
import nova.core.game.InputManager.Key
import nova.core.item.Item.{RightClickEvent, TooltipEvent}
import nova.core.network.NetworkTarget.Side
import nova.core.network.{Packet, Syncable}
import nova.core.retention.Store
import nova.scala.wrapper.FunctionalWrapper._

import scala.beans.BeanProperty

class ItemCardIdentification extends ItemCardAccess with Syncable {
	
				if (access != null) {
					access = new AccessUser(packet.readString())
				}
				else {
					access = new AccessUser(packet.readString())
				}
			}
		}
	}
} 
Example 53
Source File: FXHologramProgress.scala    From Electrodynamics   with GNU Lesser General Public License v3.0 5 votes vote down vote up
package com.calclavia.edx.optics.fx

import com.calclavia.edx.optics.content.OpticsTextures
import nova.core.component.renderer.DynamicRenderer
import nova.core.render.Color
import nova.core.render.model.{MeshModel, Model}
import nova.core.render.pipeline.BlockRenderPipeline
import nova.scala.util.ExtendedUpdater
import nova.scala.wrapper.FunctionalWrapper._

import scala.beans.BeanProperty
import scala.collection.convert.wrapAll._

class FXHologramProgress(@BeanProperty var color: Color, maxAge: Double) extends FXMFFS with ExtendedUpdater {
	var age = 0d

	components.add(new DynamicRenderer())
		.onRender(
	    (model: Model) => {
		    //		GL11.glPushMatrix
		    val completion = age / maxAge
		    model.matrix.scale(1.01, 1.01, 1.01)
		    model.matrix.translate(0, (completion - 1) / 2, 0)
		    model.matrix.scale(1, completion, 1)

		    var op = 0.5

		    if (maxAge - age <= 4) {
			    op = 0.5f - (5 - (maxAge - age)) * 0.1F
		    }

		    //OpenGlHelper.setLightmapTextureCoords(OpenGlHelper.lightmapTexUnit, 240.0F, 240.0F)
		    //RenderUtility.enableBlending
		    val cube = BlockRenderPipeline.drawCube(new MeshModel())
		    cube.bindAll(OpticsTextures.hologram)
		    cube.faces.foreach(_.vertices.foreach(_.color = (color.alpha((op * 255).toInt))))
		    model.addChild(cube)
		    //RenderUtility.disableBlending
	    }
		)

	override def update(deltaTime: Double) {
		super.update(deltaTime)

		age += deltaTime

		if (age > maxAge) {
			world.removeEntity(this)
		}
	}

} 
Example 54
Source File: TestEntity.scala    From arangodb-spark-connector   with Apache License 2.0 5 votes vote down vote up
package com.arangodb.spark

import scala.beans.BeanProperty

case class TestEntity(@BeanProperty var test: Int = Int.MaxValue,
                      @BeanProperty var booleanValue: Boolean = true,
                      @BeanProperty var doubleValue: Double = Double.MaxValue,
                      @BeanProperty var floatValue: Float = Float.MaxValue,
                      @BeanProperty var longValue: Long = Long.MaxValue,
                      @BeanProperty var intValue: Int = Int.MaxValue,
                      @BeanProperty var shortValue: Short = Short.MaxValue,
                      @BeanProperty var nullString: String = null,
                      @BeanProperty var stringValue: String = "test") {

  def this() = this(test = Int.MaxValue)

} 
Example 55
Source File: ConfigureMojo.scala    From lagom   with Apache License 2.0 5 votes vote down vote up
package com.lightbend.lagom.maven

import javax.inject.Inject
import org.apache.maven.execution.MavenSession
import org.apache.maven.plugin.AbstractMojo

import scala.beans.BeanProperty


class ConfigureMojo @Inject() (session: MavenSession) extends AbstractMojo {
  @BeanProperty
  var lagomService: Boolean = _

  @BeanProperty
  var playService: Boolean = _

  override def execute(): Unit = {
    LagomKeys.LagomService.put(session.getCurrentProject, lagomService)
    LagomKeys.PlayService.put(session.getCurrentProject, playService)
  }
} 
Example 56
Source File: PMMLModelExport.scala    From sparkoscope   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.mllib.pmml.export

import java.text.SimpleDateFormat
import java.util.{Date, Locale}

import scala.beans.BeanProperty

import org.dmg.pmml.{Application, Header, PMML, Timestamp}

private[mllib] trait PMMLModelExport {

  
  @BeanProperty
  val pmml: PMML = {
    val version = getClass.getPackage.getImplementationVersion
    val app = new Application("Apache Spark MLlib").setVersion(version)
    val timestamp = new Timestamp()
      .addContent(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US).format(new Date()))
    val header = new Header()
      .setApplication(app)
      .setTimestamp(timestamp)
    new PMML("4.2", header, null)
  }
} 
Example 57
Source File: BoostingStrategy.scala    From mllib_subpackage   with Apache License 2.0 5 votes vote down vote up
package org.apache.spark.mllib.tree.configuration

import scala.beans.BeanProperty

import org.apache.spark.annotation.{Experimental, Since}
import org.apache.spark.mllib.tree.configuration.Algo._
import org.apache.spark.mllib.tree.loss.{LogLoss, SquaredError, Loss}


  @Since("1.3.0")
  def defaultParams(algo: Algo): LambdaBoostingStrategy = {
    val treeStrategy = LambdaStrategy.defaultStrategy(algo)
    treeStrategy.maxDepth = 3
    algo match {
      case Algo.Classification =>
        treeStrategy.numClasses = 2
        new LambdaBoostingStrategy(treeStrategy, LogLoss)
      case Algo.Regression =>
        new LambdaBoostingStrategy(treeStrategy, SquaredError)
      case _ =>
        throw new IllegalArgumentException(s"$algo is not supported by boosting.")
    }
  }
} 
Example 58
Source File: ScalaDefaultValuesInjector.scala    From scala-commons   with MIT License 5 votes vote down vote up
package com.avsystem.commons
package spring

import java.lang.reflect.{Constructor, Method, Modifier}

import org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder
import org.springframework.beans.factory.config.{BeanDefinition, BeanDefinitionHolder, ConfigurableListableBeanFactory}
import org.springframework.beans.factory.support.{BeanDefinitionRegistry, BeanDefinitionRegistryPostProcessor, ManagedList, ManagedMap, ManagedSet}
import org.springframework.core.ParameterNameDiscoverer

import scala.beans.BeanProperty
import scala.reflect.{ScalaLongSignature, ScalaSignature}

class ScalaDefaultValuesInjector extends BeanDefinitionRegistryPostProcessor {
  @BeanProperty var paramNameDiscoverer: ParameterNameDiscoverer =
    new ScalaParameterNameDiscoverer

  def classLoader: ClassLoader =
    Thread.currentThread.getContextClassLoader.opt getOrElse getClass.getClassLoader

  def loadClass(name: String): Class[_] = Class.forName(name, false, classLoader)

  def postProcessBeanDefinitionRegistry(registry: BeanDefinitionRegistry): Unit = {
    def traverse(value: Any): Unit = value match {
      case bd: BeanDefinition =>
        bd.getConstructorArgumentValues.getGenericArgumentValues.asScala.foreach(traverse)
        bd.getConstructorArgumentValues.getIndexedArgumentValues.values.asScala.foreach(traverse)
        bd.getPropertyValues.getPropertyValueList.asScala.foreach(pv => traverse(pv.getValue))
        injectDefaultValues(bd)
      case bdw: BeanDefinitionHolder =>
        traverse(bdw.getBeanDefinition)
      case vh: ValueHolder =>
        traverse(vh.getValue)
      case ml: ManagedList[_] =>
        ml.asScala.foreach(traverse)
      case ms: ManagedSet[_] =>
        ms.asScala.foreach(traverse)
      case mm: ManagedMap[_, _] =>
        mm.asScala.foreach {
          case (k, v) =>
            traverse(k)
            traverse(v)
        }
      case _ =>
    }

    registry.getBeanDefinitionNames
      .foreach(n => traverse(registry.getBeanDefinition(n)))
  }

  private def isScalaClass(cls: Class[_]): Boolean = cls.getEnclosingClass match {
    case null => cls.getAnnotation(classOf[ScalaSignature]) != null ||
      cls.getAnnotation(classOf[ScalaLongSignature]) != null
    case encls => isScalaClass(encls)
  }

  private def injectDefaultValues(bd: BeanDefinition): Unit =
    bd.getBeanClassName.opt.map(loadClass)
      .recoverToOpt[ClassNotFoundException].flatten.filter(isScalaClass)
      .foreach { clazz =>
        val usingConstructor = bd.getFactoryMethodName == null
        val factoryExecs =
          if (usingConstructor) clazz.getConstructors.toVector
          else clazz.getMethods.iterator.filter(_.getName == bd.getFactoryMethodName).toVector
        val factorySymbolName =
          if (usingConstructor) "$lessinit$greater" else bd.getFactoryMethodName

        if (factoryExecs.size == 1) {
          val constrVals = bd.getConstructorArgumentValues
          val factoryExec = factoryExecs.head
          val paramNames = factoryExec match {
            case c: Constructor[_] => paramNameDiscoverer.getParameterNames(c)
            case m: Method => paramNameDiscoverer.getParameterNames(m)
          }
          (0 until factoryExec.getParameterCount).foreach { i =>
            def defaultValueMethod = clazz.getMethod(s"$factorySymbolName$$default$$${i + 1}")
              .recoverToOpt[NoSuchMethodException].filter(m => Modifier.isStatic(m.getModifiers))
            def specifiedNamed = paramNames != null &&
              constrVals.getGenericArgumentValues.asScala.exists(_.getName == paramNames(i))
            def specifiedIndexed =
              constrVals.getIndexedArgumentValues.get(i) != null
            if (!specifiedNamed && !specifiedIndexed) {
              defaultValueMethod.foreach { dvm =>
                constrVals.addIndexedArgumentValue(i, dvm.invoke(null))
              }
            }
          }
        }
      }

  def postProcessBeanFactory(beanFactory: ConfigurableListableBeanFactory): Unit = ()
} 
Example 59
Source File: HoconBeanDefinitionReaderTest.scala    From scala-commons   with MIT License 5 votes vote down vote up
package com.avsystem.commons
package spring

import java.{util => ju}

import com.typesafe.config.{Config, ConfigFactory}
import org.scalatest.funsuite.AnyFunSuite
import org.springframework.beans.factory.support.DefaultListableBeanFactory
import org.springframework.context.support.GenericApplicationContext

import scala.beans.BeanProperty

class TestBean(val constrInt: Int = 1, val constrString: String = "constrDefault") {
  @BeanProperty var int: Int = _
  @BeanProperty var string: String = _
  @BeanProperty var strIntMap: ju.Map[String, Int] = _
  @BeanProperty var strList: ju.List[String] = _
  @BeanProperty var strSet: ju.Set[String] = _
  @BeanProperty var nestedBean: TestBean = _
  @BeanProperty var config: Config = _
}
object TestBean {
  def create(theInt: Int = -1, theString: String = "factoryDefault"): TestBean =
    new TestBean(theInt, theString)
}

class HoconBeanDefinitionReaderTest extends AnyFunSuite {
  def createContext(resource: String): GenericApplicationContext = {
    val pnd = new ScalaParameterNameDiscoverer

    val beanFactory = new DefaultListableBeanFactory
    beanFactory.setParameterNameDiscoverer(pnd)

    val ctx = new GenericApplicationContext(beanFactory)
    ctx.addBeanFactoryPostProcessor(new ScalaDefaultValuesInjector)

    val rdr = new HoconBeanDefinitionReader(ctx)
    rdr.loadBeanDefinitions(resource)
    ctx.refresh()

    ctx
  }

  test("hocon bean definition reader should work") {
    val ctx = createContext("testBean.conf")

    val testBean = ctx.getBean("testBean", classOf[TestBean])
    assert(42 == testBean.constrInt)
    assert("lolzsy" == testBean.constrString)
    assert(5 == testBean.int)
    assert("lol" == testBean.string)
    assert(Map("fuu" -> 42).asJava == testBean.strIntMap)
    assert(List("a", "b").asJava == testBean.strList)
    assert(Set("A", "B").asJava == testBean.strSet)
    assert(6 == testBean.nestedBean.int)
    assert(1 == testBean.nestedBean.constrInt)
    assert("wut" == testBean.nestedBean.constrString)
    assert(2 == testBean.nestedBean.nestedBean.constrInt)
    assert("yes" == testBean.nestedBean.nestedBean.constrString)
    assert(ConfigFactory.parseString("srsly = dafuq") == testBean.config)

    val testBeanDefInt = ctx.getBean("testBeanDefInt", classOf[TestBean])
    assert(testBeanDefInt.constrInt == 1)
    assert(testBeanDefInt.constrString == "constrNonDefault")

    val testBeanDefString = ctx.getBean("testBeanDefString", classOf[TestBean])
    assert(testBeanDefString.constrInt == 2)
    assert(testBeanDefString.constrString == "constrDefault")

    val testBeanDefAll = ctx.getBean("testBeanDefAll", classOf[TestBean])
    assert(testBeanDefAll.constrInt == 1)
    assert(testBeanDefAll.constrString == "constrDefault")

    val testBeanFMDefInt = ctx.getBean("testBeanFMDefInt", classOf[TestBean])
    assert(testBeanFMDefInt.constrInt == -1)
    assert(testBeanFMDefInt.constrString == "factoryNonDefault")

    val testBeanFMDefString = ctx.getBean("testBeanFMDefString", classOf[TestBean])
    assert(testBeanFMDefString.constrInt == -2)
    assert(testBeanFMDefString.constrString == "factoryDefault")

    val testBeanFMDefAll = ctx.getBean("testBeanFMDefAll", classOf[TestBean])
    assert(testBeanFMDefAll.constrInt == -1)
    assert(testBeanFMDefAll.constrString == "factoryDefault")
  }
} 
Example 60
Source File: AnySupportSpec.scala    From cloudstate   with Apache License 2.0 5 votes vote down vote up
package io.cloudstate.javasupport.impl

import com.example.shoppingcart.Shoppingcart
import com.google.protobuf.{ByteString, Empty}
import io.cloudstate.javasupport.Jsonable
import io.cloudstate.protocol.entity.UserFunctionError
import io.cloudstate.protocol.event_sourced.EventSourcedProto
import org.scalatest.{Matchers, OptionValues, WordSpec}

import scala.beans.BeanProperty

class AnySupportSpec extends WordSpec with Matchers with OptionValues {

  private val anySupport = new AnySupport(Array(Shoppingcart.getDescriptor, EventSourcedProto.javaDescriptor),
                                          getClass.getClassLoader,
                                          "com.example")
  private val addLineItem = Shoppingcart.AddLineItem
    .newBuilder()
    .setName("item")
    .setProductId("id")
    .setQuantity(10)
    .build()

  "Any support" should {

    "support se/deserializing java protobufs" in {
      val any = anySupport.encodeScala(addLineItem)
      any.typeUrl should ===("com.example/" + Shoppingcart.AddLineItem.getDescriptor.getFullName)
      anySupport.decode(any) should ===(addLineItem)
    }

    "support se/deserializing scala protobufs" in {
      val error = UserFunctionError("error")
      val any = anySupport.encodeScala(UserFunctionError("error"))
      any.typeUrl should ===("com.example/cloudstate.UserFunctionError")
      anySupport.decode(any) should ===(error)
    }

    "support resolving a service descriptor" in {
      val methods = anySupport.resolveServiceDescriptor(Shoppingcart.getDescriptor.findServiceByName("ShoppingCart"))
      methods should have size 3
      val method = methods("AddItem")

      // Input type
      method.inputType.typeUrl should ===("com.example/" + Shoppingcart.AddLineItem.getDescriptor.getFullName)
      method.inputType.typeClass should ===(classOf[Shoppingcart.AddLineItem])
      val iBytes = method.inputType.asInstanceOf[ResolvedType[Any]].toByteString(addLineItem)
      method.inputType.parseFrom(iBytes) should ===(addLineItem)

      // Output type - this also checks that when java_multiple_files is true, it works
      method.outputType.typeUrl should ===("com.example/" + Empty.getDescriptor.getFullName)
      method.outputType.typeClass should ===(classOf[Empty])
      val oBytes = method.outputType.asInstanceOf[ResolvedType[Any]].toByteString(Empty.getDefaultInstance)
      method.outputType.parseFrom(oBytes) should ===(Empty.getDefaultInstance)
    }

    def testPrimitive[T](name: String, value: T, defaultValue: T) = {
      val any = anySupport.encodeScala(value)
      any.typeUrl should ===(AnySupport.CloudStatePrimitive + name)
      anySupport.decode(any) should ===(value)

      val defaultAny = anySupport.encodeScala(defaultValue)
      defaultAny.typeUrl should ===(AnySupport.CloudStatePrimitive + name)
      defaultAny.value.size() shouldBe 0
      anySupport.decode(defaultAny) should ===(defaultValue)
    }

    "support se/deserializing strings" in testPrimitive("string", "foo", "")
    "support se/deserializing ints" in testPrimitive("int32", 10, 0)
    "support se/deserializing longs" in testPrimitive("int64", 10L, 0L)
    "support se/deserializing floats" in testPrimitive("float", 0.5f, 0f)
    "support se/deserializing doubles" in testPrimitive("double", 0.5d, 0d)
    "support se/deserializing bytes" in testPrimitive("bytes", ByteString.copyFromUtf8("foo"), ByteString.EMPTY)
    "support se/deserializing booleans" in testPrimitive("bool", true, false)

    "support se/deserializing json" in {
      val myJsonable = new MyJsonable
      myJsonable.field = "foo"
      val any = anySupport.encodeScala(myJsonable)
      any.typeUrl should ===(AnySupport.CloudStateJson + classOf[MyJsonable].getName)
      anySupport.decode(any).asInstanceOf[MyJsonable].field should ===("foo")
    }

  }

}

@Jsonable
class MyJsonable {
  @BeanProperty var field: String = _
} 
Example 61
Source File: Model.scala    From scaldy   with Apache License 2.0 5 votes vote down vote up
package sample.beanedposts

import java.time.LocalDateTime

import scala.beans.BeanProperty

// from http://stackoverflow.com/questions/23832136/scala-domain-object-modeling

sealed trait Id {
  @BeanProperty var strVal: String
}

case class UserId(strVal: String) extends Id

case class PostId(strVal: String) extends Id

trait Model {
  @BeanProperty var id: Id

  @BeanProperty var creationDate: java.time.LocalDateTime
}

case class User(
                 id: UserId,
                 creationDate: LocalDateTime,
                 name: String,
                 email: String
                 ) extends Model

trait Post extends Model {
  @BeanProperty var id: PostId

  @BeanProperty var user: User

  @BeanProperty var title: String

  @BeanProperty var body: String
}

trait Moderated {
  @BeanProperty var isApproved: Boolean
}

case class UnModeratedPost(
                            id: PostId,
                            creationDate: LocalDateTime,
                            user: User,
                            title: String,
                            body: String
                            ) extends Post

case class ModeratedPost(
                          id: PostId,
                          creationDate: LocalDateTime,
                          user: User,
                          title: String,
                          body: String,
                          isApproved: Boolean
                          ) extends Post with Moderated