/ KOTLIN, POLYGLOT, WEBAPP

Polyglot everywhere - part 2

Last week, we set up a new project using the YAML flavor of Polyglot Maven. Now is time for some server-side code!

As a long time Vaadin advocate, let’s create a very simple Vaadin application. This will have the added advantage to let us hack something on the client-side as well for the last part of this series. As we are fully polyglot, we will avoid the old Java language and use something very cool instead. As I’ve have been to some conferences with its number 1 advocate, I settled on the Kotlin language.

Disclaimer: I’m far from a Kotlin user - and this is not about Kotlin anyway, so please pardon my mistakes in the following.

Java is still Maven’s first class citizen so the first step is to add some configuration for the Kotlin compiler to kick in. It’s quite easy, especially given the previous work on the polyglot POM:

build:
    sourceDirectory: ${project.basedir}/src/main/kotlin
    plugins:
        -   groupId: org.jetbrains.kotlin
            artifactId: kotlin-maven-plugin
            version: 0.11.91.1
            executions:
            -   id: compile
                phase: compile
                goals:
                    - compile
        # Other plugins go there

The next step is to configure the web application. It can be done either with the old XML web deployment descriptor or since Servlet 3.0 with annotations. Since XML is far from polyglot, let’s use the Kotlin way. Besides, Kotlin annotations are cooler than in Java:

WebServlet (
    name = "VaadinServlet",
    urlPatterns = array("/*"),
    initParams = array(
        WebInitParam(
            name = "UI",
            value = "ch.frankel.blog.polyglot.MainUi"
        )
    )
)

VaadinServletConfiguration(
    productionMode = false,
    ui = javaClass<MainUi>()
)

class KotlinServlet: VaadinServlet()

Regular Vaadin users know about the final step. A UI needs to be created. Again, this is quite straightforward:

class MainUi: UI() {
    override fun init(request: VaadinRequest) {
        val label = Label("Hello from Polyglot Everywhere")
        val layout = VerticalLayout(label)
        layout.setMargin(true)
        layout.setSpacing(true)
        setContent(layout)
    }
}

And with these steps, we’ve achieved a polyglot webapp!

The next article in this series will add a client-side component for Vaadin. Don’t miss it!

Nicolas Fränkel

Nicolas Fränkel

Developer Advocate with 15+ years experience consulting for many different customers, in a wide range of contexts (such as telecoms, banking, insurances, large retail and public sector). Usually working on Java/Java EE and Spring technologies, but with focused interests like Rich Internet Applications, Testing, CI/CD and DevOps. Also double as a trainer and triples as a book author.

Read More
Polyglot everywhere - part 2
Share this