Thursday, 27 October 2011

Understanding Git - Good references

Git is getting more and more popular these days. Most of the open source project moved to GIT and the ones that didn't yet are in the process of doing it.
Github itself is a great place to share your code and collaborate with other people!

Git is great but can be tricky at the beginning.

There are a few things you need to get your head around , there are new concepts and "jumping" to the command section without reading the introduction is not always a good idea, you'll have to go back to it.

The one thing that really helped me was this two videos here that I would strongly recommend you to watch.
I am the kind of guy that is suffering to sit at the chair watching a video for 2 hours but if you are struggling with GIT this is worth it.

Also another great resource is think like a git
Once you are more familiar with the concepts (you can just jump to the command section) other good references are

Tuesday, 11 October 2011

The power of three lines of scala

I was looking at this problem on project and I was amazed it only take 3 lines of scala code (with one being the number itself)!

val bigNum = "73167176531......"
def product(digits: String) = { (prod, c) => prod*c)}

That's it!
Ok let me try to explain what that code does..
First row is the string copied from the website, so good right
Second row is a definition of a function product that takes a String of digits and returns an Int.
The return type is inferred, the compiler will figure out it is an Int

Let's see what each method call does and where are those methods!
map is a function defined in StringOps and there is an implicit convertion between String and StringOps.
It takes every char in the String and applies the provided function. In our case the function is transforming the char to a number.

is just a short for
c => java.lang.String.valueOf(c).toInt

The result of this first function is a Seq of Int. Then applying the reduceLeft function we calculate the product of those digits.
reduceLeft will take two elements at a time and apply the provided binary function and then moves right and applies the same function again till the last element is reached.
our function is simply multiplying elements.

Cool so we know how to multiply elements.

Now what we need to do is go through the string taking a block of 5 digits at a time, multiply them and calculate the maximum.
That is exactly what the last line is doing. The sliding function in StringOps is taking a block of 5 elements (chars in our case) and returning an iterator over them.
The toSeq method creates a sequence out of the passed iterator.

At this stage we have something like Seq["73167","31671","16717"..]

All we need to do is map this elements to their product using our product function and then calculate the maximum so the last two functions couldn't be more expressive!

This is probably not the best way to solve this problem, if you want to post yours you're welcome!

Wednesday, 28 September 2011

Maven compiler level to java 6

This took me more than 3 mins googling so I thought I'd post it to save some time if I'll need it again.

Sunday, 18 September 2011

2011 Open Source Awards - Get ready to Vote

Get ready to vote your favorite open source project!!
Packt Publishing is organizing another contest to identify the excellence among Open Source projects, public votes for the finalists in each category were combined with ratings from a panel of Judges.
The Open Source Awards is a contest that aims to encourage, support, recognize and reward Open Source projects.
The contest has been running since 2006 and is regarded as one of the most established platforms for recognizing excellence amongst Open Source Software. Prize money given out since 2006 tops $100,000

Voting goes from the 19th of September until the 31 of October and by entering your opinion you have a chance to win a kindle!

Go on and check this page

Saturday, 20 August 2011

Liftweb setup in 10 minutes - IDE and project configuration

Scala is one of the coolest programming languages out there and Lift is a revolutionary web framework that really leverages many scala features.

But it could be daunting at the beginning, even just getting your workspace setup. In this article I will go through the basic setup of your IDE, it really takes a few minutes!

Even though I've always been an eclipse person I'm using IDEA IntelliJ with scala cause I think is way superior.
Ok let's begin. You will need IntelliJ 10.5.1, Maven or SBT.

Create the project with maven

First step is to create the project and for that we will use lift maven archetype. For the persistence layer we will use the default lift mapper. Run this command
mvn archetype:generate   -DarchetypeGroupId=net.liftweb   
  -DgroupId=com.playamatch  -DartifactId=web
You can also create a JPA based project running the same command with

You already have a full liftweb project! You can even run it with maven itself using the command

mvn jetty-run

Download and configure IntelliJ

Next open IntelliJ and install the scala plugin. You need to go to File -> Setting. This is where you can access all the configuration options. The equivalent of Window -> Preferences in eclipse.
Now go to the plugins, pick scala right click "Download and Install" and once done restart the IDE.
You should be able to see your code with syntax highlights and navigate through the classes already.

If you want you can also download the SBT plugin for IDEA. In this tutorial I will use the command line.

Another configuration change you might want to do if you are coming from eclipse if the shortcuts. IDEA comes with an eclipse profile that will allow you to keep using all the handy shortcuts you've learned so far.

Configure SBT

I would recommend using SBT for your projects with the scala compiler is very fast ( well as fast as it gets really ;) ) Here I am using SBT 0.10.1

You need to create a build.sbt in the project root folder. This is what mine looks like, it is pretty much the standard dependencies.

name := "liftplay"

scalaVersion := "2.8.1"

seq(webSettings: _*)

// If using JRebel uncomment next line
//jettyScanDirs := Nil

resolvers += " Maven2 Repository" at ""

libraryDependencies ++= {

  val liftVersion = "2.3" // Put the current/latest lift version here


    "net.liftweb" %% "lift-webkit" % liftVersion % "compile->default",

    "net.liftweb" %% "lift-mapper" % liftVersion % "compile->default",

    "net.liftweb" %% "lift-wizard" % liftVersion % "compile->default")


// Customize any further dependencies as desired

libraryDependencies ++= Seq(

  "org.eclipse.jetty" % "jetty-webapp" % "7.3.0.v20110203" % "jetty", // For Jetty 7

  //"org.mortbay.jetty" % "jetty" % "6.1.22" % "jetty,test", // For Jetty 6, add scope test to make jetty avl. for tests

  "org.scala-tools.testing" % "specs_2.9.0" % "1.6.8" % "test", // For tests

  "junit" % "junit" % "4.8" % "test->default", // For JUnit 4 testing

  "javax.servlet" % "servlet-api" % "2.5" % "provided->default",

  "com.h2database" % "h2" % "1.2.138", // In-process database, useful for development systems

  "ch.qos.logback" % "logback-classic" % "0.9.26" % "compile->default" // Logging


Then create another file under project/plugins (create these folders in your root project directory if you don't have them) called again build.sbt

resolvers ++= Seq(

  "Web plugin repo" at "",

  Resolver.url("Typesafe repository", new""))(Resolver.defaultIvyPatterns)


//Following means libraryDependencies += "com.github.siasia" %% "xsbt-web-plugin" % "0.1.0-""

libraryDependencies <+= sbtVersion(v => "com.github.siasia" %% "xsbt-web-plugin" % ("0.1.0-"+v))

last file is the under the project folder
That's it. All you need to do now is an update to download all the dependencies

Get dependencies and run application

sbt update

and then you can run your new application
sbt ~jetty-run

Enable Debug for your application

If you want to debug your application just add the following to your sbt script
Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005

and then create a remote application debug configuration from Run -> Edit configurations. First check your defaults are like this then click on the plus sign

and name your run configuration!
Happy coding!!!

Tuesday, 9 August 2011

Hibernate Gotchas!

I've been using Hibernate for some time now and when I don't work on a hibernate project for a while I find myself doing the same mistakes I did the previous times

So here is a sort of watch list for my self hopefully will be useful to someone else as well.

Implement hashCode and equals

You should always implement these methods in general but for your entities you should pay a bit more attention.The first thing you think when I say equals is probably to use the Id to distinguish between instance. Well, that's gonna cause you a lot of troubles.
You need to keep in mind that you are working with db entities and not normal POJOs.

When Hibernate is fetching objects is your using collections and hence equals and hashCode to know if an object you are looking for is in the session. For new objects id will be null or 0.
That means when trying to save two objects of the same class the second is going to overwrite the first one.
Also when hibernate saves a new instance it will set the id, thus making it a different object while it is exactly the same.
You need to use some business keys. Unique codes are great but if you can't think of anything just use a meaningful field and some timestamp (like creation date) to make it unique.

This is a good reference if you want to understand a bit further what's happening.

Careful with One-to-One and Many-to-One relations

This is something you really need to know.
When mapping a relation as One-to-One or Many-to-One on the "One" side of the relation you can't have lazy loading unless you specify the field as not nullable.

Why is that?
Essentially on the many side of the relation hibernate can use collection proxies and lazily load instances when required.
On the "One" side there is no collection interface but instead a reference to one of your model classes.
Hibernate can proxy that one as well but only if it is sure the reference will never be null!
So remember if you want to have lazy loading use the not null on the one side together with the lazy annotation (or xml equivalent).
If your relation can be null but you still really want to make it lazy then you have some options:
  • Create a value to represent that. For example if you have a relation like Person -&amp;amp;gt;Partner  just use a specific instance of Partner that means "no partner".
  • Use build time instrumentation. Check this
  • Fake the one side using a List and getting the field with get(0)

Read more on the hibernate documentation

Enable the statement logging

This is the only way to verify Hibernate is really doing what you expect him to do. Luckily enough there are different logging parameters that you can use to find out what is happening both at the HQL or if you want at the SQL level. You'll be surprised how many times hibernate is running queries and you did not except it. Try to this from the very beginning and help the team understand the importance of having the best and least possible queries or you'll surely have performance issue when running the application on some real data. To enable logging just set this property in the session configuration file
If you want to see it nicely formatted add

Watch what goes in the toString method.

This one is again related to what Hibernate fetches for you without you really being aware. Lots of times when you see queries but can't figure out why some lazy list is being loaded then check the toString method.
It might be the culprit!

What are your hibernate gotchas?

Tuesday, 17 May 2011

How to create a simple jQuery plugin - jQuery Iconify

I've been playing with jQuery in the last few days and I'm pretty impressed with the power of this library.
I still remember few years ago when you had to do everything on the server side just because it was easier!

Those days are gone.

Beacuse the best way to learn  is to get your hands dirty I'm gonna show you how to create a simple plugin that I called 'iconify'.

It is very simple and you can apply the same idea to create some custom plugins as well.
Essentially all the plugin does is takes an element on the page and wraps it into a div. Then put an icon next to it either left or right depending on some options.

Through options you can also bind some events to the icon (like click, or double click).

Let's see how is done.

Basic jQuery plugin

jQuery plugins have the following structure.
// Plugin definition goes here

Essentially you are creating a function running it and passing this function the jQuery object.

You need to use this definition to make sure the $ function is the jQuery one.

By passing jQuery to a function that defines the parameter as $, $ is guaranteed to reference jQuery within the body of the function.

Let user override defaults with $.extend

Now I'm going to use the $.extend() utility function to merge user options with default options.

Also we guard against an options object that’s null or undefined with ||{}, which supplies an empty object if options evaluates to false (null and undefined do).

var settings = $.extend({
   image: imagesPath + "/info_icon.png",
   activation: "hover"

Now work on the matched nodes

As we are building a jQuery plugin you will use the standard jQuery selectors to identify the nodes you want to work on. So the way we'll be calling this plugin is

This code will apply our icon to all the elements that have .myCssClass.
To do that in the plugin you will have to loop on this elements using the each function.

return this.each(function(){

// here goes the code that will be manipulation all matched nodes.


The complete plugin code will look like this

I've added a couple of utility functions in there to copy the style from the matching element to the wrapper div. The rest is simply adding the icon and binding the event.

 * Add an icon to an element on the page
 * @param hash options:
 *  image  : String  -> The path to the icon image to use. Either this or cssClass must be defined
 *  cssClass : String  -> The cssClass to use. It assumes the image used as background for the class
 *  iconCss  : Hash  -> Additional css styling for the icon
 *  position : String -> Where you want to see the icon. Options are right, left. Default right
 *  bind  : Hash  -> event: the event to bind to, callback: the callback function for the event
(function($) {
 $.fn.iconify= function(options) {
  var settings = $.extend({
   image    : "n/a",
   cssClass   : "n/a",
   iconCss    : {},
   copyStyle   : true,
   position   : "right", /*right, left*/
   bind    : {event:'click', callback: function() {}}
  var copyStyle = function (element, icon, wrapper){
   elementWidth = element.outerWidth(true);
   elementHeight = element.outerHeight(true);
   iconWidth = icon.outerWidth(true);
   iconHeight = icon.outerHeight(true);

   wrapper.attr('style', element.attr('style'));
   //should pick the max height
   wrapper.width(elementWidth + iconWidth);

   allClasses = getClassNames(element);
   $.each(allClasses, function(n, value){
  var getClassNames = function(element) {
   if (name = element.attr("class")) {
    return name.split(" ");
   }else {
    return [];

  return this.each(function(){
   var self = $(this); 
   var wrapper = self.wrap($('
')).parent(); var icon = null; // css class or image? if (settings.cssClass){ icon = $(" ").attr("class",settings.cssClass); } else if (settings.image){ icon = $("").attr("src",settings.image); } // position left or right? if (settings.position && settings.position.length > 0){ var strPosLength = settings.position.length; if (settings.position.substring(strPosLength - 5, strPosLength) == 'right'){ icon.insertAfter(this); }else { icon.insertBefore(this); } } //copies the element style to the wrapper copyStyle(self, icon, wrapper); // binds event to callback icon.bind(settings.bind.event, settings.bind.callback); icon.css(settings.iconCss); }); }; })(jQuery);

Thanks for reading!

Saturday, 2 April 2011

How the play framework test runner works

After working for a while with the play framework we had the need to write better integration tests.
By better I mean easier to maintain.
I like selenium but there re-usability and maintenance issues. Anyway without going any further into details I had to figure out how the @tests page works.

The @tests page for one of the sample applications

First thing let me assure you: there is no magic. Actually is quite easy to follow once you understand how the play works. The only complicated part is the selenium bit.

TestRunner is one of the default modules in the play framework together with secure, crud and some others. You can find it under PLAY_HOME/modules. You can also browse the code on github.

Adding the routes - plugin code

As far as I understand the module only contains a plugin a
Under testRunner there are four folders
  • src - the plugin source code
  • public - contains all the static html as in every play project
  • app - controllers and views
  • firephoque - just a library folder
Let's go into the details.
Under src there is a file called play.plugins that tells the framework the plugin implementation is play.modules.testrunner.TestRunnerPlugin.
This class extends PlayPlugin and has 3 methods onLoad(), onRoutesLoaded(), onApplicationReady().
This methods are invoked by the framework itself...guess when? :)
onLoad() simply adds the test folder to the classpath for the application and for all the modules.

onRoutesLoaded() adds the test routes to the application
 public void onRoutesLoaded() {
    Router.addRoute("GET", "/@tests", "TestRunner.index");
    Router.addRoute("GET", "/@tests.list", "TestRunner.list");
    Router.addRoute("GET", "/@tests/{<.*>test}", "");
    Router.addRoute("POST", "/@tests/{<.*>test}", "TestRunner.saveResult");
    Router.addRoute("GET", "/@tests/emails", "TestRunner.mockEmail");

and the last one, onApplicationReady() simply prints the test url to the console.

Note: This line is then used in auto-test mode to detect the application is ready. Look at lines 204-215

The other class in the same folder,called is used in auto-test mode to simulate the browser.

To summarise, so far we have the application started with some additional routes pointing at TestRunner methods.

Where is all happening - controllers and views

Let's have a look at the TestRunner controller.
You can see there all the methods added as routes in TestRunnerPlugin. Let's look at what some of methods do.

is using TestEngine to figure out which ones are unit test, functional tests and selenium tests. Then it just puts the three lists in the render scope and the view page index.html renders them nicely in what you see when you go to /@tests

This is a list of all the tests rendered as text. As far as i know is only used by auto-test

This is the method that actually runs the java tests and sends back the results.

This method saves a file in the test-results folder for every test that has been executed

    The java tests execution

    Unit and functional test are easy enough to understand. If you want to have more details, look at the run method in TestEngine

    The selenium tests execution

    Now here comes the tricky part

    Files ending in .test.html are selenium tests and they are passed to the TestRunner controller as .test.html.suite.

    The controller simply execute

    test = test.substring(0, test.length() - 6);
    render("TestRunner/selenium-suite.html", test);

    Now if you look at selenium-suite.html you'll be scratching your head because there is nothing else that a table with a row and a link to the test.

    To understand you need to look at index.html lines 362-380.
    This code is simply loading the selenium-core runner (pure DHTML) in an iframe, is making it visible and is calling the Testrunner controller to get the result every 2 seconds until it either returns an HTTP 500 (for a test failure) or an HTTP 200 (you guessed, test passed)

    a picture is worth a thousand words

    The frame in the mean time calls the controller (lines 82-100) loads the html test as any other application page, using the TemplateLoader.

    That is why you can use tags in your selenium tests! Isn't that great?

    Now the selenium-core runs the html test and calls the controller saveResult method (lines 120-131) that saves a file either .passed or .failed depending on the test outcome.
    That will allow the flow in index.html to move on to the next test.

    Quite good isn't it?

    Thanks for reading, please leave any feedback / corrections you feel.

    Tuesday, 15 March 2011

    Agile software development recommendations for users and new adopters

    Let me begin by saying that I am an agile believer and as a team player I enjoy this methodology much more than the traditional waterfall methods.

    Agile is like a fashion these days. Its one of the buzz words, it's "cool"...and I think it actually is.
    With popularity also comes rumors, myths and misunderstandings that often cause problems in agile projects and blames attributed to the process itself.

    There are many teams claiming to use agile without even knowing the manifesto.

    Yes, it is not be the answer to all software development problems, one size doesn't fit all, but before you adopt it in your project or blame it for not working please read this and if you have some feedback feel free to leave a comment!

    If you are considering agile take into account

    1. Project size.
      It's easy to apply agile in a new project, relatively small ( tens $K), with traditional client-server architecture and clearly defined users/customers but ..
      what about a re-write of an existing big project (hundreds or millions $) with distributed software and multiple stakeholders? Is hard to deal with that in agile methodologies...check this article for more info.
    2. Customer or product owner must be agile
      If all your customer really wants is a product with a fixed number of features in a fixed time frame, then you are probably going to fail. You are anyway even if you don't use agile. But I guess the point is agile is not going to help.
      In agile you have to continuously redefine the requirements, re-elaborate them, re-prioritize them. Your customer must know and accept that.
    3. Your organization must be agile
      No point going for an iteration based model if your company still requires all work to be signed-off in an upfront specification. To get the right value you need to be able to adapt ongoing work as it progresses.
    4. Your team might not be ready
      Yes, that's right the team might not be mature enough to be self-managing  and it is one of the points in the agile manifesto. You need to realize if you are going to start agile and lead accordingly. (Very good article here)
      You might have to hold on and train / lead your team ( and yourself ) for some more time.
      Agile approaches require a mentality shift. Simple things like saying something is "DONE" means is  production ready. It doesn't mean the code is done and now somebody else will have to make sure it works. Your team needs to be mature enough to own and  understand what they are committing to.
    5. Agile does NOT mean you are going to develop your software faster.
      That must be clear to all project managers.
      It increases business value and ROI because using agile the team focus on features that are prioritized by business value and incrementally deliver working software at regular interval.
      Less features => faster
      If you just consider the total time, agile is going to take longer due to continuous code refactor
    6. Don't do it just for the sake of it
      Embrace agile only if you believe in it. Not because people say it works.
      Or even worse just to tell your customers you are an 'agile' company.
      There are lots of successful company that never used it. 

    If you are already using agile then

    1. Make sure you have a clear and commonly accepted definition of DONE.
      The whole team needs to understand the process. Make sure they are aware, understand and accept that. Your sprint artifacts must be production ready.
      Also remember that if your sprint outcome is not actually being deployed to production (for whatever reason) you are bending contract. Just take it into account that "production" bugs might be lurking in your code.
    2. Watch the technical debt
      Very often the technical debt tends to build up over the sprints due to. You need to closely monitor it and allocated time during your sprints to skim it off.
    3. Don't let team members act as product owner
      The product owner is the most important player. Is the one telling you want to do but most of all what to do first. The right prioritization of user stories will determine the success or failure of the project and only the owner should do it.
      Sometimes if you have a large team, you might be tempted to have someone in your team (most likely the BA) wear the product owner's hat.
      That can lead to a lot of problems and misunderstandings. One of the key points of the agile manifesto is "Customer collaboration over contract negotiation". 
    4. Your team is likely to be diverseNot every team member can take every story. Someone says agile is not junior developer friendly. Well its a bit more than that. You might have different people specialized in different disciplines and that will create a lot of contention / dependencies that you need to deal with.
    5. Spend appropriate time on your automation and continuous integration
      Part of the agile process is to continuously build and integrate the software. Developers are meant to run tests all day long.
      If your build takes 30 mins, your application server needs another 5 and so on then your productivity is really going to suffer. Spend some time to break off the application in smaller more maintainable pieces.
      Don't drag along cause together with the productivity the team is going to run into the bad habit of not running or skipping the procedure.
    6. Go for the "simplest possible" but don't forget the design principles
      The idea here is to get feedback as soon as possible. So if your application is going to going to generate graphs, there is nothing wrong in having an excel based first version to be sure you understand what the customer wants.
      What you need to be careful is that software architecture and design principles are not left out!
    7. Don't forget non-functional requirements
      User stories only talk about required function. The critical “how well” attributes are totally absent. Your stakeholders will expect them anyhow and that is when you are going to get in trouble.
      All the things like performance, scalability are really making the difference these days.  Check why chrome is becoming more and more popular

    Thanks for reading this, your feedback is welcome!

    Tuesday, 8 March 2011

    Testing your Play! controllers

    Lets say you have a controller like this (from the Play! documentation)

    package controllers;
    import java.util.*;
    import play.*;
    import play.mvc.*;
    import models.*;
    public class Application extends Controller {
        public static void index() {
            Post frontPost = Post.find("order by postedAt desc").first();
            List olderPosts = Post.find(
                "order by postedAt desc"
            render(frontPost, olderPosts);

    And you want to write a test to check the render parameters are correct, how do you do that?

    Well here is a little example
    public class ApplicationTest extends FunctionalTest{
     public void before() {
     public void testHomePageFirstPost() throws MalformedURLException {
      // Call the home page
      Response response = GET("/");
      // Verify that Matches.matchCreate() passed correct params to render().
      Post frontPost = (Post) getRenderParameter("frontPost");
      assertEquals(${value_from_yml}, frontPost.postedAt);
      * Gets the parameter that was passed to render() method.
      * @param key
      * @return
     public static Object getRenderParameter(String key) {
      RenderArgs current = Scope.RenderArgs.current();

    You might want to put the getRenderParameter in a separate class to easily reuse it.

    If you want to test a controller method that take parameters is as easy.

    Let's assume you want to test a simple controller method in Users called getUserByUsername to make sure the correct user is added to render when the form is submitted

    public class Users extends Controller {
     * Gets the user given his username
        public static void getUserByUsername(String username) {
            User currentUser = User.find("byUsername", username).first();

    In this case the test would be

    public class UserLoginTest extends FunctionalTest{
     public void before() {
     public void testGetByUsername() {
      Map<String, String> params = new HashMap<String, String>();
      params.put("username", "user1");
      Response response = POST("/users/getbyusername", params, new HashMap<String, File>());
      User currentUser = (User) getRenderParameter(&quot;currentUser&quot;);

    Please feel free to comment!


    Saturday, 5 March 2011

    Run tasks in parallel and set your own time out

    Many times blocking method calls might cause problems in your applications.
    If the code is waiting for some resource availablity and you cannot specify a timeout option...well you're stuck!

    You might have seen this with javax.jms.Connection, you try to open a connection when the network is down and your application freezes. No exceptions or anything telling you what's wrong.

    Using the the java.util.concurrent api you can run the problematic code in a separate thread and wait for the completion or timeout of this execution.

    Here is some code calling a jms connection factory in a separate thread.

    private Connection getConnection() throws NamingException, JMSException {
     FutureTask<Connection> createConnectionTask = null;
     try {
      createConnectionTask = new FutureTask<Connection>(new Callable<Connection>() {
       public Connection call() throws Exception {
        logger.debug("Calling connection on the connection factory");
        Connection connection = (Connection) connectionFactory.createConnection();
        return connection;
      // start task in a new thread
      new Thread(createConnectionTask).start();
      logger.debug(String.format("Wait for the execution of createConnection to finish, timeout after %d ms", CONNECTION_TIME_OUT));
      connection = createConnectionTask.get(CONNECTION_TIME_OUT, TimeUnit.MILLISECONDS);
     } catch (TimeoutException e) {
      JMSException jmsException = new JMSException(e.getMessage());
      throw jmsException;
     return connection;

    The main thread will create a java.util.concurrent.FutureTask that returns the connection you need.
    It'll wait a maximum of CONNECTION_TIME_OUT milliseconds before throwing a TimeoutException. This way you can have complete control of your code execution!

    Friday, 4 March 2011

    Reuse your test code in maven projects!

    Here is a quick tip if you are using maven.
    If you create some test utility or simply want to reuse some test class in a different project you don't need to duplicate the code or put the code in the main section, just do this:

    In the project you have code to share add this section to you pom.xml

    and in the using project you want to use the test utilities add this


    Saturday, 26 February 2011

    Play! Framework deploy to

    I've been trying the Play framework in the last few weeks and I've to admit its a real pleasure to code in that environment.
    Now, I want to share my experience with deploying a Play! application to
    These guys offer the platform as a service, you can build, test and deploy Java web applications in the cloud. I thought it was brilliant.
    Register, for free (DEV tools seems to be free only till the end of Feb 2011) and you get a nice control panel from where you can create git/svn repositories, Jenkins (hudson) jobs, deploy you application create databases etc.

    Here are some of the steps you need in order to get your Play! application running there.

    I'm using play 1.1.1

    • Edit your play application.conf

      # The production deployment information

      What I didn't find on the Play! documentation was the first line to specify the JNDI entry
    • Create the database
      from the website you can easily create a database

    • Create the cloudbees resource file
      Create a folder war/WEB-INF in your Play! project ( if you don't have it ) and create a file called cloudbees-web.xml that looks like this
      <br /><?xml version="1.0"?><br /><br /><cloudbees-web-app xmlns=""><br /> <resource name="jdbc/${project_name}" auth="Container" type="javax.sql.DataSource"><br />  <param name="username" value="${db_username}" /><br />  <param name="password" value="${db_password}" /><br />  <param name="url" value="jdbc:cloudbees://${db_name}" /><br /> </resource><br /></cloudbees-web-app><br /><br />

    • Generate your database DDL
      Add the module db to your application

      and then run the command

      play db:export --%production

      You might get an javax.naming.NoInitialContextException, if you do comment out the line


      in the application.conf

      You might also get some sort of command not found error. Make sure you install db-1.1.1. I think if you don't specify the version it'll get version 1.0.1

    • Create your remote db
      This took me some time to figure out. I thought there was some sort of phpMyAdmin but instead you need to use your own MySql client. So on the cloudbees website if you look under
      RUN@Cloud > Databases >Manage >${your_db} in the Overview section you can find server name, port, schema and username.
      Open your client ( I've used MySql workbench ) and copy in there the sql generated at the previous step

    • Build your app
      play war ${path to your play project} -o ${war name}  --%production --zip

      This will generate the for file for production

    • Deploy your app
      I used the cloudbees sdk and works very well.
      Just download it and the first time you use it, it will ask you for some keys that you can find on cloudbees under Account > Security Keys. Put them in and you are ready to go.
      To deploy your application run the command

      bees app:deploy -a login/yourProject ${war file}

      The cool thing is that the bees deploy command will calculate the difference between the last war and the one you are uploading and will only upload that delta!

    Friday, 25 February 2011

    How to add information to a SOAP fault message with EJB 3 based web services

    Are you building a Java web service based on EJB3?
    Do you need to return a more significant message to your web service clients other that just the exception message or even worst the recurring javax.transaction.TransactionRolledbackException?
    Well if the answer is YES to the above questions then keep reading...

    The code in this article has been tested with JBoss 5.1.0 but it should (!?) work on other EJB containers as well
    1. Create a base application exception that will be extended by all the other exception, I will refer to it as MyApplicationBaseException .
      This exception contains a list of UserMessage, again a class I created with some messages and locale information
    2. You need to create a < SOAPMessageContext > implementation. Mine looks like this
      import java.util.Set;
      import javax.xml.bind.JAXBContext;
      import javax.xml.bind.JAXBException;
      import javax.xml.bind.Marshaller;
      import javax.xml.namespace.QName;
      import javax.xml.soap.SOAPException;
      import javax.xml.soap.SOAPFault;
      import javax.xml.soap.SOAPMessage;
      import org.apache.commons.lang.exception.ExceptionUtils;
      public class SoapExceptionHandler implements SOAPHandler<SOAPMessageContext> {
       private transient Logger logger = ServiceLogFactory.getLogger(SoapExceptionHandler.class);
       public void close(MessageContext context) { }
       public boolean handleFault(SOAPMessageContext context) {
        try {
         boolean outbound = (Boolean) context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
         if (outbound) {
"Processing " + context + " for exceptions");
          SOAPMessage msg = ((SOAPMessageContext) context).getMessage();
          SOAPFault fault = msg.getSOAPBody().getFault();
          // Retrives the exception from the context
          Exception ex = (Exception) context.get("exception");
          if (ex != null) {
           // Add a fault to the body if not there already
           if (fault == null) {
            fault = msg.getSOAPBody().addFault();
           // Get my exception
           int indexOfType = ExceptionUtils.indexOfType(ex, MyApplicationBaseException.class);
           if (indexOfType != -1) {
            ex = (MyApplicationBaseException)ExceptionUtils.getThrowableList(ex).get(indexOfType);
            MyApplicationBaseException myEx = (AmsException) ex;
            try {
             JAXBContext jaxContext = JAXBContext.newInstance(UserMessages.class);
             Marshaller marshaller = jaxContext.createMarshaller();
             //Add the UserMessage xml as a fault detail. Detail interface extends Node
             marshaller.marshal(amsEx.getUserMessages(), fault.addDetail());
            } catch (JAXBException e) {
             throw new RuntimeException("Can't marshall the user message ", e);
           }else {
  "This is not an AmsException");
          }else {
           logger.warn("No exception found in the webServiceContext");
        } catch (SOAPException e) {
         logger.warn("Error when trying to access the soap message", e);
        return true;
       public boolean handleMessage(SOAPMessageContext context) {
        return true;
       public Set<QName> getHeaders() {
        return null;

    3. Now that you have the exception handler you need to register this SoapHandler with the EJB. To do that you'll need to create an Xml file in your class path and add an annotation to the EJB implementation class.
      The xml file :
      <?xml version="1.0" encoding="UTF-8"?>
         <jws:handler-chains xmlns:jws="">

      and the EJB with annotation will be

      import javax.jws.HandlerChain;
        @HandlerChain(file = "soapHandler.xml")
        @Interceptors( { MyApplicationInterceptor.class })
        @SOAPBinding(style = SOAPBinding.Style.RPC)
        @WebService(endpointInterface = "", targetNamespace = "")
        public final class MyServiceImpl implements MyService {
        // service implementation

    4. To make sure all my exceptions have proper messages and that the exception is set in the SOAPMessageContext I use an Interceptor to wrap all the service methods and transform any exception to an instance of MyApplicationException
      The interceptor has a single method
        private Object setException(InvocationContext ic) throws Exception {
         Object toReturn = null;
         try {
          toReturn = ic.proceed();
         } catch (Exception e) {
          logger.error("Exception during the request processing.", e);
          //converts any exception to MyApplicationException
          e = MyApplicationExceptionHandler.getMyApplicationException(e);
          if (context != null && context.getMessageContext() != null) {
           context.getMessageContext().put("exception", e);
          throw e;
         return toReturn;
    5. That's it! You're done.

    Thursday, 24 February 2011

    My first blog

    I've been asked a million times the question "do you have a blog" and I've always thought, yeah I should get one and post some of the geeky stuff I go thru every day at work..but I never got around to actually do it! 
    Here I go my first blog...
    My non techy girl friend thought that dev in the blog name dev-in-progress stands for devil :)
    I thought it was a good name, is it not?