Tuesday, April 30, 2013

Method updated() is not setting hours, minutes and seconds in Groovy Date class

Recently, I wanted to update existing date in Groovy like:

> new Date().updated(year: 2013, month: Calendar.APRIL, date: 15, hours: 0, minutes: 0, seconds: 0)
Mon Apr 15 21:59:01 EEST 2013

It sets date, month and year perfectly, but completely ignores hours, minutes and seconds.
After checking Groovy sources I found that these are incorrect property names, and correct code is:

> new Date().updated(year: 2013, month: Calendar.APRIL, date: 15, hourOfDay: 0, minute: 0, second: 0)
Mon Apr 15 00:00:00 EEST 2013

First thing is, that it is not mentioned in GroovyDoc, but what adds confusion is that Intellij IDEA autocomplets variables in this way, because Date has deprecated setters with such property names.

Wednesday, April 24, 2013

ChannelResolutionException in Spring Integration with Groovy

Recently, after launching Spring Integration beans I got weird exception:
ChannelResolutionException: no output-channel or replyChannel header available
After some googling I found out that this error means that Spring Integration tries to configure reply channel, which in my case meant that I am replying into channel-adapter, which is not possible. So basically, problem was that my service method was defined as:
def myServiceMethod()

which Spring understands as it returns something, even if it does not. So this should be told explicitly, like:
void myServiceMethod()

and this is it.

Friday, April 19, 2013

Lightweight Groovy HTTP server for testing

Recently, I was looking for really lightweight HTTP server for integration and functional testing. Minimalistic web servers looks like popular topic now and there is a bunch of them available, like Ratpack, Graffiti or Spark. But none of them looked like what I needed: really small, expressive and convenient for tests. One, that was looking best is HttpServer from JDK, but it's horrible boilerplate code is ugly even by the Java standards, so finally I decided to write one myself.

And this is the result https://github.com/dmitart/lightweightest.

It is based on JDK HttpServer, just less verbose API and tests oriented, just check examples in the README. Enjoy. But don't put it in production.

Hosting Ivy or Maven repository on Github with Gradle

If you don't have access to any public repo, but want to host libraries - then Github can be a solution.
Unfortunately, it does not provide proper interfaces for the client applications, but this is still possible.

Lets say that local root folder of your git project is /test.
First, you need to create all necessary poms and jars. To do it, you have to add this to your build.gradle file:

uploadArchives {
  repositories {
    mavenDeployer {
      repository(url: "file:///test/repo")

URL property is for proper Maven repository - for now unfortunately, Github is not, so you will have to enter some local folder. After this is added, run Gradle task uploadArchives, then commit and push all output files with git.

Unfortunately, this is not all, as SHA signature is wrong, because your git URL is not same as local. So you will need to find out proper SHA, and until you want to calculate it yourself, one easy way to do it is find some talkative Maven client, for example Grape. Luckily it is part of normal Groovy installation.

Then, you need to add new repo into Grape configuration, open ${user.home}/.groovy/grapeConfig.xml file and add your new repo like

<ibiblio name="myrepo" root="https://raw.github.com/me/mymodule/master/repo" m2compatible="true"/>

And, to get dependencies, just run Grape in verbose mode, for example like:

grape -V install my.group mymodule 0.1

It will fail, but you will get one great error message like:

...-0.1.pom: invalid sha1: expected=7f1089041d63ce7eaa5d6a35ddda3aaa606042e3 computed=8a941ef40645d449b0740023624c2f8b67b84c59 (516ms)

8a941ef40645d449b0740023624c2f8b67b84c59 is what we were looking for.
Just find file whose signature was incorrect, for example for pom it can be mymodule.pom.sha1, update it with new signature and push into git.

That is all, you have new great Maven repository with root under https://raw.github.com/me/mymodule/master/repo.

Wednesday, April 17, 2013

jQuery / Javascript method runs several times after single call

Recently, I was developing web page and attached click handler to button, which was loading some content into div element. What was unexpected, after first click - it loaded everything just fine, but each next click was increasing amount of method calls.
As I found out, problem was that after successful method execution it also loaded default page template with every new fragment, including JavaScript page initialization scripts, which was adding new handlers to all old buttons, and so on, and so on.
It was easy to fix by creating new empty template specifically for HTML fragments.
I had this problem with Grails, but any template engine with too many conventions and defaults can be vulnerable.

Wednesday, April 10, 2013

Setting style by custom property dynamically in AngularJS

Angular JS lets to set and update style and class based on some custom property. Unfortunately, it does not support placeholders now, for example:

<!-- This works -->
<td ng-style="myStyle">

<!-- This does not -->
<td ng-style="myStyle{{test}}">

Fortunately, it works with controller's methods, so this still can be done like:

<td ng-style="getMyStyle(test)">

where getMyStyle is controller's method.

Tuesday, April 2, 2013

Run background java process from Jenkins

Jenkins is using mechanism to detect spawned processes and kill them after finishing job, so if you will just start such ad-hoc background daemon process, Jenkins will kill it with warning:

Process leaked file descriptors. See http://wiki.jenkins-ci.org/display/JENKINS/Spawning+processes+from+build for more information

Fortunately, there is easy hack to tell Jenkins ignore such process, just create fake BUILD_ID and start your application, for example, this can be entered into Execute shell build step:

BUILD_ID=dontKillMe nohup java -jar test.jar &

Warning is still there, but process should be fine and running. There is more about it here.