Nicolas Hurion

Building tools to help people.

Simple Data Access With DbUtils

I’ve been working on a side project that is more a dynamic site than a web application like I’m used to do. In this project, there is no need to have mutable states on objets, so there is no point in using Java beans and JPA is overkill for the task at hand.

So, we started with plain old JDBC and immutable objects where data is passed on to the constructor. That looks pretty much like this:

Plain Old data object (Assembly.java) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package be.noselus.model;

import com.google.common.base.Objects;

public class Assembly {

    public enum Level {
        DEPUTY_CHAMBER,
        SENAT,
        REGION,
        LOCAL,
        PROVINCE,
        EUROPE,
        FEDERAL,
        COMMUNITY
    }

    private final int id;
    private final String label;
    private final Level level;

    public Assembly(int id, String label, Level level) {
        this.id = id;
        this.label = label;
        this.level = level;
    }

    public int getId() {
        return id;
    }

    public String getLabel() {
        return label;
    }

    public Level getLevel() {
        return level;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(id, label, level);
    }

    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        final Assembly other = (Assembly) obj;
        return Objects.equal(this.id, other.id) && Objects.equal(this.label, other.label) && Objects.equal(this.level, other.level);
    }

    @Override
    public String toString() {
        return "Assembly{" +
                "id=" + id +
                ", label='" + label + '\'' +
                ", level=" + level +
                '}';
    }
}
Repository in JDBC (AssemblyRepositoryInDatabase.java) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package be.noselus.repository;

import be.noselus.model.Assembly;

import javax.inject.Inject;
import javax.inject.Singleton;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Singleton
public class AssemblyRepositoryInDatabase implements AssemblyRepository {

    private final DataSource dataSource;

    @Inject
    public AssemblyRepositoryInDatabase(final DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public Assembly findId(final int id) {
        try (Connection db = dataSource.getConnection();
             PreparedStatement stat = db.prepareStatement("SELECT * FROM assembly WHERE id = ?;")) {

            stat.setInt(1, id);
            stat.execute();
            final ResultSet resultSet = stat.getResultSet();
            resultSet.next();
            return assemblyMapping(resultSet);
        } catch (SQLException e) {
            throw new RuntimeException("Error retrieving assembly with id " + id, e);
        }
    }

    @Override
    public List<Assembly> getAssemblies() {
        List<Assembly> result = new ArrayList<>();
        try (Connection db = dataSource.getConnection();
             PreparedStatement stat = db.prepareStatement("SELECT * FROM assembly;")) {

            stat.execute();
            final ResultSet resultSet = stat.getResultSet();
            while (resultSet.next()) {
                Assembly assembly = assemblyMapping(resultSet);
                result.add(assembly);
            }
        } catch (SQLException e) {
            throw new RuntimeException("Error retrieving assemblies", e);
        }
        return result;
    }

    private Assembly assemblyMapping(final ResultSet resultSet) throws SQLException {
        final int foundId = resultSet.getInt("id");
        final String label = resultSet.getString("label");
        final String level = resultSet.getString("level");

        return new Assembly(foundId, label, Assembly.Level.valueOf(level));
    }
}

It’s working, and it’s understandable, but there are a few try catches and some boiler plate code that I’m not very fond of, so I tried to find a tool to make it better.

Developer Toolbox on Windows

Working on different environment I, like many other developers, ended up making my own toolbox for each to help me get the job done well and fast. As I recently started a new job, it was an opportunity for me to make a list of the tools I’ve been collecting over time so here is the tools I consider my basic toolbox when working on Windows.

Console 2

Console is an enhanced terminal for windows. Nice features are multiple tabs, and the possibility to define kinds of tabs, with a name, a style and a startup folder. The other nice thing is the ability to select text from the console. If you don’t have admin rights on your computer there is a portable version available here.

ls for windows (msls)

Working on different platform, at some point some commands become automatics and you type them without releasing it. For me, ls is one of those. Multiple solutions exist to have ls on windows. The one I’m using is small and simple, it’s called msls No need for admin rights, you just put it somewhere accessible from the path, and set an environment variable to have the nice colors you want and you’re set.

GrepWin

Grepwin is a great tool. it’s only purpose is search and replace, but it does it very well. The search is fast, and you can work complex regexp and ave them. Once you’re used to it, you’ll have trouble living without it. If you don’t have admin rights on your computer, I’ve found a portable version here

KDiff 3

Comparing file is something you sometimes have to do, and there are quite a few tools to do that, but there is one particular situation where a good tool make the difference is wen have to merge source code. And this is where KDiff 3 shines. Really a great tool. It does require admin rights to be installed, but if you install it on a computer and then zip the installed version you can use it as-is. I’ve also found a portable version at this location

Process explorer

It’s a task manager, but that show you much more than the task manager. Helpful to find the process that’s eating up your resources. It also have the great functionality to find which process has an handle on that file you try to delete but can’t. Normal install, portable version

Unlocker

When you try to delete a file but can’t because a process has an handle on that file, Unlocker will pop up and propose you some options to deal with the situation. I haven’t managed yet to find a version that is working wihout admin rights.

MWSnap 3

MWSnap 3 is a tool to create screen captures. Plenty of tools exists for that but I’ve stick to this one for quite a long time now and has always been happy with it. You can unzip it and put it anywhere. It support quite a few formats and continuous shoot where each capture will be saved with an incremented file name.

WinSCP

Because yoou’ll always have at some point to tranfer from or to a unix machine, WinSCP make that easy.

WinDirStat

When you run out of space on your fast SSD drive and you have to figure out where did it go and what can you remove. WinDirStat show you a noce representation of the occupation of your hard drive, with squares of different size corresponding to the relative size of a directory and different colors for different kind of files. Portable version

Notepad++

Not sure this one needs presentation. A very good editor with a lot of features and plugins. Porable version here

Charles Proxy

When you do web development, at one point or another, you’re gonna need a proxy. Charle’s Proxy helps debugging issues with JSON, XML and even AMF by showing content in a nice view, allow you to have performance info, do performance testing in different situations by simulation, for example, a 3G connection, intercept requests or responses and edit them on the fly,…

It’s not free or open source, but it’s not expensive worth every penny.

Sublime Text 3

Another text editor. This one is really good at opening those big log files and help you navigate in them. Sublime is also a pretty good development enviroment for quite a few languages. It has a ton of features and can be extended with a lot of available packages.

Not free either, you can test it for a while before deciding if it’s worth it’s price. Look up for videos on it’s nice features and I’m pretty sure you’ll be convinced that it does.

It’s also available for mac and linux, which make it a nice tool to have on your belt when you’re working on multiple platforms.

Intellij Idea

I’m not going to start a debate on which IDE is the best. I’ve worked with Eclipse, Netbeans and Intellij, each of them have pro and cons. Eventually I ended up prefering Intellij and it’s the one I’m using whenever I can.

Intellij now has two editions, the free opensource communition and the ultimate edition. The ultimate edition is definitively a great tool, especially if you’re working with spring. The interest of the community edition is less obvious if you’ve used one of the others for years, but it does have many of the features I like: the “changes” window allowing you too see all the files that changed on the server since yur last update, all the changes you’ve make, and organize them in changelists, the inspetions, basically having a small sonar in your IDE that tells you when there is a problem in your code and the “Alt+Enter” that propose the most suitable quick fix for the current situation.

So, if you have any chance, give it a try.

Deploy a Vaadin Application on Heroku

I’ve been playing with Vaadin for some times now, doing small projects to test things with it. I could always build and run them locally, but having my experiments available on internet wasn’t very easy unit the cloud offering started appearing.

Heroku is one of those cloud application platform. It’s a place where you can host small web application in java or other technologies for free and you can scale them up easily, for a fee. Deploying on heroku is done using git. You do a git push to heroku and it will take your code, compile it and then run it. You don’t install an application server, you declare in a file a web program to launch. Meaning you need an embedded server like tomcat or jetty to run a java web-app on heroku.

Fortunately, colleagues of mine already made a nice piece of software that allow to easily have an embedded tomcat configured for Vaadin, it’s called Embed for Vaadin.

The thing is, Vaadin rely heavily on session, and heroku does not allow sticky session. Meaning that if you want to scale to more than one dyno you have to store your session somewhere, either in a database or in an external memory cache system like memcached Turns out someone already worked on storing tomcat sessions in memcached and released a very nice tool for that: memcached-session-manager

Mixing all those together, I ended up creating vaadin-for-heroku.

Here is how to create a project with just 3 files and deploy it on heroku.

(pom.xml) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>helloworld</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>eu.hurion.vaadin.heroku</groupId>
            <artifactId>vaadin-for-heroku</artifactId>
            <version>0.4</version>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.4</version>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

Build Awesome Command-Line Applications in Ruby: Control Your Computer, Simplify Your Life

Ruby is a language I wanted to learn for quite some time now. So I bought a few books to help me in the process, and Build Awesome Command-Line Applications in Ruby is the first I finished.

The book uses two applications to help you learn how to build “Awesome Command-line applications”. One application to handle backup of a database, and another more complex “command suite” application to manage a todo list.

It is well written, quick and easy to read.

It will teach you all the basic tools to live in the Ruby ecosystem:

  • How to build your application with rake and bundler.
  • How to use gem to package and distribute your application through RubyGems.
  • How to organize your code.
  • How to test your application, both at the functional level with Cucumber and Aruba, and Unit test for lower level fine grained tests.

Which is exactly what I was looking for.

And not only that, but it will also teach you how to make a really good, effective, user friendly command-line application. Regarding that point, the title of the book is not an exaggeration. It doesn’t teach you how to make simple ruby script to help you in your day to day job, it teaches you how to make something really great for the user. Including integrated help, man page, configuration file management and so on.

If you’re like me new to the Ruby world, this book will only help you a little about syntax and basic libraries, but it will give you a good basis to build, document and release your first application. Which is really nice as once you have that, you’ve got something to push you to learn more about the rest of the language and available libraries.