remote-work

How to setup a remote office

Introduction

I came up across a youtube video (see below) and decided to share my experience with setting up a remote office.

The experience comes from 9 years working in completely remote environments (two US companies one with a team in US and the other with a team shattered across Europe) and from starting my own company (SoftwareMill) 4 years ago and deciding not to have any office whatsoever.

I am not gonna write here about pros and cons of remote working, cause you might find hundreds of TEDs, youtubes, articles, blogs and what-not about that subject. I am gonna write about few practical things which I see are not obvious to people who have never worked remotely.

Before getting to those let me just write about two different types of remote offices

Dispersed Team

In this case all members of your team sit in different places. Most probably everyone is sitting at home. This is how our company is set up and this is, paradoxically, the easiest situation – there’s no fall back to traditional communication channels like “discussion having a mug of coffee in the social room”, so every one *has to* communicate in the remote way.

Remote Teams

In this case there will be few teams in different geographical locations that have to work together and are collocated in groups. In my (very humble) opinion the only way to make them work efficiently and with as little communication problems as possible, you must pretend those groups are not collocated. If you stick to that, then most probably you will succeed.

Communication

OK so the first thing I usually hear when someone tells me how he’s going to set up his remote office is that “he bought a very good conference call devices”. One like this:

Conference Call UFO

So what is the problem with it, you might ask? The problem is that it is a totally wrong idea that you can depend on a communication that

  • Requires everyone to drop whatever they’re doing and go to the conference room
  • And because of that it has to be arranged specifically. Every time. Imagine you need to ask you colleague about something he has put into the repo. Via Instant Messanger it will take forever. And you won’t wait for 3 hour to have that conference room free for you.
  • And lastly the idea that the corpo-like, lets-gather-present-how-bright-we-are-while-drinking-coffee meetings can actually help you work. Oh come on!

So the result is usually (remember I mentioned a youtube ?) like this. If you have ever took part in one of those call-in meetings, I highly doubt you have not seen any of these:

What to do, then?

Bear in mind that your duty is to give your workers communication channels, not the ability to communicate once in a while. Instead of giving minimal possibility, try to mimic real shoulder to shoulder working. But how, you ask?

What we did at SoftwareMill is we used teamspeak which originally was designed as gamer’s software for in-game communication. So how it works is that we have project rooms on the server in which people are sitting, who work together on same portions of software. It has a push-to-talk functionality, which means if I want to talk to other team members I just press some key combination and start talking. No ical invites, no calling in, just press and talk. And this makes a huge difference. The good thing about it is that you can always mute if you have something you really need to focus on. Win-win.

There are alternatives to teamspeak – Mumble for example, but we’ve discovered that sound quality of teamspeak and echo reduction are unbeatable. And teamspeak is really cheap – annual license for 32 slots (users) is just 30$.

And If you want oldschool meetings, which sometimes make sense, remember the ability to focus on them grows a lot if people are able to see each other. Google Hangouts might be a good idea, but for more then 10 cameras you might need something else – we use BigBlueButton to use cameras for everyone in the company (23 people at the moment) and it works well and is free.

Allow Process Change

The important thing about remote working is that it is getting a huge momentum and becomes more and more popular. And when there is a demand, ther is supply. Every couple days there’s a new Web 2.x application release with the sole or partial purpose to aim remote workers.

Very often those apps are crap but every now and then there’s something cool and really handful. Allow you workers to try those out. Something that worked just fine might sky rocket with new tools. And it brings this exciting change to your work environment, without which you loose people.

And, honestly, stop blocking all the facebooks, news sites etc. etc. If your workers want to slack, they will slack – having access to facebook has absolutely nothing to do with it. And if you have someone slacking, you just fire him. Easy as that.

If you do block, on the other hand, then there is 100% chance, if you’re hiring developers, that they will do absolutely everything possible to hack your firewalls, filters etc. And that is – instead of bringing you business value. And guess what? They will most probably hack your protections ;-)

What you will loose as well is the ability for them to find on some website, blocked by you, inspiration how to make his work better. Think twice if you want to do that, before blocking their internet.

People are Humans

One last thing. You cannot pretend that humans aren’t humans :) Your duty as an “remote office manager” is to allow your guys to meet from time to time in person. And it is not just meeting – they must have fun, so they can make up for all those things that would have happened in a physical office.

We do it once a month and every time it includes going out for beers or clubs. And from time to time we do it bigger (like get together with whole families for a weekend, or a weekend in Berlin).

If you are dispersed across continents – then it is gonna be more difficult. Probably because of the costs and the travel time, you will have to make it less often, but I think *at least* once a year is an absolute minimum. If you can’t afford it (but you should – you’re saving on the office space ;-) ) – talk to your guys. Maybe they would want to take part of the costs on themselves, just to be able to see the others.

Hope you found some of that blog useful!

Make summaries not promises

January is about to finish, most of you have probably already abandoned the promises you have made couple weeks ago. Well, at least some of you, the rest is still fooling themselves.

Yep now you should feel motivated to prove me wrong ;-)

But I have an idea that we should rather summarise what we have achieved in the passing year, instead of trying to foresee what are we gonna make or not the next one.

Here is my list, in my personal subjective order

1. Helena

Kid number 3, a girl. Expecting lots of joy this year around April when the baby is due. It might not be a very big deal that I made my wife pregnant, but it is something I am very excited about :-)

Happy sleepless times are coming…

2. SoftwareMill 2.0

I feel the father of the transformation as it was initially my idea.

We have made our company flat. And so far, after initial post-revolution mess, which I guess happens everywhere when huge change is involved, It works pretty damn well.

You can read more about it on this post by Paweł or watch this Confitura 2013 presentation (in polish) I gave, but basically our rules of flattening are:

  • Finances are fully open to everyone
  • Decisions are made on the lowest level possible (developer -> project -> group -> whole company)
  • No-one is being told what and how to do.

Pretty simple. Empowers everyone in the company who wants to be empowered. And from time to time when I hear from my colleagues that the company is “theirs” or that there is no reason for them to be looking on the market for something else, my founder’s heart is covered in a nice warm balm ;-)

3. AgileByExample 2013

Third edition of the conference. Bigger, better, hopefully more interesting.

This year except for “our” guys (Ola and Gośka) I have organised it with Piotr Burdyło from Touk and even if we had our ups and downs, I think the cooperation went well, and I am looking forward to have Piotr on the team in the next editions.

It’s sad, though, Ola who was our main, let’s call it “practical organiser” had to leave SoftwareMill. Nevertheless thank you Ola, and good luck with Estimote!

4. Became more fit

My selfish goal I made around June (you can check above youtube video that there was a good reason for it) to loose weight and get more fit. 14 kilos and many hours on the gym later I am quite happy with the result, both how ultimately handsome I am now (ha!ha!ha!) and how more fit I feel, not dying when I have to run for more then 10 meters.

Definitely was worth the fight with myself and all in all it was a very fruitful year.

Now let’s get back to work.

How to use vert.x to make your RaspberryPi talk to your browser?

Just yesterday I attended another edition of great workshop-conference Warsjawa. The tracks I went to were – vert.x + raspberry pi by Bartek Zdanowski and Effective Presentation by Małgorzata Majewska.

Enjoyed them both equally, but in this post I will focus on what I have learned in the first one and what have I done with it.

So few words about the two things from the subject of this post.

From the project’s webpage “Vert.x is a lightweight, high performance application platform for the JVM that’s designed for modern mobile, web, and enterprise applications.”. The philosophy is that with actor-like based model you ale able to write bits in the languages you want (given they work on JVM or… browser!).

RaspberryPi in case you have not heard yet is low-cost tiny computer with ARM processor comparable to Pentium II, graphic accelerator, HDMI output, ethernet port and, what is most interesting, a set of input-output pins which you can read and write to (we’ll get there). Those inputs and outputs are called GPIO (General Purpose Input/Output).

What are we going to do? TL;DR

So the idea is simple – I’d like to have raspberry pi with attached button to it. Now on my laptop I’d like to have a web serve that will server a page. On that page I would like to have a simple box that will tell me in realtime what is the state of the button (up/down). Simple.

And the code should be equally simple. The idea of this post is to show you that vert.x makes it really simple to achieve it.

How does the vert.x work?

What you do is you write Verticles or Workers (the difference is that Verticle will be always working on a specific thread, while Worker will be able to switch to finish it’s work as soon as CPU resources are free anywhere).

Each verticle is a piece of code, executed in completely separate ClassLoader. They all have two ways of communicating with each other – one is Shared Data, which allows you to access Maps or Sets to pass data over. Second way is Event Bus – a black box which you send messages to (identified by a string) and that you can listen on in other verticles (think JMS).

When you run your application you can instantiate many instances of the same verticle – so If you have a parallelizable work, you can have many workers doing it and have another verticle coordinating the work and aggregating the results.

Vert.x is designed to make it as easy as possible to write asynchronous, parallel applications and has a lot of goodies for that. Go and read the docs If you find the above interesting enough :-)

Setup RaspberryPi and connect the button

You will need a raspberry pi. Obviously.

Then you need to install some linux on the SD card. I’ve used newest Raspbian (which is RaspberryPi-friendly Debian) Wheezy.

On your raspbian we’re gonna need few things

  • Java JDK for ARM
  • vert.x – the easiest is to install via gvm. Just install gvm and then type gvm install vertx
  • optionaly webipoi to have nice web-based access to GPIO

Then you will either need to solder, or use a breadboard to attach your button. What you need to do is to get a button that short-circuits it’s legs when pressed and then attach one of the legs to one of the GPIO pins (I have used pin number 22 – you can see the full “map“) and the other to 3.3V (which is pin number 1).

Connect to ethernet, attach power and Voila!

raspberry

Setup your laptop/desktop

JDK you probably already have, IDE you have as well. What you might be missing is vertx. You install it the same way as on RaspberryPi.

Let’s write some code!

Ok the hardware is ready. Now let’s write some verticles.

Hardware Part – RaspberryPi button monitor

So the first verticle will be the one deployed on the raspberry pi. What it will do is one thing and one thing only. It will monitor the button for changes and send messages to event bus, when it happens.

For reading and writing to GPIO we are going to use Pi4J library.

To start off I have used a maven archetype which will create you a bootstrap build in maven with all needed dependencies. It also uses vertx maven plugin to create vertx modules, which are ziped packages with all dependencies that you can easily run.

Add the maven dependency to mentioned Pi4J to pom.xml

<dependency>
      <groupId>com.pi4j</groupId>
      <artifactId>pi4j-core</artifactId>
      <version>0.0.5</version>
</dependency>

Ok let’s get to the code

public class HardwareVerticle extends Verticle {

    public void start() {

        // create gpio controller
        final GpioController gpio = GpioFactory.getInstance();

        System.out.println("GPIO LOADED");

        final GpioPinDigitalInput myButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_06, PinPullResistance.PULL_DOWN);

        // create and register gpio pin listener
        myButton.addListener(new GpioPinListenerDigital() {
            @Override
            public void handleGpioPinDigitalStateChangeEvent(GpioPinDigitalStateChangeEvent event) {
                System.out.println(new Date() + "Button change");

                vertx.eventBus().publish("buttonbus", String.valueOf(event.getState().getValue()));
            }
        });
    }
}

On line 6 I am instantiating the Pi4J GpioController, and then using it to add a listener that will fire off every time state changes on GPIO06. Please note on line 10 that I am setting up PinPullResistance.PULL_DOWN – If you don’t use it, it’s not gonna work ;-)

The main thing happens on line 18 – we’re sending the state of the button to the event bus on the “buttonbus” address. publish method means that all receivers subscribed to that address will get exactly one copy of this message.

Last thing you need to do is to edit mod.json in src/main/resources and change the "main" attribute to point to this HardwareVerticle with full package. This will instruct vertx about the “main” verticle to start when you run the code.

So now the only thing you need to do is to build it with mvn clean install and copy target/your-package-name-version-mod.zip to the raspberry pi, and then run it there with

sudo ~/.gvm/vertx/current/bin/vertx runzip your-package-name-version-mod.zip -cluster

A little explanation

  • you need to run it as sudo to be able to access GPIO pins
  • runzip is a command that allows you to run those special packages I’ve mentioned before
  • -cluster will run vertx in a mode that can cluster with other vertxes with shared event bus
    • Now try pressing that button – you should see output on the sysout like this:
      guzikpress

      Server Part – HTTP server

      The other verticle will be our web server, that we will run on the laptop. Start again with generating a new vertx project from the maven archetype and create a verticle like this

      public class ServerVerticle extends Verticle {
      
          public void start() {
              HttpServer server = vertx.createHttpServer();
      
              server.requestHandler(new Handler<HttpServerRequest>() {
                  public void handle(HttpServerRequest req) {
                      String file = "";
                      if (req.path().equals("/")) {
                          file = "index.html";
                      } else if (!req.path().contains("..")) {
                          file = req.path();
                      }
                      req.response().sendFile("web/" + file);
                  }
              }).listen(9999, "localhost");;
      
              vertx.eventBus().registerHandler("buttonbus", new Handler<Message>() {
                  @Override
                  public void handle(Message message) {
                      System.out.println("Button state is: "+message.body());
                  }
              });
      
              System.out.println("Server started");
          }
      }
      

      There are two things worth mentioning.

      Between lines 4 and 16 we are starting a HTTP server. This is a full blown HTTP server, implemented in vert.x that you need only few lines to start. Basically in the handle we are serving all the requested files from the “web” directory in resources and if the file is not provided, we’re showing index.html. And that is it.

      Create a simple index.html

      <html>
      <head><title>Hey lol</title></head>
      <body>Raspberry LOL xD</body>
      </html>
      

      Start the module as described in the raspberry pi part and go to http://localhost:9999/. You should see the above page.

      Second thing starts on line 18 – we’re registering a handler for messages send to the buttonbus. Now If you have started your module with “-cluster” and the vertxs were able to see each-other on the network via multicast, you should already see output on the console on your laptop, when pressing the button on the rapsberry pi. Wasn’t that hard to make them talk to each other, was it?

      If that doesn’t work, then probably something somewhere is blocking the muticast, or your cluster on the laptop (or the pi) could’ve attached to the wrong interface (not the common one between the two).

      You can try two things

      • Specify another parameter on the commandline -cluster-host (but leave the -cluster as well!) and provide explicitly the IP you want to use on the machine (do it for both the laptop and the pi).
      • If that still does not work, edit on your laptop’s ~/.gvm/vertx/current/conf/cluster.xml, and under network/join disable multicast, enable tcp/ip and point to your rapsberry pi IP (see snippet below)
      <network>
              <port auto-increment="true">5701</port>
              <join>
                  <multicast enabled="false">
                      <multicast-group>224.2.2.3</multicast-group>
                      <multicast-port>54327</multicast-port>
                  </multicast>
                  <tcp-ip enabled="true">
                      <interface>YOUR RASPBERRY PI IP</interface>
                  </tcp-ip>
                  <aws enabled="false">
                      <access-key>my-access-key</access-key>
                      <secret-key>my-secret-key</secret-key>
                      <region>us-east-1</region>
                  </aws>
              </join>
      

      Browser Part – SockJS server and client

      Ok the last thing is to put some data to the browser. “LOL xD” is not enough for us.

      The absolute mind-blowing thing about vert.x is that you can actually listen and write to the eventbus from the browser using javascript. Which is awesome.

      There are two ways – either use websockets, which works well, but unfortunately only with the newest browsers. Or use SockJS that can transparently fall back to other ways of communication (polling in the worst scenario) if the websockets are not available.

      So we need to do two things – enable SockJS on our 9999 server, we have just created and then use javascript to read messages from the buttonbus.

      Let’s see how our server looks like now.

      public class ServerVerticle extends Verticle {
      
          public void start() {
              HttpServer server = vertx.createHttpServer();
      
              server.requestHandler(new Handler<HttpServerRequest>() {
                  public void handle(HttpServerRequest req) {
                      String file = "";
                      if (req.path().equals("/")) {
                          file = "index.html";
                      } else if (!req.path().contains("..")) {
                          file = req.path();
                      }
                      req.response().sendFile("web/" + file);
                  }
              });
      
              JsonObject config = new JsonObject().putString("prefix", "/comm");
      
              JsonArray permitted = new JsonArray();
              permitted.add(new JsonObject());
      
              //watch out, this means ALLOW-ALL messages via JS. not production friendly!
              vertx.createSockJSServer(server).bridge(config, permitted, permitted);
      
              server.listen(9999, "localhost");
      
              vertx.eventBus().registerHandler("buttonbus", new Handler<Message>() {
                  @Override
                  public void handle(Message message) {
                      System.out.println("Button state is: "+message.body());
                  }
              });
      
              System.out.println("Server started");
          }
      

      It isn’t changed very much – the only differences is that on line 24 we’re creating SockJSServer that will listen on http://localhost:9999/comm for our vert.x specific communication. Note how we’re providing two “permitted” objects there, which means basically that our browser can read and write from and into any bus. This is a potential security risk, so in the real world we would’ve secured it – you can read more about this in the vert.x manual.

      So now we can use the event bus from javascript – how to do it?

      <html>
      <head>
          <title>Vert.x + RapsberryPI + web</title>
      
          <script src="http://cdn.sockjs.org/sockjs-0.3.4.min.js"></script>
          <script src='vertxbus.min.js'></script>
          <script src='jquery-2.0.3.min.js'></script>
      </head>
      <body>
          This is raspberry on vertx lol xD.
      
          <form>
              <label for="box">The button state is: </label>
              <span id="box">UP</span>
          </form>
      
          <script>
      
              var eb = new vertx.EventBus('http://localhost:9999/comm');
      
              eb.onopen = function() {
                  eb.registerHandler('buttonbus', function(message) {
                      console.log("Message from the button: "+message);
                      $("#box").text(message == "1" ? "DOWN" : "UP");
                  });
              }
      
          </script>
      </body>
      </html>
      

      On line 21 you can see how we are registering a handler for “buttonbus” messages that will change the span “box” content to the appropriate message. Obviously you can user vertx.publish() to send messages to the bus, same way we do from Java.

      And that’s it. If all went well you should now have a setup where when pressing a button on your RaspberryPi, you get a message on your server with the state of the button and the same shows up on you web page.

      allofthem

      Conclusion

      I have tried to show you how easy it is to use vert.x as a communication platform between different nodes and how you can you make your rapsberry pi talk to the browser.

      Hope you enjoyed this lengthy post. If you have any comments, or you have found glitches in the text, please give me a shout.

      The full source code can be found on my github.

How you can use groovy to make automatic selenium page objects

In my previous post, I have described how I used groovy to create a simple DSL for defining form layouts.

A short reminder – at the bank I work, we have created a little framework to speed up creation of form-based applications. There is a backing bean, corresponding to the form page with a set of annotations defining how those fields should be displayed and a groovy definition of the form layout (sections, rows, tables etc.).

So that is all cool, but now we come to the subject of this post. User Interface tests. In the java world when you think about UI tests, then the answer is Selenium, so this is what we used.

Page Objects

We wanted to have those tests both readable and maintainable. So we decided to write Page Objects for all the pages we hacked, to at least test the success paths. You might be asking what is a page object? Very easy – it is just a java object that corresponds to the page you are testing, hiding all the ui-test-framework crap.

For example for the http://google.com the PageObject could look like this

public class GooglePage {
   Selenium selenium;

   public GooglePage open() throws Exception {
      selenium.openURL("http://google.com");
      selenium.waitForPageToLoad("30000");

      return this;
   }

   public void typeInSearch(String query) throws Exception {
      selenium.type("css=input[name=q]", query);
   }

   public Iterator<Result> search() throws Exception {
      selenium.click("css=input[name=btnK]");

      // parse the results and return the iterator
   }
}

That approach makes it then very easy and elegant to write tests. Cause you can just do:

public class TestGooglePage {
   @Test
   public void testSoftwareMillRules() {
      // given
      GooglePage googlePage = new GooglePage().open();

      // when
      googlePage.typeInSearch("best software house");
      Iterator<Result> results = googlePage.search();

      // then
      assertThat(results.next().getLink(), "http://softwaremill.com");
   }
}

And that’s it – you can immediately see what is going on, what are we trying to test. The testing code is not polluted with some selenium nonsense with selectors etc., and most importantly if for example search field name changes and we have to update it, we do it in one place not in 300.

Groovy wrapper

OK, enough with the introductions. Where is the Groovy I came for, you ask.

So I thought – we have a bean that backs our page. We have an objective representation of the form, when the layout is parsed etc. Why do we even have to make those PageObjects. We should be able to do some groovy magic.

So the approach I took is as follows. Groovy takes two things – the class of the backing bean, which gives us a nice way to access all the fields plus the parsed form which takes the form layout, reads all the annotations, and creates the representation with information like – is the field read-only, how is it rendered (date, text input, textarea, money etc.) and such.

And then the magic happens – groovy mocks an instance of the backing bean and under the hood adds the wrappings to Selenium.

So first let’s wrap our class

    public PageObjectWrapper(Selenium selenium, String url, FormConfig formConfig) {
        this.selenium = selenium
        this.url = url
        this.formConfig = formConfig
    }

    public <T> T wrap(Class<T> jaoClass) {
        this.jao = mock(jaoClass)

        jao.metaClass.getProperty = {
            String name -> return getValueOfField(name)
        }

        jao.metaClass.setProperty = {
            String name, Object object -> setValueOfField(name, object)
        }

        jao.metaClass.open = {
            selenium.open(url);
			selenium.waitForPageToLoad("30000");
            return jao;
        }

        jao.metaClass.submit = {
            selenium.click("xpath=(//input[@data-jastin='SUBMIT'])")
		    selenium.waitForPageToLoad("30000");
        }

        return jao
    }

What is going on here? I have passed a JAO class (JAO is the backing bean) which then gets mocked (so we get a proper instance of this object) and beautified. If you remember my previous post, the getProperty and setProperty will intercept all property access on the mocked instance and forward the call to methods getValueOfField and setValueOfField. We’ll get there.

Then I am adding some extra methods on the bean – open will open the actual url, submit will submit it (doh!).

You might be asking – so what is this @data-jastin attribute on the input? Well this is something we added in HTML which makes it easier to access the fields. Every time a field(input, output, whatever has a mapping on the backing bean) is generated it either has a constant data-jastin attribute (like here SUBMIT) or the field name from the backing bean.

Ok so let’s take a look at those methods now.

    private getValueOfField(String fieldId) {
        def field = formConfig.getFieldByName(fieldId)

        Object fieldValue;

        if (field.type == Type.DROPDOWN) {
            fieldValue = selenium.getValue("//input[@data-jastin='" + fieldId + "']");
        } else {
            fieldValue = selenium.getSelectedLabel("//select[@data-jastin='" + fieldId + "']");
        }

        fieldValue.metaClass.fieldName = {
            return fieldName
        }

        return fieldValue;
    }

    private setValueOfField(String fieldId, Object value) {
		def field = formConfig.getFieldByName(fieldId)
			
        if (field.type == Type.DROPDOWN) {
            selectInDropdown(fieldId, value)
        } else {
            type(value.toString(), jastinField(fieldId))
        }
    }

I have obviously not put all the field types we have, cause the listing would be much bigger, but it is just to give you an idea. FormConfig hold our objective representation of the form definition, from which we can get the field we want to set/get and read it’s type.

Once we have a type, we can call the appropriate selenium command and ta-dam! That’s it. So how will the usage look like? Let’s pretend we’re testing the form from the previous post.

class UserFormTest extends AbstractSeleniumTest {
    @Test
    def testSubmits() {
        // given
        def page = page()

        // when
        page.name = "Tomek"
        page.lastname = "Szymański"    
        page.shoeSize = "43"

        page.street = "Street"
        page.streetNo = "1"
        page.appartmentNo = "1"
        page.city = "Warszawa"
        page.country = "Poland"

        page.submit()

        // then
        assertThat(selenium.isTextPresent("User saved!")).isTrue();
    }

    def page() {
        // this gets the objective representation
        FormConfig form = GroovyParser.parse("/groovy/UserForm.groovy");
        // and this returns the page
        return new PageObjectWrapper(selenium, "http://localhost:8080/user", form).wrap(UserJAO)
    }
}

And that’s it. Perfectly readable, almost none selenium (just for asserts, and just because, we’re looking for a message on a field. If it was part of the framework, we could’ve wrapped it and add appropriate method on the jao).

Summary

I bet you’d like to see the whole thing in action. I wish I could just put it open-sourced somewhere, but… .

I wanted to give you an idea on what you can do with wrapping your super-stable staticly-typed Java code ( ;-) ) with a dynamic language like Groovy. You can very easily make your (and your co-coders) life much easier, especially when it comes to testing.

Hope you enjoyed. Stay tuned for more.

Writing a DSL with groovy

Se here is the thing. I fell in love with Groovy recently.

But back to the point. Or almost. A little disclaimer – this post is for a total groovy-newbies. If you speak groovy in your dreams, then this might sound infantile and too easy for you ;-). You have been warned.

At the place I work, for the client I work (a Big Bank in South Africa) we are developing a set of intranet applications to drive the credit business. As you might imagine this is a heavily form-oriented business. It hugely depends on gathering client’s data and then judging if the bank should be alarmed or not just yet.

At the same time we are nerds. So we decided to create a little framework that will serve those forms and allow us to focus on the data we are gathering and the business rules, rather then the look and feel, and AJAX, and REST calls, and and and. Really. I just cannot imagine writing dozens of forms, from scratch, in JSF or anything alike. Every frikken time. Not for me.

So we have created the framework. A little on how it works and we can get to the point. The framework is called Jastin. And the project has a codename Biber. Yep.

First of all there is a Model. Model is just hibernate entities. And the model points to a database we do not own. The database is designed to store the bank’s crucial information and we as a Java team are just the user of it.

So we read those entities from the database and we map them to things called JAOs (Jastin Access Objects). The JAO might be a single entity, but it is an object that represents the form. So often it has a lot of dependencies, it also has all the annotations to make the fields render the way they should render.

And at the end there is a yaml file to describe how to layout those fields. It can look like this:


foo.bar.UserJAO:
  personalDetails:
    - name lastname
    - shoeSize
  addressDetails:
    - street streetNo appartmentNo
    - city country

So you can see that the backing entity here will be a foo.bar.UserJAO, we will show two sections on the screen – Personal Details with name and lastname in the first row and the shoe size in the seconds.
Below that we will have Address Details with street, numbers, city and country laid out in two rows as well.

So what is wrong, you ask?

Well first of all, lack of IDE support – the class name will not refactor, If we change it. There is no intellisense for the field names. It sux basically.

What can we do? Well either we can write a IDE plugin that will understand It or… or what? A-ha we can use groovy to script out those layouts.

Now it would be super-cool If I could somehow use groovy, that will use the object of type foo.bar.UserJAO so that I could call those properties and ta-dam intellisense works, refactoring works. We are good to go.

Part 1 Property Access

Ok fine. So what do we want to do here? We would like to access a property like we access it normally, but in return we would like to get the name of the property, not it’s value.

Here comes metaClass for help. MetaClass in groovy is the sugar of dynamicity. You can either manipulate it on class-level (so that all instances of it will have it) or on the object level (so that only one object is adjusted). You can add methods to it and you can add some “special” ones, like methodMissing (called every time a non-existing method is called) or getProperty/setProperty.

So let’s take a look at this getProperty. And in this case we really do not want to mess up the class across the classpath, so we will adjust the object only.

def giveMeObjectThatReturnsProperties(Class aClass) {
  def object = mock(aClass)

  object.metaClass.getProperty = {
    String propertyName -> return propertyName
  }

  return object;
}

mock(aClass) is used from the great mocking library Mockito and I just use it to get the proper instance of an object, but given some required constructors (like the default one for example) we could’ve constructed here the actual object.

And then It is just a matter of the mentioned method called getProperty. This method accepts one string parameter (yep, It is the property name) and will intercept all property access. And then we just return the property name instead of it’s value. Dead easy.

You might be worried by the notation of the assignment to the getProperty. This is groovy. Let me explain.

The {} is a closure literal. Closure is just a piece of code you can pass and later execute. Some say we will have those in Java in 2128 with the release of Java 37. Bottom line is that this is just a method that I am passing that expects one parameter which is String name of the property. By using the arrow -> you first define the expected input parameters and then you write the body of the closure.

Hey but why, how, what? You may think. But I have an Integer property bazinga and It returns String now?! Yep. This is groovy baby. Dynamic. You can mess it up really bad, but in this case it is just pure awesomeness.

Do we need anything else? Practically we can create now a DSL for the form layout with it.

Part 2 Closure Me

Ok. So now let’s take a look at some less trivial yaml example we have.


foo.bar.UserJAO:
  addressDetails:
    field: addresses
    columns: [street, streetNo, appartmentNo, city, country]

This is how we represent a table that should be generated from a list of elements of type AddressJAO. This AddressJAO has all the 5 properties used as columns.

So we need a way to actually pass somehow a typed object and then similarly like before use properties, but not to get their values but their names. We know how to do that, but how to pass that object?

UserJAO u = giveMeObjectThatReturnsProperties(UserJAO)

// so here is the usage

table("addressDetails", u.addresses, columns({
  AddresJAO a -> [a.street, a.streetNo, a.appartmentNo, a.city, a.country]
}))

// and the groovy handling

    def table(String tableSectionName, Object field, List columns) {
        output << "  $tableSectionName:\n"
        output << "    field: $field\n"
        output << "    columns: [" << columns[0]

        for (def i = 1; i < columns.size(); i++) {
            output << ", " << columns[i]
        }

        output << "]\n"
    }

    def columns(columnClosure) {
        Class aClass = columnClosure.parameterTypes[0]

        return columnClosure(giveMeObjectThatReturnsProperties(aClass))
    }

What happened here?

I have just used few nice things about groovy.

So first of all we have passed a closure as a parameter to columns method. The closure accepts an AddressJAO instance.

Then what automatically gets returned (If not specified, groovy will return the result of last line in the method’s body) is the long list of fields that are properties on the AddressJAO instance wrapped with []. This is a List literal. So what happens is that the closure, called with the parameter AddressJAO, extended with our getProperty from the previous example, will return the list of the properties we just called. And they are intellisensed, refactorable and magic.

What else might you not understand? The << operator is the syntax-sugar for dealing with buffers. Then you might also notice you can refer a variable inside a string prefixing it with a $.

And here you go. DSL ready. Hope you've learned something new. Finally our form definition looks like this:

public class UserForm extends ExampleForm<UserJAO> {

    UserForm() {
        super(UserJAO)
    }

    @Override
    def formDefinition(UserJAO u) {

        section("personDetails")
        row(u.name, u.lastName)
        row(u.shoeSize)

        table("addressDetails", u.addresses, columns({
            AddressJAO a -> [a.street, a.streetNumber, a.appartmentNumber, a.city, a.country]
        }))
    }
}

Why is groovy so awesome for a java developer? Because it is a beautiful language, but the learning curve for a java dev is zero. Almost every java code will compile as-is by just changing the suffix to .groovy. And then step by step you can learn the nitty-gritties.

I have put the whole example on github. You can go and play with it yourself.

Confitura Next Generation

20120709-205228.jpg

This year I had a privilige to take part in another edition of the Confitura conference.

Since managing SoftwareMill takes more and more time and I have few new things going on (such as AgileByExample) I could take part in it only in 3 roles – as a listener, a partner giving out a playstation and a speaker. Sadly cannot name myself an organizer this year ;-)

But to the point. I truly believe this edition was a significant milestone – the level of prelections was significantly better then previous years, the venue was much more prestigious (not necceserily better though, but will write about it later) and sponsors had much more cool stuff to both show and give away.

Continue reading

Brewing My Brew ;-)

A while i got an interesting present from my fellows at Softwaremill – a brewkit, which is basically a set of tools and ingredients to brew your own beer at home.

The whole thing is not very complicated, but takes some waiting, which i guess is the worst part ;-)

The brewkit consists of two big fermentation buckets, aerometer (something you can measure % of sugar in liquid), kitchen thermometer, a device to put caps on bottles, caps, a big can with concentrated malt and hop and a packet of yeast.

Now the whole thing starts with cleaning and desinfecting fermentation buckets, mixing malt and hop with water (i added 7.5 liter of boiled and cooled water – next time I will buy some proper mineral water – hopefully it will make a difference on the beer taste ;-) ) and yeast. Then the whole thing goes into the bucket and stays for few days, until the sugar level stops decreasing (it started with 10*Balling, now after 5 days it dropped to 4*Balling). When it happened I put everything into the second fermentation bucket, leaving all the fluffy stuff in the first one (yeast mostly i guess. Some fermentation product let’s call it ;-) ). Now more waiting, but hopefully within few weeks i will be enjoying my first own bottle of lager ;-)

Lots of fun, will keep you posted ;-)

Spring 2012 presentation tourné part 1

I had a privilege to take part in the DevCrowd conference in Szczecin that was held on April 14, 2012.

The conference was great. It was free for the attendants (thanks to the sponsors, including SoftwareMill) aimed mostly at Java developers.

At the conference I gave talks – first one was about my side-project Asamal and how JEE6 can be easily used to build your own frameworks (you can find all the code and the slides on my github).

Second talk I gave with Adam Warski – Writing Highly Available Applications using Amazon Web Services (same that we gave on Confitura 2011 and few other places). Again – code and slides are available on github.

The conference was held on the Szczecin University campus – nice venue, modern etc. but I lacked a bit the time to mingle with people. The breaks were a bit short, and two of them I had to prepare as a presenter, so all in all I felt I could discuss with others a bit more (at least there was Jacek Laskowski to fill that a bit ;-) ).

I got chance to see three presentations – first one about marrying DLNA with Java – very interesting talk by Matt Rutkowski. Then I went to see Jacek Laskowski‘s take on Clojure. As usual – Jacek talks a lot so the presentations carry more entertainment then content, but I still enjoyed It and my view on Clojure has not changed. Luckily for Jacek Paweł Szulc could not make it for the conference, so the rest of Jacek’s Clojure presentation was held then ;-)

I did enjoy also Koziołek‘s take on testing frameworks, but unfortunately had to prepare a bit for mine, so I saw only half of it. One comment though – you have said that TestNG is much better then JUnit. I agree, but at the same time it allows you writing worse unit tests ;-) I am going to blog about that soon, we can start a discussion.

After all the presentations I have finalized with Nowaker the competition that was held by SoftwareMill – the task was to create a Roshambo (Rock-Paper-Scissors) player, and the competition actually used those implementation to run a little contest. The lucky winner got his PS Vita smiling and we were ready to go for an after party.

Było grubo.

TDD – Test Driven D???

Few weeks after the great AgileByExample conference, I had the privilege to be an organizer of, I have few thoughts about TDD I would like to share.

If you are an experienced TDD hacker, then probably this would be no value for you, but If you are a beginner then you might enjoy it ;-)

The conference was really an eye opener for me. Basically I had an idea what TDD could be. I went to many presentations in the past that were trying to convince the audience, how great and awesome TDD is, and how it can cure cancer, but I think the presenters were very often missing the point there.

The point that I think I feel much better now, after taking part in a great Coding Dojo, that was lead by two great agile masters – Alexandru Bolboacă and Thomas Sundberg.

So the thing is – I always thought about TDD to be the Test Driven Development. But that is not all. That always implied for me, that I need to design first, and then do the TDD. And that really did not make much sense, cause If I knew that I need to create class Dog class Cat class DogDecorator class ViewPetHolder etc. – why would I start with test that checks that method which returns “Dog” really returns “Dog” ;-)

But if you think Test Driven Design, which I think is more accurate, you will find out, that you shouldn’t be thinking about how to achieve the goal – the actual solution will emerge from the tests.

I have seen it just yesterday when I remote paired with Alex, to practice a bit and see with my own eyes, that remote pair programming is possible.

Two things came out of it – Alex showed me how refactoring, like changing the names of variables to something more descriptive naturally creates the classes for you.

And that remote pair programming is as possible as one-room-pair-programming. We had absolutely no problems in doing that – one thing you need to do is that everyone writes on his own laptop, while the other is looking at it (try TeamViewer for example). Once one is done with his stuff the other needs to get his changes, for which any version control would be sufficient. We were using public github account.

I might not be 100% converted, but for some problems I can see a good use of this approach.

Oh and the shared code knowledge, and double the brain you get for free ;-)

New Toy – MacBookAir

Since I am taking a new, rather managing position quite soon, my company got me a brand new MacBook Air.

I’ve been using it for few days now, and wanted to share what do I think.

To start with, the specs (this is the simplest version):

  • 11.6″ LED 1366×768 display
  • 1.6Ghz dual-core Intel Core i5
  • 2GB 1337 DDR3 RAM
  • Intel HD 3000 (shared 256MB)
  • 64GB SSD drive
  • 1.08kg

More specs can be found on apple.com.

The tests

Speed

What I wanted to test is the compile time using maven of a big big project I am currently working on plus jboss (6) startup time.

I compared with my MacBook Pro I use everyday. It’s specs are:

  • 2.4Ghz C2D
  • 250GB 7200rpm HDD
  • 8GB 1067 DDR3 RAM

Results:

Maven

Repositories downloaded prior, second mvn clean install with all tests

MacBookPro : Total time: 3:59.889s

MacBookAir: Total time: 2:29.097s

JBoss Startup

note: on MBA had to decrease Xmx and Xms from 1.5GB to 1GB and permgen from 768MB to 512MB

MacBookPro: Started in 2m:843ms

MacBookAir: Started in 1m:27s:894ms

Huh ?

Yep, MBA is almost 50% faster. Why ? I think it’s the SSD (plus maybe new generation of the CPU) – the tests heavily use the filesystem for the DB tests.

Battery

I thought – I want to simulate the full load, so what I did is that I played a full HD (1080p) MKV movie, streamed from a NAS until the battery went totally flat. After turning it up I was able to tell exactly how many minutes of the movie were played (I have it in 2 parts, set to reply all in VLC).

So the wifi was up and running all the time, CPU was decoding full hd.

Result: 3 hours and 14 minutes. Pretty awesome. You can watch full blown movie using a single charge. If it was just writing docs, the apple’s up-to-5hrs might be actually true.

Overall

It is great. Not only it “feels” faster then my old MacBook Pro on boot, when opening apps etc. It *is* faster for doing what I do the most – java dev. I just need to upgrade the RAM :)

At the same time it is super-portable, super-sexy and runs my favorite Mac OS X (Lion). And the best is – you can get it for around 3000 PLN + VAT. Finally a fair price for a mac. I don’t think you can get much better alternative with windows for that kind of money and with that kind of portability.