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" "http://google-web-toolkit.googlecode.com/svn/releases/2.0/distro-source/core/src/gwt-module.dtd">
<module>
    <inherits name="org.vaadin.openesignforms.ckeditor.widgetset.VaadinCKEditorWidgetset"/>
    <inherits name="org.vaadin.tokenfield.TokenfieldWidgetset" />
</module>

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

repositories {
    // ...
    mavenRepo "http://oss.sonatype.org/content/repositories/vaadin-releases"
    mavenRepo 'http://maven.vaadin.com/vaadin-addons'
    // ...
}

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(grails.util.Environment.current.name).parse(new File("${basedir}/grails-app/conf/VaadinConfig.groovy").text)
    ant.property(name: "widgetset", value: vaadinConfig.vaadin.widgetset)
    ant.property(name: "workers", value: "4")
    ant.property(name: "widgetset-path", value: "")
    ant.property(name: "client-side-destination", value: "web-app/VAADIN/widgetsets")
    ant.property(name: "generate.widgetset", value: "1")
}

target(compile_widgetset: "widgetset") {
    depends(classpath, compile, widgetset_init)
    ant.echo message: """Compiling ${ant.project.properties.'widgetset'} into ${ant.project.properties."client-side-destination"} directory..."""
    ant.java(classname: "com.google.gwt.dev.Compiler", 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: "${ant.project.properties.'workers'}")
        arg(value: "-war")
        arg(value: ant.project.properties.'client-side-destination')
        arg(value: ant.project.properties.'widgetset')
        jvmarg(value: "-Xss1024k")
        jvmarg(value: "-Djava.awt.headless=true")
    }
}

setDefaultTarget(compile_widgetset)

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");
    scss.compile()
    def outFile = new File("$root/styles.css")
    outFile.write(scss.toString())
}

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

...
<server>
  <id>myrepo</id>
  <username>username</username>
  <filePermissions>664</filePermissions>
  <directoryPermissions>775</directoryPermissions>
</server>
...

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 {
public:
        LuaFnPtr(SWIGLUA_REF fn) : fn(fn) {};
        void operator()() {
                swiglua_ref_get(&fn);
                lua_call(this->fn.L, 0, 0);
        };
private:
        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) ...