Skip to content

Commit

Permalink
freemarker example with event bud
Browse files Browse the repository at this point in the history
  • Loading branch information
alextkachman committed Jul 20, 2012
1 parent 4a5e4fd commit 79fcc64
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 23 deletions.
11 changes: 11 additions & 0 deletions VertxTest.iml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,17 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/freemarker-2.3.19.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$MODULE_DIR$/lib/freemarker-2.3.19-sources.jar!/" />
</SOURCES>
</library>
</orderEntry>
</component>
</module>

2 changes: 1 addition & 1 deletion VertxTest.ipr
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@
<configuration default="false" name="FreeMarkerServer" type="JetRunConfigurationType" factoryName="Kotlin">
<option name="MAIN_CLASS_NAME" value="org.vertx.java.deploy.impl.cli.Starter" />
<option name="VM_PARAMETERS" value="" />
<option name="PROGRAM_PARAMETERS" value="run org.vertx.kotlin.examples.freemarker.FreeMarkerServer -instances 10 -cp $PROJECT_DIR$/out/production/VertxTest" />
<option name="PROGRAM_PARAMETERS" value="run org.vertx.kotlin.examples.freemarker.ApplicationVerticle -cp $PROJECT_DIR$/out/production/VertxTest:$PROJECT_DIR$/lib/freemarker-2.3.19.jar" />
<option name="WORKING_DIRECTORY" value="" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
Expand Down
16 changes: 16 additions & 0 deletions src/org/vertx/kotlin/core/EventBus.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.vertx.kotlin.core

import org.vertx.java.core.eventbus.EventBus
import org.vertx.java.core.eventbus.Message
import org.vertx.java.core.json.JsonObject
import org.vertx.java.core.Handler

public fun <T> EventBus.registerLocalHandler(where: String? = null, handler: Message<T>.()->Any?) : String
= (if(where != null)
registerLocalHandler(where, handler(handler))
else
registerLocalHandler(handler(handler))
)!!

public fun EventBus.post(where: String, msg: JsonObject, replyHandler: (Message<JsonObject?>)->Any?) : Unit
= this.send(where, msg, handler(replyHandler))
17 changes: 16 additions & 1 deletion src/org/vertx/kotlin/core/Verticle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.vertx.java.core.net.NetServer
import org.vertx.java.core.net.NetClient
import org.vertx.java.core.eventbus.EventBus
import org.vertx.java.core.logging.Logger
import org.vertx.java.core.json.JsonObject

public fun Verticle.createHttpServer(config: HttpServer.()->Unit) : HttpServer = getVertx().createHttpServer(config)

Expand All @@ -25,4 +26,18 @@ public val Verticle.eventBus: EventBus
public fun Verticle.runOnLoop(handler: ()->Any?) : Unit = getVertx().runOnLoop(handler);

public val Verticle.logger: Logger
get() = getContainer()!!.getLogger()!!
get() = getContainer()!!.getLogger()!!

public val Verticle.config: JsonObject
get() {
val config = getContainer()!!.getConfig()
return if(config == null) JsonObject() else config
}

public fun Verticle.deployVerticle(main: String, config: JsonObject = JsonObject(), instances: Int = 1, doneHandler: (()->Any?)? = null) {
getContainer()!!.deployVerticle(main, config, instances, if(doneHandler!=null) handler(doneHandler) else null)
}

public fun Verticle.deployVerticle(main: java.lang.Class<*>, config: JsonObject = JsonObject(), instances: Int = 1, doneHandler: (()->Any?)? = null) {
getContainer()!!.deployVerticle(main.getName(), config, instances, if(doneHandler!=null) handler(doneHandler) else null)
}
17 changes: 17 additions & 0 deletions src/org/vertx/kotlin/freemarker/FreemarkerConfiguration.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.vertx.kotlin.freemarker

import org.vertx.java.core.Vertx
Expand All @@ -16,6 +32,7 @@ import java.util.concurrent.locks.Condition

/*
We do below complex manipulations with casts to Lock due potential class loader issues
Note that it is very important to have freemarker in the system class path
*/
class FreemarkerConfiguration() : Configuration(), Shareable, Lock {
val lock = ReentrantLock()
Expand Down
60 changes: 50 additions & 10 deletions test/org/vertx/kotlin/examples/freemarker/FreeMarkerServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package org.vertx.kotlin.examples.freemarker

import org.vertx.java.core.streams.Pump
import org.vertx.java.deploy.Verticle
import org.vertx.kotlin.core.*
import freemarker.template.Configuration
import freemarker.cache.FileTemplateLoader
import java.io.File
Expand All @@ -30,20 +29,56 @@ import freemarker.template.DefaultObjectWrapper
import freemarker.template.TemplateModel
import org.vertx.java.core.json.JsonArray

import org.vertx.kotlin.core.*
import org.vertx.kotlin.freemarker.*
import org.vertx.java.core.eventbus.Message
import org.vertx.java.core.Handler

public class ApplicationVerticle() : Verticle() {
public override fun start() {
deployVerticle(
javaClass<FreeMarkerVerticle>(),
JsonObject().
putString("name","myFreemarker")!!.
putString("directoryForTemplateLoading","./test/org/vertx/kotlin/examples/freemarker")!!
)

deployVerticle(
javaClass<FreeMarkerServer>(),
JsonObject(),
10
)
}
}

public class FreeMarkerVerticle() : Verticle() {
public override fun start() {
val config = this.config

val freeMarkerConfig = Configuration()
freeMarkerConfig.setObjectWrapper(JsonWrapper())

val dir = config.getString("directoryForTemplateLoading")
if(dir != null) {
freeMarkerConfig.setDirectoryForTemplateLoading(File(dir))
}

val name = config.getString("name")

eventBus.registerLocalHandler<JsonObject>("freemarker.service.$name") {
val writer = StringWriter()
freeMarkerConfig.getTemplate(body!!.getString("template")!!)!!.process(body!!.getObject("model")!!, writer)
reply(JsonObject().putString("rendered", writer.toString()))
}
}
}

public class FreeMarkerServer() : Verticle() {
class object {
val BUFF_SIZE = 32*1024
}

public override fun start() {
val fmConfig = freemarkerConfig { first ->
if (first) {
setDirectoryForTemplateLoading(File("./test/org/vertx/kotlin/examples/freemarker"))
}
}

createHttpServer {
routeMatcher {
noMatch {
Expand All @@ -52,9 +87,14 @@ public class FreeMarkerServer() : Verticle() {
putString("message", "Hello, World!")!!.
putArray("list", JsonArray().addString("item 1")!!.addString("item 2")!!.addString("item 3"))

// we do it here to make reloading work
val template = fmConfig.getTemplate("index.ftl")!!
end(template, model)
eventBus.send("freemarker.service.myFreemarker",
JsonObject().
putString("template","index.ftl")!!.
putObject("model", model),
handler<Message<JsonObject?>>{ reply ->
end(reply.body!!.getString("rendered") as String)
}
)
}
}
}.listen(8080, "localhost")
Expand Down
11 changes: 0 additions & 11 deletions vertx/vertx.iml
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,6 @@
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="vertx" level="project" />
<orderEntry type="library" exported="" name="KotlinRuntime" level="project" />
<orderEntry type="module-library" exported="">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../lib/freemarker-2.3.19.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES>
<root url="jar://$MODULE_DIR$/../lib/freemarker-2.3.19-sources.jar!/" />
</SOURCES>
</library>
</orderEntry>
</component>
</module>

0 comments on commit 79fcc64

Please sign in to comment.