Skip to content

Instantly share code, notes, and snippets.

@dcapwell
Last active January 1, 2016 20:49
Show Gist options
  • Save dcapwell/8199975 to your computer and use it in GitHub Desktop.
Save dcapwell/8199975 to your computer and use it in GitHub Desktop.
Dropwizard + Scala + JDBI + Validation
buildscript {
repositories {
mavenCentral()
}
}
task wrapper(type: Wrapper) {
gradleVersion = '1.9'
}
allprojects {
apply plugin: 'idea'
apply plugin: 'eclipse'
group = "com.example.myapp"
}
project(':myapp') {
apply plugin: 'scala'
apply plugin: 'application'
mainClassName = 'com.example.myapp.boot.Server'
repositories {
mavenCentral()
}
dependencies {
compile 'com.yammer.dropwizard:dropwizard-core:0.6.2'
compile 'com.yammer.dropwizard:dropwizard-jdbi:0.6.2'
compile 'com.yammer.dropwizard:dropwizard-migrations:0.6.2'
// scala support
compile 'org.scala-lang:scala-library:2.10.3'
compile 'com.massrelevance:dropwizard-scala_2.10:0.6.2-1'
compile 'com.gilt.jdbi-scala:jdbi-scala_2.10:0.2.0'
testCompile 'org.scalatest:scalatest_2.10:2.0'
testCompile 'org.testng:testng:6.8.7'
testCompile 'com.h2database:h2:1.3.174'
}
test {
useTestNG()
}
}
/**
* Removes boilerplate when working with Hibernate. History behind this:
*
* In scala, if you add an annotation to a var or case class param, it will put it on the field/getter/and setter.
*
* Many java annotations frameworks work with field OR getter/setter, but don't allow both
*
* Hibernate is an example of this. Some annotations are expected on fields but not methods, and vica-versa
*/
object ScalaHibernate {
type NotNull = javax.validation.constraints.NotNull @field
type NotEmpty = org.hibernate.validator.constraints.NotEmpty @field
type Min = javax.validation.constraints.Min @getter
type Max = javax.validation.constraints.Max @getter
type Size = javax.validation.constraints.Size @getter
}
object Utils {
private val objectMapperFactory = new ObjectMapperFactory
def readConfig[T](clazz: Class[T]): Try[T] = Try {
ConfigurationFactory.forClass(clazz, new Validator, objectMapperFactory).build()
}
def readConfig[T](file: File, clazz: Class[T]): Try[T] = Try {
ConfigurationFactory.forClass(clazz, new Validator, objectMapperFactory).build(file)
}
def readConfig[T](path: String, clazz: Class[T]): Try[T] = Try {
readConfig(new File(Thread.currentThread().getContextClassLoader.getResource(path).getPath), clazz).get
}
}
object LiquibaseUtils {
def create(config: DatabaseConfiguration): ManagedLiquibase = new ManagedLiquibase(config)
def migrate(config: DatabaseConfiguration) = Try {
val base = create(config)
try {
base.update("")
} finally {
base.stop()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment