January 19, 2012
filed mid-morning by dirk husemann in: hacking
technorati tags:
QR code for this entry · average time to read 0:36 minutes

the other day i was coding a scala apply(array: Array[String}) method to instantiate an object from a CSV file. i ended up with something like this:

            apply(uuid                        = uuid,
                  source                      = source,
                  hostname                    = rewired(0),
                  type                        = rewired(0),
                  state                       = rewired(0),
                  category                    = rewired(0),
                  id                          = rewired(0),
                  lifecycle                   = rewired(0),
                  classification              = rewired(0),
                  ...
                  flag                        = rewired(0))

there were about a 100 parameters to use. i wasn’t really too excited about having to change the rewired(0) to use the proper index by hand. so, here’s what i did:

  • in emacs mark the region
  • then invoke shell-command-on-region and
  • use perl -pi -e 's{rewired\(0\)}{sprintf("rewired(%d)", $. - 1)}e;' as command to invoke

emacs will then show you the result of the command in a temporary buffer, you can either copy and paste from there, or just repeat the shell-command-on-region and prefix it with ctrl-u — emacs with then replace the region with the output of the command directly.

voila!

all content posted on these pages is an expression of my own mind. my employer is welcome to share these opinions but then again he might not want to.
July 22, 2011
filed in the early afternoon by dirk husemann in: hacking
technorati tags:
QR code for this entry · average time to read 0:51 minutes

it turns out that the sbt/web-plugin configuration i described in my last post is not quite cutting it. what we observed is that while changes to the contents of the webapp subtree were indeed effective immediately1 sbt commands like prepare-webapp and jetty-reload would sometimes work and sometimes just ignore us.

so, back to digging around in the web-plugin sources and this is what we are now using:

temporaryWarPath <<= (sourceDirectory in Runtime) / "webapp",
// watch temporaryWarPath / WEB-INF / classes
jettyScanDirs <<= (temporaryWarPath) { (target) => Seq(target / "WEB-INF" / "classes") },

this sbt configuration tells jetty to run out of the src/main/webapp directory — any changes you do in there will become effective immediately and prepare-webapp and jetty-reload are working as expected. the drawback is that your generated classes and lib files get copied to src/main/webapp/WEB-INF :-( i know, sucks, but still better than having to restart your webapp everytime you change a {html,css,js} file. if you are using git you might want to add

src/main/webapp/WEB-INF/lib/*
src/main/webapp/WEB-INF/classes/*

to your top-level .gitignore file.


  1. on browser reload, that is. 

all content posted on these pages is an expression of my own mind. my employer is welcome to share these opinions but then again he might not want to.
July 4, 2011
filed in the early afternoon by dirk husemann in: hacking
technorati tags:
QR code for this entry · average time to read 1:10 minutes

the solution described below turns out to have its flaws and is not really recommended — have a look at the recently posted update for a better solution!

simple build tool 0.7.5 comprised the jetty and webapp functionality — version 0.10.0 (sbt10) no longer does so, instead you have to pull in the sbt webplugin. one feature of sbt7.5′s webapp support that we used heavily was the ability to run jetty out of the source tree instead of the exploded temporary WAR file:

Another possibility is to directly run the web application out of the the source web application path:

 override def jettyWebappPath  = webappPath
 override def scanDirectories = mainCompilePath :: testCompilePath :: Nil

— sbt7.5, continuous redeployment

(we actually used to set scanDirectories to Nil)

achieving the same setup with sbt10 is not as simple or even obvious, the solution reported in the simple build tool newgroups does not really provide the same feature, as it still requires to have a ~ prepare-webapp running in sbt. here’s what seems to work:

// run jetty from source tree
jettyConfiguration <<= (sourceDirectory in Runtime, jettyConfiguration) map { 
    (sourceDir, jettyConf) => {
        val conf = jettyConf.asInstanceOf[DefaultJettyConfiguration] 
        new DefaultJettyConfiguration { 
            def classpath = conf.classpath
            def jettyClasspath = conf.jettyClasspath
            def war = sourceDir / "webapp"
            def contextPath = conf.contextPath
            def classpathName = conf.classpathName
            def parentLoader = conf.parentLoader
            def scanDirectories = conf.scanDirectories
            def scanInterval = conf.scanInterval
            def port = conf.port
            def log = conf.log
            def jettyEnv = conf.jettyEnv
            def webDefaultXml = conf.webDefaultXml
        }
    }
}

// set jetty scan dirs to empty list
jettyScanDirs := Nil

changes in src/main/webapp are immediately effective for the running jetty instance. voila!

all content posted on these pages is an expression of my own mind. my employer is welcome to share these opinions but then again he might not want to.
June 28, 2011
filed in the early morning by dirk husemann in: hacking
technorati tags:
QR code for this entry · average time to read 1:14 minutes

i’m an ardent emacs user. for our current project we chose the scala language as our programming language along with lift as the web application framework. initially we used maven but have switched over to the simple build tool — sbt in short — about half a year ago, as we found that to be faster than maven and also offering more features (such as test-only, test-quick, etc). to get scala support in emacs i’ve been using ensime which cooperated really well with sbt.

the sbt project recently made version 0.10.0 available — aka sbt10. switching from sbt7.5 to sbt10 is either very easy or a bit of a struggle. it’s easy if you are using a plain vanilla setup. it’s a struggle if you’ve created your own tasks — to paraphrase star trek: “it’s tasks, but not as we know it, jim”.

among the changes brought by sbt10 is that manged JARs are no longer copied into the lib_managed tree (instead the version in $HOME/.ivy2/cache is used, avoiding redundant copies) — that unfortunately confuses the heck out of ensime. florian hars, suffering from the same problem, wrote an sbt10 ensime plugin that fixes the issue for the time being (until ensime has proper sbt10 support).

to install the sbt ensime plugin, follow the instructions in its README, then in restart sbt and invoke the ensime task. after a compile of your project, it will generate a new .ensime project file (save an eventually existing old one if you care about it). then it’s just a restart of emacs and the invocation of ensime.

all content posted on these pages is an expression of my own mind. my employer is welcome to share these opinions but then again he might not want to.
April 13, 2011
filed terribly early in the morning by dirk husemann in: hacking
technorati tags:
QR code for this entry · average time to read 0:53 minutes

thunderbird is a terrific mail application (“mail user agent” is the posh name, i believe).

pretty much the only thing i don’t like about it though is its way of formatting the date and time and not providing a way of letting me configure the date format via preferences. it got bad enough that i recently spent a bit of time to figure out how get thunderbird to display date and time in yyyy-MM-dd HH:MM format — that is, 2011-04-13 20:34 instead of 04/13/2011 08:34pm or something similarly silly.

the solution was hiding in a forum post on the ubuntu forum: by switching the locale (well, at least parts of it) to en_DK.utf8 thunderbird would use the yyyy-MM-dd HH:MM format (aka ISO date–time format).

here’s how i did that on ubuntu:

     # first: create a diversion of the normal /usr/bin/thunderbird
     dpkg-divert --divert /usr/bin/thunderbird.ubuntu --rename --local --add /usr/bin/thunderbird 
     # next: create our locale setting wrapper
     cat <<HERE >/usr/bin/thunderbird
     #!/bin/bash
     export LC_TIME=en_DK.utf8
     export LC_PAPER=en_DK.utf8
     export LC_MEASUREMENT=en_DK.utf8
     exec /usr/bin/thunderbird.ubuntu "$@"
     HERE
     chmod a+x /usr/bin/thunderbird

that should do the trick.

all content posted on these pages is an expression of my own mind. my employer is welcome to share these opinions but then again he might not want to.
March 9, 2011
filed around lunchtime by dirk husemann in: hacking,linux
technorati tags:
QR code for this entry · average time to read 2:37 minutes

i’ve long been using an external 22″ monitor with my ubuntu linux powered X200 thinkpad. while ubuntu maverick (10.10) has some issues with attaching and detaching the second monitor and subsequent suspend–resume cycles (the second suspend after a detach would not resume, d’oh), ubuntu lucid (10.04.02) works just fine (as befitting a long-term-support release).

for quite a while i’ve had the old 20″ monitor which i had been using previously sitting on my desktop along with an USB DisplayLink adapter — the idea being to hook the old monitor up as well as a third monitor1. the displaylink driver provided by ubuntu lucid seemed to work properly — the monitor’s screen would turn green on connecting it and a framebuffer device (/dev/fb1) and so i tried various recipes floating around on ubuntu forums and elsewhere — all promising to achieve a grand unified desktop comprising all three monitors — and they either didn’t work or if they achieved the grand unified desktop it was unusable.

so, after another prolonged period during which the monitor and the displaylink adapter gathered even more dust, i tried a different approach: give up on the grand unified desktop goal and instead just try to make use of the monitor. the idea this time was to start up a VNC server, then use a VNC client to directly render the server into the framebuffer device (/dev/fb1) provided by linux’s displaylink driver. the vncserver bit is actually quite easy:

vncserver -name hidden -geometry 1600x1200 -depth 16 :42

which starts a VNC server for the :42 display.

the VNC client bit turned out to be a bit more difficult. ubuntu lucid does have directvnc client which is “a vnc client for the linux framebuffer device”2. that client does seem to require keyboard and mouse access and in some configurations did not work at all or locked up my keyboard (not the mouse, though, funnily enough) or crashed the running X session, so no points on that one. further research luckily turned up vnc2dl by none other than quentin stafford-fraser one of the original VNC developers (and also the inventor of the webcam it seems). vnc2dl seemed a bit more promising and — after slightly modifying3 dldevice.c — did do the job:

sudo vnc2dl :42

next up was fusing display :42 to my main display :0 on a keyboard and mouse level so that i could just move the mouse pointer over to the left and end up on display :42 — here x2x (in the equally named ubuntu package) came into play:

x2x -west -to :42 >/dev/null 2>&1 &

and, hey, presto!, both displays are linked mouse and keyboard wise.

only thing still bothering me was that cut and paste was not working. to fix that required adding

vncconfig -nowin &

to my .vnc/xstartup file.

to have firefox run on display :42 required creation of a new firefox profile — i added that to .vnc/xstartup as well.4


  1. yep, even more screen real estate; can’t have enough of that: the ur-IDE emacs in one screen, instant messaging app pidgin on the other (for communicating with the team), the third screen would be really useful to host a firefox window with the API docs and so forth. 

  2. see man directvnc for more information about that one. 

  3. vnc2dl in the version on quentin’s github is hard-wired to 1280×1040/24bpp which my old monitor doesn’t quite grok, changing the wiring to 1600×1200/16bpp made it more grokkable for my setup. 

  4. to get the new profile synchronized with the default profile i used firefox’s recently added Firefox Sync add-on. 

all content posted on these pages is an expression of my own mind. my employer is welcome to share these opinions but then again he might not want to.
March 3, 2011
filed mid-morning by dirk husemann in: hacking
technorati tags:
QR code for this entry · average time to read 1:00 minutes

i’m a big fan of IRC and we use it in our distributed team to stay in touch and also coordinate testing and upgrades. as i’m also a big fan of the ur-IDE emacs i’m using emacs’s rcirc mode for my IRC needs. typically i have a small buffer window at the bottom of my emacs frame dedicated to IRC. quite nice…

…until you start sbt or do some git work in egg as then those modes try to acquire some screen real-estate and without scruples either “recycle” my IRC buffer for their content :-( or split it into two :-(

this being emacs there had to be a way to get that IRC buffer window protected. and there is :-) digging into the emacs info docs and looking at the underlying emacs lisp files this is what i came up with:

 
 (defun rcirc-toggle-current-window-dedication ()
   (interactive)
   (let* ((window (selected-window))
          (dedicated (window-dedicated-p window)))
     (set-window-dedicated-p window (not dedicated))
     (setq window-size-fixed (not window-size-fixed))
     (message &quot;Window %sdedicated to %s&quot;
              (if dedicated &quot;no longer &quot; &quot;&quot;)
              (buffer-name))))

 (global-set-key (kbd &quot;&lt;Scroll_Lock&gt;&quot;) 'rcirc-toggle-current-window-dedication)
 

this will toggle the window-dedicated-p and window-size-fixed bits of the window in which you press the scroll-lock key — and, hey, presto! — no more pirating of the IRC buffer window.

all content posted on these pages is an expression of my own mind. my employer is welcome to share these opinions but then again he might not want to.
February 18, 2011
filed mid-afternoon by dirk husemann in: hacking
technorati tags:
QR code for this entry · average time to read 4:34 minutes

we have been using scala and the lift framework for our project for over a year now. as we are starting on a new subcomponent of it, i thought i’d take a crack at using lift’s new CSS selector transforms.

CSS transforms provide an alternative to the traditional lift templating mechanism:

    <lift:FooBar>
        <foobar:dosomething />
    <lift:FooBar>

which requires the following scala code:

class FooBar { 
    def render(in: NodeSeq): NodeSeq =
        bind("foobar", in,
             "dosomething" -> <span>dog</span>)
}

and would result in

    <span>dog</span>

with the new CSS transforms we get rid of the <lift:FooBar> tags and instead use plain HTML:

    <div class="lift:Foobar">
        <span id="animal">XXX</span>
    </div>

and then change the binding to:

class FooBar { 
    def render = "#animal *" "dog"
}

and end up, again, with

    <span id="animal">dog</span>

— just with much less code! and once you start looking into expanding lists, it really becomes obvious that CSS transforms are a huge step forward. add to that that you can now design your web apps using normal HTML design tools (which in my case is emacs, nxhtml mode, and moz-repl) and life has just become a lot easier and more productive…

…except for one thing :-) you see, we are using comets and lift’s rather cool partialUpdate(SetHtml("dog", Text(wuff! wuff!))) construct to dynamically update the “dog” span. for that we were — in the pre-CSS transform age — using calls to chooseTemplate to fetch XML snippets and re-render them. how to do this with CSS transforms?

let’s assume our HTML snippet is a bit more complex and includes an image:

    <div class="lift:Foobar">
        <div><span id="animal"><img src="XXX" /></span></div>
    </div>

the CSS selector transforms page lists the following construct:

"animal ^^" #> "ignore"

this will select the element with the id “animal”. so far, so good. what’s not so good is that the intuitively next idea

"animal ^^" #> "ignore" & "img [src]" #> "dog.png"

doesn’t work (CSS transforms are actually functions, so we can invoke them on NodeSeq in sbt’s console):

scala> val in = <div><span id="animal"><img src="XXX" /></span></div>
in: scala.xml.Elem = <div><span id="animal"><img src="XXX"></img></span></div>

scala> ("#animal ^^" #> "ignore")(in)
res0: scala.xml.NodeSeq = NodeSeq(<span id="animal"><img src="XXX"></img></span>)

scala> ("#animal ^^" #> "ignore" & "img [src]" #> "dog.png")(in) 
res1: scala.xml.NodeSeq = NodeSeq(<span id="animal"><img src="XXX"></img></span>)

what does work is the following code:

val template = ("#animal ^^" #> "ignore")(in)
("img [src]" #> "dog.png")(template)

if we try this in console we get:

scala> val template = ("#animal ^^" #> "ignore")(in)
template: scala.xml.NodeSeq = NodeSeq(<span id="animal"><img src="XXX"></img></span>)

scala> ("img [src]" #> "dog.png")(template)
res2: scala.xml.NodeSeq = NodeSeq(<span id="animal"><img src="dog.png"></img></span>)

which is what we expect. just a bit cumbersome to write and the contraction

("img [src]" #> "dog.png")(("#animal ^^" #> "ignore")(in))

while producing the same result, is a bit hard to understand.

ideally, we could write it as:

"#animal ^^" #> "ignore" ~> "img [src]" #> "dog.png"

as in: first select the template, then apply the following transform. well, we are in scala land, where (almost) everything is possible! so, why not create that operator? how hard can it be?

“not very” is the answer. first step is to figure out what it is that we want: CssBindFunc can be viewed as (NodeSeq) => NodeSeq functions. so, we really want to concatenate one (NodeSeq) => NodeSeq with another one. writing this as a function that translates into:

def andThenInto(first: (NodeSeq) => NodeSeq, second: (NodeSeq) => NodeSeq): (NodeSeq) => NodeSeq =
    (ns: NodeSeq) => second(first(ns))

throwing that into the sbt console yields:

scala> def andThenInto(first: (NodeSeq) => NodeSeq, second: (NodeSeq) => NodeSeq): (NodeSeq) => NodeSeq =
    (ns: NodeSeq) => second(first(ns))
andThenInto: (first: (scala.xml.NodeSeq) => scala.xml.NodeSeq,second: (scala.xml.NodeSeq) => scala.xml.NodeSeq)(scala.xml.NodeSeq) => scala.xml.NodeSeq

scala> andThenInto("#animal ^^" #> "ignore", "img [src]" #> "dog.png")
res4: (scala.xml.NodeSeq) => scala.xml.NodeSeq = <function1>

scala> res4(in)
res4(in)
res5: scala.xml.NodeSeq = NodeSeq(<span id="animal"><img src="dog.png"></img></span>)

scala> 

which is exactly what we are after — well, almost. andThenInto is a bit long, we wanted to use ~>. so, let’s try that:

def ~>(first: (NodeSeq) => NodeSeq, second: (NodeSeq) => NodeSeq): (NodeSeq) => NodeSeq =
    (ns: NodeSeq) => second(first(ns))
$tilde$greater: (first: (scala.xml.NodeSeq) => scala.xml.NodeSeq,second: (scala.xml.NodeSeq) => scala.xml.NodeSeq)(scala.xml.NodeSeq) => scala.xml.NodeSeq

doesn’t look too bad. let’s give it a spin:

~>("#animal ^^" #> "ignore", "img [src]" #> "dog.png")
res6: (scala.xml.NodeSeq) => scala.xml.NodeSeq = <function1>

scala> res6(in)
res6(in)
res7: scala.xml.NodeSeq = NodeSeq(<span id="animal"><img src="dog.png"></img></span>)

looking good. next, let’s pimp CssBindFunc with that:

implicit def pimpCssBindFuncWithAndThenInto(first: (NodeSeq) => NodeSeq) = new { 
    def ~>(second: (NodeSeq) => NodeSeq): (NodeSeq) => NodeSeq =
        (ns: NodeSeq) => second(first(ns))
}

and give it a go:

scala> "#animal ^^" #> "ignore" ~> "img [src]" #> "dog.png"
<console>:14: error: type mismatch;
 found   : java.lang.String("img [src]")
 required: (scala.xml.NodeSeq) => scala.xml.NodeSeq
       "#animal ^^" #> "ignore" ~> "img [src]" #> "dog.png"

ooops. what happened? the compiler seems to be doing the grouping in a different way then we expected. “operator precedence” was jumping up and down in my mind in the back row desparate to get called on: looking up that topic in the “scala bible” turned up the explanation: the ~ character is in the highest precedence category together with #. to avoid having to use parenthesis we changed our “andThenInto” operator to &~>:

implicit def pimpCssBindFuncWithAndThenInto(first: (NodeSeq) => NodeSeq) = new { 
    def &~>(second: (NodeSeq) => NodeSeq): (NodeSeq) => NodeSeq =
        (ns: NodeSeq) => second(first(ns))
}

which then yields the desired result:

"#animal ^^" #> "ignore" &~> "img [src]" #> "dog.png"
res10: (scala.xml.NodeSeq) => scala.xml.NodeSeq = <function1>

scala> res10(in)
res10(in)
res11: scala.xml.NodeSeq = NodeSeq(<span id="animal"><img src="dog.png"></img></span>)

scala> 

:-)

all content posted on these pages is an expression of my own mind. my employer is welcome to share these opinions but then again he might not want to.
January 15, 2010
filed mid-afternoon by dirk husemann in: hacking
technorati tags:
QR code for this entry · average time to read 0:57 minutes

one thing i quite like about scala is that ability to run it in “interpreted” mode via the scala command. that allows me — similiar to python or ipython — to experiment and quickly try things out or even test my classes and objects.

what was a bit of a pain was getting the proper classpath constructed so that not only target/classes was picked up but also all the required dependencies. poking a bit around in maven’s plugin documentation i came across the dependency plugin and in particular the dependency:build-classpath mojo, armed with that information i came up with the following rather useful shell script:

#!/bin/bash
top=$(pwd)
cwd=$(pwd)
while  [ "$top" != "/" -a ! -e "$top/pom.xml" ] ; do
    cd ..
    top=$(pwd)
done
cd $cwd

if [ -e "$top/pom.xml" ] ; then
    cd $top

    echo "generating scala classpath based on maven pom.xml"
    mvn dependency:build-classpath -Dmdep.outputFile=.classpath-scala

    echo "starting scala"
    scala -cp target/classes:$(cat .classpath-scala)

else

    echo "cannot find top level pom.xml! must have taken a wrong turn somewhere. sorry."
    exit 1

fi

save under a convenient name, stir, and enjoy: call this script instead of calling scala directly.

all content posted on these pages is an expression of my own mind. my employer is welcome to share these opinions but then again he might not want to.
January 13, 2010
filed mid-afternoon by dirk husemann in: hacking
technorati tags:
QR code for this entry · average time to read 0:32 minutes

note to self: maven differentiates between installing and deploying a package (which might have been built via a mvn package):

  • install — just installs the JAR file and the POM file, if the -DcreateChecksum=true flag is provided it will create the SHA1 for the JAR only
  • deploy — install JAR and POM and all necessary meta data along with SHA1s

to install a package to a shared repository a deploy is to be preferred over an install as with an install we’ll get missing checksum errors later in life.

so, this works like this (using the lift-ldap package as an example):

$ mvn deploy:deploy-file -DgroupId=net.liftweb -DartifactId=lift-ldap -Dversion=1.0.0 -Dpackaging=jar -Dfile=target/lift-ldap-1.0.0.jar -DcreateChecksum=true  -Durl=scp://eyeteadee.zurich.ibm.com/var/www/eyeteadee.zurich.ibm.com/maven

all content posted on these pages is an expression of my own mind. my employer is welcome to share these opinions but then again he might not want to.
next page »