Open Source Consultant, Software Developer and System Administrator

If you are interrested in hiring a consultant for the effective use of open source software on an enterprise grade, take a look around in the About section to see, what I have to offer.

Blog and snippets

Various snippets or code parts I found useful, so I keep them here for reference.

Compile the Vaadin Widgetset with Grails

With "regular" Java, compiling of the WidgetSet is done using a Maven target. This is not possible with Grails directly. The Vaadin Wiki explains how it is done, but depends on setting the classpath proplerly.

So here is how I do it:

Set the name of your WidgetSet in grails-app/conf/VaadinConfig.groovy; there should already be a comment

vaadin {
  // ...
  widgetset = 'net.ofnir.project.widgetset'
  // ...

Create a Widgetset. E.g.: src/java/net/ofnir/project/widgetset.gwt.xml

<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit 2.0//EN" "">
    <inherits name="org.vaadin.openesignforms.ckeditor.widgetset.VaadinCKEditorWidgetset"/>
    <inherits name="org.vaadin.tokenfield.TokenfieldWidgetset" />

Be sure to add the repos needed for your modules and the vaadin-client-compiler. E.g. grails-app/conf/BuildConfig.groovy

repositories {
    // ...
    mavenRepo ""
    mavenRepo ''
    // ...

dependencies {
    // ...
    compile "com.vaadin:vaadin-client-compiler:${vaadinVersion}"
    compile 'org.vaadin.addons:ckeditor-wrapper-for-vaadin:7.8.3'
    compile 'org.vaadin.addons:tokenfield:7.0.1'
    // ...

Create scripts/VaadinCompile.groovy

includeTargets << grailsScript("_GrailsInit")
includeTargets << grailsScript("_GrailsClasspath")
includeTargets << grailsScript("_GrailsRun")

target(widgetset_init: "init") {
    vaadinConfig = new ConfigSlurper( File("${basedir}/grails-app/conf/VaadinConfig.groovy").text) "widgetset", value: vaadinConfig.vaadin.widgetset) "workers", value: "4") "widgetset-path", value: "") "client-side-destination", value: "web-app/VAADIN/widgetsets") "generate.widgetset", value: "1")

target(compile_widgetset: "widgetset") {
    depends(classpath, compile, widgetset_init)
    ant.echo message: """Compiling ${'widgetset'} into ${"client-side-destination"} directory...""" "", maxmemory: "512m", failonerror: true, fork: true, classpathref: "grails.compile.classpath") {
        ant.classpath {
            pathelement location: "${basedir}/src/java"
            pathelement location: "${basedir}/target/classes"
        arg(value: "-localWorkers")
        arg(value: "${'workers'}")
        arg(value: "-war")
        jvmarg(value: "-Xss1024k")
        jvmarg(value: "-Djava.awt.headless=true")


And finally you can build your Widgetset with

grails vaadin-compile

If you are rolling a WAR file and want to be sure, to have it as slim as possible, add some clean up code to scripts/_Events.groovy

eventCreateWarStart = { name, stagingDir ->
    new File("${stagingDir}/VAADIN/gwt-unitCache").deleteDir()
    ant.delete(dir:"${stagingDir}/WEB-INF/lib/", includes: "vaadin-client-compiler-*.*.*.jar", verbose: true)
    ant.delete(dir:"${stagingDir}/WEB-INF/lib/", includes: "vaadin-theme-compiler-*.*.*.jar", verbose: true)

Compile the scss for the WAR for Vaadin with grails

While running with run-app and in developer mode, Vaadin will compile the styles.scss on the fly for you. But once you want to roll your WAR file for deployment, you have to do this on your own (in plain Java, the Maven file from Vaadin will take care I guess)

Create or add scripts/_Events.groovy and change the theme var there

import com.vaadin.sass.internal.ScssStylesheet

eventCreateWarStart = { name, stagingDir ->
    def theme = "<themename>"
    def root = "${stagingDir}/VAADIN/themes/${theme}"
    ScssStylesheet scss = ScssStylesheet.get("${root}/styles.scss");
    def outFile = new File("$root/styles.css")

Bonus points: make the script find all themes; delete the *.scss files from the war.

Deploy a Grails plugin by scp to a private maven repo

Configure the repo in BuildConfig.groovy

grails.project.repos.default = "myrepo" // use the name from ~/.m2/settings.xml
grails.project.repos.myrepo.url = "scpexe://server/path/to/repo"

be sure to have the repo configured in ~/.m2/settings.xml


Now deploy with

grails maven-deploy

Pass a Lua function to a std::function with Swig

Let's assume for sake of simplicity, a std::function<void()> written in Lua has to be passed down from your script back into your C++ engine.

First be sure to place the lua_fnptr module of Swig in your .i file

%include "lua_fnptr.i"

Next provide a wrapper class

class LuaFnPtr {
        LuaFnPtr(SWIGLUA_REF fn) : fn(fn) {};
        void operator()() {
                lua_call(this->fn.L, 0, 0);
        SWIGLUA_REF fn;

Now you can wrap some callback function right away, e.g.:

void addListener(const char *event, SWIGLUA_REF callback)
        LuaFnPtr lua_fn_ptr(callback);
        self->addListener(event, lua_fn_ptr);

void addListener(const char *event, std::function<void()> f) ...