Skip to content

Commit 805eecb

Browse files
committed
Update to latest Ratpack and Kotlin
1 parent 972510a commit 805eecb

File tree

9 files changed

+255
-240
lines changed

9 files changed

+255
-240
lines changed

annotations/ratpack/ratpack/handling/annotations.xml

Lines changed: 157 additions & 163 deletions
Large diffs are not rendered by default.

build.gradle

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,50 @@ buildscript {
33
jcenter()
44
}
55
dependencies {
6-
classpath "io.ratpack:ratpack-gradle:0.9.7"
7-
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:0.8.11"
6+
classpath "io.ratpack:ratpack-gradle:0.9.13",
7+
"com.github.jengelman.gradle.plugins:shadow:1.2.1",
8+
"org.jetbrains.kotlin:kotlin-gradle-plugin:0.10.770"
89
}
910
}
1011

12+
if (!JavaVersion.current().java8Compatible) {
13+
throw new IllegalStateException("Must be built with Java 8 or higher")
14+
}
15+
1116
apply plugin: "io.ratpack.ratpack-java"
17+
apply plugin: "com.github.johnrengelman.shadow"
1218
apply plugin: "kotlin"
1319

1420
repositories {
15-
maven { url "http://oss.jfrog.org/artifactory/repo" }
21+
maven { url "http://clinker.netty.io/nexus/content/repositories/snapshots" }
1622
jcenter()
1723
}
1824

1925
dependencies {
20-
compile "org.jetbrains.kotlin:kotlin-stdlib:0.8.11"
26+
compile "org.jetbrains.kotlin:kotlin-stdlib:0.10.770"
2127
compile ratpack.dependency("guice")
22-
testCompile "junit:junit:4.11"
23-
springloaded "org.springframework:springloaded:1.2.0.RELEASE"
28+
testCompile "junit:junit:4.12"
2429
}
2530

2631
compileKotlin {
2732
kotlinOptions.annotations = file("annotations/ratpack")
2833
}
2934

30-
apply plugin: "idea"
35+
apply plugin: "idea"
36+
37+
idea {
38+
project {
39+
//use JDK 1.8 in idea
40+
jdkName "1.8"
41+
languageLevel "1.8"
42+
ipr {
43+
withXml { provider ->
44+
def node = provider.asNode()
45+
//configure git support for the project in idea
46+
node.component.find { it.'@name' == 'VcsDirectoryMappings' }?.mapping[0].'@vcs' = 'Git'
47+
}
48+
}
49+
}
50+
}
51+
52+
mainClassName = "ratpack.example.kotlin.KotlinPackage"

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.0-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.3-all.zip
Lines changed: 32 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,37 @@
11
package ratpack.example.kotlin
22

3-
import ratpack.launch.LaunchConfig
4-
import ratpack.handling.Handler
5-
import ratpack.guice.Guice.handler
6-
import ratpack.handling.ChainAction
7-
import ratpack.handling.Chain
8-
import ratpack.func.Action
9-
import ratpack.guice.BindingsSpec
3+
import ratpack.registry.Registry
4+
import ratpack.guice.Guice
105

116
class HandlerFactory : ratpack.launch.HandlerFactory {
12-
override fun create(launchConfig : LaunchConfig?) = handler(launchConfig!!, registerModules, routes)
13-
14-
/**
15-
* Registers all of the Guice modules that make up the application.
16-
*
17-
* This is only invoked once during application bootstrap. If you change the
18-
* module configuration of an application, you must restart it.
19-
*/
20-
private val registerModules = Action {(registry : BindingsSpec?) ->
21-
registry!!.add(MyModule())
22-
}
23-
24-
private val routes = Action {(chain : Chain?) ->
25-
Routes().execute(chain!!)
26-
}
27-
28-
private class Routes : ChainAction() {
29-
/**
30-
* Adds potential routes.
31-
*
32-
* After this method completes, a handler chain will be constructed from
33-
* the specified routes.
34-
*
35-
* This method will be called for every request. This makes it possible
36-
* to dynamically define the routes if necessary.
37-
*/
38-
override fun execute() {
39-
// Map to /foo
40-
handler("foo") { context -> context.render("from the foo handler") }
41-
42-
// Map to /bar
43-
handler("bar") { context -> context.render("from the bar handler") }
44-
45-
// Set up a nested routing block, which is delegated to `nestedHandler`
46-
prefix("nested") {(nested : Chain?) ->
47-
// Map to /nested/*/*
48-
nested!!.handler(":var1/:var2?") { context ->
49-
// The path tokens are the :var1 and :var2 path components above
50-
val pathTokens = context.getPathTokens()!!
51-
context.render("from the nested handler, var1: ${pathTokens["var1"]}, var2: ${pathTokens["var2"]}")
52-
}
53-
}
54-
55-
// Map to a dependency injected handler
56-
handler("injected", getRegistry()!![javaClass<MyHandler>()])
57-
58-
// Bind the /static app path to the src/ratpack/assets/images dir
59-
// Try /static/logo.png
60-
prefix("static") {(nested : Chain?) -> nested!!.assets("assets/images") }
61-
62-
// If nothing above matched, we'll get to here.
63-
handler { context -> context.render("root handler!") }
64-
}
65-
}
7+
override fun create(registry : Registry) = Guice.builder(registry)
8+
.bindings({ registry -> registry.add(MyModule()) })
9+
.build({ chain ->
10+
chain
11+
// Map to /foo
12+
.handler("foo") { context -> context.render("from the foo handler") }
13+
14+
// Map to /bar
15+
.handler("bar") { context -> context.render("from the bar handler") }
16+
17+
// Set up a nested routing block, which is delegated to `nestedHandler`
18+
.prefix("nested") { nested ->
19+
// Map to /nested/*/*
20+
nested.handler(":var1/:var2?") { context ->
21+
// The path tokens are the :var1 and :var2 path components above
22+
val pathTokens = context.getPathTokens()
23+
context.render("from the nested handler, var1: ${pathTokens["var1"]}, var2: ${pathTokens["var2"]}")
24+
}
25+
}
26+
27+
// Map to a dependency injected handler
28+
.handler("injected", chain.getRegistry()[javaClass<MyHandler>()])
29+
30+
// Bind the /static app path to the src/ratpack/assets/images dir
31+
// Try /static/logo.png
32+
.prefix("static") { nested -> nested.assets("assets/images") }
33+
34+
// If nothing above matched, we'll get to here.
35+
.handler { context -> context.render("root handler!") }
36+
})
6637
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package ratpack.example.kotlin
2+
3+
import ratpack.server.RatpackServer
4+
import ratpack.server.ServerConfig
5+
6+
fun main(args : Array<String>) {
7+
RatpackServer.of({ b ->
8+
b.serverConfig(ServerConfig.findBaseDirProps())
9+
b.handler({ registry ->
10+
HandlerFactory().create(registry)
11+
})
12+
}).start()
13+
}

src/main/kotlin/ratpack/example/kotlin/MyHandler.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ import javax.inject.Inject
1313
*/
1414
Singleton class MyHandler [Inject] (private val myService : MyService) : Handler {
1515
override fun handle(context : Context) {
16-
context.getResponse()!!.send("service value: ${myService.getValue()}")
16+
context.getResponse().send("service value: ${myService.getValue()}")
1717
}
1818
}

src/main/kotlin/ratpack/example/kotlin/MyModule.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class MyModule : AbstractModule(), HandlerDecoratingModule {
1717
* @see MyHandler
1818
*/
1919
protected override fun configure() {
20-
bind(javaClass<MyService>())!!.to(javaClass<MyServiceImpl>())
20+
bind(javaClass<MyService>()).to(javaClass<MyServiceImpl>())
2121
}
2222

2323
/**
@@ -30,4 +30,4 @@ class MyModule : AbstractModule(), HandlerDecoratingModule {
3030
override fun decorate(injector : Injector?, handler : Handler?) = chain(LoggingHandler(), handler)
3131
}
3232

33-
fun AbstractModule.chain(vararg handlers : Handler) = chain(handlers.toList())!!
33+
fun AbstractModule.chain(vararg handlers : Handler) = chain(handlers.toList())
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ratpack.example.kotlin
2+
3+
import ratpack.test.ServerBackedApplicationUnderTest
4+
import ratpack.server.RatpackServer
5+
import ratpack.server.internal.ServerCapturer
6+
import ratpack.registry.Registries
7+
import ratpack.registry.Registry
8+
import kotlin.reflect.KFunction1
9+
10+
class KotlinApplicationUnderTest(private val mainFun : KFunction1<Array<String>, Unit>) : ServerBackedApplicationUnderTest() {
11+
protected fun createOverrides(serverRegistry : Registry) : Registry = Registries.empty()
12+
13+
override fun createServer() : RatpackServer = ServerCapturer.capture(ServerCapturer.Overrides().port(0).development(true).registry { registry -> createOverrides(registry) }, {
14+
mainFun(array<String>())
15+
}).orElseThrow { -> IllegalStateException("${mainFun.javaClass}.main() did not start a Ratpack server") }
16+
}

src/test/kotlin/ratpack/example/kotlin/SiteTest.kt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ import java.io.InputStreamReader
55
import org.junit.runner.RunWith
66
import org.junit.runners.JUnit4
77
import org.junit.Test
8-
import ratpack.test.RatpackMainApplicationUnderTest
98
import kotlin.test.assertEquals
109
import kotlin.test.fail
1110

1211
[RunWith(javaClass<JUnit4>())]
1312
class SiteTest {
1413

15-
val aut = RatpackMainApplicationUnderTest()
14+
val aut = KotlinApplicationUnderTest(::main)
1615

1716
Test fun fooHandler() {
1817
assertEquals("from the foo handler", get("/foo"))
@@ -41,12 +40,12 @@ class SiteTest {
4140
}
4241

4342
private fun get(path : String) : String {
44-
val uri = aut.getAddress()!!.resolve(path)
43+
val uri = aut.getAddress().resolve(path)
4544
val stream = uri.toURL().openStream()
4645
try {
47-
val reader = InputStreamReader(stream!!)
46+
val reader = InputStreamReader(stream)
4847
try {
49-
return CharStreams.toString(reader)!!
48+
return CharStreams.toString(reader)
5049
} finally {
5150
reader.close()
5251
}

0 commit comments

Comments
 (0)