Tuesday, January 17, 2012

Use Spring 3 Expression Language (spEL) to get an Integer from an environment variable

Today, I played with Spring Batch. As you probably know, in a classic XML Spring config file, you can use ${my.property} to inject a system property in a bean attribute. That's quite simple.

But, in a JPA item reader (Spring Batch users should understand that), you cannot use this old syntax to inject values in your HQL query (using a parameterValues property). So, I decided to test the fabulously complicated spEL. A wonderful idea...

So, my problem is to get an Integer value from an argument passed to my JVM with something -Dmy.prop=value. Of course, if I don't have any JVM param, I want to have a default value.

Here is the solution :

#{(T(java.lang.Integer).parseInt(systemProperties['process.past.days']?:0))}

So, let's explain it :
  1. #{(systemProperties['process.past.days'])} is required to retrieve a system property called process.past.days
  2. #{(systemProperties['process.past.days']?:0)} is required if you want to set 0 as default value... but... it's horrible... system properties are Strings!!! So, let's parse it.
  3. #{(T(java.lang.Integer).parseInt(...))} is required to convert your String to an Integer
Waoooh! I did it!

My conclusion is that spEL is powerfull but too complicated. How could we maintain that in a few months or years?

Saturday, December 31, 2011

Colorify your Eclipse

Yesterday, while I was watching a video about the Play! framework, I realized that I was contemplating TextMate's colors. So, after some googlings, I discovered a "color plugin" which changes the text theme (it doesn't concern the overall look and feel but just the text apparence here). It is so nice so I put here the update-site url :

Wednesday, December 14, 2011

Spring saved my day on an UTF-8 encoding problem

I'm working on Jasig CAS (an SSO server) and I had an issue with special chars. For example, it was impossible to log in with a password like &é"'(-.
  • I checked my jsp file :it was encoded in UTF-8
  • I checked the encoding directive (<%@ page contentType="text/html; charset=UTF-8" %>) : OK
The idea was to force the request encoding with something like :

request.setCharacterEncoding("UTF-8");

But I was in Spring MVC and I didn't want an ugly hack. Fortunately, Spring saved my day with a builtin servlet filter to declare in the web.xml.

<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Friday, November 18, 2011

Ubuntu 11.10 with classic gnome shell (like gnome 2)

I discovered that there's a port of gnome indicator applet which allows to have Ubuntu 11.10 with a gnome 3 which looks like gnome 2. It could be nice if you don't like gnome-shell.

http://www.webupd8.org/2011/11/indicator-applet-ported-to-gnome-3-can.html

Wednesday, November 16, 2011

Common Linux shell commands

Terminal Cheat Sheet V5

Thursday, November 10, 2011

Kill Oracle session after a JVM was killed

For the moment, I'm playing with Spring Batch. In development, it appends that I need to kill my JVM. The problem is that C3P0 is not allowed to acquire another connection on Oracle on next restart. It's just because the previous one is still there. If you have an Oracle account with the required privileges, the you can see your old session in v$session and destroy it :

  • select * from v$session
  • alter system kill session 'sid,serial#'

Tuesday, November 01, 2011

Ubuntu 11.10 : how to disable the startup sound

The quickest way is to simply rename the login sound file from a terminal :

  • mv /usr/share/sounds/ubuntu/stereo/desktop-login.ogg /usr/share/sounds/ubuntu/stereo/desktop-login.ogg.disabled